From 7e1eb2bd253a4231cb6ee0f89b1fde8635435ad2 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Fri, 30 Aug 2013 23:06:25 +0200 Subject: [PATCH] Add live validation of the hint chars setting - onchange: to lowercase and remove duplicate chars - Update pref defaults in locales --- extension/bootstrap.coffee | 3 + extension/locale/de/options.dtd | 4 +- extension/locale/el-GR/options.dtd | 6 +- extension/locale/en-US/options.dtd | 4 +- extension/locale/hu/options.dtd | 4 +- extension/locale/id/options.dtd | 6 +- extension/locale/nl/options.dtd | 4 +- extension/locale/pl/options.dtd | 4 +- extension/locale/ru/options.dtd | 4 +- extension/locale/zh-CN/options.dtd | 4 +- extension/packages/options.coffee | 28 +++++++++ extension/packages/utils.coffee | 91 +++++++++++++++--------------- 12 files changed, 97 insertions(+), 65 deletions(-) create mode 100644 extension/packages/options.coffee diff --git a/extension/bootstrap.coffee b/extension/bootstrap.coffee index 632f956..21961fd 100644 --- a/extension/bootstrap.coffee +++ b/extension/bootstrap.coffee @@ -48,6 +48,7 @@ do (global = this) -> , initPrefValues } = require 'prefs' { setButtonInstallPosition , addToolbarButton } = require 'button' + options = require 'options' { watchWindows } = require 'window-utils' { unload } = require 'unload' @@ -61,6 +62,8 @@ do (global = this) -> loadCss('style') + options.observe() + watchWindows(addEventListeners, 'navigator:browser') watchWindows(addToolbarButton, 'navigator:browser') diff --git a/extension/locale/de/options.dtd b/extension/locale/de/options.dtd index 0486cca..898fdc4 100644 --- a/extension/locale/de/options.dtd +++ b/extension/locale/de/options.dtd @@ -1,8 +1,8 @@ - + - + diff --git a/extension/locale/el-GR/options.dtd b/extension/locale/el-GR/options.dtd index f6e934d..718d69b 100644 --- a/extension/locale/el-GR/options.dtd +++ b/extension/locale/el-GR/options.dtd @@ -1,9 +1,9 @@ - + - + diff --git a/extension/locale/en-US/options.dtd b/extension/locale/en-US/options.dtd index 6a189ce..0998cba 100644 --- a/extension/locale/en-US/options.dtd +++ b/extension/locale/en-US/options.dtd @@ -1,9 +1,9 @@ - + +Wildcards allowed: *, !. Default: *mail.google.com*"> diff --git a/extension/locale/hu/options.dtd b/extension/locale/hu/options.dtd index e2e1674..6691bb2 100644 --- a/extension/locale/hu/options.dtd +++ b/extension/locale/hu/options.dtd @@ -1,8 +1,8 @@ - + - + diff --git a/extension/locale/id/options.dtd b/extension/locale/id/options.dtd index e9038b1..02c08bf 100644 --- a/extension/locale/id/options.dtd +++ b/extension/locale/id/options.dtd @@ -1,9 +1,9 @@ - + - + diff --git a/extension/locale/nl/options.dtd b/extension/locale/nl/options.dtd index 2e1ca79..f889829 100644 --- a/extension/locale/nl/options.dtd +++ b/extension/locale/nl/options.dtd @@ -1,9 +1,9 @@ - + +Speciale tekens toegestaan: *, !. Standaard: *mail.google.com*"> diff --git a/extension/locale/pl/options.dtd b/extension/locale/pl/options.dtd index 393c156..e511bec 100644 --- a/extension/locale/pl/options.dtd +++ b/extension/locale/pl/options.dtd @@ -1,9 +1,9 @@ - + +Dopuszczalne wieloznaczniki: *, !. Domyślnie: *mail.google.com*"> diff --git a/extension/locale/ru/options.dtd b/extension/locale/ru/options.dtd index 2772f4e..c9c6a79 100644 --- a/extension/locale/ru/options.dtd +++ b/extension/locale/ru/options.dtd @@ -1,9 +1,9 @@ - + +Разрешены одстановки: *, !. По умолчанию: *mail.google.com*"> diff --git a/extension/locale/zh-CN/options.dtd b/extension/locale/zh-CN/options.dtd index ac85f0d..3e150b7 100644 --- a/extension/locale/zh-CN/options.dtd +++ b/extension/locale/zh-CN/options.dtd @@ -1,9 +1,9 @@ - + +允许的通配符:*, !. 默认:*mail.google.com*"> diff --git a/extension/packages/options.coffee b/extension/packages/options.coffee new file mode 100644 index 0000000..a9d60da --- /dev/null +++ b/extension/packages/options.coffee @@ -0,0 +1,28 @@ +{ removeDuplicateCharacters } = require 'utils' +{ unload } = require 'unload' +{ getPref } = require 'prefs' + +observer = + observe: (document, topic, addon) -> + return unless addon == getPref('addon_id') + hintCharsInput = document.querySelector('setting[pref="extensions.VimFx.hint_chars"]') + switch topic + when 'addon-options-displayed' + hintCharsInput.addEventListener('change', filterChars, false) + when 'addon-options-hidden' + hintCharsInput.removeEventListener('change', filterChars, false) + +filterChars = (event) -> + input = event.target + input.value = removeDuplicateCharacters(input.value).replace(/\s/g, '') + input.valueToPreference() + +observe = -> + Services.obs.addObserver(observer, 'addon-options-displayed', false) + Services.obs.addObserver(observer, 'addon-options-hidden', false) + + unload -> + Services.obs.removeObserver(observer, 'addon-options-displayed') + Services.obs.removeObserver(observer, 'addon-options-hidden') + +exports.observe = observe diff --git a/extension/packages/utils.coffee b/extension/packages/utils.coffee index 906b6c1..1106fe0 100644 --- a/extension/packages/utils.coffee +++ b/extension/packages/utils.coffee @@ -239,21 +239,21 @@ browserSearchSubmission = (str) -> # Get hint characters, convert them to lower case and fall back # to default hint characters if there are less than 3 chars -getHintChars = do -> - # Remove duplicate characters from string (case insensitive) - removeDuplicateCharacters = (str) -> - seen = {} - return str.toLowerCase() - .split('') - .filter((char) -> if seen[char] then false else (seen[char] = true)) - .join('') - - return -> - hintChars = removeDuplicateCharacters(getPref('hint_chars')) - if hintChars.length < 2 - hintChars = getDefaultPref('hint_chars') - - return hintChars +getHintChars = -> + hintChars = removeDuplicateCharacters(getPref('hint_chars')) + if hintChars.length < 2 + hintChars = getDefaultPref('hint_chars') + + return hintChars + +# Remove duplicate characters from string (case insensitive) +removeDuplicateCharacters = (str) -> + seen = {} + return str + .toLowerCase() + .split('') + .filter((char) -> if seen[char] then false else (seen[char] = true)) + .join('') # Return URI to some file in the extension packaged as resource getResourceURI = do -> @@ -263,33 +263,34 @@ getResourceURI = do -> # Escape string to render it usable in regular expressions regexpEscape = (s) -> s and s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') -exports.Bucket = Bucket -exports.getCurrentTabWindow = getCurrentTabWindow -exports.getEventWindow = getEventWindow -exports.getEventRootWindow = getEventRootWindow -exports.getEventTabBrowser = getEventTabBrowser - -exports.getWindowId = getWindowId -exports.getRootWindow = getRootWindow -exports.isTextInputElement = isTextInputElement -exports.isElementEditable = isElementEditable -exports.getSessionStore = getSessionStore - -exports.loadCss = loadCss - -exports.simulateClick = simulateClick -exports.simulateWheel = simulateWheel -exports.WHEEL_MODE_PIXEL = WHEEL_MODE_PIXEL -exports.WHEEL_MODE_LINE = WHEEL_MODE_LINE -exports.WHEEL_MODE_PAGE = WHEEL_MODE_PAGE -exports.readFromClipboard = readFromClipboard -exports.writeToClipboard = writeToClipboard -exports.timeIt = timeIt -exports.isBlacklisted = isBlacklisted -exports.getVersion = getVersion -exports.parseHTML = parseHTML -exports.isURL = isURL -exports.browserSearchSubmission = browserSearchSubmission -exports.getHintChars = getHintChars -exports.getResourceURI = getResourceURI -exports.regexpEscape = regexpEscape +exports.Bucket = Bucket +exports.getCurrentTabWindow = getCurrentTabWindow +exports.getEventWindow = getEventWindow +exports.getEventRootWindow = getEventRootWindow +exports.getEventTabBrowser = getEventTabBrowser + +exports.getWindowId = getWindowId +exports.getRootWindow = getRootWindow +exports.isTextInputElement = isTextInputElement +exports.isElementEditable = isElementEditable +exports.getSessionStore = getSessionStore + +exports.loadCss = loadCss + +exports.simulateClick = simulateClick +exports.simulateWheel = simulateWheel +exports.WHEEL_MODE_PIXEL = WHEEL_MODE_PIXEL +exports.WHEEL_MODE_LINE = WHEEL_MODE_LINE +exports.WHEEL_MODE_PAGE = WHEEL_MODE_PAGE +exports.readFromClipboard = readFromClipboard +exports.writeToClipboard = writeToClipboard +exports.timeIt = timeIt +exports.isBlacklisted = isBlacklisted +exports.getVersion = getVersion +exports.parseHTML = parseHTML +exports.isURL = isURL +exports.browserSearchSubmission = browserSearchSubmission +exports.getHintChars = getHintChars +exports.removeDuplicateCharacters = removeDuplicateCharacters +exports.getResourceURI = getResourceURI +exports.regexpEscape = regexpEscape -- 2.39.3