PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/toolkit/content/globalOverlay.js

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