]> git.gir.st - VimFx.git/blob - packages/utils.coffee
Reorganized some of the packages and fixed a bug where event handlers were not remove...
[VimFx.git] / packages / utils.coffee
1 { interfaces: Ci, classes: Cc } = Components
2
3 HTMLInputElement = Ci.nsIDOMHTMLInputElement
4 HTMLTextAreaElement = Ci.nsIDOMHTMLTextAreaElement
5 HTMLSelectElement = Ci.nsIDOMHTMLSelectElement
6 XULDocument = Ci.nsIDOMXULDocument
7 XULElement = Ci.nsIDOMXULElement
8 HTMLDocument = Ci.nsIDOMHTMLDocument
9 HTMLElement = Ci.nsIDOMHTMLElement
10 Window = Ci.nsIDOMWindow
11 ChromeWindow = Ci.nsIDOMChromeWindow
12 KeyboardEvent = Ci.nsIDOMKeyEvent
13
14 _sss = Cc["@mozilla.org/content/style-sheet-service;1"].getService(Ci.nsIStyleSheetService)
15 _clip = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard)
16
17 class Bucket
18 constructor: (@idFunc, @newFunc) ->
19 @bucket = {}
20
21 get: (obj) ->
22 id = @idFunc obj
23 if container = @bucket[id]
24 return container
25 else
26 return @bucket[id] = @newFunc obj
27
28 forget: (obj) ->
29 delete @bucket[id] if id = @idFunc obj
30
31 isRootWindow = (window) ->
32 window.location == "chrome://browser/content/browser.xul"
33
34 getEventWindow = (event) ->
35 if event.originalTarget instanceof Window
36 return event.originalTarget
37 else
38 doc = event.originalTarget.ownerDocument or event.originalTarget
39 if doc instanceof HTMLDocument or doc instanceof XULDocument
40 return doc.defaultView
41
42 getEventTabWindow = (event) ->
43 if window = getEventWindow event
44 if isRootWindow window
45 return window.gBrowser.tabs.selectedItem?.contentWindow.wrappedJSObject
46 else
47 return window
48
49 getEventRootWindow = (event) ->
50 if window = getEventWindow event
51 return getRootWindow window
52
53 getEventTabBrowser = (event) ->
54 cw.gBrowser if cw = getEventRootWindow event
55
56 getRootWindow = (window) ->
57 return window.QueryInterface(Ci.nsIInterfaceRequestor)
58 .getInterface(Ci.nsIWebNavigation)
59 .QueryInterface(Ci.nsIDocShellTreeItem)
60 .rootTreeItem
61 .QueryInterface(Ci.nsIInterfaceRequestor)
62 .getInterface(Window);
63
64 isElementEditable = (element) ->
65 return element.isContentEditable or \
66 element instanceof HTMLInputElement or \
67 element instanceof HTMLTextAreaElement or \
68 element instanceof HTMLSelectElement
69
70 getWindowId = (window) ->
71 return window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
72 .getInterface(Components.interfaces.nsIDOMWindowUtils)
73 .outerWindowID
74
75 getSessionStore = ->
76 Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
77
78 # Function that returns a URI to the css file that's part of the extension
79 cssUri = do () ->
80 tools = {}
81 Cu.import "resource://gre/modules/Services.jsm", tools
82 (name) ->
83 baseURI = tools.Services.io.newURI __SCRIPT_URI_SPEC__, null, null
84 uri = tools.Services.io.newURI "resources/#{ name }.css", null, baseURI
85 return uri
86
87 # Loads the css identified by the name in the StyleSheetService as User Stylesheet
88 # The stylesheet is then appended to every document, but it can be overwritten by
89 # any user css
90 loadCss = (name) ->
91 _sss.loadAndRegisterSheet(cssUri(name), _sss.AGENT_SHEET)
92
93 # Unloads the css file that's been loaded earlier with `loadCss`
94 unloadCss = (name) ->
95 uri = cssUri(name)
96 if _sss.sheetRegistered(uri, _sss.AGENT_SHEET)
97 _sss.unregisterSheet(uri, _sss.AGENT_SHEET)
98
99 # processes the keyboard event and extracts string representation
100 # of the key *without modifiers* in case this is the kind of a key
101 # that can be handled by the extension
102 #
103 # Currently we handle letters, Escape and Tab keys
104 keyboardEventChar = (keyboardEvent) ->
105 if keyboardEvent.charCode > 0
106 char = String.fromCharCode(keyboardEvent.charCode)
107 if char.match /\s/
108 char = undefined
109 else
110 switch keyboardEvent.keyCode
111 when KeyboardEvent.DOM_VK_ESCAPE then char = 'Esc'
112 when KeyboardEvent.DOM_VK_BACK_SPACE then char = 'Backspace'
113 else char = undefined
114
115 return char
116
117 # extracts string representation of the KeyboardEvent and adds
118 # relevant modifiers (_ctrl_, _alt_, _meta_) in case they were pressed
119 keyboardEventKey = (keyboardEvent) ->
120 char = keyboardEventChar keyboardEvent
121
122 {
123 shiftKey: shift,
124 altKey: alt,
125 ctrlKey: ctrl,
126 metaKey: meta,
127 } = keyboardEvent
128
129 if alt or ctrl or meta
130 k = (a, b) -> if a then b else ''
131 return "<#{ k(ctrl, 'c') + k(alt, 'a') + k(meta, 'm') }-#{ char }>"
132 else
133 return char
134
135 # Simulate mouse click with full chain of event
136 # Copied from Vimium codebase
137 simulateClick = (element, modifiers) ->
138 document = element.ownerDocument
139 window = document.defaultView
140 modifiers ||= {}
141
142 eventSequence = ["mouseover", "mousedown", "mouseup", "click"]
143 for event in eventSequence
144 mouseEvent = document.createEvent("MouseEvents")
145 mouseEvent.initMouseEvent(event, true, true, window, 1, 0, 0, 0, 0, modifiers.ctrlKey, false, false,
146 modifiers.metaKey, 0, null)
147 # Debugging note: Firefox will not execute the element's default action if we dispatch this click event,
148 # but Webkit will. Dispatching a click on an input box does not seem to focus it; we do that separately
149 element.dispatchEvent(mouseEvent)
150
151 # Write a string into system clipboard
152 writeToClipboard = (text) ->
153 str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
154 str.data = text
155
156 trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
157 trans.addDataFlavor("text/unicode");
158 trans.setTransferData("text/unicode", str, text.length * 2);
159
160 _clip.setData trans, null, Ci.nsIClipboard.kGlobalClipboard
161 #
162 # Write a string into system clipboard
163 readFromClipboard = () ->
164 trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
165 trans.addDataFlavor("text/unicode");
166
167 _clip.getData trans, Ci.nsIClipboard.kGlobalClipboard
168
169 str = {}
170 strLength = {}
171
172 trans.getTransferData("text/unicode", str, strLength)
173
174 if str
175 str = str.value.QueryInterface(Ci.nsISupportsString);
176 return str.data.substring 0, strLength.value / 2
177
178 return undefined
179
180 exports.Bucket = Bucket
181 exports.isRootWindow = isRootWindow
182 exports.getEventWindow = getEventWindow
183 exports.getEventTabWindow = getEventTabWindow
184 exports.getEventRootWindow = getEventRootWindow
185 exports.getEventTabBrowser = getEventTabBrowser
186
187 exports.getWindowId = getWindowId
188 exports.getRootWindow = getRootWindow
189 exports.isElementEditable = isElementEditable
190 exports.getSessionStore = getSessionStore
191
192 exports.loadCss = loadCss
193 exports.unloadCss = unloadCss
194
195 exports.keyboardEventKey = keyboardEventKey
196 exports.simulateClick = simulateClick
197 exports.readFromClipboard = readFromClipboard
198 exports.writeToClipboard = writeToClipboard
Imprint / Impressum