|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 function closeWindow(aClose, aPromptFunction) |
|
6 { |
|
7 # Closing the last window doesn't quit the application on OS X. |
|
8 #ifndef XP_MACOSX |
|
9 var windowCount = 0; |
|
10 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] |
|
11 .getService(Components.interfaces.nsIWindowMediator); |
|
12 var e = wm.getEnumerator(null); |
|
13 |
|
14 while (e.hasMoreElements()) { |
|
15 var w = e.getNext(); |
|
16 if (w.closed) { |
|
17 continue; |
|
18 } |
|
19 if (++windowCount == 2) |
|
20 break; |
|
21 } |
|
22 |
|
23 // If we're down to the last window and someone tries to shut down, check to make sure we can! |
|
24 if (windowCount == 1 && !canQuitApplication("lastwindow")) |
|
25 return false; |
|
26 else if (windowCount != 1) |
|
27 #endif |
|
28 if (typeof(aPromptFunction) == "function" && !aPromptFunction()) |
|
29 return false; |
|
30 |
|
31 if (aClose) |
|
32 window.close(); |
|
33 |
|
34 return true; |
|
35 } |
|
36 |
|
37 function canQuitApplication(aData) |
|
38 { |
|
39 var os = Components.classes["@mozilla.org/observer-service;1"] |
|
40 .getService(Components.interfaces.nsIObserverService); |
|
41 if (!os) return true; |
|
42 |
|
43 try { |
|
44 var cancelQuit = Components.classes["@mozilla.org/supports-PRBool;1"] |
|
45 .createInstance(Components.interfaces.nsISupportsPRBool); |
|
46 os.notifyObservers(cancelQuit, "quit-application-requested", aData || null); |
|
47 |
|
48 // Something aborted the quit process. |
|
49 if (cancelQuit.data) |
|
50 return false; |
|
51 } |
|
52 catch (ex) { } |
|
53 return true; |
|
54 } |
|
55 |
|
56 function goQuitApplication() |
|
57 { |
|
58 if (!canQuitApplication()) |
|
59 return false; |
|
60 |
|
61 var appStartup = Components.classes['@mozilla.org/toolkit/app-startup;1']. |
|
62 getService(Components.interfaces.nsIAppStartup); |
|
63 |
|
64 appStartup.quit(Components.interfaces.nsIAppStartup.eAttemptQuit); |
|
65 return true; |
|
66 } |
|
67 |
|
68 // |
|
69 // Command Updater functions |
|
70 // |
|
71 function goUpdateCommand(aCommand) |
|
72 { |
|
73 try { |
|
74 var controller = top.document.commandDispatcher |
|
75 .getControllerForCommand(aCommand); |
|
76 |
|
77 var enabled = false; |
|
78 if (controller) |
|
79 enabled = controller.isCommandEnabled(aCommand); |
|
80 |
|
81 goSetCommandEnabled(aCommand, enabled); |
|
82 } |
|
83 catch (e) { |
|
84 Components.utils.reportError("An error occurred updating the " + |
|
85 aCommand + " command: " + e); |
|
86 } |
|
87 } |
|
88 |
|
89 function goDoCommand(aCommand) |
|
90 { |
|
91 try { |
|
92 var controller = top.document.commandDispatcher |
|
93 .getControllerForCommand(aCommand); |
|
94 if (controller && controller.isCommandEnabled(aCommand)) |
|
95 controller.doCommand(aCommand); |
|
96 } |
|
97 catch (e) { |
|
98 Components.utils.reportError("An error occurred executing the " + |
|
99 aCommand + " command: " + e); |
|
100 } |
|
101 } |
|
102 |
|
103 |
|
104 function goSetCommandEnabled(aID, aEnabled) |
|
105 { |
|
106 var node = document.getElementById(aID); |
|
107 |
|
108 if (node) { |
|
109 if (aEnabled) |
|
110 node.removeAttribute("disabled"); |
|
111 else |
|
112 node.setAttribute("disabled", "true"); |
|
113 } |
|
114 } |
|
115 |
|
116 function goSetMenuValue(aCommand, aLabelAttribute) |
|
117 { |
|
118 var commandNode = top.document.getElementById(aCommand); |
|
119 if (commandNode) { |
|
120 var label = commandNode.getAttribute(aLabelAttribute); |
|
121 if (label) |
|
122 commandNode.setAttribute("label", label); |
|
123 } |
|
124 } |
|
125 |
|
126 function goSetAccessKey(aCommand, aValueAttribute) |
|
127 { |
|
128 var commandNode = top.document.getElementById(aCommand); |
|
129 if (commandNode) { |
|
130 var value = commandNode.getAttribute(aValueAttribute); |
|
131 if (value) |
|
132 commandNode.setAttribute("accesskey", value); |
|
133 } |
|
134 } |
|
135 |
|
136 // this function is used to inform all the controllers attached to a node that an event has occurred |
|
137 // (e.g. the tree controllers need to be informed of blur events so that they can change some of the |
|
138 // menu items back to their default values) |
|
139 function goOnEvent(aNode, aEvent) |
|
140 { |
|
141 var numControllers = aNode.controllers.getControllerCount(); |
|
142 var controller; |
|
143 |
|
144 for (var controllerIndex = 0; controllerIndex < numControllers; controllerIndex++) { |
|
145 controller = aNode.controllers.getControllerAt(controllerIndex); |
|
146 if (controller) |
|
147 controller.onEvent(aEvent); |
|
148 } |
|
149 } |
|
150 |
|
151 function visitLink(aEvent) { |
|
152 var node = aEvent.target; |
|
153 while (node.nodeType != Node.ELEMENT_NODE) |
|
154 node = node.parentNode; |
|
155 var url = node.getAttribute("link"); |
|
156 if (!url) |
|
157 return; |
|
158 |
|
159 var protocolSvc = Components.classes["@mozilla.org/uriloader/external-protocol-service;1"] |
|
160 .getService(Components.interfaces.nsIExternalProtocolService); |
|
161 var ioService = Components.classes["@mozilla.org/network/io-service;1"] |
|
162 .getService(Components.interfaces.nsIIOService); |
|
163 var uri = ioService.newURI(url, null, null); |
|
164 |
|
165 // if the scheme is not an exposed protocol, then opening this link |
|
166 // should be deferred to the system's external protocol handler |
|
167 if (protocolSvc.isExposedProtocol(uri.scheme)) { |
|
168 var win = window.top; |
|
169 if (win instanceof Components.interfaces.nsIDOMChromeWindow) { |
|
170 while (win.opener && !win.opener.closed) |
|
171 win = win.opener; |
|
172 } |
|
173 win.open(uri.spec); |
|
174 } |
|
175 else |
|
176 protocolSvc.loadUrl(uri); |
|
177 } |
|
178 |
|
179 function setTooltipText(aID, aTooltipText) |
|
180 { |
|
181 var element = document.getElementById(aID); |
|
182 if (element) |
|
183 element.setAttribute("tooltiptext", aTooltipText); |
|
184 } |
|
185 |
|
186 this.__defineGetter__("NS_ASSERT", function() { |
|
187 delete this.NS_ASSERT; |
|
188 var tmpScope = {}; |
|
189 Components.utils.import("resource://gre/modules/debug.js", tmpScope); |
|
190 return this.NS_ASSERT = tmpScope.NS_ASSERT; |
|
191 }); |