From d3aa23a59ff3fb730d8255a8017475ebdace42fa Mon Sep 17 00:00:00 2001 From: Anton Khodakivskiy Date: Mon, 24 Dec 2012 23:26:51 +0200 Subject: [PATCH] Closes #39. Checkboxes are added to the Help Dialog allowing users to disable certain commands --- extension/bootstrap.coffee | 2 - extension/packages/commands.coffee | 19 +++--- extension/packages/help.coffee | 102 +++++++++++++++++------------ extension/packages/prefs.coffee | 78 ++++++++++------------ extension/packages/vim.coffee | 12 ++-- extension/resources/style.css | 2 +- 6 files changed, 115 insertions(+), 100 deletions(-) diff --git a/extension/bootstrap.coffee b/extension/bootstrap.coffee index 3307886..77b7bb7 100644 --- a/extension/bootstrap.coffee +++ b/extension/bootstrap.coffee @@ -54,7 +54,6 @@ do (global = this) -> { loadCss } = require 'utils' { addEventListeners } = require 'events' { getPref - , installPrefObserver , transferPrefs } = require 'prefs' { setButtonInstallPosition , addToolbarButton } = require 'button' @@ -74,7 +73,6 @@ do (global = this) -> loadCss 'style' wu.watchWindows addEventListeners, 'navigator:browser' wu.watchWindows addToolbarButton, 'navigator:browser' - installPrefObserver() # Firefox will call this method on shutdown/disabling global.shutdown = (data, reason) -> diff --git a/extension/packages/commands.coffee b/extension/packages/commands.coffee index 046da11..8baee76 100644 --- a/extension/packages/commands.coffee +++ b/extension/packages/commands.coffee @@ -5,7 +5,7 @@ hints = require 'hints' help = require 'help' find = require 'find' -{ getPref } = require 'prefs' +{ getPref, setPref } = require 'prefs' # Navigate to the address that is currently stored in the system clipboard command_p = (vim) -> @@ -276,10 +276,11 @@ commandGroups = 'H': [ command_H, _('help_command_H') ] 'L': [ command_L, _('help_command_L') ] 'misc': - '/': [ command_find, _('help_command_find') ] + # `.` is added to find command mapping to hack around Russian keyboard layout + '\.|/': [ command_find, _('help_command_find') ] 'n': [ command_n, _('help_command_n') ] 'N': [ command_N, _('help_command_N') ] - # `>` is added to help command mapping to hack around russian keyboard layout + # `>` is added to help command mapping to hack around Russian keyboard layout # See key-utils.coffee for more info '?|>': [ command_help, _('help_command_help') ] 'Esc': [ command_Esc, _('help_command_Esc') ] @@ -300,8 +301,7 @@ commandsHelp = do (commandGroups) -> for group, commandsList of commandGroups helpGroup = {} for keys, command of commandsList - key = keys.replace(/,/g, '').replace('|', ', ') - helpGroup[key] = command[1] + helpGroup[keys] = command[1] helpStrings[group] = helpGroup return helpStrings @@ -358,8 +358,7 @@ findCharHandler = (vim, keyStr, charCode) -> find.setFindStr vim.window.document, "#{ vim.findStr } (Not Found)" - -exports.hintCharHandler = hintCharHandler -exports.findCharHandler = findCharHandler -exports.commands = commands -exports.commandsHelp = commandsHelp +exports.hintCharHandler = hintCharHandler +exports.findCharHandler = findCharHandler +exports.commands = commands +exports.commandsHelp = commandsHelp diff --git a/extension/packages/help.coffee b/extension/packages/help.coffee index c368e80..1c946ea 100644 --- a/extension/packages/help.coffee +++ b/extension/packages/help.coffee @@ -1,4 +1,5 @@ -utils = require 'utils' +utils = require 'utils' +prefs = require 'prefs' CONTAINER_ID = 'VimFxHelpDialogContainer' @@ -18,6 +19,8 @@ injectHelp = (document, commandsHelp) -> document.documentElement.appendChild div + installCheckboxHandlers document + if button = document.getElementById('VimFxClose') clickHandler = (event) -> event.stopPropagation() @@ -25,12 +28,31 @@ injectHelp = (document, commandsHelp) -> removeHelp(document) button.addEventListener 'click', clickHandler, false +installCheckboxHandlers = (document) -> + cbs = document.getElementsByClassName "VimFxKeyCheckbox" + for cb in cbs + cb.addEventListener "change", (event)-> + key = event.target.getAttribute "data-key" + + # Checkbox if checked => command is in use + if event.target.checked + prefs.enableCommand key + else + prefs.disableCommand key + td = (text, klass='') -> """#{ text }""" tr = (key, text) -> - key = """#{ key } """ - """#{ td(key, 'VimFxSequence') }#{ td(text) }""" + disabled = prefs.isCommandDisabled key + checked = if disabled then null else "checked" + key = """ + #{ key.replace(/,/g, '').replace('|', ', ') } + + + """ + + return """#{ td(key, 'VimFxSequence') }#{ td(text) }""" table = (commands) -> """ @@ -47,50 +69,48 @@ section = (title, commands) -> helpDialogHtml = (help) -> return """ - -
-
-
- VimFx - #{ _('help') } +
+
+
+ VimFx + #{ _('help') } +
+ #{ _('help_version') } #{ utils.getVersion() } + +
- #{ _('help_version') } #{ utils.getVersion() } - -
-
-
-
- #{ section(_('help_section_urls'), help['urls']) } - #{ section(_('help_section_nav'), help['nav']) } -
-
- #{ section(_('help_section_tabs'), help['tabs']) } - #{ section(_('help_section_browse'), help['browse']) } - #{ section(_('help_section_misc'), help['misc']) } +
+
+ #{ section(_('help_section_urls'), help['urls']) } + #{ section(_('help_section_nav'), help['nav']) } +
+
+ #{ section(_('help_section_tabs'), help['tabs']) } + #{ section(_('help_section_browse'), help['browse']) } + #{ section(_('help_section_misc'), help['misc']) } +
+
-
-
-
-
-

