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)
45 if buttonParent = button.parentNode
46 buttonParent.removeChild(button)
47 if keysetParent = keyset.parentNode
48 keysetParent.removeChild(keyset)
49 $(document, 'navigator-toolbox').palette.removeChild(button)
51 createButton = (window) ->
52 document = window.document
54 button = document.createElement('toolbarbutton')
55 button.setAttribute('id', BUTTON_ID)
56 button.setAttribute('type', 'menu-button')
57 button.setAttribute('label', 'VimFx')
58 button.setAttribute('class', 'toolbarbutton-1')
60 onButtonCommand = (event) ->
61 disabled = not getPref('disabled')
62 setPref('disabled', disabled)
63 updateToolbarButton(window, {disabled})
65 event.stopPropagation()
67 button.addEventListener('command', onButtonCommand, false)
69 createMenupopup(window, button)
71 vimkey = document.createElement('key')
72 vimkey.setAttribute('id', KEY_ID)
73 vimkey.setAttribute('key', 'V')
74 vimkey.setAttribute('modifiers', 'shift,alt')
75 vimkey.setAttribute('oncommand', 'void(0);')
76 vimkey.addEventListener('command', onButtonCommand, false)
78 keyset = document.createElement('keyset')
79 keyset.setAttribute('id', KEYSET_ID)
80 keyset.appendChild(vimkey)
82 return [button, keyset]
84 createMenupopup = (window, button) ->
85 document = window.document
87 blacklistTextbox = document.createElement('textbox')
88 blacklistTextbox.id = TEXTBOX_BLACKLIST_ID
89 blacklistButton = document.createElement('toolbarbutton')
90 blacklistButton.id = BUTTON_BLACKLIST_ID
91 blacklistButton.setAttribute('class', 'toolbarbutton-1')
92 hbox = document.createElement('hbox')
93 hbox.appendChild(blacklistTextbox)
94 hbox.appendChild(blacklistButton)
96 itemPreferences = document.createElement('menuitem')
97 itemPreferences.id = MENU_ITEM_PREF
98 itemPreferences.setAttribute('label', _('item_preferences'))
100 itemHelp = document.createElement('menuitem')
101 itemHelp.id = MENU_ITEM_HELP
102 itemHelp.setAttribute('label', _('help_title'))
104 menupopup = document.createElement('menupopup')
105 menupopup.id = MENUPOPUP_ID
106 menupopup.setAttribute('ignorekeys', true)
107 menupopup.appendChild(hbox)
108 menupopup.appendChild(itemPreferences)
109 menupopup.appendChild(itemHelp)
111 onPopupShowing = (event) ->
112 return unless tabWindow = utils.getCurrentTabWindow(window)
114 if button.VimFx.blacklisted
115 matchingRules = utils.getMatchingBlacklistRules(tabWindow.location.href)
116 blacklistTextbox.value = matchingRules.join(', ')
117 blacklistTextbox.setAttribute('readonly', true)
118 blacklistButton.setAttribute('tooltiptext', _('item_blacklist_button_inverse_tooltip'))
119 blacklistButton.style.listStyleImage = iconUrl('blacklist_inverse')
121 # In `about:` pages, the `host` property is an empty string. Fall back to the whole URL
122 blacklistTextbox.value = "*#{ tabWindow.location.host or tabWindow.location.href }*"
123 blacklistTextbox.removeAttribute('readonly')
124 blacklistButton.setAttribute('tooltiptext', _('item_blacklist_button_tooltip'))
125 blacklistButton.style.listStyleImage = iconUrl('blacklist')
127 onBlacklistButtonCommand = (event) ->
128 return unless tabWindow = utils.getCurrentTabWindow(window)
130 if button.VimFx.blacklisted
131 utils.updateBlacklist({remove: blacklistTextbox.value})
133 utils.updateBlacklist({add: blacklistTextbox.value})
135 menupopup.hidePopup()
137 tabWindow.location.reload(false)
139 event.stopPropagation()
141 onPreferencesCommand = (event) ->
142 id = encodeURIComponent(getPref('addon_id'))
143 window.BrowserOpenAddonsMgr("addons://detail/#{ id }/preferences")
145 event.stopPropagation()
147 onHelpCommand = (event) ->
148 if tabWindow = utils.getCurrentTabWindow(window)
149 injectHelp(tabWindow.document, commands)
151 event.stopPropagation()
153 menupopup.addEventListener('popupshowing', onPopupShowing, false)
154 blacklistButton.addEventListener('command', onBlacklistButtonCommand, false)
155 itemPreferences.addEventListener('command', onPreferencesCommand, false)
156 itemHelp.addEventListener('command', onHelpCommand, false)
158 button.appendChild(menupopup)
161 restorePosition = (document, button) ->
162 $(document, 'navigator-toolbox').palette.appendChild(button)
164 for tb in $$(document, 'toolbar')
165 currentset = tb.getAttribute('currentset').split(',')
166 idx = currentset.indexOf(button.id)
171 # Saved position not found, using the default one, after persisting it
172 if !toolbar and button.id of positions
173 { toolbarId, beforeId } = positions[button.id]
174 if toolbar = $(document, toolbarId)
175 [ currentset, idx ] = persist(document, toolbar, button.id, beforeId)
177 if toolbar and idx != -1
178 # Inserting the button before the first item in `currentset`
179 # after `idx` that is present in the document
180 for id in currentset[idx+1..]
181 if before = $(document, id)
182 toolbar.insertItem(button.id, before)
185 toolbar.insertItem(button.id)
187 persist = (document, toolbar, buttonId, beforeId) ->
188 currentset = toolbar.currentSet.split(',')
189 idx = if beforeId then currentset.indexOf(beforeId) else -1
191 currentset.splice(idx, 0, buttonId)
193 currentset.push(buttonId)
195 toolbar.setAttribute('currentset', currentset.join(','))
196 document.persist(toolbar.id, 'currentset')
197 return [currentset, idx]
199 updateToolbarButton = (window, { disabled, blacklisted, insertMode }) ->
200 return unless button = $(window.document, BUTTON_ID)
202 button.VimFx.disabled = disabled if disabled?
203 button.VimFx.blacklisted = blacklisted if blacklisted?
204 button.VimFx.insertMode = insertMode if insertMode?
206 [ icon, tooltip ] = switch
207 when button.VimFx.disabled
209 when button.VimFx.blacklisted
210 ['red', 'blacklisted']
211 when button.VimFx.insertMode
212 ['grey', 'insertMode']
214 ['normal', 'enabled']
216 button.style.listStyleImage = iconUrl(icon)
217 button.setAttribute('tooltiptext', _("button_tooltip_#{ tooltip }"))
220 url = utils.getResourceURI("resources/icon16-#{ kind }.png").spec
221 return "url(#{ url })"
223 exports.setButtonInstallPosition = setButtonInstallPosition
224 exports.addToolbarButton = addToolbarButton
225 exports.updateToolbarButton = updateToolbarButton