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',
28 removeHandler.bind(undefined, document, commands), false)
29 container.addEventListener('click',
30 addHandler.bind(undefined, document, commands), false)
32 if button = document.getElementById('VimFxClose')
33 clickHandler = (event) ->
34 event.stopPropagation()
35 event.preventDefault()
37 button.addEventListener('click', clickHandler, false)
39 promptService = Cc['@mozilla.org/embedcomp/prompt-service;1']
40 .getService(Ci.nsIPromptService)
42 removeHandler = (document, commands, event) ->
43 return unless event.target.classList.contains('VimFxKeyLink')
44 event.preventDefault()
45 event.stopPropagation()
46 key = event.target.getAttribute('data-key')
47 name = event.target.getAttribute('data-command')
48 if cmd = commands.reduce(((m, v) -> if (v.name == name) then v else m), null)
49 title = _('help_remove_shortcut_title')
50 text = _('help_remove_shortcut_text')
51 if promptService.confirm(document.defaultView, title, text)
52 cmd.keys(cmd.keys().filter((a) -> a != key))
55 addHandler = (document, commands, event) ->
56 return unless event.target.classList.contains('VimFxAddShortcutLink')
57 event.preventDefault()
58 event.stopPropagation()
59 name = event.target.getAttribute('data-command')
60 if cmd = commands.reduce(((m, v) -> if (v.name == name) then v else m), null)
61 title = _('help_add_shortcut_title')
62 text = _('help_add_shortcut_text')
65 if promptService.prompt(document.defaultView, title, text, value, null, check)
66 return if value.value.length == 0
67 conflicts = getConflicts(commands, value.value)
68 if conflicts.length == 0 or overwriteCmd(document, conflicts, value.value)
69 cmd.keys(cmd.keys().concat(value.value))
70 for div in document.getElementsByClassName('VimFxKeySequence')
71 if div.getAttribute('data-command') == cmd.name
72 div.insertAdjacentHTML('beforeend', hint(cmd, value.value))
75 getConflicts = (commands, value) ->
77 for command in commands
79 for key in command.keys()
80 shortest = Math.min(value.length, key.length)
81 if "#{ value },"[..shortest] == "#{ key },"[..shortest]
82 conflictingKeys.push(key)
83 if conflictingKeys.length > 0
84 conflicts.push({command, conflictingKeys})
87 overwriteCmd = (document, conflicts, key) ->
88 title = _('help_add_shortcut_title')
89 conflictSummary = conflicts.map((conflict) ->
90 return "#{ conflict.command.help() }: #{ conflict.conflictingKeys.join(' ') }"
93 #{ _('help_add_shortcut_text_overwrite', key) }
97 if promptService.confirm(document.defaultView, title, text)
98 for { command, conflictingKeys } in conflicts
99 command.keys(command.keys().filter((key) -> key not in conflictingKeys))
100 for key in conflictingKeys
101 document.querySelector("a[data-key='#{ key }']").remove()
106 td = (text, klass='') ->
107 """<td class="#{ klass }">#{ text }</td>"""
110 keyDisplay = key.replace(/,/g, '')
111 """<a href="#" class="VimFxReset VimFxKeyLink" title="#{ _('help_remove_shortcut') }"
112 data-command="#{ cmd.name }" data-key="#{ key }">#{ keyDisplay }</a>"""
116 <div class="VimFxKeySequence" data-command="#{ cmd.name }">
117 #{ (hint(cmd, key) for key in cmd.keys()).join('\n') }
120 dot = """<span class="VimFxDot">∙</span>"""
121 a = """#{ cmd.help() }"""
123 <a href="#" data-command="#{ cmd.name }"
124 class="VimFxAddShortcutLink" title="#{ _('help_add_shortcut') }">⊞</a>
136 table = (commands) ->
139 #{ (tr(cmd) for cmd in commands).join('') }
143 section = (title, commands) ->
145 <div class="VimFxSectionTitle">#{ title }</div>
149 helpDialogHtml = (commands) ->
151 <div id="VimFxHelpDialog">
152 <div class="VimFxHeader">
153 <div class="VimFxTitle">
154 <span class="VimFxTitleVim">Vim</span><span class="VimFxTitleFx">Fx</span>
155 <span>#{ _('help_title') }</span>
157 <span class="VimFxVersion">#{ _('help_version') } #{ utils.getVersion() }</span>
158 <a class="VimFxClose" id="VimFxClose" href="#">✖</a>
159 <div class="VimFxClearFix"></div>
160 <p>Did you know that you can add/remove shortucts in this dialog?</p>
161 <div class="VimFxClearFix"></div>
162 <p>Click the shortcut to remove it, and click ⊞ to add new shortcut!</p>
165 <div class="VimFxBody">
166 <div class="VimFxColumn">
167 #{ section(_('help_section_urls'), commands.filter((a) -> a.group == 'urls')) }
168 #{ section(_('help_section_nav'), commands.filter((a) -> a.group == 'nav')) }
170 <div class="VimFxColumn">
171 #{ section(_('help_section_tabs'), commands.filter((a) -> a.group == 'tabs')) }
172 #{ section(_('help_section_browse'), commands.filter((a) -> a.group == 'browse')) }
173 #{ section(_('help_section_misc'), commands.filter((a) -> a.group == 'misc')) }
175 <div class="VimFxClearFix"></div>
178 <div class="VimFxFooter">
179 <p>#{ _('help_overlapping_hints') }</p>
181 #{ _('help_found_bug') }
182 <a target="_blank" href="https://github.com/akhodakivskiy/VimFx/issues">
183 #{ _('help_report_bug') }
187 #{ _('help_enjoying') }
188 <a target="_blank" href="https://addons.mozilla.org/en-US/firefox/addon/vimfx/">
189 #{ _('help_feedback') }
196 exports.injectHelp = injectHelp
197 exports.removeHelp = removeHelp