- #{ _('help_found_bug') } - - #{ _('help_report_bug') } - -

-

- #{ _('help_enjoying') } - - #{ _('help_feedback') } - -

+
+
+

+ #{ _('help_found_bug') } + + #{ _('help_report_bug') } + +

+

+ #{ _('help_enjoying') } + + #{ _('help_feedback') } + +

+
-
- -""" + """ exports.injectHelp = injectHelp exports.removeHelp = removeHelp diff --git a/extension/packages/prefs.coffee b/extension/packages/prefs.coffee index 208556d..d8db215 100644 --- a/extension/packages/prefs.coffee +++ b/extension/packages/prefs.coffee @@ -5,7 +5,7 @@ PREF_BRANCH = "extensions.VimFx."; # Default values for the preference # All used preferences should be mentioned here becuase # preference type is derived from here -PREFS = +DEFAULT_PREF_VALUES = addon_id: 'VimFx@akhodakivskiy.github.com' hint_chars: 'asdfgercvhjkl;uinm' disabled: false @@ -14,27 +14,27 @@ PREFS = black_list: '*mail.google.com*' blur_on_esc: true -# Get Firefox preference value of type specified in `PREFS` -getFFPref = do -> +getPref = do -> branch = Services.prefs.getBranch PREF_BRANCH - return (key) -> - value = PREFS[key] + return (key, defaultValue=undefined) -> + type = branch.getPrefType(key) - # Return default value if the preference value hasn't been set yet - if branch.getPrefType(key) == branch.PREF_INVALID - return value; - - switch typeof value - when 'boolean' + switch type + when branch.PREF_BOOL return branch.getBoolPref key - when 'number' + when branch.PREF_INT return branch.getIntPref key - else + when branch.PREF_STRING return branch.getCharPref key + else + if defaultValue != undefined + return defaultValue + else + return DEFAULT_PREF_VALUES[key]; # Assign and save Firefox preference value -setFFPref = do -> +setPref = do -> branch = Services.prefs.getBranch PREF_BRANCH return (key, value) -> @@ -43,35 +43,11 @@ setFFPref = do -> branch.setBoolPref(key, value) when 'number' branch.setIntPref(key, value) + when 'string' + branch.setCharPref(key, value); else - branch.setCharPref(key, String(value)); - -# Set default values and update previously stored values for the preferences -do -> - branch = Services.prefs.getBranch PREF_BRANCH - for key in Object.keys(PREFS) - if branch.getPrefType(key) == branch.PREF_INVALID - setFFPref key, PREFS[key] - else - PREFS[key] = getFFPref key + branch.clearUserPref(key); -# Monitor preference changes and update values in local cache - PREFS -installPrefObserver = -> - branch = Services.prefs.getBranch(PREF_BRANCH) - - observer = - observe: (subject, topic, data) -> - if topic == 'nsPref:changed' and data in Object.keys(PREFS) - PREFS[data] = getFFPref data - - branch.addObserver "", observer, false - unload -> branch.removeObserver "", observer - -# Get preference value from local cache - PREFS -getPref = (key) -> return PREFS[key] - -# Set preference value -setPref = (key, value) -> setFFPref key, value # Transfer all setting values from one branch to another transferPrefs = (from, to) -> @@ -93,8 +69,26 @@ transferPrefs = (from, to) -> fromBranch.deleteBranch("") +# Checks if given command is disabled in the preferences +isCommandDisabled = (key) -> + return getPref("disabled_commands", "").indexOf(key) > -1 + +# Adds command to the disabled list +disableCommand = (key) -> + dc = getPref("disabled_commands", "").split("||") + dc.push key + setPref "disabled_commands", dc.join("||") + +# Enables command +enableCommand = (key) -> + dc = getPref("disabled_commands", "").split("||") + while (index = dc.indexOf(key)) > -1 + dc.splice(index, 1) + setPref "disabled_commands", dc.join("||") exports.getPref = getPref exports.setPref = setPref -exports.installPrefObserver = installPrefObserver exports.transferPrefs = transferPrefs +exports.isCommandDisabled = isCommandDisabled +exports.disableCommand = disableCommand +exports.enableCommand = enableCommand diff --git a/extension/packages/vim.coffee b/extension/packages/vim.coffee index 084cc50..55f73f0 100644 --- a/extension/packages/vim.coffee +++ b/extension/packages/vim.coffee @@ -5,7 +5,9 @@ utils = require 'utils' , findCharHandler } = require 'commands' -{ getPref } = require 'prefs' +{ getPref +, isCommandDisabled +} = require 'prefs' MODE_NORMAL = 1 @@ -64,8 +66,10 @@ class Vim findCommand = (keys) -> for i in [0...keys.length] - if com = commands[keys.slice(i).join(',')] - return com + seq = keys.slice(i).join(',') + if com = commands[seq] + if not isCommandDisabled(seq) + return com return undefined @@ -74,7 +78,7 @@ maybeCommand = (keys) -> sequence = keys.slice(i).join(',') for seq, com of commands if seq.indexOf(sequence) == 0 - return true + return not isCommandDisabled(seq) return false diff --git a/extension/resources/style.css b/extension/resources/style.css index 994df69..78b8a23 100644 --- a/extension/resources/style.css +++ b/extension/resources/style.css @@ -95,7 +95,7 @@ div#VimFxHelpDialog { position:fixed !important; border-radius:3px !important; padding:15px 30px !important; - width:600px !important; + width:700px !important; left:50% !important; /* This needs to be 1/2 width to horizontally center the help dialog */ margin-left:-330px !important; -- 2.39.3