]> git.gir.st - VimFx.git/blob - extension/packages/button.coffee
Closes #5. Created simple build script. The extension contents are moved into
[VimFx.git] / extension / packages / button.coffee
1 { getPref
2 , setPref } = require 'prefs'
3
4 positions = {}
5
6 persist = (document, toolbar, buttonID, beforeID) ->
7 currentset = tb.getAttribute('currentset').split(',')
8 idx = if beforeID then currentset.indexOf(beforeID) else -1;
9 if idx != -1
10 currentset.splice(idx, 0, buttonID);
11 else
12 currentset.push(buttonID);
13
14 toolbar.setAttribute "currentset", currentset.join(",")
15 document.persist toolbar.id, "currentset"
16 return [currentset, idx]
17
18 setButtonDefaultPosition = (buttonId, toolbarId, beforeId) ->
19 positions[buttonId] = [toolbarId, beforeId]
20
21 $ = (doc, sel) -> doc.getElementById(sel)
22 $$ = (doc, sel) -> doc.querySelectorAll(sel)
23
24 restorePosition = (doc, button) ->
25
26 ($(doc, "navigator-toolbox") || $(doc, "mail-toolbox")).palette.appendChild(button)
27
28 for tb in $$(doc, "toolbar")
29 currentset = tb.getAttribute('currentset').split(',')
30 idx = currentset.indexOf button.id
31 if idx > -1
32 toolbar = tb
33 break
34
35 # Saved position not found, using the default one, after persisting it
36 if !toolbar and (button.id in Object.keys(positions))
37 [tbID, beforeID] = positions[button.id];
38 toolbar = $(doc, tbID)
39 [currentset, idx] = persist(doc, toolbar, button.id, beforeID)
40
41 if toolbar
42 if idx > -1
43 # Inserting the button before the first item in `currentset`
44 # after `idx` that is present in the document
45 for i in [idx + 1 ... currentset.length]
46 if before = $(doc, currentset[i])
47 toolbar.insertItem button.id, before
48 return;
49
50 toolbar.insertItem button.id
51
52 iconUrl = do ->
53 kinds =
54 normal: getResourceURI('resources/icon16.png').spec
55 grey: getResourceURI('resources/icon16-grey.png').spec
56 red: getResourceURI('resources/icon16-red.png').spec
57 blacklist: getResourceURI('resources/icon16-blacklist.png').spec
58
59 return (kind) -> "url(#{ kinds[kind] })"
60
61 addToolbarButton = (window) ->
62 disabled = getPref 'disabled'
63
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 button = doc.createElement 'toolbarbutton'
87 button.setAttribute 'id', getPref 'button_id'
88 button.setAttribute 'type', 'menu-button'
89 button.setAttribute 'label', 'VimFx'
90 button.setAttribute 'class', 'toolbarbutton-1'
91 button.appendChild menupopup
92
93 updateToolbarButton button
94
95 # Create and install event listeners
96 onButtonCommand = (event) ->
97 # Change disabled state value which is stored in Prefs
98 setPref('disabled', not getPref 'disabled')
99 updateToolbarButton button
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
108 if blackList.length > 0
109 blackList += ', '
110 blackList += blacklistTextbox.value
111
112 setPref 'black_list', blackList
113 menupopup.hidePopup()
114
115 event.stopPropagation()
116
117 onPreferencesCommand = (event) ->
118 id = encodeURIComponent getPref('addon_id')
119 window.BrowserOpenAddonsMgr("addons://detail/#{ id }/preferences")
120 event.stopPropagation()
121
122 onHelpCommand = (event) ->
123 event.stopPropagation()
124
125 button.addEventListener 'command', onButtonCommand, false
126 menupopup.addEventListener 'popupshowing', onPopupShowing, false
127 blacklistButton.addEventListener 'command', onBlacklistButtonCommand, false
128 itemPreferences.addEventListener 'command', onPreferencesCommand, false
129 itemHelp.addEventListener 'popupshowing', onHelpCommand, false
130
131 restorePosition doc, button, 'nav-bar', 'bookmarks-menu-button-container'
132
133 unload -> button.parentNode.removeChild button
134
135 updateToolbarButton = (button) ->
136 if getPref 'disabled'
137 button.style.listStyleImage = iconUrl('grey')
138 button.setAttribute 'tooltiptext', 'VimFx is Disabled. Click to Enable'
139 else if button['VimFx_blacklisted']
140 button.style.listStyleImage = iconUrl('red')
141 button.setAttribute 'tooltiptext', 'VimFx is Blacklisted on this Site'
142 else
143 button.style.listStyleImage = iconUrl('normal')
144 button.setAttribute 'tooltiptext', 'VimFx is Enabled. Click to Disable'
145
146 setWindowBlacklisted = (window, blacklisted) ->
147 if button = $(window.document, getPref 'button_id')
148 button['VimFx_blacklisted'] = blacklisted
149 updateToolbarButton button
150
151 exports.addToolbarButton = addToolbarButton
152 exports.setWindowBlacklisted = setWindowBlacklisted
153 exports.setButtonDefaultPosition = setButtonDefaultPosition
Imprint / Impressum