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 conflict_cmd = commands.filter((c) -> c.keys().indexOf(value.value) != -1)
64 if conflict_cmd.length < 1 or overwriteCmd(conflict_cmd[0], 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 overwriteCmd = (cmd, key) ->
72 title = _('help_add_shortcut_title')
73 text = _('help_add_shortcut_text_overwrite', null, key, cmd.help())
74 if promptService.confirm(document.defaultView, title, text)
75 cmd.keys(cmd.keys().filter((a) -> a != key))
76 dom = document.querySelectorAll("a[data-key='#{key}']")
77 dom[0].remove() if dom.length > 0
82 td = (text, klass='') ->
83 """<td class="#{ klass }">#{ text }</td>"""
86 keyDisplay = key.replace(/,/g, '')
87 """<a href="#" class="VimFxReset VimFxKeyLink" title="#{ _('help_remove_shortcut') }"
88 data-command="#{ cmd.name }" data-key="#{ key }">#{ keyDisplay }</a>"""
92 <div class="VimFxKeySequence" data-command="#{ cmd.name }">
93 #{ (hint(cmd, key) for key in cmd.keys()).join('\n') }
96 dot = """<span class="VimFxDot">∙</span>"""
97 a = """#{ cmd.help() }"""
99 <a href="#" data-command="#{ cmd.name }"
100 class="VimFxAddShortcutLink" title="#{ _('help_add_shortcut') }">⊞</a>
112 table = (commands) ->
115 #{ (tr(cmd) for cmd in commands).join('') }
119 section = (title, commands) ->
121 <div class="VimFxSectionTitle">#{ title }</div>
125 helpDialogHtml = (commands) ->
127 <div id="VimFxHelpDialog">
128 <div class="VimFxHeader">
129 <div class="VimFxTitle">
130 <span class="VimFxTitleVim">Vim</span><span class="VimFxTitleFx">Fx</span>
131 <span>#{ _('help_title') }</span>
133 <span class="VimFxVersion">#{ _('help_version') } #{ utils.getVersion() }</span>
134 <a class="VimFxClose" id="VimFxClose" href="#">✖</a>
135 <div class="VimFxClearFix"></div>
136 <p>Did you know that you can add/remove shortucts in this dialog?</p>
137 <div class="VimFxClearFix"></div>
138 <p>Click the shortcut to remove it, and click ⊞ to add new shortcut!</p>
141 <div class="VimFxBody">
142 <div class="VimFxColumn">
143 #{ section(_('help_section_urls'), commands.filter((a) -> a.group == 'urls')) }
144 #{ section(_('help_section_nav'), commands.filter((a) -> a.group == 'nav')) }
146 <div class="VimFxColumn">
147 #{ section(_('help_section_tabs'), commands.filter((a) -> a.group == 'tabs')) }
148 #{ section(_('help_section_browse'), commands.filter((a) -> a.group == 'browse')) }
149 #{ section(_('help_section_misc'), commands.filter((a) -> a.group == 'misc')) }
151 <div class="VimFxClearFix"></div>
154 <div class="VimFxFooter">
155 <p>#{ _('help_overlapping_hints') }</p>
157 #{ _('help_found_bug') }
158 <a target="_blank" href="https://github.com/akhodakivskiy/VimFx/issues">
159 #{ _('help_report_bug') }
163 #{ _('help_enjoying') }
164 <a target="_blank" href="https://addons.mozilla.org/en-US/firefox/addon/vimfx/">
165 #{ _('help_feedback') }
172 exports.injectHelp = injectHelp
173 exports.removeHelp = removeHelp