]> git.gir.st - VimFx.git/blob - extension/packages/button.coffee
Closes #18. Toolbar button that's been removed from the toolbar won't be restored...
[VimFx.git] / extension / packages / button.coffee
1 { getPref
2 , setPref } = require 'prefs'
3
4 { showHelp } = require 'help'
5 { commandsHelp } = require 'commands'
6
7 positions = {}
8
9 persist = (document, toolbar, buttonID, beforeID) ->
10 currentset = toolbar.getAttribute('currentset').split(',')
11 idx = if beforeID then currentset.indexOf(beforeID) else -1
12 if idx != -1
13 currentset.splice(idx, 0, buttonID);
14 else
15 currentset.push(buttonID);
16
17 toolbar.setAttribute "currentset", currentset.join(",")
18 document.persist toolbar.id, "currentset"
19 return [currentset, idx]
20
21 setButtonDefaultPosition = (buttonId, toolbarId, beforeId) ->
22 positions[buttonId] = [toolbarId, beforeId]
23
24 $ = (doc, sel) -> doc.getElementById(sel)
25 $$ = (doc, sel) -> doc.querySelectorAll(sel)
26
27 restorePosition = (doc, button) ->
28 $(doc, "navigator-toolbox").palette.appendChild(button)
29
30 for tb in $$(doc, "toolbar")
31 currentset = tb.getAttribute('currentset').split ','
32 idx = currentset.indexOf button.id
33 if idx != -1
34 toolbar = tb
35 break
36
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)
42
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
49 return
50
51 toolbar.insertItem button.id
52
53 iconUrl = do ->
54 kinds =
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
59
60 return (kind) -> "url(#{ kinds[kind] })"
61
62 createMenupopup = (window) ->
63 doc = window.document
64
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
73
74 itemPreferences = doc.createElement 'menuitem'
75 itemPreferences.setAttribute 'label', 'Preferences'
76
77 itemHelp = doc.createElement 'menuitem'
78 itemHelp.setAttribute 'label', 'Help'
79
80 menupopup = doc.createElement 'menupopup'
81 menupopup.appendChild hbox
82 menupopup.appendChild itemPreferences
83 menupopup.appendChild itemHelp
84
85 onPopupShowing = (event) ->
86 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
87 blacklistTextbox.value = "*#{ tabWindow.location.host }*"
88
89 onBlacklistButtonCommand = (event) ->
90 blackList = getPref 'black_list'
91 blackList += ', ' if blackList.length > 0
92 blackList += blacklistTextbox.value
93
94 setPref 'black_list', blackList
95 menupopup.hidePopup()
96
97 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
98 tabWindow.location.reload(false)
99
100 event.stopPropagation()
101
102 onPreferencesCommand = (event) ->
103 id = encodeURIComponent getPref('addon_id')
104 window.BrowserOpenAddonsMgr("addons://detail/#{ id }/preferences")
105
106 event.stopPropagation()
107
108 onHelpCommand = (event) ->
109 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
110 showHelp tabWindow.document, commandsHelp
111
112 event.stopPropagation()
113
114 menupopup.addEventListener 'popupshowing', onPopupShowing, false
115 blacklistButton.addEventListener 'command', onBlacklistButtonCommand, false
116 itemPreferences.addEventListener 'command', onPreferencesCommand, false
117 itemHelp.addEventListener 'command', onHelpCommand, false
118
119 return menupopup
120
121 createButton = (window) ->
122 doc = window.document
123
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'
129 #
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
135
136 button.addEventListener 'command', onButtonCommand, false
137
138 menupopup = createMenupopup window
139 button.appendChild menupopup
140
141 return button
142
143
144 addToolbarButton = (window) ->
145 doc = window.document
146
147 button = createButton window
148 updateToolbarButton button
149
150 restorePosition doc, button, 'nav-bar', 'bookmarks-menu-button-container'
151
152 unload ->
153 if buttonParent = button.parentNode
154 buttonParent.removeChild button
155 $(doc, "navigator-toolbox").palette.removeChild(button)
156
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'
164 else
165 button.style.listStyleImage = iconUrl('normal')
166 button.setAttribute 'tooltiptext', 'VimFx is Enabled. Click to Disable'
167
168 setWindowBlacklisted = (window, blacklisted) ->
169 if button = $(window.document, getPref 'button_id')
170 button['VimFx_blacklisted'] = blacklisted
171 updateToolbarButton button
172
173 exports.addToolbarButton = addToolbarButton
174 exports.setWindowBlacklisted = setWindowBlacklisted
175 exports.setButtonDefaultPosition = setButtonDefaultPosition
Imprint / Impressum