]> git.gir.st - VimFx.git/blob - extension/packages/help.coffee
Merge branch 'develop' of github.com:akhodakivskiy/VimFx into develop
[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 conflict_cmd = commands.filter((c) => c.keys().indexOf(value.value) != -1)
63 if conflict_cmd.length < 1 || overwriteCmd(conflict_cmd[0], value.value)
64 cmd.keys(cmd.keys().concat(value.value))
65 for div in document.getElementsByClassName('VimFxKeySequence')
66 if div.getAttribute('data-command') == cmd.name
67 div.insertAdjacentHTML('beforeend', hint(cmd, value.value))
68 return
69
70 overwriteCmd = (cmd, key) ->
71 title = _('help_add_shortcut_title')
72 text = _('help_add_shortcut_text_overwrite', null, key, cmd.help())
73 if promptService.confirm(document.defaultView, title, text)
74 cmd.keys(cmd.keys().filter((a) -> a != key))
75 dom = document.querySelectorAll("a[data-key='#{key}']")
76 dom[0].remove() if dom.length > 0
77 return true
78 else
79 return false
80
81 td = (text, klass='') ->
82 """<td class="#{ klass }">#{ text }</td>"""
83
84 hint = (cmd, key) ->
85 keyDisplay = key.replace(/,/g, '')
86 """<a href="#" class="VimFxReset VimFxKeyLink" title="#{ _('help_remove_shortcut') }"
87 data-command="#{ cmd.name }" data-key="#{ key }">#{ keyDisplay }</a>"""
88
89 tr = (cmd) ->
90 hints = """
91 <div class="VimFxKeySequence" data-command="#{ cmd.name }">
92 #{ (hint(cmd, key) for key in cmd.keys()).join('\n') }
93 </div>
94 """
95 dot = """<span class="VimFxDot">&#8729;</span>"""
96 a = """#{ cmd.help() }"""
97 add = """
98 <a href="#" data-command="#{ cmd.name }"
99 class="VimFxAddShortcutLink" title="#{ _('help_add_shortcut') }">&#8862;</a>
100 """
101
102 return """
103 <tr>
104 #{ td(hints) }
105 #{ td(add) }
106 #{ td(dot) }
107 #{ td(a) }
108 </tr>
109 """
110
111 table = (commands) ->
112 """
113 <table>
114 #{ (tr(cmd) for cmd in commands).join('') }
115 </table>
116 """
117
118 section = (title, commands) ->
119 """
120 <div class="VimFxSectionTitle">#{ title }</div>
121 #{ table(commands) }
122 """
123
124 helpDialogHtml = (commands) ->
125 return """
126 <div id="VimFxHelpDialog">
127 <div class="VimFxHeader">
128 <div class="VimFxTitle">
129 <span class="VimFxTitleVim">Vim</span><span class="VimFxTitleFx">Fx</span>
130 <span>#{ _('help_title') }</span>
131 </div>
132 <span class="VimFxVersion">#{ _('help_version') } #{ utils.getVersion() }</span>
133 <a class="VimFxClose" id="VimFxClose" href="#">&#10006;</a>
134 <div class="VimFxClearFix"></div>
135 <p>Did you know that you can add/remove shortucts in this dialog?</p>
136 <div class="VimFxClearFix"></div>
137 <p>Click the shortcut to remove it, and click &#8862; to add new shortcut!</p>
138 </div>
139
140 <div class="VimFxBody">
141 <div class="VimFxColumn">
142 #{ section(_('help_section_urls'), commands.filter((a) -> a.group == 'urls')) }
143 #{ section(_('help_section_nav'), commands.filter((a) -> a.group == 'nav')) }
144 </div>
145 <div class="VimFxColumn">
146 #{ section(_('help_section_tabs'), commands.filter((a) -> a.group == 'tabs')) }
147 #{ section(_('help_section_browse'), commands.filter((a) -> a.group == 'browse')) }
148 #{ section(_('help_section_misc'), commands.filter((a) -> a.group == 'misc')) }
149 </div>
150 <div class="VimFxClearFix"></div>
151 </div>
152
153 <div class="VimFxFooter">
154 <p>#{ _('help_overlapping_hints') }</p>
155 <p>
156 #{ _('help_found_bug') }
157 <a target="_blank" href="https://github.com/akhodakivskiy/VimFx/issues">
158 #{ _('help_report_bug') }
159 </a>
160 </p>
161 <p>
162 #{ _('help_enjoying') }
163 <a target="_blank" href="https://addons.mozilla.org/en-US/firefox/addon/vimfx/">
164 #{ _('help_feedback') }
165 </a>
166 </p>
167 </div>
168 </div>
169 """
170
171 exports.injectHelp = injectHelp
172 exports.removeHelp = removeHelp
Imprint / Impressum