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) ->
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 pos = positions[button.id]
39 [tbID, beforeID] = pos
40 if toolbar = $(doc, tbID)
41 [currentset, idx] = persist(doc, toolbar, button.id, beforeID)
43 if toolbar and idx != -1
44 # Inserting the button before the first item in `currentset`
45 # after `idx` that is present in the document
46 for i in [idx + 1 ... currentset.length]
47 if before = $(doc, currentset[i])
48 toolbar.insertItem button.id, before
51 toolbar.insertItem button.id
55 normal: getResourceURI('resources/icon16.png').spec
56 grey: getResourceURI('resources/icon16-grey.png').spec
57 red: getResourceURI('resources/icon16-red.png').spec
58 blacklist: getResourceURI('resources/icon16-blacklist.png').spec
60 return (kind) -> "url(#{ kinds[kind] })"
62 createMenupopup = (window) ->
65 blacklistTextbox = doc.createElement 'textbox'
66 blacklistButton = doc.createElement 'toolbarbutton'
67 blacklistButton.setAttribute 'tooltiptext', 'Blacklist'
68 blacklistButton.setAttribute 'class', 'toolbarbutton-1'
69 blacklistButton.style.listStyleImage = iconUrl('blacklist')
70 hbox = doc.createElement 'hbox'
71 hbox.appendChild blacklistTextbox
72 hbox.appendChild blacklistButton
74 itemPreferences = doc.createElement 'menuitem'
75 itemPreferences.setAttribute 'label', 'Preferences'
77 itemHelp = doc.createElement 'menuitem'
78 itemHelp.setAttribute 'label', 'Help'
80 menupopup = doc.createElement 'menupopup'
81 menupopup.appendChild hbox
82 menupopup.appendChild itemPreferences
83 menupopup.appendChild itemHelp
85 onPopupShowing = (event) ->
86 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
87 blacklistTextbox.value = "*#{ tabWindow.location.host }*"
89 onBlacklistButtonCommand = (event) ->
90 blackList = getPref 'black_list'
91 blackList += ', ' if blackList.length > 0
92 blackList += blacklistTextbox.value
94 setPref 'black_list', blackList
97 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
98 tabWindow.location.reload(false)
100 event.stopPropagation()
102 onPreferencesCommand = (event) ->
103 id = encodeURIComponent getPref('addon_id')
104 window.BrowserOpenAddonsMgr("addons://detail/#{ id }/preferences")
106 event.stopPropagation()
108 onHelpCommand = (event) ->
109 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
110 showHelp tabWindow.document, commandsHelp
112 event.stopPropagation()
114 menupopup.addEventListener 'popupshowing', onPopupShowing, false
115 blacklistButton.addEventListener 'command', onBlacklistButtonCommand, false
116 itemPreferences.addEventListener 'command', onPreferencesCommand, false
117 itemHelp.addEventListener 'command', onHelpCommand, false
121 createButton = (window) ->
122 doc = window.document
124 button = doc.createElement 'toolbarbutton'
125 button.setAttribute 'id', getPref 'button_id'
126 button.setAttribute 'type', 'menu-button'
127 button.setAttribute 'label', 'VimFx'
128 button.setAttribute 'class', 'toolbarbutton-1'
130 # Create and install event listeners
131 onButtonCommand = (event) ->
132 # Change disabled state value which is stored in Prefs
133 setPref('disabled', not getPref 'disabled')
134 updateToolbarButton button
136 button.addEventListener 'command', onButtonCommand, false
138 menupopup = createMenupopup window
139 button.appendChild menupopup
144 addToolbarButton = (window) ->
145 doc = window.document
147 button = createButton window
148 updateToolbarButton button
150 restorePosition doc, button, 'nav-bar', 'bookmarks-menu-button-container'
153 if buttonParent = button.parentNode
154 buttonParent.removeChild button
155 $(doc, "navigator-toolbox").palette.removeChild(button)
157 updateToolbarButton = (button) ->
158 if getPref 'disabled'
159 button.style.listStyleImage = iconUrl('grey')
160 button.setAttribute 'tooltiptext', 'VimFx is Disabled. Click to Enable'
161 else if button['VimFx_blacklisted']
162 button.style.listStyleImage = iconUrl('red')
163 button.setAttribute 'tooltiptext', 'VimFx is Blacklisted on this Site'
165 button.style.listStyleImage = iconUrl('normal')
166 button.setAttribute 'tooltiptext', 'VimFx is Enabled. Click to Disable'
168 setWindowBlacklisted = (window, blacklisted) ->
169 if button = $(window.document, getPref 'button_id')
170 button['VimFx_blacklisted'] = blacklisted
171 updateToolbarButton button
173 exports.addToolbarButton = addToolbarButton
174 exports.setWindowBlacklisted = setWindowBlacklisted
175 exports.setButtonDefaultPosition = setButtonDefaultPosition