/toolkit/content/globalOverlay.js

http://github.com/zpao/v8monkey · JavaScript · 193 lines · 155 code · 27 blank · 11 comment · 37 complexity · 2d334fda01772f110d21d90f76b07710 MD5 · raw file

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