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'
58 onButtonCommand = (event) ->
59 disabled = not getPref('disabled')
60 setPref('disabled', disabled)
61 updateToolbarButton(window, {disabled})
63 event.stopPropagation()
65 button.addEventListener('command', onButtonCommand, false)
67 createMenupopup(window, button)
69 vimkey = utils.createElement document, 'key',
72 modifiers: 'shift,alt'
74 vimkey.addEventListener('command', onButtonCommand, false)
76 keyset = utils.createElement document, 'keyset',
78 keyset.appendChild(vimkey)
80 return [button, keyset]
82 createMenupopup = (window, button) ->
83 document = window.document
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)
94 itemPreferences = utils.createElement document, 'menuitem',
96 label: _('item_preferences')
98 itemHelp = utils.createElement document, 'menuitem',
100 label: _('help_title')
102 menupopup = utils.createElement document, 'menupopup',
105 menupopup.appendChild(blacklistControls)
106 menupopup.appendChild(itemPreferences)
107 menupopup.appendChild(itemHelp)
109 onPopupShowing = (event) ->
110 return unless tabWindow = utils.getCurrentTabWindow(window)
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')
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')
125 onBlacklistButtonCommand = (event) ->
126 return unless tabWindow = utils.getCurrentTabWindow(window)
128 if button.VimFx.blacklisted
129 utils.updateBlacklist({remove: blacklistTextbox.value})
131 utils.updateBlacklist({add: blacklistTextbox.value})
133 menupopup.hidePopup()
135 tabWindow.location.reload(false)
137 event.stopPropagation()
139 onPreferencesCommand = (event) ->
140 id = encodeURIComponent(getPref('addon_id'))
141 window.BrowserOpenAddonsMgr("addons://detail/#{ id }/preferences")
143 event.stopPropagation()
145 onHelpCommand = (event) ->
146 if tabWindow = utils.getCurrentTabWindow(window)
147 injectHelp(tabWindow.document, commands)
149 event.stopPropagation()
151 menupopup.addEventListener('popupshowing', onPopupShowing, false)
152 blacklistButton.addEventListener('command', onBlacklistButtonCommand, false)
153 itemPreferences.addEventListener('command', onPreferencesCommand, false)
154 itemHelp.addEventListener('command', onHelpCommand, false)
156 button.appendChild(menupopup)
159 restorePosition = (document, button) ->
160 $(document, 'navigator-toolbox').palette.appendChild(button)
162 for tb in $$(document, 'toolbar')
163 currentset = tb.getAttribute('currentset').split(',')
164 idx = currentset.indexOf(button.id)
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)
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)
183 toolbar.insertItem(button.id)
185 persist = (document, toolbar, buttonId, beforeId) ->
186 currentset = toolbar.currentSet.split(',')
187 idx = if beforeId then currentset.indexOf(beforeId) else -1
189 currentset.splice(idx, 0, buttonId)
191 currentset.push(buttonId)
193 toolbar.setAttribute('currentset', currentset.join(','))
194 document.persist(toolbar.id, 'currentset')
195 return [currentset, idx]
197 updateToolbarButton = (window, { disabled, blacklisted, insertMode }) ->
198 return unless button = $(window.document, BUTTON_ID)
200 button.VimFx.disabled = disabled if disabled?
201 button.VimFx.blacklisted = blacklisted if blacklisted?
202 button.VimFx.insertMode = insertMode if insertMode?
204 [ icon, tooltip ] = switch
205 when button.VimFx.disabled
207 when button.VimFx.blacklisted
208 ['red', 'blacklisted']
209 when button.VimFx.insertMode
210 ['grey', 'insertMode']
212 ['normal', 'enabled']
214 button.style.listStyleImage = iconUrl(icon)
215 button.setAttribute('tooltiptext', _("button_tooltip_#{ tooltip }"))
218 url = utils.getResourceURI("resources/icon16-#{ kind }.png").spec
219 return "url(#{ url })"
221 exports.setButtonInstallPosition = setButtonInstallPosition
222 exports.addToolbarButton = addToolbarButton
223 exports.updateToolbarButton = updateToolbarButton