]> git.gir.st - VimFx.git/blob - extension/lib/vim.coffee
Fix broken notifications
[VimFx.git] / extension / lib / vim.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013.
3 # Copyright Simon Lydell 2013, 2014, 2015.
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 Public API. Underscored names are private and
25 # should not be used by API consumers.
26
27 messageManager = require('./message-manager')
28 statusPanel = require('./status-panel')
29 utils = require('./utils')
30
31 ChromeWindow = Ci.nsIDOMChromeWindow
32
33 class Vim
34 constructor: (browser, @_parent) ->
35 @_setBrowser(browser)
36 @_storage = {}
37
38 @_resetState()
39
40 Object.defineProperty(this, 'options', {
41 get: => @_parent.options
42 enumerable: true
43 })
44
45 # Since this is done in the constructor, defer location change handling to
46 # the next tick so that this `vim` instance is saved in `vimfx.vims` first.
47 # This allows 'locationChange' listeners to use `@vimfx.getCurrentVim()`.
48 utils.nextTick(@window, =>
49 @_onLocationChange(@browser.currentURI.spec)
50 )
51
52 @_addListeners()
53
54 _addListeners: ->
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}) =>
59 options = {}
60 for pref in prefs
61 options[pref] = @options[pref]
62 return options
63 )
64
65 @_listen('vimMethod', ({method, args = []}, {callback = null}) =>
66 result = @[method](args...)
67 @_send(callback, result) if callback
68 )
69
70 @_listen('vimMethodSync', ({method, args = []}) =>
71 return @[method](args...)
72 )
73
74 @_listen('DOMWindowCreated', => @_state.frameCanReceiveEvents = true)
75
76 @_listen('locationChange', @_onLocationChange.bind(this))
77
78 _setBrowser: (browser) ->
79 refresh = @browser?
80 @browser = browser
81 @window = @browser.ownerGlobal
82 @_messageManager = @browser.messageManager
83
84 @_statusPanel?.remove()
85 @_statusPanel = statusPanel.injectStatusPanel(@browser)
86 @_statusPanel.onclick = @hideNotification.bind(this)
87
88 @_addListeners() if refresh
89
90 _resetState: ->
91 @_state =
92 frameCanReceiveEvents: false
93
94 _isBlacklisted: (url) -> @options.black_list.some((regex) -> regex.test(url))
95
96 isUIEvent: (event) ->
97 target = event.originalTarget
98 return not @_state.frameCanReceiveEvents or
99 if MULTI_PROCESS_ENABLED
100 (target != @window.gBrowser.selectedBrowser)
101 else
102 (target.ownerGlobal instanceof ChromeWindow)
103
104 # `args...` is passed to the mode's `onEnter` method.
105 enterMode: (mode, args...) ->
106 return if @mode == mode
107
108 unless utils.has(@_parent.modes, mode)
109 modes = Object.keys(@_parent.modes).join(', ')
110 throw new Error("VimFx: Unknown mode. Available modes are: #{modes}.
111 Got: #{mode}")
112
113 @_call('onLeave') if @mode?
114 @mode = mode
115 result = @_call('onEnter', null, args...)
116 @_parent.emit('modeChange', this)
117 @_send('modeChange', {mode})
118 return result
119
120 _consumeKeyEvent: (event, focusType) ->
121 return @_parent.consumeKeyEvent(event, this, focusType)
122
123 _onInput: (match, uiEvent = false) ->
124 suppress = @_call('onInput', {uiEvent, count: match.count}, match)
125 return suppress
126
127 _onLocationChange: (url) ->
128 @enterMode(if @_isBlacklisted(url) then 'ignore' else 'normal')
129 @_parent.emit('locationChange', {vim: this, location: new @window.URL(url)})
130
131 _call: (method, data = {}, extraArgs...) ->
132 args = Object.assign({vim: this, storage: @_storage[@mode] ?= {}}, data)
133 currentMode = @_parent.modes[@mode]
134 return currentMode[method].call(currentMode, args, extraArgs...)
135
136 _run: (name, data = {}, callback = null) ->
137 @_send('runCommand', {name, data}, callback)
138
139 _listen: (name, listener) ->
140 messageManager.listen(name, listener, @_messageManager)
141
142 _listenOnce: (name, listener) ->
143 messageManager.listenOnce(name, listener, @_messageManager)
144
145 _send: (name, data, callback = null) ->
146 messageManager.send(name, data, @_messageManager, callback)
147
148 notify: (message) ->
149 @_parent.emit('notification', {vim: this, message})
150 if @_parent.options.notifications_enabled
151 @_statusPanel.setAttribute('label', message)
152 @_statusPanel.removeAttribute('inactive')
153
154 hideNotification: ->
155 @_parent.emit('hideNotification', {vim: this})
156 @_statusPanel.setAttribute('inactive', 'true')
157
158 markPageInteraction: ->
159 @_send('markPageInteraction')
160
161 _focusMarkerElement: (elementIndex, options = {}) ->
162 # If you, for example, focus the location bar, unfocus it by pressing
163 # `<esc>` and then try to focus a link or text input in a web page the focus
164 # won’t work unless `@browser` is focused first.
165 @browser.focus()
166 @_run('focus_marker_element', {elementIndex, options})
167
168 module.exports = Vim
Imprint / Impressum