!import
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
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 of the GNU General Public License Version 2 or later (the "GPL"),
26 * or 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 MPL, 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 MPL, 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
editDocument
61 function editDocument(aDocument)
62 {
63 if (!aDocument)
64 aDocument = window.content.document;
65
66 editPage(aDocument.URL);
67 }
68
editPageOrFrame
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 editPage(getContentFrameURI(focusedWindow));
76 }
77
78 // Any non-editor window wanting to create an editor with a URL
79 // should use this instead of "window.openDialog..."
80 // We must always find an existing window with requested URL
editPage
81 function editPage(url)
82 {
83 // Always strip off "view-source:" and #anchors
84 url = url.replace(/^view-source:/, "").replace(/#.*/, "");
85
86 // if the current window is a browser window, then extract the current charset menu setting from the current
87 // document and use it to initialize the new composer window...
88
89 var wintype = document.documentElement.getAttribute('windowtype');
90 var charsetArg;
91
92 if (wintype == "navigator:browser" && content.document)
93 charsetArg = "charset=" + content.document.characterSet;
94
95 try {
96 var uri = createURI(url, null, null);
97
98 var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService();
99 var windowManagerInterface = windowManager.QueryInterface( Components.interfaces.nsIWindowMediator);
100 var enumerator = windowManagerInterface.getEnumerator( "composer:html" );
101 var emptyWindow;
102 while ( enumerator.hasMoreElements() )
103 {
104 var win = enumerator.getNext().QueryInterface(Components.interfaces.nsIDOMWindowInternal);
105 if ( win && win.IsWebComposer())
106 {
107 if (CheckOpenWindowForURIMatch(uri, win))
108 {
109 // We found an editor with our url
110 win.focus();
111 return;
112 }
113 else if (!emptyWindow && win.PageIsEmptyAndUntouched())
114 {
115 emptyWindow = win;
116 }
117 }
118 }
119
120 if (emptyWindow)
121 {
122 // we have an empty window we can use
123 if (emptyWindow.IsInHTMLSourceMode())
124 emptyWindow.SetEditMode(emptyWindow.PreviousNonSourceDisplayMode);
125 emptyWindow.EditorLoadUrl(url);
126 emptyWindow.focus();
127 emptyWindow.SetSaveAndPublishUI(url);
128 return;
129 }
130
131 // Create new Composer window
132 openDialog("chrome://editor/content", "_blank", "chrome,all,dialog=no", url, charsetArg);
133
134 } catch(e) {}
135 }
136
createURI
137 function createURI(urlstring)
138 {
139 try {
140 var ioserv = Components.classes["@mozilla.org/network/io-service;1"]
141 .getService(Components.interfaces.nsIIOService);
142 return ioserv.newURI(urlstring, null, null);
143 } catch (e) {}
144
145 return null;
146 }
147
CheckOpenWindowForURIMatch
148 function CheckOpenWindowForURIMatch(uri, win)
149 {
150 try {
151 var contentWindow = win.content; // need to QI win to nsIDOMWindowInternal?
152 var contentDoc = contentWindow.document;
153 var htmlDoc = contentDoc.QueryInterface(Components.interfaces.nsIDOMHTMLDocument);
154 var winuri = createURI(htmlDoc.URL);
155 return winuri.equals(uri);
156 } catch (e) {}
157
158 return false;
159 }
160
NewEditorFromTemplate
161 function NewEditorFromTemplate()
162 {
163 // XXX not implemented
164 }
165
NewEditorFromDraft
166 function NewEditorFromDraft()
167 {
168 // XXX not implemented
169 }