]> git.gir.st - VimFx.git/blob - extension/lib/events-frame.coffee
Fix error in the 'underflow' event
[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 messageManager = require('./message-manager')
23 utils = require('./utils')
24
25 class FrameEventManager
26 constructor: (@vim) ->
27 @numFocusToSuppress = 0
28
29 listen: utils.listen.bind(null, FRAME_SCRIPT_ENVIRONMENT)
30 listenOnce: utils.listenOnce.bind(null, FRAME_SCRIPT_ENVIRONMENT)
31
32 addListeners: ->
33 # If the page already was loaded when VimFx was initialized, send the
34 # 'DOMWindowCreated' message straight away.
35 if @vim.content.document.readyState in ['interactive', 'complete']
36 messageManager.send('DOMWindowCreated')
37 else
38 @listen('DOMWindowCreated', (event) ->
39 messageManager.send('DOMWindowCreated')
40 )
41
42 @listen('readystatechange', (event) =>
43 target = event.originalTarget
44
45 # When the page starts loading `.readyState` changes to 'interactive'.
46 return unless target.readyState == 'interactive'
47
48 # If the topmost document starts loading, it means that we have navigated
49 # to a new page or refreshed the page.
50 if target == @vim.content.document
51 @vim.resetState()
52 messageManager.send('locationChange', @vim.content.location.href)
53 else
54 # If the target isn’t the topmost document, it means that a frame has
55 # started loading. Some sites change the `src` attribute of `<iframe>`s
56 # dynamically. Any scrollable elements for the old frame `src` then
57 # become dead and need to be filtered out.
58 @vim.state.scrollableElements.reject(Cu.isDeadWrapper)
59 )
60
61 @listen('click', (event) =>
62 if @vim.mode == 'hints' and event.isTrusted
63 messageManager.send('enterMode', {mode: 'normal'})
64 )
65
66 @listen('overflow', (event) =>
67 target = event.originalTarget
68 return unless computedStyle = @vim.content.getComputedStyle(target)
69 return if computedStyle.getPropertyValue('overflow') == 'hidden'
70 @vim.state.scrollableElements.add(target)
71 )
72
73 @listen('underflow', (event) =>
74 target = event.originalTarget
75 @vim.state.scrollableElements.delete(target)
76 )
77
78 @listen('keydown', (event) =>
79 suppress = @vim.onInput(event)
80
81 # This also suppresses the 'keypress' and 'keyup' events. (Yes, in frame
82 # scripts, suppressing the 'keydown' events does seem to even suppress
83 # the 'keyup' event!)
84 utils.suppressEvent(event) if suppress
85
86 # From this line on, the rest of the code in `addListeners` is more or
87 # less devoted to autofocus prevention. When enabled, focus events that
88 # occur before the user has interacted with page are prevented.
89 #
90 # If this keydown event wasn’t suppressed (`not suppress`), it’s an
91 # obvious interaction with the page. If it _was_ suppressed, though, it’s
92 # an interaction depending on the command triggered; if it calls
93 # `vim.markPageInteraction()` or not.
94 @vim.markPageInteraction() unless suppress
95 )
96
97 @listen('keydown', ((event) ->
98 suppress = messageManager.get('lateKeydown', {
99 defaultPrevented: event.defaultPrevented
100 })
101 utils.suppressEvent(event) if suppress
102 ), false)
103
104 # Clicks are always counted as page interaction. Listen for 'mousedown'
105 # instead of 'click' to mark the interaction as soon as possible.
106 @listen('mousedown', @vim.markPageInteraction.bind(@vim))
107
108 messageManager.listen('browserRefocus', =>
109 # Suppress the next two focus events (for `document` and `window`; see
110 # `blurActiveBrowserElement`).
111 @numFocusToSuppress = 2
112 )
113
114 @listen('focus', (event) =>
115 target = event.originalTarget
116
117 if @numFocusToSuppress > 0
118 utils.suppressEvent(event)
119 @numFocusToSuppress--
120 return
121
122 # Reset `hasInteraction` when (re-)selecting a tab, or coming back from
123 # another window, in order to prevent the common “automatically re-focus
124 # when switching back to the tab” behaviour many sites have, unless a text
125 # input _should_ be re-focused when coming back to the tab (see the 'blur'
126 # event below).
127 if target == @vim.content.document
128 if @vim.state.shouldRefocus
129 @vim.state.hasInteraction = true
130 @vim.state.shouldRefocus = false
131 else
132 @vim.state.hasInteraction = false
133 return
134
135 # Save the last focused text input regardless of whether that input might
136 # be blurred because of autofocus prevention.
137 if utils.isTextInputElement(target)
138 @vim.state.lastFocusedTextInput = target
139
140 focusManager = Cc['@mozilla.org/focus-manager;1']
141 .getService(Ci.nsIFocusManager)
142
143 # When moving a tab to another window, there is a short period of time
144 # when there’s no listener for this call.
145 return unless options = @vim.options(
146 ['prevent_autofocus', 'prevent_autofocus_modes']
147 )
148
149 # Blur the focus target, if autofocus prevention is enabled…
150 if options.prevent_autofocus and
151 @vim.mode in options.prevent_autofocus_modes and
152 # …and the user has interacted with the page…
153 not @vim.state.hasInteraction and
154 # …and the event is programmatic (not caused by clicks or keypresses)…
155 focusManager.getLastFocusMethod(null) == 0 and
156 # …and the target may steal most keystrokes.
157 (utils.isTypingElement(target) or utils.isContentEditable(target))
158 # Some sites (such as icloud.com) re-focuses inputs if they are blurred,
159 # causing an infinite loop of autofocus prevention and re-focusing.
160 # Therefore, blur events that happen just after an autofocus prevention
161 # are suppressed.
162 @listenOnce('blur', utils.suppressEvent)
163 target.blur()
164 )
165
166 @listen('blur', (event) =>
167 target = event.originalTarget
168
169 # If a text input is blurred immediately before the document loses focus,
170 # it most likely means that the user switched tab, for example by pressing
171 # `<c-tab>`, or switched to another window, while the text input was
172 # focused. In this case, when switching back to that tab, the text input
173 # will, and should, be re-focused (because it was focused when you left
174 # the tab). This case is kept track of so that the autofocus prevention
175 # does not catch it.
176 if utils.isTypingElement(target) or utils.isContentEditable(target)
177 utils.nextTick(@vim.content, =>
178 @vim.state.shouldRefocus = not @vim.content.document.hasFocus()
179 )
180 )
181
182 module.exports = FrameEventManager
Imprint / Impressum