]> git.gir.st - VimFx.git/blob - extension/packages/button.coffee
Merge branch 'find' into develop
[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.stopPropagation()
153
154 button.addEventListener 'command', onButtonCommand, false
155
156 menupopup = createMenupopup window
157 button.appendChild menupopup
158
159 vimkey = doc.createElement 'key'
160 vimkey.setAttribute "id", KEY_ID
161 vimkey.setAttribute "key", "V"
162 vimkey.setAttribute "modifiers", "shift,alt"
163 vimkey.setAttribute "oncommand", "void(0);"
164 vimkey.addEventListener "command", onButtonCommand, false
165
166 keyset = doc.createElement 'keyset'
167 keyset.setAttribute 'id', KEYSET_ID
168 keyset.appendChild(vimkey)
169
170 return [button, keyset]
171
172
173 addToolbarButton = (window) ->
174 doc = window.document
175 win = doc.querySelector 'window'
176
177 [button, keyset] = createButton window
178 updateToolbarButton button
179
180 restorePosition doc, button
181 win.appendChild keyset
182
183 unload ->
184 if buttonParent = button.parentNode
185 buttonParent.removeChild button
186 if keysetParent = keyset.parentNode
187 keysetParent.removeChild keyset
188 $(doc, "navigator-toolbox").palette.removeChild(button)
189
190 updateToolbarButton = (button) ->
191 if getPref 'disabled'
192 button.style.listStyleImage = iconUrl('grey')
193 button.setAttribute 'tooltiptext', _('button_tooltip_disabled')
194 else if button['VimFx_blacklisted']
195 button.style.listStyleImage = iconUrl('red')
196 button.setAttribute 'tooltiptext', _('button_tooltip_blacklisted')
197 else
198 button.style.listStyleImage = iconUrl('normal')
199 button.setAttribute 'tooltiptext', _('button_tooltip_enabled')
200
201 setWindowBlacklisted = (window, blacklisted) ->
202 if button = $(window.document, BUTTON_ID)
203 button['VimFx_blacklisted'] = blacklisted
204 updateToolbarButton button
205
206 exports.addToolbarButton = addToolbarButton
207 exports.setWindowBlacklisted = setWindowBlacklisted
208 exports.setButtonInstallPosition = setButtonInstallPosition
Imprint / Impressum