]> git.gir.st - VimFx.git/blob - extension/packages/button.coffee
#16, Closes #19. Changed u/d to scroll half a page, added c-f/c-b to scroll full...
[VimFx.git] / extension / packages / button.coffee
1 { getPref
2 , setPref } = require 'prefs'
3
4 { showHelp } = 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.getAttribute("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', 'Blacklist'
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', 'Preferences'
89
90 itemHelp = doc.createElement 'menuitem'
91 itemHelp.id = MENU_ITEM_HELP
92 itemHelp.setAttribute 'label', 'Help'
93
94 menupopup = doc.createElement 'menupopup'
95 menupopup.id = MENUPOPUP_ID
96 menupopup.appendChild hbox
97 menupopup.appendChild itemPreferences
98 menupopup.appendChild itemHelp
99
100 onPopupShowing = (event) ->
101 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
102 blacklistTextbox.value = "*#{ tabWindow.location.host }*"
103
104 onBlacklistButtonCommand = (event) ->
105 blackList = getPref 'black_list'
106 blackList += ', ' if blackList.length > 0
107 blackList += blacklistTextbox.value
108
109 setPref 'black_list', blackList
110 menupopup.hidePopup()
111
112 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
113 tabWindow.location.reload(false)
114
115 event.stopPropagation()
116
117 onPreferencesCommand = (event) ->
118 id = encodeURIComponent getPref('addon_id')
119 window.BrowserOpenAddonsMgr("addons://detail/#{ id }/preferences")
120
121 event.stopPropagation()
122
123 onHelpCommand = (event) ->
124 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
125 showHelp tabWindow.document, commandsHelp
126
127 event.stopPropagation()
128
129 menupopup.addEventListener 'popupshowing', onPopupShowing, false
130 blacklistButton.addEventListener 'command', onBlacklistButtonCommand, false
131 itemPreferences.addEventListener 'command', onPreferencesCommand, false
132 itemHelp.addEventListener 'command', onHelpCommand, false
133
134 return menupopup
135
136 createButton = (window) ->
137 doc = window.document
138
139 button = doc.createElement 'toolbarbutton'
140 button.setAttribute 'id', BUTTON_ID
141 button.setAttribute 'type', 'menu-button'
142 button.setAttribute 'label', 'VimFx'
143 button.setAttribute 'class', 'toolbarbutton-1'
144 #
145 # Create and install event listeners
146 onButtonCommand = (event) ->
147 # Change disabled state value which is stored in Prefs
148 setPref('disabled', not getPref 'disabled')
149 updateToolbarButton button
150
151 event.stopPropagation()
152
153 button.addEventListener 'command', onButtonCommand, false
154
155 menupopup = createMenupopup window
156 button.appendChild menupopup
157
158 vimkey = doc.createElement 'key'
159 vimkey.setAttribute "id", KEY_ID
160 vimkey.setAttribute "key", "V"
161 vimkey.setAttribute "modifiers", "shift,alt"
162 vimkey.setAttribute "oncommand", "void(0);"
163 vimkey.addEventListener "command", onButtonCommand, false
164
165 keyset = doc.createElement 'keyset'
166 keyset.setAttribute 'id', KEYSET_ID
167 keyset.appendChild(vimkey)
168
169 return [button, keyset]
170
171
172 addToolbarButton = (window) ->
173 doc = window.document
174 win = doc.querySelector 'window'
175
176 try
177 [button, keyset] = createButton window
178 updateToolbarButton button
179 catch err
180 console.log err
181
182 restorePosition doc, button
183 win.appendChild keyset
184
185 unload ->
186 if buttonParent = button.parentNode
187 buttonParent.removeChild button
188 if keysetParent = keyset.parentNode
189 keysetParent.removeChild keyset
190 $(doc, "navigator-toolbox").palette.removeChild(button)
191
192 updateToolbarButton = (button) ->
193 if getPref 'disabled'
194 button.style.listStyleImage = iconUrl('grey')
195 button.setAttribute 'tooltiptext', 'VimFx is Disabled. Click to Enable (Shift+Alt+V)'
196 else if button['VimFx_blacklisted']
197 button.style.listStyleImage = iconUrl('red')
198 button.setAttribute 'tooltiptext', 'VimFx is Blacklisted on this Page'
199 else
200 button.style.listStyleImage = iconUrl('normal')
201 button.setAttribute 'tooltiptext', 'VimFx is Enabled. Click to Disable (Shift+Alt+V)'
202
203 setWindowBlacklisted = (window, blacklisted) ->
204 if button = $(window.document, BUTTON_ID)
205 button['VimFx_blacklisted'] = blacklisted
206 updateToolbarButton button
207
208 exports.addToolbarButton = addToolbarButton
209 exports.setWindowBlacklisted = setWindowBlacklisted
210 exports.setButtonInstallPosition = setButtonInstallPosition
Imprint / Impressum