]> git.gir.st - VimFx.git/blob - extension/lib/events-frame.coffee
Don't use the same hint for GitHub's diff expansion buttons
[VimFx.git] / extension / lib / events-frame.coffee
1 ###
2 # Copyright Simon Lydell 2015.
3 #
4 # This file is part of VimFx.
5 #
6 # VimFx is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # VimFx is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
18 ###
19
20 # This file is the equivalent to events.coffee, but for frame scripts.
21
22 notation = require('vim-like-key-notation')
23 commands = require('./commands-frame')
24 messageManager = require('./message-manager')
25 utils = require('./utils')
26
27 class FrameEventManager
28 constructor: (@vim) ->
29 @numFocusToSuppress = 0
30 @keepInputs = false
31 @currentUrl = false
32
33 listen: utils.listen.bind(null, FRAME_SCRIPT_ENVIRONMENT)
34 listenOnce: utils.listenOnce.bind(null, FRAME_SCRIPT_ENVIRONMENT)
35
36 addListeners: ->
37 # If the page already was loaded when VimFx was initialized, send the
38 # 'frameCanReceiveEvents' message straight away.
39 if @vim.content.document.readyState == 'complete'
40 messageManager.send('frameCanReceiveEvents', true)
41
42 @listen('readystatechange', (event) =>
43 target = event.originalTarget
44 @currentUrl = @vim.content.location.href
45
46 if target == @vim.content.document
47 switch target.readyState
48 when 'interactive'
49 messageManager.send('locationChange', @currentUrl)
50 when 'complete'
51 messageManager.send('frameCanReceiveEvents', true)
52 )
53
54 @listen('pageshow', (event) =>
55 [oldUrl, @currentUrl] = [@currentUrl, @vim.content.location.href]
56
57 # When navigating the history, `event.persisted` is `true` (meaning that
58 # the page loaded from cache) and 'readystatechange' won’t be fired, so
59 # send a 'locationChange' message to make sure that the blacklist is
60 # applied etc. The reason we don’t simply _always_ do this on the
61 # 'pageshow' event, is because it usually fires too late. However, it also
62 # fires after having moved a tab to another window. In that case it is
63 # _not_ a location change; the blacklist should not be applied.
64 if event.persisted
65 url = @vim.content.location.href
66 messageManager.send('cachedPageshow', null, (movedToNewTab) =>
67 if not movedToNewTab and oldUrl != @currentUrl
68 messageManager.send('locationChange', @currentUrl)
69 )
70 )
71
72 @listen('pagehide', (event) =>
73 target = event.originalTarget
74 @currentUrl = null
75
76 if target == @vim.content.document
77 messageManager.send('frameCanReceiveEvents', false)
78
79 # If the target isn’t the topmost document, it means that a frame has
80 # changed: It could have been removed or its `src` attribute could have
81 # been changed. If the frame contains other frames, 'pagehide' events have
82 # already been fired for them.
83 @vim.resetState(target)
84 )
85
86 @listen('overflow', (event) =>
87 target = event.originalTarget
88 @vim.state.scrollableElements.addChecked(target)
89 )
90
91 @listen('underflow', (event) =>
92 target = event.originalTarget
93 @vim.state.scrollableElements.deleteChecked(target)
94 )
95
96 @listen('keydown', (event) =>
97 @keepInputs = false
98
99 suppress = @vim.onInput(event)
100
101 # This also suppresses the 'keypress' and 'keyup' events. (Yes, in frame
102 # scripts, suppressing the 'keydown' events does seem to even suppress
103 # the 'keyup' event!)
104 utils.suppressEvent(event) if suppress
105
106 # From this line on, the rest of the code in `addListeners` is more or
107 # less devoted to autofocus prevention. When enabled, focus events that
108 # occur before the user has interacted with page are prevented.
109 #
110 # If this keydown event wasn’t suppressed (`not suppress`), it’s an
111 # obvious interaction with the page. If it _was_ suppressed, though, it’s
112 # an interaction depending on the command triggered; if it calls
113 # `vim.markPageInteraction()` or not.
114 @vim.markPageInteraction() unless suppress
115 )
116
117 @listen('keydown', ((event) =>
118 suppress = messageManager.get('lateKeydown', {
119 defaultPrevented: event.defaultPrevented
120 })
121
122 if @vim.state.inputs and @vim.mode == 'normal' and not suppress and
123 not event.defaultPrevented
124 # There is no need to take `ignore_keyboard_layout` and `translations`
125 # into account here, since we want to override the _native_ `<tab>`
126 # behavior. Then, `event.key` is the way to go. (Unless the prefs are
127 # customized. YAGNI until requested.)
128 keyStr = notation.stringify(event)
129 options = @vim.options(['focus_previous_key', 'focus_next_key'])
130 direction = switch keyStr
131 when '' then null
132 when options.focus_previous_key then -1
133 when options.focus_next_key then +1
134 else null
135 if direction?
136 suppress = commands.move_focus({@vim, direction})
137 @keepInputs = true
138
139 utils.suppressEvent(event) if suppress
140 ), false)
141
142 @listen('mousedown', (event) =>
143 # Allow clicking on another text input without exiting “gi mode”. Listen
144 # for 'mousedown' instead of 'click', because only the former runs before
145 # the 'blur' event. Also, `event.originalTarget` does _not_ work here.
146 @keepInputs = (@vim.state.inputs and event.target in @vim.state.inputs)
147
148 # Clicks are always counted as page interaction. Listen for 'mousedown'
149 # instead of 'click' to mark the interaction as soon as possible.
150 @vim.markPageInteraction()
151 )
152
153 messageManager.listen('browserRefocus', =>
154 # Suppress the next two focus events (for `document` and `window`; see
155 # `blurActiveBrowserElement`).
156 @numFocusToSuppress = 2
157 )
158
159 sendFocusType = =>
160 focusType = utils.getFocusType(utils.getActiveElement(@vim.content))
161 messageManager.send('focusType', focusType)
162
163 @listen('focus', (event) =>
164 target = event.originalTarget
165
166 if @numFocusToSuppress > 0
167 utils.suppressEvent(event)
168 @numFocusToSuppress--
169 return
170
171 @vim.state.explicitBodyFocus = (target == @vim.content.document.body)
172
173 sendFocusType()
174
175 # Reset `hasInteraction` when (re-)selecting a tab, or coming back from
176 # another window, in order to prevent the common “automatically re-focus
177 # when switching back to the tab” behaviour many sites have, unless a text
178 # input _should_ be re-focused when coming back to the tab (see the 'blur'
179 # event below).
180 if target == @vim.content.document
181 if @vim.state.shouldRefocus
182 @vim.state.hasInteraction = true
183 @vim.state.shouldRefocus = false
184 else
185 @vim.state.hasInteraction = false
186 return
187
188 # Save the last focused text input regardless of whether that input might
189 # be blurred because of autofocus prevention.
190 if utils.isTextInputElement(target)
191 @vim.state.lastFocusedTextInput = target
192 @vim.state.hasFocusedTextInput = true
193
194 focusManager = Cc['@mozilla.org/focus-manager;1']
195 .getService(Ci.nsIFocusManager)
196
197 # When moving a tab to another window, there is a short period of time
198 # when there’s no listener for this call.
199 return unless options = @vim.options(
200 ['prevent_autofocus', 'prevent_autofocus_modes']
201 )
202
203 # Blur the focus target, if autofocus prevention is enabled…
204 if options.prevent_autofocus and
205 @vim.mode in options.prevent_autofocus_modes and
206 # …and the user has interacted with the page…
207 not @vim.state.hasInteraction and
208 # …and the event is programmatic (not caused by clicks or keypresses)…
209 focusManager.getLastFocusMethod(null) == 0 and
210 # …and the target may steal most keystrokes.
211 utils.isTypingElement(target)
212 # Some sites (such as icloud.com) re-focuses inputs if they are blurred,
213 # causing an infinite loop of autofocus prevention and re-focusing.
214 # Therefore, blur events that happen just after an autofocus prevention
215 # are suppressed.
216 @listenOnce('blur', utils.suppressEvent)
217 target.blur()
218 @vim.state.hasFocusedTextInput = false
219 )
220
221 @listen('blur', (event) =>
222 target = event.originalTarget
223
224 sendFocusType()
225
226 # If a text input is blurred immediately before the document loses focus,
227 # it most likely means that the user switched tab, for example by pressing
228 # `<c-tab>`, or switched to another window, while the text input was
229 # focused. In this case, when switching back to that tab, the text input
230 # will, and should, be re-focused (because it was focused when you left
231 # the tab). This case is kept track of so that the autofocus prevention
232 # does not catch it.
233 if utils.isTypingElement(target)
234 utils.nextTick(@vim.content, =>
235 @vim.state.shouldRefocus = not @vim.content.document.hasFocus()
236
237 # “gi mode” ends when blurring a text input, unless `<tab>` was just
238 # pressed.
239 unless @vim.state.shouldRefocus or @keepInputs
240 commands.clear_inputs({@vim})
241 )
242 )
243
244 module.exports = FrameEventManager
Imprint / Impressum