]> git.gir.st - VimFx.git/blob - extension/packages/button.coffee
Call `utils.createElement` consistently
[VimFx.git] / extension / packages / button.coffee
1 { getPref
2 , setPref } = require 'prefs'
3 { injectHelp } = require 'help'
4 { commands } = require 'commands'
5 utils = require 'utils'
6 { unload } = require 'unload'
7 { _ } = require 'l10n'
8
9 KEYSET_ID = 'vimfx-keyset'
10 BUTTON_ID = 'vimfx-toolbar-button'
11 KEY_ID = 'vimfx-key'
12 MENUPOPUP_ID = 'vimfx-menupopup'
13 MENU_ITEM_PREF = 'vimfx-menu-item-preferences'
14 MENU_ITEM_HELP = 'vimfx-menu-item-help'
15 TEXTBOX_BLACKLIST_ID = 'vimfx-textbox-blacklist-id'
16 BUTTON_BLACKLIST_ID = 'vimfx-button-blacklist-id'
17
18 $ = (document, sel) -> document.getElementById(sel)
19 $$ = (document, sel) -> document.querySelectorAll(sel)
20
21 positions = {}
22
23 setButtonInstallPosition = (toolbarId, beforeId) ->
24 positions[BUTTON_ID] = {toolbarId, beforeId}
25
26 addToolbarButton = (window) ->
27 document = window.document
28 win = document.querySelector('window')
29
30 [ button, keyset ] = createButton(window)
31
32 # Namespace to put the VimFx state on, for example
33 button.VimFx = {}
34
35 restorePosition(document, button)
36
37 if tabWindow = utils.getCurrentTabWindow(window)
38 blacklisted = utils.isBlacklisted(tabWindow.location.href)
39 disabled = getPref('disabled')
40 updateToolbarButton(window, {disabled, blacklisted})
41
42 win.appendChild(keyset)
43
44 unload ->
45 button.remove()
46 keyset.remove()
47 $(document, 'navigator-toolbox').palette.removeChild(button)
48
49 createButton = (window) ->
50 document = window.document
51
52 button = utils.createElement(document, 'toolbarbutton', {
53 id: BUTTON_ID
54 type: 'menu-button'
55 label: 'VimFx'
56 class: 'toolbarbutton-1'
57 })
58
59 onButtonCommand = (event) ->
60 disabled = not getPref('disabled')
61 setPref('disabled', disabled)
62 updateToolbarButton(window, {disabled})
63
64 event.stopPropagation()
65
66 button.addEventListener('command', onButtonCommand, false)
67
68 createMenupopup(window, button)
69
70 vimkey = utils.createElement(document, 'key', {
71 id: KEY_ID
72 key: 'V'
73 modifiers: 'shift,alt'
74 oncommand: 'void(0);'
75 })
76 vimkey.addEventListener('command', onButtonCommand, false)
77
78 keyset = utils.createElement(document, 'keyset', {id: KEYSET_ID})
79 keyset.appendChild(vimkey)
80
81 return [button, keyset]
82
83 createMenupopup = (window, button) ->
84 document = window.document
85
86 blacklistTextbox = utils.createElement(document, 'textbox', {
87 id: TEXTBOX_BLACKLIST_ID
88 })
89 blacklistButton = utils.createElement(document, 'toolbarbutton', {
90 id: BUTTON_BLACKLIST_ID
91 class: 'toolbarbutton-1'
92 })
93 blacklistControls = utils.createElement(document, 'hbox')
94 blacklistControls.appendChild(blacklistTextbox)
95 blacklistControls.appendChild(blacklistButton)
96
97 itemPreferences = utils.createElement(document, 'menuitem', {
98 id: MENU_ITEM_PREF
99 label: _('item_preferences')
100 })
101
102 itemHelp = utils.createElement(document, 'menuitem', {
103 id: MENU_ITEM_HELP
104 label: _('help_title')
105 })
106
107 menupopup = utils.createElement(document, 'menupopup', {
108 id: MENUPOPUP_ID
109 ignorekeys: true
110 })
111 menupopup.appendChild(blacklistControls)
112 menupopup.appendChild(itemPreferences)
113 menupopup.appendChild(itemHelp)
114
115 onPopupShowing = (event) ->
116 return unless tabWindow = utils.getCurrentTabWindow(window)
117
118 if button.VimFx.blacklisted
119 matchingRules = utils.getMatchingBlacklistRules(tabWindow.location.href)
120 blacklistTextbox.value = matchingRules.join(', ')
121 blacklistTextbox.setAttribute('readonly', true)
122 blacklistButton.setAttribute('tooltiptext', _('item_blacklist_button_inverse_tooltip'))
123 blacklistButton.style.listStyleImage = iconUrl('blacklist_inverse')
124 else
125 # In `about:` pages, the `host` property is an empty string. Fall back to the whole URL
126 blacklistTextbox.value = "*#{ tabWindow.location.host or tabWindow.location.href }*"
127 blacklistTextbox.removeAttribute('readonly')
128 blacklistButton.setAttribute('tooltiptext', _('item_blacklist_button_tooltip'))
129 blacklistButton.style.listStyleImage = iconUrl('blacklist')
130
131 onBlacklistButtonCommand = (event) ->
132 return unless tabWindow = utils.getCurrentTabWindow(window)
133
134 if button.VimFx.blacklisted
135 utils.updateBlacklist({remove: blacklistTextbox.value})
136 else
137 utils.updateBlacklist({add: blacklistTextbox.value})
138
139 menupopup.hidePopup()
140
141 tabWindow.location.reload(false)
142
143 event.stopPropagation()
144
145 onPreferencesCommand = (event) ->
146 id = encodeURIComponent(getPref('addon_id'))
147 window.BrowserOpenAddonsMgr("addons://detail/#{ id }/preferences")
148
149 event.stopPropagation()
150
151 onHelpCommand = (event) ->
152 if tabWindow = utils.getCurrentTabWindow(window)
153 injectHelp(tabWindow.document, commands)
154
155 event.stopPropagation()
156
157 menupopup.addEventListener('popupshowing', onPopupShowing, false)
158 blacklistButton.addEventListener('command', onBlacklistButtonCommand, false)
159 itemPreferences.addEventListener('command', onPreferencesCommand, false)
160 itemHelp.addEventListener('command', onHelpCommand, false)
161
162 button.appendChild(menupopup)
163 return menupopup
164
165 restorePosition = (document, button) ->
166 $(document, 'navigator-toolbox').palette.appendChild(button)
167
168 for tb in $$(document, 'toolbar')
169 currentset = tb.getAttribute('currentset').split(',')
170 idx = currentset.indexOf(button.id)
171 if idx != -1
172 toolbar = tb
173 break
174
175 # Saved position not found, using the default one, after persisting it
176 if !toolbar and button.id of positions
177 { toolbarId, beforeId } = positions[button.id]
178 if toolbar = $(document, toolbarId)
179 [ currentset, idx ] = persist(document, toolbar, button.id, beforeId)
180
181 if toolbar and idx != -1
182 # Inserting the button before the first item in `currentset`
183 # after `idx` that is present in the document
184 for id in currentset[idx+1..]
185 if before = $(document, id)
186 toolbar.insertItem(button.id, before)
187 return
188
189 toolbar.insertItem(button.id)
190
191 persist = (document, toolbar, buttonId, beforeId) ->
192 currentset = toolbar.currentSet.split(',')
193 idx = if beforeId then currentset.indexOf(beforeId) else -1
194 if idx != -1
195 currentset.splice(idx, 0, buttonId)
196 else
197 currentset.push(buttonId)
198
199 toolbar.setAttribute('currentset', currentset.join(','))
200 document.persist(toolbar.id, 'currentset')
201 return [currentset, idx]
202
203 updateToolbarButton = (window, { disabled, blacklisted, insertMode }) ->
204 return unless button = $(window.document, BUTTON_ID)
205
206 button.VimFx.disabled = disabled if disabled?
207 button.VimFx.blacklisted = blacklisted if blacklisted?
208 button.VimFx.insertMode = insertMode if insertMode?
209
210 [ icon, tooltip ] = switch
211 when button.VimFx.disabled
212 ['grey', 'disabled']
213 when button.VimFx.blacklisted
214 ['red', 'blacklisted']
215 when button.VimFx.insertMode
216 ['grey', 'insertMode']
217 else
218 ['normal', 'enabled']
219
220 button.style.listStyleImage = iconUrl(icon)
221 button.setAttribute('tooltiptext', _("button_tooltip_#{ tooltip }"))
222
223 iconUrl = (kind) ->
224 url = utils.getResourceURI("resources/icon16-#{ kind }.png").spec
225 return "url(#{ url })"
226
227 exports.setButtonInstallPosition = setButtonInstallPosition
228 exports.addToolbarButton = addToolbarButton
229 exports.updateToolbarButton = updateToolbarButton
Imprint / Impressum