2 , setPref } = require 'prefs'
3 { injectHelp } = require 'help'
4 { commands } = require 'commands'
5 utils = require 'utils'
6 { unload } = require 'unload'
9 KEYSET_ID = 'vimfx-keyset'
10 BUTTON_ID = 'vimfx-toolbar-button'
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'
18 $ = (document, sel) -> document.getElementById(sel)
19 $$ = (document, sel) -> document.querySelectorAll(sel)
23 setButtonInstallPosition = (toolbarId, beforeId) ->
24 positions[BUTTON_ID] = {toolbarId, beforeId}
26 addToolbarButton = (window) ->
27 document = window.document
28 win = document.querySelector('window')
30 [ button, keyset ] = createButton(window)
32 # Namespace to put the VimFx state on, for example
35 restorePosition(document, button)
37 if tabWindow = utils.getCurrentTabWindow(window)
38 blacklisted = utils.isBlacklisted(tabWindow.location.href)
39 disabled = getPref('disabled')
40 updateToolbarButton(window, {disabled, blacklisted})
42 win.appendChild(keyset)
47 $(document, 'navigator-toolbox').palette.removeChild(button)
49 createButton = (window) ->
50 document = window.document
52 button = utils.createElement(document, 'toolbarbutton', {
56 class: 'toolbarbutton-1'
59 onButtonCommand = (event) ->
60 disabled = not getPref('disabled')
61 setPref('disabled', disabled)
62 updateToolbarButton(window, {disabled})
64 event.stopPropagation()
66 button.addEventListener('command', onButtonCommand, false)
68 createMenupopup(window, button)
70 vimkey = utils.createElement(document, 'key', {
73 modifiers: 'shift,alt'
76 vimkey.addEventListener('command', onButtonCommand, false)
78 keyset = utils.createElement(document, 'keyset', {id: KEYSET_ID})
79 keyset.appendChild(vimkey)
81 return [button, keyset]
83 createMenupopup = (window, button) ->
84 document = window.document
86 blacklistTextbox = utils.createElement(document, 'textbox', {
87 id: TEXTBOX_BLACKLIST_ID
89 blacklistButton = utils.createElement(document, 'toolbarbutton', {
90 id: BUTTON_BLACKLIST_ID
91 class: 'toolbarbutton-1'
93 blacklistControls = utils.createElement(document, 'hbox')
94 blacklistControls.appendChild(blacklistTextbox)
95 blacklistControls.appendChild(blacklistButton)
97 itemPreferences = utils.createElement(document, 'menuitem', {
99 label: _('item_preferences')
102 itemHelp = utils.createElement(document, 'menuitem', {
104 label: _('help_title')
107 menupopup = utils.createElement(document, 'menupopup', {
111 menupopup.appendChild(blacklistControls)
112 menupopup.appendChild(itemPreferences)
113 menupopup.appendChild(itemHelp)
115 onPopupShowing = (event) ->
116 return unless tabWindow = utils.getCurrentTabWindow(window)
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')
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')
131 onBlacklistButtonCommand = (event) ->
132 return unless tabWindow = utils.getCurrentTabWindow(window)
134 if button.VimFx.blacklisted
135 utils.updateBlacklist({remove: blacklistTextbox.value})
137 utils.updateBlacklist({add: blacklistTextbox.value})
139 menupopup.hidePopup()
141 tabWindow.location.reload(false)
143 event.stopPropagation()
145 onPreferencesCommand = (event) ->
146 id = encodeURIComponent(getPref('addon_id'))
147 window.BrowserOpenAddonsMgr("addons://detail/#{ id }/preferences")
149 event.stopPropagation()
151 onHelpCommand = (event) ->
152 if tabWindow = utils.getCurrentTabWindow(window)
153 injectHelp(tabWindow.document, commands)
155 event.stopPropagation()
157 menupopup.addEventListener('popupshowing', onPopupShowing, false)
158 blacklistButton.addEventListener('command', onBlacklistButtonCommand, false)
159 itemPreferences.addEventListener('command', onPreferencesCommand, false)
160 itemHelp.addEventListener('command', onHelpCommand, false)
162 button.appendChild(menupopup)
165 restorePosition = (document, button) ->
166 $(document, 'navigator-toolbox').palette.appendChild(button)
168 for tb in $$(document, 'toolbar')
169 currentset = tb.getAttribute('currentset').split(',')
170 idx = currentset.indexOf(button.id)
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)
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)
189 toolbar.insertItem(button.id)
191 persist = (document, toolbar, buttonId, beforeId) ->
192 currentset = toolbar.currentSet.split(',')
193 idx = if beforeId then currentset.indexOf(beforeId) else -1
195 currentset.splice(idx, 0, buttonId)
197 currentset.push(buttonId)
199 toolbar.setAttribute('currentset', currentset.join(','))
200 document.persist(toolbar.id, 'currentset')
201 return [currentset, idx]
203 updateToolbarButton = (window, { disabled, blacklisted, insertMode }) ->
204 return unless button = $(window.document, BUTTON_ID)
206 button.VimFx.disabled = disabled if disabled?
207 button.VimFx.blacklisted = blacklisted if blacklisted?
208 button.VimFx.insertMode = insertMode if insertMode?
210 [ icon, tooltip ] = switch
211 when button.VimFx.disabled
213 when button.VimFx.blacklisted
214 ['red', 'blacklisted']
215 when button.VimFx.insertMode
216 ['grey', 'insertMode']
218 ['normal', 'enabled']
220 button.style.listStyleImage = iconUrl(icon)
221 button.setAttribute('tooltiptext', _("button_tooltip_#{ tooltip }"))
224 url = utils.getResourceURI("resources/icon16-#{ kind }.png").spec
225 return "url(#{ url })"
227 exports.setButtonInstallPosition = setButtonInstallPosition
228 exports.addToolbarButton = addToolbarButton
229 exports.updateToolbarButton = updateToolbarButton