]> git.gir.st - VimFx.git/blob - extension/packages/button.coffee
Closes #2. Added the Help Dialog, changed the style of the hint markers. Fixed the...
[VimFx.git] / extension / packages / button.coffee
1 { getPref
2 , setPref } = require 'prefs'
3
4 { showHelp } = require 'help'
5
6 positions = {}
7
8 persist = (document, toolbar, buttonID, beforeID) ->
9 currentset = toolbar.getAttribute('currentset').split(',')
10 idx = if beforeID then currentset.indexOf(beforeID) else -1;
11 if idx != -1
12 currentset.splice(idx, 0, buttonID);
13 else
14 currentset.push(buttonID);
15
16 toolbar.setAttribute "currentset", currentset.join(",")
17 document.persist toolbar.id, "currentset"
18 return [currentset, idx]
19
20 setButtonDefaultPosition = (buttonId, toolbarId, beforeId) ->
21 positions[buttonId] = [toolbarId, beforeId]
22
23 $ = (doc, sel) -> doc.getElementById(sel)
24 $$ = (doc, sel) -> doc.querySelectorAll(sel)
25
26 restorePosition = (doc, button) ->
27
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 (button.id in Object.keys(positions))
39 [tbID, beforeID] = positions[button.id];
40 toolbar = $(doc, tbID)
41 [currentset, idx] = persist(doc, toolbar, button.id, beforeID)
42
43 if toolbar
44 if idx > -1
45 # Inserting the button before the first item in `currentset`
46 # after `idx` that is present in the document
47 for i in [idx + 1 ... currentset.length]
48 if before = $(doc, currentset[i])
49 toolbar.insertItem button.id, before
50 return;
51
52 toolbar.insertItem button.id
53
54 iconUrl = do ->
55 kinds =
56 normal: getResourceURI('resources/icon16.png').spec
57 grey: getResourceURI('resources/icon16-grey.png').spec
58 red: getResourceURI('resources/icon16-red.png').spec
59 blacklist: getResourceURI('resources/icon16-blacklist.png').spec
60
61 return (kind) -> "url(#{ kinds[kind] })"
62
63 createMenupopup = (window) ->
64 doc = window.document
65
66 blacklistTextbox = doc.createElement 'textbox'
67 blacklistButton = doc.createElement 'toolbarbutton'
68 blacklistButton.setAttribute 'tooltiptext', 'Blacklist'
69 blacklistButton.setAttribute 'class', 'toolbarbutton-1'
70 blacklistButton.style.listStyleImage = iconUrl('blacklist')
71 hbox = doc.createElement 'hbox'
72 hbox.appendChild blacklistTextbox
73 hbox.appendChild blacklistButton
74
75 itemPreferences = doc.createElement 'menuitem'
76 itemPreferences.setAttribute 'label', 'Preferences'
77
78 itemHelp = doc.createElement 'menuitem'
79 itemHelp.setAttribute 'label', 'Help'
80
81 menupopup = doc.createElement 'menupopup'
82 menupopup.appendChild hbox
83 menupopup.appendChild itemPreferences
84 menupopup.appendChild itemHelp
85
86 onPopupShowing = (event) ->
87 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
88 blacklistTextbox.value = "*#{ tabWindow.location.host }*"
89
90 onBlacklistButtonCommand = (event) ->
91 blackList = getPref 'black_list'
92 blackList += ', ' if blackList.length > 0
93 blackList += blacklistTextbox.value
94
95 setPref 'black_list', blackList
96 menupopup.hidePopup()
97
98 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
99 tabWindow.location.reload(false)
100
101 event.stopPropagation()
102
103 onPreferencesCommand = (event) ->
104 id = encodeURIComponent getPref('addon_id')
105 window.BrowserOpenAddonsMgr("addons://detail/#{ id }/preferences")
106
107 event.stopPropagation()
108
109 onHelpCommand = (event) ->
110 if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow
111 showHelp tabWindow.document
112
113 event.stopPropagation()
114
115 menupopup.addEventListener 'popupshowing', onPopupShowing, false
116 blacklistButton.addEventListener 'command', onBlacklistButtonCommand, false
117 itemPreferences.addEventListener 'command', onPreferencesCommand, false
118 itemHelp.addEventListener 'command', onHelpCommand, false
119
120 return menupopup
121
122 createButton = (window) ->
123 doc = window.document
124
125 button = doc.createElement 'toolbarbutton'
126 button.setAttribute 'id', getPref 'button_id'
127 button.setAttribute 'type', 'menu-button'
128 button.setAttribute 'label', 'VimFx'
129 button.setAttribute 'class', 'toolbarbutton-1'
130 #
131 # Create and install event listeners
132 onButtonCommand = (event) ->
133 # Change disabled state value which is stored in Prefs
134 setPref('disabled', not getPref 'disabled')
135 updateToolbarButton button
136
137 button.addEventListener 'command', onButtonCommand, false
138
139 menupopup = createMenupopup window
140 button.appendChild menupopup
141
142 return button
143
144
145 addToolbarButton = (window) ->
146 doc = window.document
147
148 try
149 button = createButton window
150
151 updateToolbarButton button
152 restorePosition doc, button, 'nav-bar', 'bookmarks-menu-button-container'
153 catch err
154 console.log err
155
156 unload ->
157 button.parentNode.removeChild button
158 $(doc, "navigator-toolbox").palette.removeChild(button)
159
160 updateToolbarButton = (button) ->
161 if getPref 'disabled'
162 button.style.listStyleImage = iconUrl('grey')
163 button.setAttribute 'tooltiptext', 'VimFx is Disabled. Click to Enable'
164 else if button['VimFx_blacklisted']
165 button.style.listStyleImage = iconUrl('red')
166 button.setAttribute 'tooltiptext', 'VimFx is Blacklisted on this Site'
167 else
168 button.style.listStyleImage = iconUrl('normal')
169 button.setAttribute 'tooltiptext', 'VimFx is Enabled. Click to Disable'
170
171 setWindowBlacklisted = (window, blacklisted) ->
172 if button = $(window.document, getPref 'button_id')
173 button['VimFx_blacklisted'] = blacklisted
174 updateToolbarButton button
175
176 exports.addToolbarButton = addToolbarButton
177 exports.setWindowBlacklisted = setWindowBlacklisted
178 exports.setButtonDefaultPosition = setButtonDefaultPosition
Imprint / Impressum