2 , setPref } = require 'prefs'
4 { showHelp } = require 'help'
8 persist = (document, toolbar, buttonID, beforeID) ->
9 currentset = toolbar.getAttribute('currentset').split(',')
10 idx = if beforeID then currentset.indexOf(beforeID) else -1;
12 currentset.splice(idx, 0, buttonID);
14 currentset.push(buttonID);
16 toolbar.setAttribute "currentset", currentset.join(",")
17 document.persist toolbar.id, "currentset"
18 return [currentset, idx]
20 setButtonDefaultPosition = (buttonId, toolbarId, beforeId) ->
21 positions[buttonId] = [toolbarId, beforeId]
23 $ = (doc, sel) -> doc.getElementById(sel)
24 $$ = (doc, sel) -> doc.querySelectorAll(sel)
26 restorePosition = (doc, button) ->
28 $(doc, "navigator-toolbox").palette.appendChild(button)
30 for tb in $$(doc, "toolbar")
31 currentset = tb.getAttribute('currentset').split(',')
32 idx = currentset.indexOf button.id
37 # Saved position not found, using the default one, after persisting it
38 if !toolbar and (button.id in Object.keys(positions))
39 [tbID, beforeID] = positions[button.id];
40 toolbar = $(doc, tbID)
41 [currentset, idx] = persist(doc, toolbar, button.id, beforeID)
45 # Inserting the button before the first item in `currentset`
46 # after `idx` that is present in the document
47 for i in [idx + 1 ... currentset.length]
48 if before = $(doc, currentset[i])
49 toolbar.insertItem button.id, before
52 toolbar.insertItem button.id
56 normal: getResourceURI('resources/icon16.png').spec
57 grey: getResourceURI('resources/icon16-grey.png').spec
58 red: getResourceURI('resources/icon16-red.png').spec
59 blacklist: getResourceURI('resources/icon16-blacklist.png').spec
61 return (kind) -> "url(#{ kinds[kind] })"
63 createMenupopup = (window) ->
66 blacklistTextbox = doc.createElement 'textbox'
67 blacklistButton = doc.createElement 'toolbarbutton'
68 blacklistButton.setAttribute 'tooltiptext', 'Blacklist'
69 blacklistButton.setAttribute 'class', 'toolbarbutton-1'
70 blacklistButton.style.listStyleImage = iconUrl('blacklist')
71 hbox = doc.createElement 'hbox'
72 hbox.appendChild blacklistTextbox
73 hbox.appendChild blacklistButton
75 itemPreferences = doc.createElement 'menuitem'
76 itemPreferences.setAttribute 'label', 'Preferences'
78 itemHelp = doc.createElement 'menuitem'
79 itemHelp.setAttribute 'label', 'Help'
81 menupopup = doc.createElement 'menupopup'
82 menupopup.appendChild hbox
83 menupopup.appendChild itemPreferences
84 menupopup.appendChild itemHelp
86 onPopupShowing = (event) ->
87 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
88 blacklistTextbox.value = "*#{ tabWindow.location.host }*"
90 onBlacklistButtonCommand = (event) ->
91 blackList = getPref 'black_list'
92 blackList += ', ' if blackList.length > 0
93 blackList += blacklistTextbox.value
95 setPref 'black_list', blackList
98 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
99 tabWindow.location.reload(false)
101 event.stopPropagation()
103 onPreferencesCommand = (event) ->
104 id = encodeURIComponent getPref('addon_id')
105 window.BrowserOpenAddonsMgr("addons://detail/#{ id }/preferences")
107 event.stopPropagation()
109 onHelpCommand = (event) ->
110 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
111 showHelp tabWindow.document
113 event.stopPropagation()
115 menupopup.addEventListener 'popupshowing', onPopupShowing, false
116 blacklistButton.addEventListener 'command', onBlacklistButtonCommand, false
117 itemPreferences.addEventListener 'command', onPreferencesCommand, false
118 itemHelp.addEventListener 'command', onHelpCommand, false
122 createButton = (window) ->
123 doc = window.document
125 button = doc.createElement 'toolbarbutton'
126 button.setAttribute 'id', getPref 'button_id'
127 button.setAttribute 'type', 'menu-button'
128 button.setAttribute 'label', 'VimFx'
129 button.setAttribute 'class', 'toolbarbutton-1'
131 # Create and install event listeners
132 onButtonCommand = (event) ->
133 # Change disabled state value which is stored in Prefs
134 setPref('disabled', not getPref 'disabled')
135 updateToolbarButton button
137 button.addEventListener 'command', onButtonCommand, false
139 menupopup = createMenupopup window
140 button.appendChild menupopup
145 addToolbarButton = (window) ->
146 doc = window.document
149 button = createButton window
151 updateToolbarButton button
152 restorePosition doc, button, 'nav-bar', 'bookmarks-menu-button-container'
157 button.parentNode.removeChild button
158 $(doc, "navigator-toolbox").palette.removeChild(button)
160 updateToolbarButton = (button) ->
161 if getPref 'disabled'
162 button.style.listStyleImage = iconUrl('grey')
163 button.setAttribute 'tooltiptext', 'VimFx is Disabled. Click to Enable'
164 else if button['VimFx_blacklisted']
165 button.style.listStyleImage = iconUrl('red')
166 button.setAttribute 'tooltiptext', 'VimFx is Blacklisted on this Site'
168 button.style.listStyleImage = iconUrl('normal')
169 button.setAttribute 'tooltiptext', 'VimFx is Enabled. Click to Disable'
171 setWindowBlacklisted = (window, blacklisted) ->
172 if button = $(window.document, getPref 'button_id')
173 button['VimFx_blacklisted'] = blacklisted
174 updateToolbarButton button
176 exports.addToolbarButton = addToolbarButton
177 exports.setWindowBlacklisted = setWindowBlacklisted
178 exports.setButtonDefaultPosition = setButtonDefaultPosition