]> git.gir.st - VimFx.git/blob - extension/lib/vim.coffee
Fix blacklisting on already loaded tabs
[VimFx.git] / extension / lib / vim.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013.
3 # Copyright Simon Lydell 2013, 2014.
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 utils = require('./utils')
29
30 class Vim
31 constructor: (@browser, @_parent) ->
32 @window = @browser.ownerGlobal
33 @_messageManager = @browser.messageManager
34 @_storage = {}
35
36 @_frameCanReceiveEvents = false
37 @_listen('DOMWindowCreated', => @_frameCanReceiveEvents = true)
38
39 Object.defineProperty(this, 'options', {
40 get: => @_parent.options
41 enumerable: true
42 })
43
44 @_onLocationChange(@browser.currentURI.spec)
45
46 # Require the subset of the options needed to be listed explicitly (as
47 # opposed to sending _all_ options) for performance. Each option access
48 # might trigger an optionOverride.
49 @_listen('options', ({ prefs }) =>
50 options = {}
51 for pref in prefs
52 options[pref] = @options[pref]
53 return options
54 )
55
56 @_listen('vimMethod', ({ method, args = [] }, { callback = null }) =>
57 result = @[method](args...)
58 @_send(callback, result) if callback
59 )
60
61 @_listen('vimMethodSync', ({ method, args = [] }) =>
62 return @[method](args...)
63 )
64
65 _isBlacklisted: (url) -> @options.black_list.some((regex) -> regex.test(url))
66
67 isFrameEvent: (event) ->
68 return (@_frameCanReceiveEvents and
69 event.originalTarget == @window.gBrowser.selectedBrowser)
70
71 isCurrent: -> @_parent.getCurrentVim(utils.getCurrentWindow()) == this
72
73 # `args` is an array of arguments to be passed to the mode's `onEnter` method.
74 enterMode: (mode, args...) ->
75 return false if @mode == mode
76
77 unless utils.has(@_parent.modes, mode)
78 modes = Object.keys(@_parent.modes).join(', ')
79 throw new Error("VimFx: Unknown mode. Available modes are: #{ modes }.
80 Got: #{ mode }")
81
82 @_call('onLeave') if @mode?
83 @mode = mode
84 @_call('onEnter', null, args...)
85 @_parent.emit('modeChange', this)
86 @_send('modeChange', {mode})
87 return true
88
89 _onInput: (event, focusType, { isFrameEvent = false } = {}) ->
90 match = @_parent.consumeKeyEvent(event, this, focusType)
91 return null unless match
92 suppress = @_call('onInput', {isFrameEvent, count: match.count}, match)
93 return suppress
94
95 _onLocationChange: (url) ->
96 @enterMode(if @_isBlacklisted(url) then 'ignore' else 'normal')
97 @_parent.emit('locationChange', {vim: this, location: new @window.URL(url)})
98 @_send('locationChange')
99
100 _call: (method, data = {}, extraArgs...) ->
101 args = Object.assign({vim: this, storage: @_storage[@mode] ?= {}}, data)
102 currentMode = @_parent.modes[@mode]
103 currentMode[method].call(currentMode, args, extraArgs...)
104
105 _run: (name, data = {}, callback = null) ->
106 @_send('runCommand', {name, data}, callback)
107
108 _listen: (name, listener) ->
109 messageManager.listen(name, listener, @_messageManager)
110
111 _listenOnce: (name, listener) ->
112 messageManager.listenOnce(name, listener, @_messageManager)
113
114 _send: (name, data, callback = null) ->
115 messageManager.send(name, data, @_messageManager, callback)
116
117 notify: (title, options = {}) ->
118 new @window.Notification(title, Object.assign({
119 icon: 'chrome://vimfx/skin/icon128.png'
120 tag: 'VimFx-notification'
121 }, options))
122
123 markPageInteraction: ->
124 @_send('markPageInteraction')
125
126 _focusMarkerElement: (elementIndex, options = {}) ->
127 # If you, for example, focus the location bar, unfocus it by pressing
128 # `<esc>` and then try to focus a link or text input in a web page the focus
129 # won’t work unless `@browser` is focused first.
130 @browser.focus()
131 @_run('focus_marker_element', {elementIndex, options})
132
133 module.exports = Vim
Imprint / Impressum