]> git.gir.st - VimFx.git/blob - extension/packages/button.coffee
Toolbar button Help link bug fix
[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
29 $(doc, "navigator-toolbox").palette.appendChild(button)
30
31 for tb in $$(doc, "toolbar")
32 currentset = tb.getAttribute('currentset').split(',')
33 idx = currentset.indexOf button.id
34 if idx > -1
35 toolbar = tb
36 break
37
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)
43
44 if toolbar
45 if idx > -1
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
51 return;
52
53 toolbar.insertItem button.id
54
55 iconUrl = do ->
56 kinds =
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
61
62 return (kind) -> "url(#{ kinds[kind] })"
63
64 createMenupopup = (window) ->
65 doc = window.document
66
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
75
76 itemPreferences = doc.createElement 'menuitem'
77 itemPreferences.setAttribute 'label', 'Preferences'
78
79 itemHelp = doc.createElement 'menuitem'
80 itemHelp.setAttribute 'label', 'Help'
81
82 menupopup = doc.createElement 'menupopup'
83 menupopup.appendChild hbox
84 menupopup.appendChild itemPreferences
85 menupopup.appendChild itemHelp
86
87 onPopupShowing = (event) ->
88 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
89 blacklistTextbox.value = "*#{ tabWindow.location.host }*"
90
91 onBlacklistButtonCommand = (event) ->
92 blackList = getPref 'black_list'
93 blackList += ', ' if blackList.length > 0
94 blackList += blacklistTextbox.value
95
96 setPref 'black_list', blackList
97 menupopup.hidePopup()
98
99 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
100 tabWindow.location.reload(false)
101
102 event.stopPropagation()
103
104 onPreferencesCommand = (event) ->
105 id = encodeURIComponent getPref('addon_id')
106 window.BrowserOpenAddonsMgr("addons://detail/#{ id }/preferences")
107
108 event.stopPropagation()
109
110 onHelpCommand = (event) ->
111 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
112 showHelp tabWindow.document, commandsHelp
113
114 event.stopPropagation()
115
116 menupopup.addEventListener 'popupshowing', onPopupShowing, false
117 blacklistButton.addEventListener 'command', onBlacklistButtonCommand, false
118 itemPreferences.addEventListener 'command', onPreferencesCommand, false
119 itemHelp.addEventListener 'command', onHelpCommand, false
120
121 return menupopup
122
123 createButton = (window) ->
124 doc = window.document
125
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'
131 #
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
137
138 button.addEventListener 'command', onButtonCommand, false
139
140 menupopup = createMenupopup window
141 button.appendChild menupopup
142
143 return button
144
145
146 addToolbarButton = (window) ->
147 doc = window.document
148
149 try
150 button = createButton window
151
152 updateToolbarButton button
153 restorePosition doc, button, 'nav-bar', 'bookmarks-menu-button-container'
154 catch err
155 console.log err
156
157 unload ->
158 button.parentNode.removeChild button
159 $(doc, "navigator-toolbox").palette.removeChild(button)
160
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'
168 else
169 button.style.listStyleImage = iconUrl('normal')
170 button.setAttribute 'tooltiptext', 'VimFx is Enabled. Click to Disable'
171
172 setWindowBlacklisted = (window, blacklisted) ->
173 if button = $(window.document, getPref 'button_id')
174 button['VimFx_blacklisted'] = blacklisted
175 updateToolbarButton button
176
177 exports.addToolbarButton = addToolbarButton
178 exports.setWindowBlacklisted = setWindowBlacklisted
179 exports.setButtonDefaultPosition = setButtonDefaultPosition
Imprint / Impressum