diff options
| author | mrmr1993 | 2015-06-02 17:00:14 +0100 | 
|---|---|---|
| committer | mrmr1993 | 2015-06-10 17:21:23 +0100 | 
| commit | 3e3ec8925d2646f9a7fbec3b69c75df8bdc5e86d (patch) | |
| tree | b0faaa45f58177cf49262cda1fb3f40957eb9cb0 | |
| parent | 36fe16dcafd57484bf4219262c67cc5612bad722 (diff) | |
| download | vimium-3e3ec8925d2646f9a7fbec3b69c75df8bdc5e86d.tar.bz2 | |
Move FindModeHistory to its own file to be used by the HUD iframe too
| -rw-r--r-- | content_scripts/vimium_frontend.coffee | 44 | ||||
| -rw-r--r-- | lib/find_mode_history.coffee | 47 | ||||
| -rw-r--r-- | pages/hud.coffee | 2 | ||||
| -rw-r--r-- | pages/hud.html | 1 | 
4 files changed, 50 insertions, 44 deletions
| diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 295736c6..ef554cae 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -623,50 +623,6 @@ window.refreshCompletionKeys = (response) ->  isValidFirstKey = (keyChar) ->    validFirstKeys[keyChar] || /^[1-9]/.test(keyChar) -# This implements find-mode query history (using the "findModeRawQueryList" setting) as a list of raw queries, -# most recent first. -window.FindModeHistory = -  storage: chrome.storage.local -  key: "findModeRawQueryList" -  max: 50 -  rawQueryList: null -  isIncognitoMode: chrome.extension.inIncognitoContext - -  init: -> -    unless @rawQueryList -      @rawQueryList = [] # Prevent repeated initialization. -      @key = "findModeRawQueryListIncognito" if @isIncognitoMode -      @storage.get @key, (items) => -        unless chrome.runtime.lastError -          @rawQueryList = items[@key] if items[@key] -          if @isIncognitoMode and not items[@key] -            # This is the first incognito tab, so we need to initialize the incognito-mode query history. -            @storage.get "findModeRawQueryList", (items) => -              unless chrome.runtime.lastError -                @rawQueryList = items.findModeRawQueryList -                @storage.set findModeRawQueryListIncognito: @rawQueryList - -    chrome.storage.onChanged.addListener (changes, area) => -      @rawQueryList = changes[@key].newValue if changes[@key] - -  getQuery: (index = 0) -> -    @rawQueryList[index] or "" - -  saveQuery: (query) -> -    if 0 < query.length -      @rawQueryList = @refreshRawQueryList query, @rawQueryList -      newSetting = {}; newSetting[@key] = @rawQueryList -      @storage.set newSetting -      # If there are any active incognito-mode tabs, then propagte this query to those tabs too. -      unless @isIncognitoMode -        @storage.get "findModeRawQueryListIncognito", (items) => -          if not chrome.runtime.lastError and items.findModeRawQueryListIncognito -            @storage.set -              findModeRawQueryListIncognito: @refreshRawQueryList query, items.findModeRawQueryListIncognito - -  refreshRawQueryList: (query, rawQueryList) -> -    ([ query ].concat rawQueryList.filter (q) => q != query)[0..@max] -  # should be called whenever rawQuery is modified.  window.updateFindModeQuery = ->    # the query can be treated differently (e.g. as a plain string versus regex depending on the presence of diff --git a/lib/find_mode_history.coffee b/lib/find_mode_history.coffee new file mode 100644 index 00000000..dd21a2b9 --- /dev/null +++ b/lib/find_mode_history.coffee @@ -0,0 +1,47 @@ +# NOTE(mrmr1993): This is under lib/ since it is used by both content scripts and iframes from pages/. +# This implements find-mode query history (using the "findModeRawQueryList" setting) as a list of raw queries, +# most recent first. +FindModeHistory = +  storage: chrome.storage.local +  key: "findModeRawQueryList" +  max: 50 +  rawQueryList: null +  isIncognitoMode: chrome.extension.inIncognitoContext + +  init: -> +    unless @rawQueryList +      @rawQueryList = [] # Prevent repeated initialization. +      @key = "findModeRawQueryListIncognito" if @isIncognitoMode +      @storage.get @key, (items) => +        unless chrome.runtime.lastError +          @rawQueryList = items[@key] if items[@key] +          if @isIncognitoMode and not items[@key] +            # This is the first incognito tab, so we need to initialize the incognito-mode query history. +            @storage.get "findModeRawQueryList", (items) => +              unless chrome.runtime.lastError +                @rawQueryList = items.findModeRawQueryList +                @storage.set findModeRawQueryListIncognito: @rawQueryList + +    chrome.storage.onChanged.addListener (changes, area) => +      @rawQueryList = changes[@key].newValue if changes[@key] + +  getQuery: (index = 0) -> +    @rawQueryList[index] or "" + +  saveQuery: (query) -> +    if 0 < query.length +      @rawQueryList = @refreshRawQueryList query, @rawQueryList +      newSetting = {}; newSetting[@key] = @rawQueryList +      @storage.set newSetting +      # If there are any active incognito-mode tabs, then propagte this query to those tabs too. +      unless @isIncognitoMode +        @storage.get "findModeRawQueryListIncognito", (items) => +          if not chrome.runtime.lastError and items.findModeRawQueryListIncognito +            @storage.set +              findModeRawQueryListIncognito: @refreshRawQueryList query, items.findModeRawQueryListIncognito + +  refreshRawQueryList: (query, rawQueryList) -> +    ([ query ].concat rawQueryList.filter (q) => q != query)[0..@max] + +root = exports ? window +root.FindModeHistory = FindModeHistory diff --git a/pages/hud.coffee b/pages/hud.coffee index 01abba74..a3599c8a 100644 --- a/pages/hud.coffee +++ b/pages/hud.coffee @@ -63,3 +63,5 @@ handlers =  UIComponentServer.registerHandler (event) ->    {data} = event    handlers[data.name]? data + +FindModeHistory.init() diff --git a/pages/hud.html b/pages/hud.html index 031c2fbf..60d737e1 100644 --- a/pages/hud.html +++ b/pages/hud.html @@ -4,6 +4,7 @@      <link rel="stylesheet" type="text/css" href="../content_scripts/vimium.css" />      <script type="text/javascript" src="../lib/dom_utils.js"></script>      <script type="text/javascript" src="../lib/keyboard_utils.js"></script> +    <script type="text/javascript" src="../lib/find_mode_history.js"></script>      <script type="text/javascript" src="ui_component_server.js"></script>      <script type="text/javascript" src="hud.js"></script>    </head> | 
