aboutsummaryrefslogtreecommitdiffstats
path: root/content_scripts/mode_insert.coffee
diff options
context:
space:
mode:
authorStephen Blott2015-01-16 12:08:42 +0000
committerStephen Blott2015-01-16 13:42:32 +0000
commitcd49c88eaad6550ba768159347be6c88f1c26d15 (patch)
tree43702a2e9e074ad2e23f9c51d3c5fabf6afa3d76 /content_scripts/mode_insert.coffee
parentbbc7257842293fbd58dd2f84a58c86691ceae3e1 (diff)
downloadvimium-cd49c88eaad6550ba768159347be6c88f1c26d15.tar.bz2
Modes; clean up.
Diffstat (limited to 'content_scripts/mode_insert.coffee')
-rw-r--r--content_scripts/mode_insert.coffee33
1 files changed, 18 insertions, 15 deletions
diff --git a/content_scripts/mode_insert.coffee b/content_scripts/mode_insert.coffee
index 204c629d..ef7223ad 100644
--- a/content_scripts/mode_insert.coffee
+++ b/content_scripts/mode_insert.coffee
@@ -1,10 +1,14 @@
class InsertMode extends Mode
- # There is one permanently-installed instance of InsertMode.
+ # There is one permanently-installed instance of InsertMode. It watches for focus changes and
+ # activates/deactivates itself accordingly.
@permanentInstance: null
constructor: (options = {}) ->
InsertMode.permanentInstance ||= @
+ @permanent = (@ == InsertMode.permanentInstance)
+
+ # If truthy, then options.global indicates that we were activated by the user (with "i").
@global = options.global
defaults =
@@ -17,7 +21,7 @@ class InsertMode extends Mode
@insertModeLock =
if document.activeElement and DomUtils.isEditable document.activeElement
- # We have already focused an input element, so use it.
+ # An input element is already active, so use it.
document.activeElement
else
null
@@ -26,15 +30,16 @@ class InsertMode extends Mode
"blur": (event) => @alwaysContinueBubbling =>
target = event.target
# We can't rely on focus and blur events arriving in the expected order. When the active element
- # changes, we might get "blur" before "focus". The approach we take is to track the active element in
- # @insertModeLock, and exit only when the that element blurs.
+ # changes, we might get "blur" before "focus". We track the active element in @insertModeLock, and
+ # exit only when that element blurs.
@exit event, target if target == @insertModeLock and DomUtils.isFocusable target
"focus": (event) => @alwaysContinueBubbling =>
if @insertModeLock != event.target and DomUtils.isFocusable event.target
@insertModeLock = event.target
Mode.updateBadge()
- isActive: ->
+ isActive: (event) ->
+ return false if event == InsertMode.suppressedEvent
return true if @insertModeLock or @global
# Some sites (e.g. inbox.google.com) change the contentEditable property on the fly (see #1245); and
# unfortunately, the focus event fires *before* the change. Therefore, we need to re-check whether the
@@ -45,7 +50,7 @@ class InsertMode extends Mode
@insertModeLock != null
handleKeydownEvent: (event) ->
- return @continueBubbling if event == InsertMode.suppressedEvent or not @isActive()
+ return @continueBubbling unless @isActive event
return @stopBubblingAndTrue unless KeyboardUtils.isEscape event
DomUtils.suppressKeyupAfterEscape handlerStack
@exit event, event.srcElement
@@ -53,26 +58,24 @@ class InsertMode extends Mode
# Handles keypress and keyup events.
handleKeyEvent: (event) ->
- if @isActive() and event != InsertMode.suppressedEvent then @stopBubblingAndTrue else @continueBubbling
+ if @isActive event then @stopBubblingAndTrue else @continueBubbling
exit: (_, target) ->
if (target and target == @insertModeLock) or @global or target == undefined
@insertModeLock = null
if target and DomUtils.isFocusable target
- # Remove the focus, so the user can't just get himself back into insert mode by typing in the same input
- # box.
+ # Remove the focus, so the user can't just get back into insert mode by typing in the same input box.
# NOTE(smblott, 2014/12/22) Including embeds for .blur() etc. here is experimental. It appears to be
# the right thing to do for most common use cases. However, it could also cripple flash-based sites and
# games. See discussion in #1211 and #1194.
target.blur()
- # Really exit, but only if this isn't the permanently-installed instance.
- if @ == InsertMode.permanentInstance then Mode.updateBadge() else super()
+ # Exit, but only if this isn't the permanently-installed instance.
+ if @permanent then Mode.updateBadge() else super()
- chooseBadge: (badge) ->
- return if badge == InsertMode.suppressedEvent
- badge.badge ||= "I" if @isActive()
+ updateBadge: (badge) ->
+ badge.badge ||= "I" if @isActive badge
- # Static stuff to allow PostFindMode to suppress insert mode.
+ # Static stuff. This allows PostFindMode to suppress insert mode.
@suppressedEvent: null
@suppressEvent: (event) -> @suppressedEvent = event