2 , setPref } = require 'prefs'
3 { injectHelp } = require 'help'
4 { commandsHelp } = require 'commands'
5 utils = require 'utils'
7 KEYSET_ID = 'vimfx-keyset'
8 BUTTON_ID = 'vimfx-toolbar-button'
10 MENUPOPUP_ID = 'vimfx-menupopup'
11 MENU_ITEM_PREF = 'vimfx-menu-item-preferences'
12 MENU_ITEM_HELP = 'vimfx-menu-item-help'
13 TEXTBOX_BLACKLIST_ID = 'vimfx-textbox-blacklist-id'
14 BUTTON_BLACKLIST_ID = 'vimfx-button-blacklist-id'
16 $ = (doc, sel) -> doc.getElementById(sel)
17 $$ = (doc, sel) -> doc.querySelectorAll(sel)
21 setButtonInstallPosition = (toolbarId, beforeId) ->
22 positions[BUTTON_ID] = [toolbarId, beforeId]
24 persist = (document, toolbar, buttonId, beforeId) ->
25 currentset = toolbar.currentSet.split(',')
26 idx = if beforeId then currentset.indexOf(beforeId) else -1
28 currentset.splice(idx, 0, buttonId)
30 currentset.push(buttonId)
32 toolbar.setAttribute('currentset', currentset.join(','))
33 document.persist(toolbar.id, 'currentset')
34 return [currentset, idx]
36 restorePosition = (doc, button) ->
37 $(doc, 'navigator-toolbox').palette.appendChild(button)
39 for tb in $$(doc, 'toolbar')
40 currentset = tb.getAttribute('currentset').split(',')
41 idx = currentset.indexOf(button.id)
46 # Saved position not found, using the default one, after persisting it
47 if !toolbar and pos = positions[button.id]
48 [tbID, beforeId] = pos
49 if toolbar = $(doc, tbID)
50 [currentset, idx] = persist(doc, toolbar, button.id, beforeId)
52 if toolbar and idx != -1
53 # Inserting the button before the first item in `currentset`
54 # after `idx` that is present in the document
55 for i in [idx + 1 ... currentset.length]
56 if before = $(doc, currentset[i])
57 toolbar.insertItem(button.id, before)
60 toolbar.insertItem(button.id)
64 normal: getResourceURI('resources/icon16.png').spec
65 grey: getResourceURI('resources/icon16-grey.png').spec
66 red: getResourceURI('resources/icon16-red.png').spec
67 blacklist: getResourceURI('resources/icon16-blacklist.png').spec
69 return (kind) -> "url(#{ kinds[kind] })"
72 createMenupopup = (window) ->
75 blacklistTextbox = doc.createElement('textbox')
76 blacklistTextbox.id = TEXTBOX_BLACKLIST_ID
77 blacklistButton = doc.createElement('toolbarbutton')
78 blacklistButton.id = BUTTON_BLACKLIST_ID
79 blacklistButton.setAttribute('tooltiptext', _('item_blacklist_button_tooltip'))
80 blacklistButton.setAttribute('class', 'toolbarbutton-1')
81 blacklistButton.style.listStyleImage = iconUrl('blacklist')
82 hbox = doc.createElement('hbox')
83 hbox.appendChild(blacklistTextbox)
84 hbox.appendChild(blacklistButton)
86 itemPreferences = doc.createElement('menuitem')
87 itemPreferences.id = MENU_ITEM_PREF
88 itemPreferences.setAttribute('label', _('item_preferences'))
90 itemHelp = doc.createElement('menuitem')
91 itemHelp.id = MENU_ITEM_HELP
92 itemHelp.setAttribute('label', _('item_help'))
94 menupopup = doc.createElement('menupopup')
95 menupopup.id = MENUPOPUP_ID
96 menupopup.setAttribute('ignorekeys', true)
97 menupopup.appendChild(hbox)
98 menupopup.appendChild(itemPreferences)
99 menupopup.appendChild(itemHelp)
101 onPopupShowing = (event) ->
102 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
103 blacklistTextbox.value = "*#{ tabWindow.location.host }*"
105 onBlacklistButtonCommand = (event) ->
106 blackList = getPref('black_list')
107 blackList += ', ' if blackList.length > 0
108 blackList += blacklistTextbox.value
110 setPref('black_list', blackList)
111 menupopup.hidePopup()
113 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
114 tabWindow.location.reload(false)
116 event.stopPropagation()
118 onPreferencesCommand = (event) ->
119 id = encodeURIComponent(getPref('addon_id'))
120 window.BrowserOpenAddonsMgr("addons://detail/#{ id }/preferences")
122 event.stopPropagation()
124 onHelpCommand = (event) ->
125 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
126 injectHelp(tabWindow.document, commandsHelp)
128 event.stopPropagation()
130 menupopup.addEventListener 'popupshowing', onPopupShowing, false
131 blacklistButton.addEventListener 'command', onBlacklistButtonCommand, false
132 itemPreferences.addEventListener 'command', onPreferencesCommand, false
133 itemHelp.addEventListener 'command', onHelpCommand, false
137 createButton = (window) ->
138 doc = window.document
140 button = doc.createElement('toolbarbutton')
141 button.setAttribute('id', BUTTON_ID)
142 button.setAttribute('type', 'menu-button')
143 button.setAttribute('label', 'VimFx')
144 button.setAttribute('class', 'toolbarbutton-1')
146 # Create and install event listeners
147 onButtonCommand = (event) ->
148 # Change disabled state value which is stored in Prefs
149 setPref('disabled', not getPref 'disabled')
150 updateToolbarButton(button)
152 event.stopPropagation()
154 button.addEventListener('command', onButtonCommand, false)
156 menupopup = createMenupopup(window)
157 button.appendChild(menupopup)
159 vimkey = doc.createElement('key')
160 vimkey.setAttribute('id', KEY_ID)
161 vimkey.setAttribute('key', 'V')
162 vimkey.setAttribute('modifiers', 'shift,alt')
163 vimkey.setAttribute('oncommand', 'void(0);')
164 vimkey.addEventListener('command', onButtonCommand, false)
166 keyset = doc.createElement('keyset')
167 keyset.setAttribute('id', KEYSET_ID)
168 keyset.appendChild(vimkey)
170 return [button, keyset]
172 addToolbarButton = (window) ->
173 doc = window.document
174 win = doc.querySelector('window')
176 [button, keyset] = createButton(window)
177 updateToolbarButton(button)
179 restorePosition(doc, button)
180 win.appendChild(keyset)
183 if buttonParent = button.parentNode
184 buttonParent.removeChild(button)
185 if keysetParent = keyset.parentNode
186 keysetParent.removeChild(keyset)
187 $(doc, 'navigator-toolbox').palette.removeChild(button)
189 updateToolbarButton = (button) ->
190 if getPref('disabled')
191 button.style.listStyleImage = iconUrl('grey')
192 button.setAttribute('tooltiptext', _('button_tooltip_disabled'))
193 else if button['VimFx_blacklisted']
194 button.style.listStyleImage = iconUrl('red')
195 button.setAttribute('tooltiptext', _('button_tooltip_blacklisted'))
197 button.style.listStyleImage = iconUrl('normal')
198 button.setAttribute('tooltiptext', _('button_tooltip_enabled'))
200 setWindowBlacklisted = (window, blacklisted) ->
201 if button = $(window.document, BUTTON_ID)
202 button['VimFx_blacklisted'] = blacklisted
203 updateToolbarButton(button)
205 exports.addToolbarButton = addToolbarButton
206 exports.setWindowBlacklisted = setWindowBlacklisted
207 exports.setButtonInstallPosition = setButtonInstallPosition