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 if document instanceof XULDocument
19 container = utils.createElement(document, 'box')
20 # The `.VimFxReset` class is not needed in XUL documents. Instead it actullay causes layout
22 container.classList.remove('VimFxReset')
24 container = utils.createElement(document, 'div')
25 container.id = CONTAINER_ID
27 container.appendChild(utils.parseHTML(document, helpDialogHtml(commands)))
28 for element in container.getElementsByTagName('*')
29 element.classList.add('VimFxReset')
31 document.documentElement.appendChild(container)
33 container.addEventListener('click', removeHandler.bind(undefined, document, commands), false)
34 container.addEventListener('click', addHandler.bind(undefined, document, commands), false)
36 if button = document.getElementById('VimFxClose')
37 clickHandler = (event) ->
38 event.stopPropagation()
39 event.preventDefault()
41 button.addEventListener('click', clickHandler, false)
43 promptService = Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService)
44 removeHandler = (document, commands, event) ->
45 return unless event.target.classList.contains('VimFxKeyLink')
46 event.preventDefault()
47 event.stopPropagation()
48 key = event.target.getAttribute('data-key')
49 name = event.target.getAttribute('data-command')
50 if cmd = commands.reduce(((m, v) -> if (v.name == name) then v else m), null)
51 title = _('help_remove_shortcut_title')
52 text = _('help_remove_shortcut_text')
53 if promptService.confirm(document.defaultView, title, text)
54 cmd.keys(cmd.keys().filter((a) -> a != key))
57 addHandler = (document, commands, event) ->
58 return unless event.target.classList.contains('VimFxAddShortcutLink')
59 event.preventDefault()
60 event.stopPropagation()
61 name = event.target.getAttribute('data-command')
62 if cmd = commands.reduce(((m, v) -> if (v.name == name) then v else m), null)
63 title = _('help_add_shortcut_title')
64 text = _('help_add_shortcut_text')
65 value = { value: null }
66 check = { value: null }
67 if promptService.prompt(document.defaultView, title, text, value, null, check)
68 conflict_cmd = commands.filter((c) => c.keys().indexOf(value.value) != -1)
69 if conflict_cmd.length < 1 || overwriteCmd(conflict_cmd[0], value.value)
70 cmd.keys(cmd.keys().concat(value.value))
71 for div in document.getElementsByClassName('VimFxKeySequence')
72 if div.getAttribute('data-command') == cmd.name
73 div.insertAdjacentHTML('beforeend', hint(cmd, value.value))
76 overwriteCmd = (cmd, key) ->
77 title = _('help_add_shortcut_title')
78 text = _('help_add_shortcut_text_overwrite', null, key, cmd.help())
79 if promptService.confirm(document.defaultView, title, text)
80 cmd.keys(cmd.keys().filter((a) -> a != key))
81 dom = document.querySelectorAll("a[data-key='#{key}']")
82 dom[0].remove() if dom.length > 0
87 td = (text, klass='') ->
88 """<td class="#{ klass }">#{ text }</td>"""
91 keyDisplay = key.replace(/,/g, '')
92 """<a href="#" class="VimFxReset VimFxKeyLink" title="#{ _('help_remove_shortcut') }"
93 data-command="#{ cmd.name }" data-key="#{ key }">#{ keyDisplay }</a>"""
97 <div class="VimFxKeySequence" data-command="#{ cmd.name }">
98 #{ (hint(cmd, key) for key in cmd.keys()).join('\n') }
101 dot = """<span class="VimFxDot">∙</span>"""
102 a = """#{ cmd.help() }"""
104 <a href="#" data-command="#{ cmd.name }"
105 class="VimFxAddShortcutLink" title="#{ _('help_add_shortcut') }">⊞</a>
117 table = (commands) ->
120 #{ (tr(cmd) for cmd in commands).join('') }
124 section = (title, commands) ->
126 <div class="VimFxSectionTitle">#{ title }</div>
130 helpDialogHtml = (commands) ->
132 <div id="VimFxHelpDialog">
133 <div class="VimFxHeader">
134 <div class="VimFxTitle">
135 <span class="VimFxTitleVim">Vim</span><span class="VimFxTitleFx">Fx</span>
136 <span>#{ _('help_title') }</span>
138 <span class="VimFxVersion">#{ _('help_version') } #{ utils.getVersion() }</span>
139 <a class="VimFxClose" id="VimFxClose" href="#">✖</a>
140 <div class="VimFxClearFix"></div>
141 <p>Did you know that you can add/remove shortucts in this dialog?</p>
142 <div class="VimFxClearFix"></div>
143 <p>Click the shortcut to remove it, and click ⊞ to add new shortcut!</p>
146 <div class="VimFxBody">
147 <div class="VimFxColumn">
148 #{ section(_('help_section_urls'), commands.filter((a) -> a.group == 'urls')) }
149 #{ section(_('help_section_nav'), commands.filter((a) -> a.group == 'nav')) }
151 <div class="VimFxColumn">
152 #{ section(_('help_section_tabs'), commands.filter((a) -> a.group == 'tabs')) }
153 #{ section(_('help_section_browse'), commands.filter((a) -> a.group == 'browse')) }
154 #{ section(_('help_section_misc'), commands.filter((a) -> a.group == 'misc')) }
156 <div class="VimFxClearFix"></div>
159 <div class="VimFxFooter">
160 <p>#{ _('help_overlapping_hints') }</p>
162 #{ _('help_found_bug') }
163 <a target="_blank" href="https://github.com/akhodakivskiy/VimFx/issues">
164 #{ _('help_report_bug') }
168 #{ _('help_enjoying') }
169 <a target="_blank" href="https://addons.mozilla.org/en-US/firefox/addon/vimfx/">
170 #{ _('help_feedback') }
177 exports.injectHelp = injectHelp
178 exports.removeHelp = removeHelp