]> git.gir.st - VimFx.git/blob - extension/packages/help.coffee
Merge pull request #341 from akhodakivskiy/next-prev-matching
[VimFx.git] / extension / packages / help.coffee
1 utils = require 'utils'
2 prefs = require 'prefs'
3 { _ } = require 'l10n'
4
5 { classes: Cc, interfaces: Ci, utils: Cu } = Components
6
7 XULDocument = Ci.nsIDOMXULDocument
8
9 CONTAINER_ID = 'VimFxHelpDialogContainer'
10
11 removeHelp = (document) ->
12 document.getElementById(CONTAINER_ID)?.remove()
13
14 injectHelp = (document, commands) ->
15 if document.documentElement
16 removeHelp(document)
17
18 type = if document instanceof XULDocument then 'box' else 'div'
19 container = utils.createElement(document, type, {id: CONTAINER_ID})
20
21 container.appendChild(utils.parseHTML(document, helpDialogHtml(commands)))
22 for element in container.getElementsByTagName('*')
23 element.classList.add('VimFxReset')
24
25 document.documentElement.appendChild(container)
26
27 container.addEventListener('click', removeHandler.bind(undefined, document, commands), false)
28 container.addEventListener('click', addHandler.bind(undefined, document, commands), false)
29
30 if button = document.getElementById('VimFxClose')
31 clickHandler = (event) ->
32 event.stopPropagation()
33 event.preventDefault()
34 removeHelp(document)
35 button.addEventListener('click', clickHandler, false)
36
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))
49 event.target.remove()
50
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))
69 return
70
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
78 return true
79 else
80 return false
81
82 td = (text, klass='') ->
83 """<td class="#{ klass }">#{ text }</td>"""
84
85 hint = (cmd, key) ->
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>"""
89
90 tr = (cmd) ->
91 hints = """
92 <div class="VimFxKeySequence" data-command="#{ cmd.name }">
93 #{ (hint(cmd, key) for key in cmd.keys()).join('\n') }
94 </div>
95 """
96 dot = """<span class="VimFxDot">&#8729;</span>"""
97 a = """#{ cmd.help() }"""
98 add = """
99 <a href="#" data-command="#{ cmd.name }"
100 class="VimFxAddShortcutLink" title="#{ _('help_add_shortcut') }">&#8862;</a>
101 """
102
103 return """
104 <tr>
105 #{ td(hints) }
106 #{ td(add) }
107 #{ td(dot) }
108 #{ td(a) }
109 </tr>
110 """
111
112 table = (commands) ->
113 """
114 <table>
115 #{ (tr(cmd) for cmd in commands).join('') }
116 </table>
117 """
118
119 section = (title, commands) ->
120 """
121 <div class="VimFxSectionTitle">#{ title }</div>
122 #{ table(commands) }
123 """
124
125 helpDialogHtml = (commands) ->
126 return """
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>
132 </div>
133 <span class="VimFxVersion">#{ _('help_version') } #{ utils.getVersion() }</span>
134 <a class="VimFxClose" id="VimFxClose" href="#">&#10006;</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 &#8862; to add new shortcut!</p>
139 </div>
140
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')) }
145 </div>
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')) }
150 </div>
151 <div class="VimFxClearFix"></div>
152 </div>
153
154 <div class="VimFxFooter">
155 <p>#{ _('help_overlapping_hints') }</p>
156 <p>
157 #{ _('help_found_bug') }
158 <a target="_blank" href="https://github.com/akhodakivskiy/VimFx/issues">
159 #{ _('help_report_bug') }
160 </a>
161 </p>
162 <p>
163 #{ _('help_enjoying') }
164 <a target="_blank" href="https://addons.mozilla.org/en-US/firefox/addon/vimfx/">
165 #{ _('help_feedback') }
166 </a>
167 </p>
168 </div>
169 </div>
170 """
171
172 exports.injectHelp = injectHelp
173 exports.removeHelp = removeHelp
Imprint / Impressum