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')
12 constructor: (browser, @_parent) ->
15 @_setBrowser(browser, {addListeners: false})
20 Object.defineProperty(this, 'options', {
21 get: => @_parent.options
26 @_onLocationChange(@browser.currentURI.spec)
28 focusType = utils.getFocusType(utils.getActiveElement(@window))
29 @_setFocusType(focusType)
32 # Require the subset of the options needed to be listed explicitly (as
33 # opposed to sending _all_ options) for performance. Each option access
34 # might trigger an optionOverride.
35 @_listen('options', ({prefs}) =>
38 options[pref] = @options[pref]
42 @_listen('vimMethod', ({method, args = []}, callback = null) =>
43 result = this[method](args...)
47 @_listen('vimMethodSync', ({method, args = []}) =>
48 return this[method](args...)
51 @_listen('locationChange', @_onLocationChange.bind(this))
53 @_listen('frameCanReceiveEvents', (value) =>
54 @_state.frameCanReceiveEvents = value
57 @_listen('focusType', (focusType) =>
58 # If the focus moves from a web page element to a browser UI element, the
59 # focus and blur events happen in the expected order, but the message from
60 # the frame script arrives too late. Therefore, check that the currently
61 # active element isn’t a browser UI element first.
62 unless @_isUIElement(utils.getActiveElement(@window))
63 @_setFocusType(focusType)
66 _setBrowser: (@browser, {addListeners = true} = {}) ->
67 @window = @browser.ownerGlobal
68 @_messageManager = @browser.messageManager
70 @_addListeners() if addListeners
74 frameCanReceiveEvents: false
75 scrollableElements: new ScrollableElements(@window)
76 lastNotification: null
77 persistentNotification: null
79 enteredKeysTimeout: null
82 _isBlacklisted: (url) -> @options.blacklist.some((regex) -> regex.test(url))
85 return not @_state.frameCanReceiveEvents or
86 @_isUIElement(event.originalTarget)
88 _isUIElement: (element) ->
89 return (element.ownerGlobal.isChromeWindow or
90 utils.isDockedDevtoolsElement(element)) and
91 element != @window.gBrowser.selectedBrowser
93 # `args...` is passed to the mode's `onEnter` method.
94 _enterMode: (mode, args...) ->
95 return if @mode == mode
97 unless utils.has(@_parent.modes, mode)
98 modes = Object.keys(@_parent.modes).join(', ')
100 "VimFx: Unknown mode. Available modes are: #{modes}. Got: #{mode}"
103 @_call('onLeave') if @mode?
105 @_call('onEnter', null, args...)
106 @_parent.emit('modeChange', {vim: this})
107 @_send('modeChange', {mode})
109 _consumeKeyEvent: (event) ->
110 match = @_parent.consumeKeyEvent(event, this)
111 return match if not match or typeof match == 'boolean'
113 if @options.notify_entered_keys
114 if match.type in ['none', 'full'] or match.likelyConflict
117 @_pushEnteredKey(match.keyStr)
123 _onInput: (match, event) ->
124 suppress = @_call('onInput', {event, count: match.count}, match)
127 _onLocationChange: (url) ->
129 when @_isBlacklisted(url)
130 @_enterMode('ignore', {type: 'blacklist'})
131 when (@mode == 'ignore' and @_storage.ignore.type == 'blacklist') or
133 @_enterMode('normal')
134 @_parent.emit('locationChange', {vim: this, location: new @window.URL(url)})
136 _call: (method, data = {}, extraArgs...) ->
137 args = Object.assign({vim: this, storage: @_storage[@mode] ?= {}}, data)
138 currentMode = @_parent.modes[@mode]
139 return currentMode[method].call(currentMode, args, extraArgs...)
141 _run: (name, data = {}, callback = null) ->
142 @_send('runCommand', {name, data}, callback)
144 _messageManagerOptions: (options) ->
145 return Object.assign({
146 messageManager: @_messageManager
149 _listen: (name, listener, options = {}) ->
150 messageManager.listen(name, listener, @_messageManagerOptions(options))
152 _listenOnce: (name, listener, options = {}) ->
153 messageManager.listenOnce(name, listener, @_messageManagerOptions(options))
155 _send: (name, data, callback = null, options = {}) ->
156 messageManager.send(name, data, callback, @_messageManagerOptions(options))
159 @_state.lastNotification = message
160 @_parent.emit('notification', {vim: this, message})
161 if @options.notifications_enabled
162 @window.StatusPanel?._label = message
164 _notifyPersistent: (message) ->
165 @_state.persistentNotification = message
168 _refreshPersistentNotification: ->
169 @notify(@_state.persistentNotification) if @_state.persistentNotification
172 @_parent.emit('hideNotification', {vim: this})
173 @window.StatusPanel?._label = '' # or .update()
174 @_state.lastNotification = null
175 @_state.persistentNotification = null
177 _clearEnteredKeys: ->
178 @_clearEnteredKeysTimeout()
179 return unless @_state.enteredKeys.length > 0
181 @_state.enteredKeys = []
182 if @_state.persistentNotification
183 @notify(@_state.persistentNotification)
187 _pushEnteredKey: (keyStr) ->
188 @_state.enteredKeys.push(keyStr)
189 @_clearEnteredKeysTimeout()
190 @notify(@_state.enteredKeys.join(''))
191 clear = @_clearEnteredKeys.bind(this)
192 @_state.enteredKeysTimeout =
193 @window.setTimeout((-> clear()), @options.timeout)
195 _clearEnteredKeysTimeout: ->
196 if @_state.enteredKeysTimeout?
197 @window.clearTimeout(@_state.enteredKeysTimeout)
198 @_state.enteredKeysTimeout = null
200 _modal: (type, args, callback = null) ->
201 @_run('modal', {type, args}, callback)
203 markPageInteraction: (value = null) -> @_send('markPageInteraction', value)
205 _focusMarkerElement: (elementIndex, options = {}) ->
206 # If you, for example, focus the location bar, unfocus it by pressing
207 # `<esc>` and then try to focus a link or text input in a web page the focus
208 # won’t work unless `@browser` is focused first.
210 browserOffset = @_getBrowserOffset()
211 @_run('focus_marker_element', {elementIndex, browserOffset, options})
213 _setFocusType: (focusType) ->
214 return if focusType == @focusType
215 @focusType = focusType
217 when @focusType == 'ignore'
218 @_enterMode('ignore', {type: 'focusType'})
219 when @mode == 'ignore' and @_storage.ignore.type == 'focusType'
220 @_enterMode('normal')
221 when @mode == 'normal' and @focusType == 'findbar'
223 when @mode == 'find' and @focusType != 'findbar'
224 @_enterMode('normal')
225 @_parent.emit('focusTypeChange', {vim: this})
227 _getBrowserOffset: ->
228 browserRect = @browser.getBoundingClientRect()
230 x: @window.screenX + browserRect.left
231 y: @window.screenY + browserRect.top