2 , setPref } = require 'prefs'
4 { injectHelp } = require 'help'
5 { commandsHelp } = require 'commands'
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.preventDefault()
153 event.stopPropagation()
155 button.addEventListener 'command', onButtonCommand, true
157 menupopup = createMenupopup window
158 button.appendChild menupopup
160 vimkey = doc.createElement 'key'
161 vimkey.setAttribute "id", KEY_ID
162 vimkey.setAttribute "key", "V"
163 vimkey.setAttribute "modifiers", "shift,alt"
164 vimkey.setAttribute "oncommand", "void(0);"
165 vimkey.addEventListener "command", onButtonCommand, true
167 keyset = doc.createElement 'keyset'
168 keyset.setAttribute 'id', KEYSET_ID
169 keyset.appendChild(vimkey)
171 return [button, keyset]
174 addToolbarButton = (window) ->
175 doc = window.document
176 win = doc.querySelector 'window'
178 [button, keyset] = createButton window
179 updateToolbarButton button
181 restorePosition doc, button
182 win.appendChild keyset
185 if buttonParent = button.parentNode
186 buttonParent.removeChild button
187 if keysetParent = keyset.parentNode
188 keysetParent.removeChild keyset
189 $(doc, "navigator-toolbox").palette.removeChild(button)
191 updateToolbarButton = (button) ->
192 if getPref 'disabled'
193 button.style.listStyleImage = iconUrl('grey')
194 button.setAttribute 'tooltiptext', _('button_tooltip_disabled')
195 else if button['VimFx_blacklisted']
196 button.style.listStyleImage = iconUrl('red')
197 button.setAttribute 'tooltiptext', _('button_tooltip_blacklisted')
199 button.style.listStyleImage = iconUrl('normal')
200 button.setAttribute 'tooltiptext', _('button_tooltip_enabled')
202 setWindowBlacklisted = (window, blacklisted) ->
203 if button = $(window.document, BUTTON_ID)
204 button['VimFx_blacklisted'] = blacklisted
205 updateToolbarButton button
207 exports.addToolbarButton = addToolbarButton
208 exports.setWindowBlacklisted = setWindowBlacklisted
209 exports.setButtonInstallPosition = setButtonInstallPosition