2 , setPref } = require 'prefs'
4 { showHelp } = require 'help'
5 { commandsHelp } = require 'commands'
9 persist = (document, toolbar, buttonID, beforeID) ->
10 currentset = toolbar.getAttribute('currentset').split(',')
11 idx = if beforeID then currentset.indexOf(beforeID) else -1;
13 currentset.splice(idx, 0, buttonID);
15 currentset.push(buttonID);
17 toolbar.setAttribute "currentset", currentset.join(",")
18 document.persist toolbar.id, "currentset"
19 return [currentset, idx]
21 setButtonDefaultPosition = (buttonId, toolbarId, beforeId) ->
22 positions[buttonId] = [toolbarId, beforeId]
24 $ = (doc, sel) -> doc.getElementById(sel)
25 $$ = (doc, sel) -> doc.querySelectorAll(sel)
27 restorePosition = (doc, button) ->
29 $(doc, "navigator-toolbox").palette.appendChild(button)
31 for tb in $$(doc, "toolbar")
32 currentset = tb.getAttribute('currentset').split(',')
33 idx = currentset.indexOf button.id
38 # Saved position not found, using the default one, after persisting it
39 if !toolbar and (button.id in Object.keys(positions))
40 [tbID, beforeID] = positions[button.id];
41 toolbar = $(doc, tbID)
42 [currentset, idx] = persist(doc, toolbar, button.id, beforeID)
46 # Inserting the button before the first item in `currentset`
47 # after `idx` that is present in the document
48 for i in [idx + 1 ... currentset.length]
49 if before = $(doc, currentset[i])
50 toolbar.insertItem button.id, before
53 toolbar.insertItem button.id
57 normal: getResourceURI('resources/icon16.png').spec
58 grey: getResourceURI('resources/icon16-grey.png').spec
59 red: getResourceURI('resources/icon16-red.png').spec
60 blacklist: getResourceURI('resources/icon16-blacklist.png').spec
62 return (kind) -> "url(#{ kinds[kind] })"
64 createMenupopup = (window) ->
67 blacklistTextbox = doc.createElement 'textbox'
68 blacklistButton = doc.createElement 'toolbarbutton'
69 blacklistButton.setAttribute 'tooltiptext', 'Blacklist'
70 blacklistButton.setAttribute 'class', 'toolbarbutton-1'
71 blacklistButton.style.listStyleImage = iconUrl('blacklist')
72 hbox = doc.createElement 'hbox'
73 hbox.appendChild blacklistTextbox
74 hbox.appendChild blacklistButton
76 itemPreferences = doc.createElement 'menuitem'
77 itemPreferences.setAttribute 'label', 'Preferences'
79 itemHelp = doc.createElement 'menuitem'
80 itemHelp.setAttribute 'label', 'Help'
82 menupopup = doc.createElement 'menupopup'
83 menupopup.appendChild hbox
84 menupopup.appendChild itemPreferences
85 menupopup.appendChild itemHelp
87 onPopupShowing = (event) ->
88 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
89 blacklistTextbox.value = "*#{ tabWindow.location.host }*"
91 onBlacklistButtonCommand = (event) ->
92 blackList = getPref 'black_list'
93 blackList += ', ' if blackList.length > 0
94 blackList += blacklistTextbox.value
96 setPref 'black_list', blackList
99 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
100 tabWindow.location.reload(false)
102 event.stopPropagation()
104 onPreferencesCommand = (event) ->
105 id = encodeURIComponent getPref('addon_id')
106 window.BrowserOpenAddonsMgr("addons://detail/#{ id }/preferences")
108 event.stopPropagation()
110 onHelpCommand = (event) ->
111 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
112 showHelp tabWindow.document, commandsHelp
114 event.stopPropagation()
116 menupopup.addEventListener 'popupshowing', onPopupShowing, false
117 blacklistButton.addEventListener 'command', onBlacklistButtonCommand, false
118 itemPreferences.addEventListener 'command', onPreferencesCommand, false
119 itemHelp.addEventListener 'command', onHelpCommand, false
123 createButton = (window) ->
124 doc = window.document
126 button = doc.createElement 'toolbarbutton'
127 button.setAttribute 'id', getPref 'button_id'
128 button.setAttribute 'type', 'menu-button'
129 button.setAttribute 'label', 'VimFx'
130 button.setAttribute 'class', 'toolbarbutton-1'
132 # Create and install event listeners
133 onButtonCommand = (event) ->
134 # Change disabled state value which is stored in Prefs
135 setPref('disabled', not getPref 'disabled')
136 updateToolbarButton button
138 button.addEventListener 'command', onButtonCommand, false
140 menupopup = createMenupopup window
141 button.appendChild menupopup
146 addToolbarButton = (window) ->
147 doc = window.document
150 button = createButton window
152 updateToolbarButton button
153 restorePosition doc, button, 'nav-bar', 'bookmarks-menu-button-container'
158 button.parentNode.removeChild button
159 $(doc, "navigator-toolbox").palette.removeChild(button)
161 updateToolbarButton = (button) ->
162 if getPref 'disabled'
163 button.style.listStyleImage = iconUrl('grey')
164 button.setAttribute 'tooltiptext', 'VimFx is Disabled. Click to Enable'
165 else if button['VimFx_blacklisted']
166 button.style.listStyleImage = iconUrl('red')
167 button.setAttribute 'tooltiptext', 'VimFx is Blacklisted on this Site'
169 button.style.listStyleImage = iconUrl('normal')
170 button.setAttribute 'tooltiptext', 'VimFx is Enabled. Click to Disable'
172 setWindowBlacklisted = (window, blacklisted) ->
173 if button = $(window.document, getPref 'button_id')
174 button['VimFx_blacklisted'] = blacklisted
175 updateToolbarButton button
177 exports.addToolbarButton = addToolbarButton
178 exports.setWindowBlacklisted = setWindowBlacklisted
179 exports.setButtonDefaultPosition = setButtonDefaultPosition