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