home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / editor / editorApplicationOverlay.js < prev    next >
Text File  |  2003-06-08  |  6KB  |  192 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998-1999
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the NPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the NPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. /* Implementations of nsIControllerCommand for composer commands */
  39.  
  40. function initEditorContextMenuItems(aEvent)
  41. {
  42.   var shouldShowEditPage = !gContextMenu.onImage && !gContextMenu.onLink && !gContextMenu.onTextInput && !gContextMenu.inDirList;
  43.   gContextMenu.showItem( "context-editpage", shouldShowEditPage );
  44.  
  45.   var shouldShowEditLink = gContextMenu.onSaveableLink; 
  46.   gContextMenu.showItem( "context-editlink", shouldShowEditLink );
  47.  
  48.   // Hide the applications separator if there's no add-on apps present. 
  49.   gContextMenu.showItem("context-sep-apps", gContextMenu.shouldShowSeparator("context-sep-apps"));
  50. }
  51.   
  52. function initEditorContextMenuListener(aEvent)
  53. {
  54.   var popup = document.getElementById("contentAreaContextMenu");
  55.   if (popup)
  56.     popup.addEventListener("popupshowing", initEditorContextMenuItems, false);
  57. }
  58.  
  59. addEventListener("load", initEditorContextMenuListener, false);
  60.  
  61. function editDocument(aDocument)      
  62. {
  63.   if (!aDocument)
  64.     aDocument = window._content.document;
  65.  
  66.   editPage(aDocument.URL, window, false); 
  67. }
  68.  
  69. function editPageOrFrame()
  70. {
  71.   var focusedWindow = document.commandDispatcher.focusedWindow;
  72.  
  73.   // if the uri is a specific frame, grab it, else use the frameset uri 
  74.   // and let Composer handle error if necessary
  75.   var url = getContentFrameURI(focusedWindow);
  76.   editPage(url, window, false)
  77. }
  78.  
  79. // Any non-editor window wanting to create an editor with a URL
  80. //   should use this instead of "window.openDialog..."
  81. //  We must always find an existing window with requested URL
  82. // (When calling from a dialog, "launchWindow" is dialog's "opener"
  83. //   and we need a delay to let dialog close)
  84. function editPage(url, launchWindow, delay)
  85. {
  86.   // Always strip off "view-source:" and #anchors
  87.   url = url.replace(/^view-source:/, "").replace(/#.*/, "");
  88.  
  89.   // User may not have supplied a window
  90.   if (!launchWindow)
  91.   {
  92.     if (window)
  93.     {
  94.       launchWindow = window;
  95.     }
  96.     else
  97.     {
  98.       dump("No window to launch an editor from!\n");
  99.       return;
  100.     }
  101.   }
  102.  
  103.   // if the current window is a browser window, then extract the current charset menu setting from the current 
  104.   // document and use it to initialize the new composer window...
  105.  
  106.   var wintype = document.firstChild.getAttribute('windowtype');
  107.   var charsetArg;
  108.  
  109.   if (launchWindow && (wintype == "navigator:browser") && launchWindow._content.document)
  110.     charsetArg = "charset=" + launchWindow._content.document.characterSet;
  111.  
  112.   try {
  113.     var uri = createURI(url, null, null);
  114.  
  115.     var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService();
  116.     var windowManagerInterface = windowManager.QueryInterface( Components.interfaces.nsIWindowMediator);
  117.     var enumerator = windowManagerInterface.getEnumerator( "composer:html" );
  118.     var emptyWindow;
  119.     while ( enumerator.hasMoreElements() )
  120.     {
  121.       var win = enumerator.getNext().QueryInterface(Components.interfaces.nsIDOMWindowInternal);
  122.       if ( win && win.IsWebComposer())
  123.       {
  124.         if (CheckOpenWindowForURIMatch(uri, win))
  125.         {
  126.           // We found an editor with our url
  127.           win.focus();
  128.           return;
  129.         }
  130.         else if (!emptyWindow && win.PageIsEmptyAndUntouched())
  131.         {
  132.           emptyWindow = win;
  133.         }
  134.       }
  135.     }
  136.  
  137.     if (emptyWindow)
  138.     {
  139.       // we have an empty window we can use
  140.       if (emptyWindow.IsInHTMLSourceMode())
  141.         emptyWindow.SetEditMode(emptyWindow.PreviousNonSourceDisplayMode);
  142.       emptyWindow.EditorLoadUrl(url);
  143.       emptyWindow.focus();
  144.       emptyWindow.SetSaveAndPublishUI(url);
  145.       return;
  146.     }
  147.  
  148.     // Create new Composer window
  149.     if (delay)
  150.     {
  151.       launchWindow.delayedOpenWindow("chrome://editor/content", "chrome,all,dialog=no", url);
  152.     }
  153.     else
  154.       launchWindow.openDialog("chrome://editor/content", "_blank", "chrome,all,dialog=no", url, charsetArg);
  155.  
  156.   } catch(e) {}
  157. }
  158.  
  159. function createURI(urlstring)
  160. {
  161.   try {
  162.     var ioserv = Components.classes["@mozilla.org/network/io-service;1"]
  163.                .getService(Components.interfaces.nsIIOService);
  164.     return ioserv.newURI(urlstring, null, null);
  165.   } catch (e) {}
  166.  
  167.   return null;
  168. }
  169.  
  170. function CheckOpenWindowForURIMatch(uri, win)
  171. {
  172.   try {
  173.     var contentWindow = win.content;  // need to QI win to nsIDOMWindowInternal?
  174.     var contentDoc = contentWindow.document;
  175.     var htmlDoc = contentDoc.QueryInterface(Components.interfaces.nsIDOMHTMLDocument);
  176.     var winuri = createURI(htmlDoc.URL);
  177.     return winuri.equals(uri);
  178.   } catch (e) {}
  179.   
  180.   return false;
  181. }
  182.  
  183. function NewEditorFromTemplate()
  184. {
  185.   // XXX not implemented
  186. }
  187.  
  188. function NewEditorFromDraft()
  189. {
  190.   // XXX not implemented
  191. }
  192.