2 # Copyright Anton Khodakivskiy 2012, 2013.
3 # Copyright Simon Lydell 2013, 2014, 2015, 2016.
5 # This file is part of VimFx.
7 # VimFx is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # VimFx is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
21 # This file defines the `vim` API, available to all modes and commands. There is
22 # one `Vim` instance for each tab. Most importantly, it provides access to the
23 # owning Firefox window and the current mode, and allows you to change mode.
24 # `vim` objects are exposed by the config file API. Underscored names are
25 # private and should not be used by API consumers.
27 messageManager = require('./message-manager')
28 ScrollableElements = require('./scrollable-elements')
29 statusPanel = require('./status-panel')
30 utils = require('./utils')
32 ChromeWindow = Ci.nsIDOMChromeWindow
35 constructor: (browser, @_parent) ->
38 @_setBrowser(browser, {addListeners: false})
43 Object.defineProperty(this, 'options', {
44 get: => @_parent.options
49 @_onLocationChange(@browser.currentURI.spec)
51 focusType = utils.getFocusType(utils.getActiveElement(@window))
52 @_setFocusType(focusType)
55 # Require the subset of the options needed to be listed explicitly (as
56 # opposed to sending _all_ options) for performance. Each option access
57 # might trigger an optionOverride.
58 @_listen('options', ({prefs}) =>
61 options[pref] = @options[pref]
65 @_listen('vimMethod', ({method, args = []}, callback = null) =>
66 result = this[method](args...)
70 @_listen('vimMethodSync', ({method, args = []}) =>
71 return this[method](args...)
74 @_listen('locationChange', @_onLocationChange.bind(this))
76 @_listen('frameCanReceiveEvents', (value) =>
77 @_state.frameCanReceiveEvents = value
80 @_listen('focusType', (focusType) =>
81 # If the focus moves from a web page element to a browser UI element, the
82 # focus and blur events happen in the expected order, but the message from
83 # the frame script arrives too late. Therefore, check that the currently
84 # active element isn’t a browser UI element first.
85 unless @_isUIElement(utils.getActiveElement(@window))
86 @_setFocusType(focusType)
89 _setBrowser: (@browser, {addListeners = true} = {}) ->
90 @window = @browser.ownerGlobal
91 @_messageManager = @browser.messageManager
93 @_statusPanel?.remove()
94 @_statusPanel = statusPanel.injectStatusPanel(@browser)
96 @_addListeners() if addListeners
100 frameCanReceiveEvents: false
101 scrollableElements: new ScrollableElements(@window)
104 _isBlacklisted: (url) -> @options.blacklist.some((regex) -> regex.test(url))
106 isUIEvent: (event) ->
107 return not @_state.frameCanReceiveEvents or
108 @_isUIElement(event.originalTarget)
110 _isUIElement: (element) ->
111 # TODO: The `element.ownerGlobal` check will be redundant when
112 # non-multi-process is removed from Firefox.
113 return element.ownerGlobal instanceof ChromeWindow and
114 element != @window.gBrowser.selectedBrowser
116 # `args...` is passed to the mode's `onEnter` method.
117 _enterMode: (mode, args...) ->
118 return if @mode == mode
120 unless utils.has(@_parent.modes, mode)
121 modes = Object.keys(@_parent.modes).join(', ')
123 "VimFx: Unknown mode. Available modes are: #{modes}. Got: #{mode}"
126 @_call('onLeave') if @mode?
128 @_call('onEnter', null, args...)
129 @_parent.emit('modeChange', {vim: this})
130 @_send('modeChange', {mode})
132 _consumeKeyEvent: (event) ->
133 return @_parent.consumeKeyEvent(event, this)
135 _onInput: (match, event) ->
136 uiEvent = if @isUIEvent(event) then event else false
137 suppress = @_call('onInput', {uiEvent, count: match.count}, match)
140 _onLocationChange: (url) ->
142 when @_isBlacklisted(url)
143 @_enterMode('ignore', {type: 'blacklist'})
144 when (@mode == 'ignore' and @_storage.ignore.type == 'blacklist') or
146 @_enterMode('normal')
147 @_parent.emit('locationChange', {vim: this, location: new @window.URL(url)})
149 _call: (method, data = {}, extraArgs...) ->
150 args = Object.assign({vim: this, storage: @_storage[@mode] ?= {}}, data)
151 currentMode = @_parent.modes[@mode]
152 return currentMode[method].call(currentMode, args, extraArgs...)
154 _run: (name, data = {}, callback = null) ->
155 @_send('runCommand', {name, data}, callback)
157 _messageManagerOptions: (options) ->
158 return Object.assign({
159 messageManager: @_messageManager
162 _listen: (name, listener, options = {}) ->
163 messageManager.listen(name, listener, @_messageManagerOptions(options))
165 _listenOnce: (name, listener, options = {}) ->
166 messageManager.listenOnce(name, listener, @_messageManagerOptions(options))
168 _send: (name, data, callback = null, options = {}) ->
169 messageManager.send(name, data, callback, @_messageManagerOptions(options))
172 @_parent.emit('notification', {vim: this, message})
173 if @options.notifications_enabled
174 @_statusPanel.setAttribute('label', message)
175 @_statusPanel.removeAttribute('inactive')
178 @_parent.emit('hideNotification', {vim: this})
179 @_statusPanel.setAttribute('inactive', 'true')
181 markPageInteraction: (value = null) -> @_send('markPageInteraction', value)
183 _focusMarkerElement: (elementIndex, options = {}) ->
184 # If you, for example, focus the location bar, unfocus it by pressing
185 # `<esc>` and then try to focus a link or text input in a web page the focus
186 # won’t work unless `@browser` is focused first.
188 @_run('focus_marker_element', {elementIndex, options})
190 _setFocusType: (focusType) ->
191 return if focusType == @focusType
192 @focusType = focusType
194 when @focusType == 'ignore'
195 @_enterMode('ignore', {type: 'focusType'})
196 when @mode == 'ignore' and @_storage.ignore.type == 'focusType'
197 @_enterMode('normal')
198 when @mode == 'normal' and @focusType == 'findbar'
200 when @mode == 'find' and @focusType != 'findbar'
201 @_enterMode('normal')
202 @_parent.emit('focusTypeChange', {vim: this})