!import
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is Mozilla Communicator client code, released
15 * March 31, 1998.
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 * Charles Manske (cmanske@netscape.com)
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39 var gNewTitle = "";
40 var gAuthor = "";
41 var gDescription = "";
42 var gAuthorElement;
43 var gDescriptionElement;
44 var gInsertNewAuthor = false;
45 var gInsertNewDescription = false;
46 var gTitleWasEdited = false;
47 var gAuthorWasEdited = false;
48 var gDescWasEdited = false;
49
50 //Cancel() is in EdDialogCommon.js
51 // dialog initialization code
Startup
52 function Startup()
53 {
54 var editor = GetCurrentEditor();
55 if (!editor)
56 {
57 window.close();
58 return;
59 }
60
61 gDialog.PageLocation = document.getElementById("PageLocation");
62 gDialog.PageModDate = document.getElementById("PageModDate");
63 gDialog.TitleInput = document.getElementById("TitleInput");
64 gDialog.AuthorInput = document.getElementById("AuthorInput");
65 gDialog.DescriptionInput = document.getElementById("DescriptionInput");
66
67 // Default string for new page is set from DTD string in XUL,
68 // so set only if not new doc URL
69 var location = GetDocumentUrl();
70 var lastmodString = GetString("Unknown");
71
72 if (!IsUrlAboutBlank(location))
73 {
74 // NEVER show username and password in clear text
75 gDialog.PageLocation.setAttribute("value", StripPassword(location));
76
77 // Get last-modified file date+time
78 // TODO: Convert this to local time?
79 var lastmod;
80 try {
81 lastmod = editor.document.lastModified; // get string of last modified date
82 } catch (e) {}
83 // Convert modified string to date (0 = unknown date or January 1, 1970 GMT)
84 if(Date.parse(lastmod))
85 {
86 try {
87 const nsScriptableDateFormat_CONTRACTID = "@mozilla.org/intl/scriptabledateformat;1";
88 const nsIScriptableDateFormat = Components.interfaces.nsIScriptableDateFormat;
89 var dateService = Components.classes[nsScriptableDateFormat_CONTRACTID]
90 .getService(nsIScriptableDateFormat);
91
92 var lastModDate = new Date();
93 lastModDate.setTime(Date.parse(lastmod));
94 lastmodString = dateService.FormatDateTime("",
95 dateService.dateFormatLong,
96 dateService.timeFormatSeconds,
97 lastModDate.getFullYear(),
98 lastModDate.getMonth()+1,
99 lastModDate.getDate(),
100 lastModDate.getHours(),
101 lastModDate.getMinutes(),
102 lastModDate.getSeconds());
103 } catch (e) {}
104 }
105 }
106 gDialog.PageModDate.value = lastmodString;
107
108 gAuthorElement = GetMetaElement("author");
109 if (!gAuthorElement)
110 {
111 gAuthorElement = CreateMetaElement("author");
112 if (!gAuthorElement)
113 {
114 window.close();
115 return;
116 }
117 gInsertNewAuthor = true;
118 }
119
120 gDescriptionElement = GetMetaElement("description");
121 if (!gDescriptionElement)
122 {
123 gDescriptionElement = CreateMetaElement("description");
124 if (!gDescriptionElement)
125 window.close();
126
127 gInsertNewDescription = true;
128 }
129
130 InitDialog();
131
132 SetTextboxFocus(gDialog.TitleInput);
133
134 SetWindowLocation();
135 }
136
InitDialog
137 function InitDialog()
138 {
139 gDialog.TitleInput.value = GetDocumentTitle();
140
141 var gAuthor = TrimString(gAuthorElement.getAttribute("content"));
142 if (!gAuthor)
143 {
144 // Fill in with value from editor prefs
145 var prefs = GetPrefs();
146 if (prefs)
147 gAuthor = prefs.getCharPref("editor.author");
148 }
149 gDialog.AuthorInput.value = gAuthor;
150 gDialog.DescriptionInput.value = gDescriptionElement.getAttribute("content");
151 }
152
TextboxChanged
153 function TextboxChanged(ID)
154 {
155 switch(ID)
156 {
157 case "TitleInput":
158 gTitleWasEdited = true;
159 break;
160 case "AuthorInput":
161 gAuthorWasEdited = true;
162 break;
163 case "DescriptionInput":
164 gDescWasEdited = true;
165 break;
166 }
167 }
168
ValidateData
169 function ValidateData()
170 {
171 gNewTitle = TrimString(gDialog.TitleInput.value);
172 gAuthor = TrimString(gDialog.AuthorInput.value);
173 gDescription = TrimString(gDialog.DescriptionInput.value);
174 return true;
175 }
176
onAccept
177 function onAccept()
178 {
179 if (ValidateData())
180 {
181 var editor = GetCurrentEditor();
182 editor.beginTransaction();
183
184 // Set title contents even if string is empty
185 // because TITLE is a required HTML element
186 if (gTitleWasEdited)
187 SetDocumentTitle(gNewTitle);
188
189 if (gAuthorWasEdited)
190 SetMetaElementContent(gAuthorElement, gAuthor, gInsertNewAuthor, false);
191
192 if (gDescWasEdited)
193 SetMetaElementContent(gDescriptionElement, gDescription, gInsertNewDescription, false);
194
195 editor.endTransaction();
196
197 SaveWindowLocation();
198 return true; // do close the window
199 }
200 return false;
201 }