diff options
| author | Stephen Blott | 2015-01-09 10:51:19 +0000 | 
|---|---|---|
| committer | Stephen Blott | 2015-01-09 11:22:04 +0000 | 
| commit | 2e6eb69e99f29acc432b750501168d2a15116e6f (patch) | |
| tree | 345ccca18c55aa9893f94e09b6236a8f94b15d88 /content_scripts/mode_insert.coffee | |
| parent | 5d653d8fbab350ae7737d6f91a93df10477b172d (diff) | |
| download | vimium-2e6eb69e99f29acc432b750501168d2a15116e6f.tar.bz2 | |
Modes; various changes...
- Refactor insert-mode constructor.
- Gneralise focusInput.
Diffstat (limited to 'content_scripts/mode_insert.coffee')
| -rw-r--r-- | content_scripts/mode_insert.coffee | 42 | 
1 files changed, 26 insertions, 16 deletions
diff --git a/content_scripts/mode_insert.coffee b/content_scripts/mode_insert.coffee index bfc79aa9..c0a61d31 100644 --- a/content_scripts/mode_insert.coffee +++ b/content_scripts/mode_insert.coffee @@ -1,8 +1,8 @@  # This mode is installed when insert mode is active.  class InsertMode extends Mode -  constructor: (@insertModeLock = null) -> -    super +  constructor: (options = {}) -> +    defaults =        name: "insert"        badge: "I"        singleton: InsertMode @@ -10,18 +10,23 @@ class InsertMode extends Mode        keypress: (event) => @stopBubblingAndTrue        keyup: (event) => @stopBubblingAndTrue        exitOnEscape: true -      exitOnBlur: @insertModeLock +      blurOnExit: true + +    options = extend defaults, options +    options.exitOnBlur = options.targetElement || null +    super options    exit: (event = null) ->      super() -    element = event?.srcElement -    if element and DomUtils.isFocusable element -      # Remove the focus so the user can't just get himself back into insert mode by typing in the same -      # input box. -      # NOTE(smblott, 2014/12/22) Including embeds for .blur() 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. -      element.blur() +    if @options.blurOnExit +      element = event?.srcElement +      if element and DomUtils.isFocusable element +        # Remove the focus so the user can't just get himself back into insert mode by typing in the same +        # input box. +        # NOTE(smblott, 2014/12/22) Including embeds for .blur() 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. +        element.blur()  # Automatically trigger insert mode:  #   - On a keydown event in a contentEditable element. @@ -39,18 +44,21 @@ class InsertModeTrigger extends Mode            # and unfortunately, the focus event happens *before* the change is made.  Therefore, we need to            # check again whether the active element is contentEditable.            return @continueBubbling unless document.activeElement?.isContentEditable -          new InsertMode document.activeElement +          new InsertMode +            targetElement: document.activeElement            @stopBubblingAndTrue      @push        focus: (event) =>          triggerSuppressor.unlessSuppressed =>            return unless DomUtils.isFocusable event.target -          new InsertMode event.target +          new InsertMode +            targetElement: event.target      # We may already have focussed an input, so check.      if document.activeElement and DomUtils.isEditable document.activeElement -      new InsertMode document.activeElement +      new InsertMode +        targetElement: document.activeElement  # Used by InsertModeBlocker to suppress InsertModeTrigger; see below.  triggerSuppressor = new Utils.Suppressor true @@ -61,6 +69,7 @@ class InsertModeBlocker extends Mode    constructor: (options = {}) ->      triggerSuppressor.suppress()      options.name ||= "insert-blocker" +    options.onClickMode ||= InsertMode      super options      @onExit -> triggerSuppressor.unsuppress() @@ -72,10 +81,11 @@ class InsertModeBlocker extends Mode            # However, there's a corner case.  If the active element is focusable, then we would have been in            # insert mode had we not been blocking the trigger.  Now, clicking on the element will not generate            # a new focus event, so the insert-mode trigger will not fire.  We have to handle this case -          # specially. +          # specially.  @options.onClickMode is the mode to use.            if document.activeElement and                event.target == document.activeElement and DomUtils.isEditable document.activeElement -            new InsertMode document.activeElement +            new @options.onClickMode +              targetElement: document.activeElement  root = exports ? window  root.InsertMode = InsertMode  | 
