1 utils = require 'utils'
2 prefs = require 'prefs'
5 { classes: Cc, interfaces: Ci, utils: Cu } = Components
7 XULDocument = Ci.nsIDOMXULDocument
9 CONTAINER_ID = 'VimFxHelpDialogContainer'
11 removeHelp = (document) ->
12 document.getElementById(CONTAINER_ID)?.remove()
14 injectHelp = (document, commands) ->
15 if document.documentElement
18 type = if document instanceof XULDocument then 'box' else 'div'
19 container = utils.createElement(document, type, {id: CONTAINER_ID})
21 container.appendChild(utils.parseHTML(document, helpDialogHtml(commands)))
22 for element in container.getElementsByTagName('*')
23 element.classList.add('VimFxReset')
25 document.documentElement.appendChild(container)
27 container.addEventListener('click', removeHandler.bind(undefined, document, commands), false)
28 container.addEventListener('click', addHandler.bind(undefined, document, commands), false)
30 if button = document.getElementById('VimFxClose')
31 clickHandler = (event) ->
32 event.stopPropagation()
33 event.preventDefault()
35 button.addEventListener('click', clickHandler, false)
37 promptService = Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService)
38 removeHandler = (document, commands, event) ->
39 return unless event.target.classList.contains('VimFxKeyLink')
40 event.preventDefault()
41 event.stopPropagation()
42 key = event.target.getAttribute('data-key')
43 name = event.target.getAttribute('data-command')
44 if cmd = commands.reduce(((m, v) -> if (v.name == name) then v else m), null)
45 title = _('help_remove_shortcut_title')
46 text = _('help_remove_shortcut_text')
47 if promptService.confirm(document.defaultView, title, text)
48 cmd.keys(cmd.keys().filter((a) -> a != key))
51 addHandler = (document, commands, event) ->
52 return unless event.target.classList.contains('VimFxAddShortcutLink')
53 event.preventDefault()
54 event.stopPropagation()
55 name = event.target.getAttribute('data-command')
56 if cmd = commands.reduce(((m, v) -> if (v.name == name) then v else m), null)
57 title = _('help_add_shortcut_title')
58 text = _('help_add_shortcut_text')
59 value = { value: null }
60 check = { value: null }
61 if promptService.prompt(document.defaultView, title, text, value, null, check)
62 return if value.value.length == 0
63 conflicts = getConflicts(commands, value.value)
64 if conflicts.length == 0 or overwriteCmd(document, conflicts, value.value)
65 cmd.keys(cmd.keys().concat(value.value))
66 for div in document.getElementsByClassName('VimFxKeySequence')
67 if div.getAttribute('data-command') == cmd.name
68 div.insertAdjacentHTML('beforeend', hint(cmd, value.value))
71 getConflicts = (commands, value) ->
73 for command in commands
75 for key in command.keys()
76 shortest = Math.min(value.length, key.length)
77 if "#{value},"[..shortest] == "#{key},"[..shortest]
78 conflictingKeys.push(key)
79 if conflictingKeys.length > 0
80 conflicts.push({ command, conflictingKeys })
83 overwriteCmd = (document, conflicts, key) ->
84 title = _('help_add_shortcut_title')
85 conflictSummary = conflicts.map((conflict) ->
86 return "#{ conflict.command.help() }: #{ conflict.conflictingKeys.join(' ') }"
89 #{ _('help_add_shortcut_text_overwrite', key) }
93 if promptService.confirm(document.defaultView, title, text)
94 for { command, conflictingKeys } in conflicts
95 command.keys(command.keys().filter((key) -> key not in conflictingKeys))
96 for key in conflictingKeys
97 document.querySelector("a[data-key='#{key}']").remove()
102 td = (text, klass='') ->
103 """<td class="#{ klass }">#{ text }</td>"""
106 keyDisplay = key.replace(/,/g, '')
107 """<a href="#" class="VimFxReset VimFxKeyLink" title="#{ _('help_remove_shortcut') }"
108 data-command="#{ cmd.name }" data-key="#{ key }">#{ keyDisplay }</a>"""
112 <div class="VimFxKeySequence" data-command="#{ cmd.name }">
113 #{ (hint(cmd, key) for key in cmd.keys()).join('\n') }
116 dot = """<span class="VimFxDot">∙</span>"""
117 a = """#{ cmd.help() }"""
119 <a href="#" data-command="#{ cmd.name }"
120 class="VimFxAddShortcutLink" title="#{ _('help_add_shortcut') }">⊞</a>
132 table = (commands) ->
135 #{ (tr(cmd) for cmd in commands).join('') }
139 section = (title, commands) ->
141 <div class="VimFxSectionTitle">#{ title }</div>
145 helpDialogHtml = (commands) ->
147 <div id="VimFxHelpDialog">
148 <div class="VimFxHeader">
149 <div class="VimFxTitle">
150 <span class="VimFxTitleVim">Vim</span><span class="VimFxTitleFx">Fx</span>
151 <span>#{ _('help_title') }</span>
153 <span class="VimFxVersion">#{ _('help_version') } #{ utils.getVersion() }</span>
154 <a class="VimFxClose" id="VimFxClose" href="#">✖</a>
155 <div class="VimFxClearFix"></div>
156 <p>Did you know that you can add/remove shortucts in this dialog?</p>
157 <div class="VimFxClearFix"></div>
158 <p>Click the shortcut to remove it, and click ⊞ to add new shortcut!</p>
161 <div class="VimFxBody">
162 <div class="VimFxColumn">
163 #{ section(_('help_section_urls'), commands.filter((a) -> a.group == 'urls')) }
164 #{ section(_('help_section_nav'), commands.filter((a) -> a.group == 'nav')) }
166 <div class="VimFxColumn">
167 #{ section(_('help_section_tabs'), commands.filter((a) -> a.group == 'tabs')) }
168 #{ section(_('help_section_browse'), commands.filter((a) -> a.group == 'browse')) }
169 #{ section(_('help_section_misc'), commands.filter((a) -> a.group == 'misc')) }
171 <div class="VimFxClearFix"></div>
174 <div class="VimFxFooter">
175 <p>#{ _('help_overlapping_hints') }</p>
177 #{ _('help_found_bug') }
178 <a target="_blank" href="https://github.com/akhodakivskiy/VimFx/issues">
179 #{ _('help_report_bug') }
183 #{ _('help_enjoying') }
184 <a target="_blank" href="https://addons.mozilla.org/en-US/firefox/addon/vimfx/">
185 #{ _('help_feedback') }
192 exports.injectHelp = injectHelp
193 exports.removeHelp = removeHelp