]> git.gir.st - VimFx.git/blob - packages/button.coffee
Resolve Issue #1: gmail fix, improved key code and char handling, and Esc handling fix
[VimFx.git] / 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 restorePosition = (doc, button) ->
22 $ = (sel, all) -> doc[if all then "querySelectorAll" else "getElementById"](sel)
23
24 ($("navigator-toolbox") || $("mail-toolbox")).palette.appendChild(button)
25
26 for tb in $("toolbar", true)
27 currentset = tb.getAttribute('currentset').split(',')
28 idx = currentset.indexOf button.id
29 if idx > -1
30 toolbar = tb
31 break
32
33 # Saved position not found, using the default one, after persisting it
34 if !toolbar and (button.id in Object.keys(positions))
35 [tbID, beforeID] = positions[button.id];
36 toolbar = $(tbID)
37 [currentset, idx] = persist(doc, toolbar, button.id, beforeID)
38
39 if toolbar
40 if idx > -1
41 # Inserting the button before the first item in `currentset`
42 # after `idx` that is present in the document
43 for i in [idx + 1 ... currentset.length]
44 if before = $(currentset[i])
45 toolbar.insertItem button.id, before
46 return;
47
48 toolbar.insertItem button.id
49
50 iconUrl = do ->
51 icon_normal = getResourceURI('resources/icon16.png').spec
52 icon_grey = getResourceURI('resources/icon16-grey.png').spec
53
54 return (disabled) -> "url(#{ if disabled then icon_grey else icon_normal })"
55
56 addToolbarButton = (window) ->
57 buttonId = getPref 'button_id'
58 disabled = getPref 'disabled'
59
60 doc = window.document
61 button = doc.createElement 'toolbarbutton'
62 button.setAttribute 'id', buttonId
63 button.setAttribute 'type', 'checkbox'
64 button.setAttribute 'label', 'Vim for Firefox'
65 button.setAttribute 'class', 'toolbarbutton-1 chromeclass-toolbar-additional'
66 button.setAttribute 'tooltiptext', 'Enable/Disable VimFx'
67 button.checked = disabled
68 button.style.listStyleImage = iconUrl(disabled)
69
70 onButtonCommand = (event) ->
71 dis = button.checked
72 setPref 'disabled', dis
73 button.style.listStyleImage = iconUrl(dis)
74
75 button.addEventListener 'command', onButtonCommand, false
76
77
78 restorePosition doc, button, 'nav-bar', 'bookmarks-menu-button-container'
79
80 unload -> button.parentNode.removeChild button
81
82 exports.addToolbarButton = addToolbarButton
83 exports.setButtonDefaultPosition = setButtonDefaultPosition
Imprint / Impressum