From 21da3fcafbb29540788037dbcdbdce79ad14e650 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Wed, 30 Mar 2016 13:07:05 +0100 Subject: Rename handlerStack constants. Problems: - The meanings of some of the Mode/handlerStack constant names is far from obvious. - The same thing is named different things in different places. This changes various constant names such that: - the names used in the handler stack and in the modes are the same. - ditto vis-a-vis DomUtils. Also, break out the core of the handler stacks' `bubbleEvent` method into a switch statements. This makes it more obvious that the cases are mutually exclusive. --- content_scripts/link_hints.coffee | 1 - content_scripts/mode.coffee | 36 +++++++++++++-------------------- content_scripts/mode_insert.coffee | 8 ++++---- content_scripts/mode_key_handler.coffee | 10 ++++----- 4 files changed, 23 insertions(+), 32 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/link_hints.coffee b/content_scripts/link_hints.coffee index 59649e5d..254d9811 100644 --- a/content_scripts/link_hints.coffee +++ b/content_scripts/link_hints.coffee @@ -750,7 +750,6 @@ class WaitForEnter extends Mode else if KeyboardUtils.isEscape event @exit() callback false # false -> isSuccess. - DomUtils.suppressEvent event root = exports ? window root.LinkHints = LinkHints diff --git a/content_scripts/mode.coffee b/content_scripts/mode.coffee index 317fbc86..37321660 100644 --- a/content_scripts/mode.coffee +++ b/content_scripts/mode.coffee @@ -31,12 +31,15 @@ class Mode @modes: [] # Constants; short, readable names for the return values expected by handlerStack.bubbleEvent. - continueBubbling: true - suppressEvent: false - stopBubblingAndTrue: handlerStack.stopBubblingAndTrue - stopBubblingAndFalse: handlerStack.stopBubblingAndFalse + continueBubbling: handlerStack.continueBubbling + suppressEvent: handlerStack.suppressEvent + passEventToPage: handlerStack.passEventToPage + suppressPropagation: handlerStack.suppressPropagation restartBubbling: handlerStack.restartBubbling + alwaysContinueBubbling: handlerStack.alwaysContinueBubbling + alwaysSuppressPropagation: handlerStack.alwaysSuppressPropagation + constructor: (@options = {}) -> @handlers = [] @exitHandlers = [] @@ -53,7 +56,8 @@ class Mode # or 2) to worry about event suppression and event-handler return values. if @options.suppressAllKeyboardEvents for type in [ "keydown", "keypress", "keyup" ] - @options[type] = @alwaysSuppressEvent @options[type] + do (handler = @options[type]) => + @options[type] = (event) => @alwaysSuppressPropagation => handler? event @push keydown: @options.keydown || null @@ -67,7 +71,7 @@ class Mode if @options.indicator? if HUD?.isReady() if @options.indicator then HUD.show @options.indicator else HUD.hide true, false - @stopBubblingAndTrue + @passEventToPage else @continueBubbling # If @options.exitOnEscape is truthy, then the mode will exit when the escape key is pressed. @@ -126,7 +130,7 @@ class Mode _name: "mode-#{@id}/passInitialKeyupEvents" keydown: => @alwaysContinueBubbling -> handlerStack.remove() keyup: (event) => - if KeyboardUtils.isPrintable event then @stopBubblingAndFalse else @stopBubblingAndTrue + if KeyboardUtils.isPrintable event then @suppressPropagation else @passEventToPage # if @options.suppressTrailingKeyEvents is set, then -- on exit -- we suppress all key events until a # subsquent (non-repeat) keydown or keypress. In particular, the intention is to catch keyup events for @@ -136,16 +140,16 @@ class Mode @onExit -> handler = (event) -> if event.repeat - false # Suppress event. + handlerStack.suppressEvent else keyEventSuppressor.exit() - true # Do not suppress event. + handlerStack.continueBubbling keyEventSuppressor = new Mode name: "suppress-trailing-key-events" keydown: handler keypress: handler - keyup: -> handlerStack.stopBubblingAndFalse + keyup: -> handlerStack.suppressPropagation Mode.modes.push this @setIndicator() @@ -181,18 +185,6 @@ class Mode @modeIsActive = false @setIndicator() - # Shorthand for an otherwise long name. This wraps a handler with an arbitrary return value, and always - # yields @continueBubbling instead. This simplifies handlers if they always continue bubbling (a common - # case), because they do not need to be concerned with the value they yield. - alwaysContinueBubbling: handlerStack.alwaysContinueBubbling - - # Shorthand for an event handler which always suppresses event propagation. - alwaysSuppressEvent: (handler = null) -> - (event) => - handler? event - DomUtils.suppressPropagation event - @stopBubblingAndFalse - # Debugging routines. logModes: -> if Mode.debug diff --git a/content_scripts/mode_insert.coffee b/content_scripts/mode_insert.coffee index f86038d6..9cab52e1 100644 --- a/content_scripts/mode_insert.coffee +++ b/content_scripts/mode_insert.coffee @@ -16,7 +16,7 @@ class InsertMode extends Mode new PassNextKeyMode return false - return @stopBubblingAndTrue unless event.type == 'keydown' and KeyboardUtils.isEscape event + return @passEventToPage unless event.type == 'keydown' and KeyboardUtils.isEscape event DomUtils.suppressKeyupAfterEscape handlerStack target = event.srcElement if target and DomUtils.isFocusable target @@ -115,19 +115,19 @@ class PassNextKeyMode extends Mode # We exit on blur because, once we lose the focus, we can no longer track key events. exitOnBlur: window keypress: => - @stopBubblingAndTrue + @passEventToPage keydown: => seenKeyDown = true keyDownCount += 1 - @stopBubblingAndTrue + @passEventToPage keyup: => if seenKeyDown unless 0 < --keyDownCount unless 0 < --count @exit() - @stopBubblingAndTrue + @passEventToPage root = exports ? window root.InsertMode = InsertMode diff --git a/content_scripts/mode_key_handler.coffee b/content_scripts/mode_key_handler.coffee index 96792306..ca20be46 100644 --- a/content_scripts/mode_key_handler.coffee +++ b/content_scripts/mode_key_handler.coffee @@ -40,12 +40,12 @@ class KeyHandlerMode extends Mode if isEscape and (@countPrefix != 0 or @keyState.length != 1) @keydownEvents[event.keyCode] = true @reset() - false # Suppress event. + @suppressEvent # If the help dialog loses the focus, then Escape should hide it; see point 2 in #2045. else if isEscape and HelpDialog?.showing @keydownEvents[event.keyCode] = true HelpDialog.hide() - false # Suppress event. + @suppressEvent else if isEscape @continueBubbling else if @isMappedKey keyChar @@ -57,7 +57,7 @@ class KeyHandlerMode extends Mode # prevent triggering page event listeners (e.g. Google instant Search). @keydownEvents[event.keyCode] = true DomUtils.suppressPropagation event - @stopBubblingAndTrue + @passEventToPage else @continueBubbling @@ -68,7 +68,7 @@ class KeyHandlerMode extends Mode else if @isCountKey keyChar digit = parseInt keyChar @reset if @keyState.length == 1 then @countPrefix * 10 + digit else digit - false # Suppress event. + @suppressEvent else @reset() @continueBubbling @@ -77,7 +77,7 @@ class KeyHandlerMode extends Mode return @continueBubbling unless event.keyCode of @keydownEvents delete @keydownEvents[event.keyCode] DomUtils.suppressPropagation event - @stopBubblingAndTrue + @passEventToPage # This tests whether there is a mapping of keyChar in the current key state (and accounts for pass keys). isMappedKey: (keyChar) -> -- cgit v1.2.3 From dc6562ae1374272c61d9155a4cc50a4b5f5f12ab Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Wed, 30 Mar 2016 19:38:05 +0100 Subject: Tweak 21da3fcafbb29540788037dbcdbdce79ad14e650. --- content_scripts/mode_key_handler.coffee | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/mode_key_handler.coffee b/content_scripts/mode_key_handler.coffee index ca20be46..5e73f100 100644 --- a/content_scripts/mode_key_handler.coffee +++ b/content_scripts/mode_key_handler.coffee @@ -56,8 +56,7 @@ class KeyHandlerMode extends Mode # We will possibly be handling a subsequent keypress event, so suppress propagation of this event to # prevent triggering page event listeners (e.g. Google instant Search). @keydownEvents[event.keyCode] = true - DomUtils.suppressPropagation event - @passEventToPage + @suppressPropagation else @continueBubbling @@ -76,8 +75,7 @@ class KeyHandlerMode extends Mode onKeyup: (event) -> return @continueBubbling unless event.keyCode of @keydownEvents delete @keydownEvents[event.keyCode] - DomUtils.suppressPropagation event - @passEventToPage + @suppressPropagation # This tests whether there is a mapping of keyChar in the current key state (and accounts for pass keys). isMappedKey: (keyChar) -> -- cgit v1.2.3 From 342ec4a2a5a435420b770cec4517e3aa6b31c014 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Thu, 31 Mar 2016 08:46:17 +0100 Subject: Use @suppressEvent instead of false. --- content_scripts/mode_insert.coffee | 2 +- content_scripts/mode_key_handler.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/mode_insert.coffee b/content_scripts/mode_insert.coffee index 9cab52e1..4cb0a39e 100644 --- a/content_scripts/mode_insert.coffee +++ b/content_scripts/mode_insert.coffee @@ -14,7 +14,7 @@ class InsertMode extends Mode # Check for a pass-next-key key. if KeyboardUtils.getKeyCharString(event) in Settings.get "passNextKeyKeys" new PassNextKeyMode - return false + return @suppressEvent return @passEventToPage unless event.type == 'keydown' and KeyboardUtils.isEscape event DomUtils.suppressKeyupAfterEscape handlerStack diff --git a/content_scripts/mode_key_handler.coffee b/content_scripts/mode_key_handler.coffee index 5e73f100..08222d98 100644 --- a/content_scripts/mode_key_handler.coffee +++ b/content_scripts/mode_key_handler.coffee @@ -102,7 +102,7 @@ class KeyHandlerMode extends Mode bgLog "Call #{command.command}[#{count}] (#{@name})" @reset() @commandHandler {command, count} - false # Suppress event. + @suppressEvent root = exports ? window root.KeyHandlerMode = KeyHandlerMode -- cgit v1.2.3