]> git.gir.st - VimFx.git/blob - extension/lib/vim.coffee
Improve Ignore mode and the blacklist
[VimFx.git] / extension / lib / vim.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013.
3 # Copyright Simon Lydell 2013, 2014, 2015, 2016.
4 #
5 # This file is part of VimFx.
6 #
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.
11 #
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.
16 #
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/>.
19 ###
20
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.
26
27 messageManager = require('./message-manager')
28 ScrollableElements = require('./scrollable-elements')
29 statusPanel = require('./status-panel')
30 utils = require('./utils')
31
32 ChromeWindow = Ci.nsIDOMChromeWindow
33
34 class Vim
35 constructor: (browser, @_parent) ->
36 @_setBrowser(browser, {addListeners: false})
37 @_storage = {}
38
39 @_resetState()
40
41 Object.defineProperty(this, 'options', {
42 get: => @_parent.options
43 enumerable: true
44 })
45
46 _start: ->
47 @_onLocationChange(@browser.currentURI.spec)
48 @_addListeners()
49
50 _addListeners: ->
51 # Require the subset of the options needed to be listed explicitly (as
52 # opposed to sending _all_ options) for performance. Each option access
53 # might trigger an optionOverride.
54 @_listen('options', ({prefs}) =>
55 options = {}
56 for pref in prefs
57 options[pref] = @options[pref]
58 return options
59 )
60
61 @_listen('vimMethod', ({method, args = []}, callback = null) =>
62 result = @[method](args...)
63 callback?(result)
64 )
65
66 @_listen('vimMethodSync', ({method, args = []}) =>
67 return @[method](args...)
68 )
69
70 @_listen('locationChange', @_onLocationChange.bind(this))
71
72 @_listen('frameCanReceiveEvents', (value) =>
73 @_state.frameCanReceiveEvents = value
74 )
75
76 @_listen('focusType', (focusType) =>
77 # If the focus moves from a web page element to a browser UI element, the
78 # focus and blur events happen in the expected order, but the message from
79 # the frame script arrives too late. Therefore, check that the currently
80 # active element isn’t a browser UI element first.
81 unless @_isUIElement(utils.getActiveElement(@window))
82 @_parent.emit('focusTypeChange', {vim: this, focusType})
83 )
84
85 _setBrowser: (@browser, {addListeners = true} = {}) ->
86 @window = @browser.ownerGlobal
87 @_messageManager = @browser.messageManager
88
89 @_statusPanel?.remove()
90 @_statusPanel = statusPanel.injectStatusPanel(@browser)
91 @_statusPanel.onclick = @hideNotification.bind(this)
92
93 @_addListeners() if addListeners
94
95 _resetState: ->
96 @_state = {
97 frameCanReceiveEvents: false
98 scrollableElements: new ScrollableElements(@window)
99 }
100
101 _isBlacklisted: (url) -> @options.black_list.some((regex) -> regex.test(url))
102
103 isUIEvent: (event) ->
104 return not @_state.frameCanReceiveEvents or
105 @_isUIElement(event.originalTarget)
106
107 _isUIElement: (element) ->
108 # TODO: The `element.ownerGlobal` check will be redundant when
109 # non-multi-process is removed from Firefox.
110 return element.ownerGlobal instanceof ChromeWindow and
111 element != @window.gBrowser.selectedBrowser
112
113 # `args...` is passed to the mode's `onEnter` method.
114 enterMode: (mode, args...) ->
115 return if @mode == mode
116
117 unless utils.has(@_parent.modes, mode)
118 modes = Object.keys(@_parent.modes).join(', ')
119 throw new Error("VimFx: Unknown mode. Available modes are: #{modes}.
120 Got: #{mode}")
121
122 @_call('onLeave') if @mode?
123 @mode = mode
124 result = @_call('onEnter', null, args...)
125 @_parent.emit('modeChange', this)
126 @_send('modeChange', {mode})
127 return result
128
129 _consumeKeyEvent: (event, focusType) ->
130 return @_parent.consumeKeyEvent(event, this, focusType)
131
132 _onInput: (match, uiEvent = false) ->
133 suppress = @_call('onInput', {uiEvent, count: match.count}, match)
134 return suppress
135
136 _onLocationChange: (url) ->
137 unless @mode == 'ignore' and @_storage.ignore.type == 'explicit'
138 if @_isBlacklisted(url)
139 @enterMode('ignore', {type: 'blacklist'})
140 else
141 @enterMode('normal')
142 @_parent.emit('locationChange', {vim: this, location: new @window.URL(url)})
143
144 _call: (method, data = {}, extraArgs...) ->
145 args = Object.assign({vim: this, storage: @_storage[@mode] ?= {}}, data)
146 currentMode = @_parent.modes[@mode]
147 return currentMode[method].call(currentMode, args, extraArgs...)
148
149 _run: (name, data = {}, callback = null) ->
150 @_send('runCommand', {name, data}, callback)
151
152 _messageManagerOptions: (options) ->
153 return Object.assign({
154 messageManager: @_messageManager
155 }, options)
156
157 _listen: (name, listener, options = {}) ->
158 messageManager.listen(name, listener, @_messageManagerOptions(options))
159
160 _listenOnce: (name, listener, options = {}) ->
161 messageManager.listenOnce(name, listener, @_messageManagerOptions(options))
162
163 _send: (name, data, callback = null, options = {}) ->
164 messageManager.send(name, data, callback, @_messageManagerOptions(options))
165
166 notify: (message) ->
167 @_parent.emit('notification', {vim: this, message})
168 if @_parent.options.notifications_enabled
169 @_statusPanel.setAttribute('label', message)
170 @_statusPanel.removeAttribute('inactive')
171
172 hideNotification: ->
173 @_parent.emit('hideNotification', {vim: this})
174 @_statusPanel.setAttribute('inactive', 'true')
175
176 markPageInteraction: (value = null) -> @_send('markPageInteraction', value)
177
178 _focusMarkerElement: (elementIndex, options = {}) ->
179 # If you, for example, focus the location bar, unfocus it by pressing
180 # `<esc>` and then try to focus a link or text input in a web page the focus
181 # won’t work unless `@browser` is focused first.
182 @browser.focus()
183 @_run('focus_marker_element', {elementIndex, options})
184
185 module.exports = Vim
Imprint / Impressum