1 # This file defines the `vim` API, available to all modes and commands. There is
2 # one `Vim` instance for each tab. Most importantly, it provides access to the
3 # owning Firefox window and the current mode, and allows you to change mode.
4 # `vim` objects are exposed by the config file API. Underscored names are
5 # private and should not be used by API consumers.
7 messageManager = require('./message-manager')
8 ScrollableElements = require('./scrollable-elements')
9 utils = require('./utils')
11 ChromeWindow = Ci.nsIDOMChromeWindow
14 constructor: (browser, @_parent) ->
17 @_setBrowser(browser, {addListeners: false})
22 Object.defineProperty(this, 'options', {
23 get: => @_parent.options
28 @_onLocationChange(@browser.currentURI.spec)
30 focusType = utils.getFocusType(utils.getActiveElement(@window))
31 @_setFocusType(focusType)
34 # Require the subset of the options needed to be listed explicitly (as
35 # opposed to sending _all_ options) for performance. Each option access
36 # might trigger an optionOverride.
37 @_listen('options', ({prefs}) =>
40 options[pref] = @options[pref]
44 @_listen('vimMethod', ({method, args = []}, callback = null) =>
45 result = this[method](args...)
49 @_listen('vimMethodSync', ({method, args = []}) =>
50 return this[method](args...)
53 @_listen('locationChange', @_onLocationChange.bind(this))
55 @_listen('frameCanReceiveEvents', (value) =>
56 @_state.frameCanReceiveEvents = value
59 @_listen('focusType', (focusType) =>
60 # If the focus moves from a web page element to a browser UI element, the
61 # focus and blur events happen in the expected order, but the message from
62 # the frame script arrives too late. Therefore, check that the currently
63 # active element isn’t a browser UI element first.
64 unless @_isUIElement(utils.getActiveElement(@window))
65 @_setFocusType(focusType)
68 _setBrowser: (@browser, {addListeners = true} = {}) ->
69 @window = @browser.ownerGlobal
70 @_messageManager = @browser.messageManager
72 @_addListeners() if addListeners
76 frameCanReceiveEvents: false
77 scrollableElements: new ScrollableElements(@window)
78 lastNotification: null
79 persistentNotification: null
81 enteredKeysTimeout: null
84 _isBlacklisted: (url) -> @options.blacklist.some((regex) -> regex.test(url))
87 return not @_state.frameCanReceiveEvents or
88 @_isUIElement(event.originalTarget)
90 _isUIElement: (element) ->
91 return (element.ownerGlobal instanceof ChromeWindow or
92 utils.isDockedDevtoolsElement(element)) and
93 element != @window.gBrowser.selectedBrowser
95 # `args...` is passed to the mode's `onEnter` method.
96 _enterMode: (mode, args...) ->
97 return if @mode == mode
99 unless utils.has(@_parent.modes, mode)
100 modes = Object.keys(@_parent.modes).join(', ')
102 "VimFx: Unknown mode. Available modes are: #{modes}. Got: #{mode}"
105 @_call('onLeave') if @mode?
107 @_call('onEnter', null, args...)
108 @_parent.emit('modeChange', {vim: this})
109 @_send('modeChange', {mode})
111 _consumeKeyEvent: (event) ->
112 match = @_parent.consumeKeyEvent(event, this)
113 return match if not match or typeof match == 'boolean'
115 if @options.notify_entered_keys
116 if match.type in ['none', 'full'] or match.likelyConflict
119 @_pushEnteredKey(match.keyStr)
125 _onInput: (match, event) ->
126 suppress = @_call('onInput', {event, count: match.count}, match)
129 _onLocationChange: (url) ->
131 when @_isBlacklisted(url)
132 @_enterMode('ignore', {type: 'blacklist'})
133 when (@mode == 'ignore' and @_storage.ignore.type == 'blacklist') or
135 @_enterMode('normal')
136 @_parent.emit('locationChange', {vim: this, location: new @window.URL(url)})
138 _call: (method, data = {}, extraArgs...) ->
139 args = Object.assign({vim: this, storage: @_storage[@mode] ?= {}}, data)
140 currentMode = @_parent.modes[@mode]
141 return currentMode[method].call(currentMode, args, extraArgs...)
143 _run: (name, data = {}, callback = null) ->
144 @_send('runCommand', {name, data}, callback)
146 _messageManagerOptions: (options) ->
147 return Object.assign({
148 messageManager: @_messageManager
151 _listen: (name, listener, options = {}) ->
152 messageManager.listen(name, listener, @_messageManagerOptions(options))
154 _listenOnce: (name, listener, options = {}) ->
155 messageManager.listenOnce(name, listener, @_messageManagerOptions(options))
157 _send: (name, data, callback = null, options = {}) ->
158 messageManager.send(name, data, callback, @_messageManagerOptions(options))
161 @_state.lastNotification = message
162 @_parent.emit('notification', {vim: this, message})
163 if @options.notifications_enabled
164 @window.StatusPanel?._label = message
166 _notifyPersistent: (message) ->
167 @_state.persistentNotification = message
170 _refreshPersistentNotification: ->
171 @notify(@_state.persistentNotification) if @_state.persistentNotification
174 @_parent.emit('hideNotification', {vim: this})
175 @window.StatusPanel?._label = '' # or .update()
176 @_state.lastNotification = null
177 @_state.persistentNotification = null
179 _clearEnteredKeys: ->
180 @_clearEnteredKeysTimeout()
181 return unless @_state.enteredKeys.length > 0
183 @_state.enteredKeys = []
184 if @_state.persistentNotification
185 @notify(@_state.persistentNotification)
189 _pushEnteredKey: (keyStr) ->
190 @_state.enteredKeys.push(keyStr)
191 @_clearEnteredKeysTimeout()
192 @notify(@_state.enteredKeys.join(''))
193 clear = @_clearEnteredKeys.bind(this)
194 @_state.enteredKeysTimeout =
195 @window.setTimeout((-> clear()), @options.timeout)
197 _clearEnteredKeysTimeout: ->
198 if @_state.enteredKeysTimeout?
199 @window.clearTimeout(@_state.enteredKeysTimeout)
200 @_state.enteredKeysTimeout = null
202 _modal: (type, args, callback = null) ->
203 @_run('modal', {type, args}, callback)
205 markPageInteraction: (value = null) -> @_send('markPageInteraction', value)
207 _focusMarkerElement: (elementIndex, options = {}) ->
208 # If you, for example, focus the location bar, unfocus it by pressing
209 # `<esc>` and then try to focus a link or text input in a web page the focus
210 # won’t work unless `@browser` is focused first.
212 browserOffset = @_getBrowserOffset()
213 @_run('focus_marker_element', {elementIndex, browserOffset, options})
215 _setFocusType: (focusType) ->
216 return if focusType == @focusType
217 @focusType = focusType
219 when @focusType == 'ignore'
220 @_enterMode('ignore', {type: 'focusType'})
221 when @mode == 'ignore' and @_storage.ignore.type == 'focusType'
222 @_enterMode('normal')
223 when @mode == 'normal' and @focusType == 'findbar'
225 when @mode == 'find' and @focusType != 'findbar'
226 @_enterMode('normal')
227 @_parent.emit('focusTypeChange', {vim: this})
229 _getBrowserOffset: ->
230 browserRect = @browser.getBoundingClientRect()
232 x: @window.screenX + browserRect.left
233 y: @window.screenY + browserRect.top