]> git.gir.st - VimFx.git/blob - extension/packages/button.coffee
Housekeeping
[VimFx.git] / extension / packages / button.coffee
1 { getPref
2 , setPref } = require 'prefs'
3
4 { injectHelp } = require 'help'
5 { commandsHelp } = require 'commands'
6
7 KEYSET_ID = 'vimfx-keyset'
8 BUTTON_ID = 'vimfx-toolbar-button'
9 KEY_ID = 'vimfx-key'
10 MENUPOPUP_ID = 'vimfx-menupopup'
11 MENU_ITEM_PREF = 'vimfx-menu-item-preferences'
12 MENU_ITEM_HELP = 'vimfx-menu-item-help'
13 TEXTBOX_BLACKLIST_ID = 'vimfx-textbox-blacklist-id'
14 BUTTON_BLACKLIST_ID = 'vimfx-button-blacklist-id'
15
16 $ = (doc, sel) -> doc.getElementById(sel)
17 $$ = (doc, sel) -> doc.querySelectorAll(sel)
18
19 positions = {}
20
21 setButtonInstallPosition = (toolbarId, beforeId) ->
22 positions[BUTTON_ID] = [toolbarId, beforeId]
23
24 persist = (document, toolbar, buttonId, beforeId) ->
25 currentset = toolbar.currentSet.split ','
26 idx = if beforeId then currentset.indexOf(beforeId) else -1
27 if idx != -1
28 currentset.splice(idx, 0, buttonId);
29 else
30 currentset.push(buttonId);
31
32 toolbar.setAttribute "currentset", currentset.join ','
33 document.persist toolbar.id, "currentset"
34 return [currentset, idx]
35
36 restorePosition = (doc, button) ->
37 $(doc, "navigator-toolbox").palette.appendChild(button)
38
39 for tb in $$(doc, "toolbar")
40 currentset = tb.getAttribute("currentset").split ','
41 idx = currentset.indexOf button.id
42 if idx != -1
43 toolbar = tb
44 break
45
46 # Saved position not found, using the default one, after persisting it
47 if !toolbar and pos = positions[button.id]
48 [tbID, beforeId] = pos
49 if toolbar = $(doc, tbID)
50 [currentset, idx] = persist(doc, toolbar, button.id, beforeId)
51
52 if toolbar and idx != -1
53 # Inserting the button before the first item in `currentset`
54 # after `idx` that is present in the document
55 for i in [idx + 1 ... currentset.length]
56 if before = $(doc, currentset[i])
57 toolbar.insertItem button.id, before
58 return
59
60 toolbar.insertItem button.id
61
62 iconUrl = do ->
63 kinds =
64 normal: getResourceURI('resources/icon16.png').spec
65 grey: getResourceURI('resources/icon16-grey.png').spec
66 red: getResourceURI('resources/icon16-red.png').spec
67 blacklist: getResourceURI('resources/icon16-blacklist.png').spec
68
69 return (kind) -> "url(#{ kinds[kind] })"
70
71
72 createMenupopup = (window) ->
73 doc = window.document
74
75 blacklistTextbox = doc.createElement 'textbox'
76 blacklistTextbox.id = TEXTBOX_BLACKLIST_ID
77 blacklistButton = doc.createElement 'toolbarbutton'
78 blacklistButton.id = BUTTON_BLACKLIST_ID
79 blacklistButton.setAttribute 'tooltiptext', _('item_blacklist_button_tooltip')
80 blacklistButton.setAttribute 'class', 'toolbarbutton-1'
81 blacklistButton.style.listStyleImage = iconUrl('blacklist')
82 hbox = doc.createElement 'hbox'
83 hbox.appendChild blacklistTextbox
84 hbox.appendChild blacklistButton
85
86 itemPreferences = doc.createElement 'menuitem'
87 itemPreferences.id = MENU_ITEM_PREF
88 itemPreferences.setAttribute 'label', _('item_preferences')
89
90 itemHelp = doc.createElement 'menuitem'
91 itemHelp.id = MENU_ITEM_HELP
92 itemHelp.setAttribute 'label', _('item_help')
93
94 menupopup = doc.createElement 'menupopup'
95 menupopup.id = MENUPOPUP_ID
96 menupopup.setAttribute 'ignorekeys', true
97 menupopup.appendChild hbox
98 menupopup.appendChild itemPreferences
99 menupopup.appendChild itemHelp
100
101 onPopupShowing = (event) ->
102 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
103 blacklistTextbox.value = "*#{ tabWindow.location.host }*"
104
105 onBlacklistButtonCommand = (event) ->
106 blackList = getPref 'black_list'
107 blackList += ', ' if blackList.length > 0
108 blackList += blacklistTextbox.value
109
110 setPref 'black_list', blackList
111 menupopup.hidePopup()
112
113 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
114 tabWindow.location.reload(false)
115
116 event.stopPropagation()
117
118 onPreferencesCommand = (event) ->
119 id = encodeURIComponent getPref('addon_id')
120 window.BrowserOpenAddonsMgr("addons://detail/#{ id }/preferences")
121
122 event.stopPropagation()
123
124 onHelpCommand = (event) ->
125 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
126 injectHelp tabWindow.document, commandsHelp
127
128 event.stopPropagation()
129
130 menupopup.addEventListener 'popupshowing', onPopupShowing, false
131 blacklistButton.addEventListener 'command', onBlacklistButtonCommand, false
132 itemPreferences.addEventListener 'command', onPreferencesCommand, false
133 itemHelp.addEventListener 'command', onHelpCommand, false
134
135 return menupopup
136
137 createButton = (window) ->
138 doc = window.document
139
140 button = doc.createElement 'toolbarbutton'
141 button.setAttribute 'id', BUTTON_ID
142 button.setAttribute 'type', 'menu-button'
143 button.setAttribute 'label', 'VimFx'
144 button.setAttribute 'class', 'toolbarbutton-1'
145 #
146 # Create and install event listeners
147 onButtonCommand = (event) ->
148 # Change disabled state value which is stored in Prefs
149 setPref('disabled', not getPref 'disabled')
150 updateToolbarButton button
151
152 event.preventDefault()
153 event.stopPropagation()
154
155 button.addEventListener 'command', onButtonCommand, true
156
157 menupopup = createMenupopup window
158 button.appendChild menupopup
159
160 vimkey = doc.createElement 'key'
161 vimkey.setAttribute "id", KEY_ID
162 vimkey.setAttribute "key", "V"
163 vimkey.setAttribute "modifiers", "shift,alt"
164 vimkey.setAttribute "oncommand", "void(0);"
165 vimkey.addEventListener "command", onButtonCommand, true
166
167 keyset = doc.createElement 'keyset'
168 keyset.setAttribute 'id', KEYSET_ID
169 keyset.appendChild(vimkey)
170
171 return [button, keyset]
172
173
174 addToolbarButton = (window) ->
175 doc = window.document
176 win = doc.querySelector 'window'
177
178 [button, keyset] = createButton window
179 updateToolbarButton button
180
181 restorePosition doc, button
182 win.appendChild keyset
183
184 unload ->
185 if buttonParent = button.parentNode
186 buttonParent.removeChild button
187 if keysetParent = keyset.parentNode
188 keysetParent.removeChild keyset
189 $(doc, "navigator-toolbox").palette.removeChild(button)
190
191 updateToolbarButton = (button) ->
192 if getPref 'disabled'
193 button.style.listStyleImage = iconUrl('grey')
194 button.setAttribute 'tooltiptext', _('button_tooltip_disabled')
195 else if button['VimFx_blacklisted']
196 button.style.listStyleImage = iconUrl('red')
197 button.setAttribute 'tooltiptext', _('button_tooltip_blacklisted')
198 else
199 button.style.listStyleImage = iconUrl('normal')
200 button.setAttribute 'tooltiptext', _('button_tooltip_enabled')
201
202 setWindowBlacklisted = (window, blacklisted) ->
203 if button = $(window.document, BUTTON_ID)
204 button['VimFx_blacklisted'] = blacklisted
205 updateToolbarButton button
206
207 exports.addToolbarButton = addToolbarButton
208 exports.setWindowBlacklisted = setWindowBlacklisted
209 exports.setButtonInstallPosition = setButtonInstallPosition
Imprint / Impressum