!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) 2001
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Charley Manske (cmanske@netscape.com)
24 * Ryan Cassin (rcassin@supernova.org)
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40 var gPublishSiteData;
41 var gReturnData;
42 var gDefaultSiteIndex = -1;
43 var gDefaultSiteName;
44 var gPreviousDefaultDir;
45 var gPreviousTitle;
46 var gSettingsChanged = false;
47 var gInitialSiteName;
48 var gInitialSiteIndex = -1;
49 var gPasswordManagerOn = true;
50
51 // Dialog initialization code
Startup
52 function Startup()
53 {
54 window.opener.ok = false;
55
56 // Element to edit is passed in
57 gInitialSiteName = window.arguments[1];
58 gReturnData = window.arguments[2];
59 if (!gReturnData || !GetCurrentEditor())
60 {
61 dump("Publish: No editor or return data object not supplied\n");
62 window.close();
63 return;
64 }
65
66 gDialog.TabBox = document.getElementById("TabBox");
67 gDialog.PublishTab = document.getElementById("PublishTab");
68 gDialog.SettingsTab = document.getElementById("SettingsTab");
69
70 // Publish panel
71 gDialog.PageTitleInput = document.getElementById("PageTitleInput");
72 gDialog.FilenameInput = document.getElementById("FilenameInput");
73 gDialog.SiteList = document.getElementById("SiteList");
74 gDialog.DocDirList = document.getElementById("DocDirList");
75 gDialog.OtherDirCheckbox = document.getElementById("OtherDirCheckbox");
76 gDialog.OtherDirRadiogroup = document.getElementById("OtherDirRadiogroup");
77 gDialog.SameLocationRadio = document.getElementById("SameLocationRadio");
78 gDialog.UseSubdirRadio = document.getElementById("UseSubdirRadio");
79 gDialog.OtherDirList = document.getElementById("OtherDirList");
80
81 // Settings Panel
82 gDialog.SiteNameInput = document.getElementById("SiteNameInput");
83 gDialog.PublishUrlInput = document.getElementById("PublishUrlInput");
84 gDialog.BrowseUrlInput = document.getElementById("BrowseUrlInput");
85 gDialog.UsernameInput = document.getElementById("UsernameInput");
86 gDialog.PasswordInput = document.getElementById("PasswordInput");
87 gDialog.SavePassword = document.getElementById("SavePassword");
88
89 gPasswordManagerOn = GetBoolPref("signon.rememberSignons");
90 gDialog.SavePassword.disabled = !gPasswordManagerOn;
91
92 gPublishSiteData = GetPublishSiteData();
93 gDefaultSiteName = GetDefaultPublishSiteName();
94
95 var addNewSite = false;
96 if (gPublishSiteData)
97 {
98 FillSiteList();
99 }
100 else
101 {
102 // No current site data, start a new item in the Settings panel
103 AddNewSite();
104 addNewSite = true;
105 }
106
107 var docUrl = GetDocumentUrl();
108 var scheme = GetScheme(docUrl);
109 var filename = "";
110
111 if (scheme)
112 {
113 filename = GetFilename(docUrl);
114
115 if (scheme != "file")
116 {
117 var siteFound = false;
118
119 // Editing a remote URL.
120 // Attempt to find doc URL in Site Data
121 if (gPublishSiteData)
122 {
123 var dirObj = {};
124 var siteIndex = FindSiteIndexAndDocDir(gPublishSiteData, docUrl, dirObj);
125
126 // Select this site only if the same as user's intended site, or there wasn't one
127 if (siteIndex != -1 && (gInitialSiteIndex == -1 || siteIndex == gInitialSiteIndex))
128 {
129 siteFound = true;
130
131 // Select the site we found
132 gDialog.SiteList.selectedIndex = siteIndex;
133 var docDir = dirObj.value;
134
135 // Use the directory within site in the editable menulist
136 gPublishSiteData[siteIndex].docDir = docDir;
137
138 //XXX HOW DO WE DECIDE WHAT "OTHER" DIR TO USE?
139 //gPublishSiteData[siteIndex].otherDir = docDir;
140 }
141 }
142 if (!siteFound)
143 {
144 // Not found in site database
145 // Setup for a new site and use data from a remote URL
146 if (!addNewSite)
147 AddNewSite();
148
149 addNewSite = true;
150
151 var publishData = CreatePublishDataFromUrl(docUrl);
152 if (publishData)
153 {
154 filename = publishData.filename;
155 gDialog.SiteNameInput.value = publishData.siteName;
156 gDialog.PublishUrlInput.value = publishData.publishUrl;
157 gDialog.BrowseUrlInput.value = publishData.browseUrl;
158 gDialog.UsernameInput.value = publishData.username;
159 gDialog.PasswordInput.value = publishData.password;
160 gDialog.SavePassword.checked = false;
161 }
162 }
163 }
164 }
165 try {
166 gPreviousTitle = GetDocumentTitle();
167 } catch (e) {}
168
169 gDialog.PageTitleInput.value = gPreviousTitle;
170 gDialog.FilenameInput.value = decodeURIComponent(filename);
171
172 if (!addNewSite)
173 {
174 // If not adding a site and we haven't selected a site -- use initial or default site
175 if (gDialog.SiteList.selectedIndex == -1)
176 gDialog.SiteList.selectedIndex = (gInitialSiteIndex != -1) ? gInitialSiteIndex : gDefaultSiteIndex;
177
178 // Fill in all the site data for currently-selected site
179 SelectSiteList();
180 SetTextboxFocus(gDialog.PageTitleInput);
181 }
182
183 if (gDialog.SiteList.selectedIndex == -1)
184 {
185 // No selected site -- assume same directory
186 gDialog.OtherDirRadiogroup.selectedItem = gDialog.SameLocationRadio;
187 }
188 else if (gPublishSiteData[gDialog.SiteList.selectedIndex].docDir ==
189 gPublishSiteData[gDialog.SiteList.selectedIndex].otherDir)
190 {
191 // For now, check "same location" if dirs are already set to same directory
192 gDialog.OtherDirRadiogroup.selectedItem = gDialog.SameLocationRadio;
193 }
194 else
195 {
196 gDialog.OtherDirRadiogroup.selectedItem = gDialog.UseSubdirRadio;
197 }
198
199 doEnabling();
200
201 SetWindowLocation();
202 }
203
FillSiteList
204 function FillSiteList()
205 {
206 gDialog.SiteList.removeAllItems();
207 gDefaultSiteIndex = -1;
208
209 // Fill the site lists
210 var count = gPublishSiteData.length;
211 var i;
212
213 for (i = 0; i < count; i++)
214 {
215 var name = gPublishSiteData[i].siteName;
216 var menuitem = gDialog.SiteList.appendItem(name);
217 // Highlight the default site
218 if (name == gDefaultSiteName)
219 {
220 gDefaultSiteIndex = i;
221 if (menuitem)
222 {
223 menuitem.setAttribute("class", "menuitem-highlight-1");
224 menuitem.setAttribute("default", "true");
225 }
226 }
227 // Find initial site location
228 if (name == gInitialSiteName)
229 gInitialSiteIndex = i;
230 }
231 }
232
doEnabling
233 function doEnabling()
234 {
235 var disableOther = !gDialog.OtherDirCheckbox.checked;
236 gDialog.SameLocationRadio.disabled = disableOther;
237 gDialog.UseSubdirRadio.disabled = disableOther;
238 gDialog.OtherDirList.disabled = (disableOther || gDialog.SameLocationRadio.selected);
239 }
240
SelectSiteList
241 function SelectSiteList()
242 {
243 var selectedSiteIndex = gDialog.SiteList.selectedIndex;
244
245 var siteName = "";
246 var publishUrl = "";
247 var browseUrl = "";
248 var username = "";
249 var password = "";
250 var savePassword = false;
251 var publishOtherFiles = true;
252
253 gDialog.DocDirList.removeAllItems();
254 gDialog.OtherDirList.removeAllItems();
255
256 if (gPublishSiteData && selectedSiteIndex != -1)
257 {
258 siteName = gPublishSiteData[selectedSiteIndex].siteName;
259 publishUrl = gPublishSiteData[selectedSiteIndex].publishUrl;
260 browseUrl = gPublishSiteData[selectedSiteIndex].browseUrl;
261 username = gPublishSiteData[selectedSiteIndex].username;
262 savePassword = gPasswordManagerOn ? gPublishSiteData[selectedSiteIndex].savePassword : false;
263 if (savePassword)
264 password = gPublishSiteData[selectedSiteIndex].password;
265
266 // Fill the directory menulists
267 if (gPublishSiteData[selectedSiteIndex].dirList.length)
268 {
269 for (var i = 0; i < gPublishSiteData[selectedSiteIndex].dirList.length; i++)
270 {
271 gDialog.DocDirList.appendItem(gPublishSiteData[selectedSiteIndex].dirList[i]);
272 gDialog.OtherDirList.appendItem(gPublishSiteData[selectedSiteIndex].dirList[i]);
273 }
274 }
275 gDialog.DocDirList.value = FormatDirForPublishing(gPublishSiteData[selectedSiteIndex].docDir);
276 gDialog.OtherDirList.value = FormatDirForPublishing(gPublishSiteData[selectedSiteIndex].otherDir);
277 publishOtherFiles = gPublishSiteData[selectedSiteIndex].publishOtherFiles;
278
279 }
280 else
281 {
282 gDialog.DocDirList.value = "";
283 gDialog.OtherDirList.value = "";
284 }
285
286 gDialog.SiteNameInput.value = siteName;
287 gDialog.PublishUrlInput.value = publishUrl;
288 gDialog.BrowseUrlInput.value = browseUrl;
289 gDialog.UsernameInput.value = username;
290 gDialog.PasswordInput.value = password;
291 gDialog.SavePassword.checked = savePassword;
292 gDialog.OtherDirCheckbox.checked = publishOtherFiles;
293
294 doEnabling();
295 }
296
AddNewSite
297 function AddNewSite()
298 {
299 // Button in Publish panel allows user
300 // to automatically switch to "Settings" panel
301 // to enter data for new site
302 SwitchPanel(gDialog.SettingsTab);
303
304 gDialog.SiteList.selectedIndex = -1;
305
306 SelectSiteList();
307
308 gSettingsChanged = true;
309
310 SetTextboxFocus(gDialog.SiteNameInput);
311 }
312
SelectPublishTab
313 function SelectPublishTab()
314 {
315 if (gSettingsChanged && !ValidateSettings())
316 return;
317
318 SwitchPanel(gDialog.PublishTab);
319 SetTextboxFocus(gDialog.PageTitleInput);
320 }
321
SelectSettingsTab
322 function SelectSettingsTab()
323 {
324 SwitchPanel(gDialog.SettingsTab);
325 SetTextboxFocus(gDialog.SiteNameInput);
326 }
327
SwitchPanel
328 function SwitchPanel(tab)
329 {
330 if (gDialog.TabBox.selectedTab != tab)
331 gDialog.TabBox.selectedTab = tab;
332 }
333
onInputSettings
334 function onInputSettings()
335 {
336 // TODO: Save current data during SelectSite and compare here
337 // to detect if real change has occured?
338 gSettingsChanged = true;
339 }
340
GetPublishUrlInput
341 function GetPublishUrlInput()
342 {
343 gDialog.PublishUrlInput.value = FormatUrlForPublishing(gDialog.PublishUrlInput.value);
344 return gDialog.PublishUrlInput.value;
345 }
346
GetBrowseUrlInput
347 function GetBrowseUrlInput()
348 {
349 gDialog.BrowseUrlInput.value = FormatUrlForPublishing(gDialog.BrowseUrlInput.value);
350 return gDialog.BrowseUrlInput.value;
351 }
352
GetDocDirInput
353 function GetDocDirInput()
354 {
355 gDialog.DocDirList.value = FormatDirForPublishing(gDialog.DocDirList.value);
356 return gDialog.DocDirList.value;
357 }
358
GetOtherDirInput
359 function GetOtherDirInput()
360 {
361 gDialog.OtherDirList.value = FormatDirForPublishing(gDialog.OtherDirList.value);
362 return gDialog.OtherDirList.value;
363 }
364
ChooseDir
365 function ChooseDir(menulist)
366 {
367 //TODO: For FTP publish destinations, get file listing of just dirs
368 // and build a tree to let user select dir
369 }
370
ValidateSettings
371 function ValidateSettings()
372 {
373 var siteName = TrimString(gDialog.SiteNameInput.value);
374 if (!siteName)
375 {
376 ShowErrorInPanel(gDialog.SettingsTab, "MissingSiteNameError", gDialog.SiteNameInput);
377 return false;
378 }
379 if (PublishSiteNameExists(siteName, gPublishSiteData, gDialog.SiteList.selectedIndex))
380 {
381 SwitchPanel(gDialog.SettingsTab);
382 ShowInputErrorMessage(GetString("DuplicateSiteNameError").replace(/%name%/, siteName));
383 SetTextboxFocus(gDialog.SiteNameInput);
384 return false;
385 }
386
387 // Extract username and password while removing them from publishingUrl
388 var urlUserObj = {};
389 var urlPassObj = {};
390 var publishUrl = StripUsernamePassword(gDialog.PublishUrlInput.value, urlUserObj, urlPassObj);
391 if (publishUrl)
392 {
393 publishUrl = FormatUrlForPublishing(publishUrl);
394
395 // Assume scheme = "ftp://" if missing
396 // This compensates when user enters hostname w/o scheme (as most ISPs provide)
397 if (!GetScheme(publishUrl))
398 publishUrl = "ftp://" + publishUrl;
399
400 gDialog.PublishUrlInput.value = publishUrl;
401 }
402 else
403 {
404 ShowErrorInPanel(gDialog.SettingsTab, "MissingPublishUrlError", gDialog.PublishUrlInput);
405 return false;
406 }
407 var browseUrl = GetBrowseUrlInput();
408
409 var username = TrimString(gDialog.UsernameInput.value);
410 var savePassword = gDialog.SavePassword.checked;
411 var password = gDialog.PasswordInput.value;
412 var publishOtherFiles = gDialog.OtherDirCheckbox.checked;
413
414 //XXX If there was a username and/or password in the publishUrl
415 // AND in the input field, which do we use?
416 // Let's use those in url only if input is empty
417 if (!username)
418 {
419 username = urlUserObj.value;
420 gDialog.UsernameInput.value = username;
421 gSettingsChanged = true;
422 }
423 if (!password)
424 {
425 password = urlPassObj.value;
426 gDialog.PasswordInput.value = password;
427 gSettingsChanged = true;
428 }
429
430 // Update or add data for a site
431 var siteIndex = gDialog.SiteList.selectedIndex;
432 var newSite = false;
433
434 if (siteIndex == -1)
435 {
436 // No site is selected, add a new site at the end
437 if (gPublishSiteData)
438 {
439 siteIndex = gPublishSiteData.length;
440 }
441 else
442 {
443 // First time: start entire site array
444 gPublishSiteData = new Array(1);
445 siteIndex = 0;
446 gDefaultSiteIndex = 0;
447 gDefaultSiteName = siteName;
448 }
449 gPublishSiteData[siteIndex] = {};
450 gPublishSiteData[siteIndex].docDir = "";
451 gPublishSiteData[siteIndex].otherDir = "";
452 gPublishSiteData[siteIndex].dirList = [""];
453 gPublishSiteData[siteIndex].publishOtherFiles = true;
454 gPublishSiteData[siteIndex].previousSiteName = siteName;
455 newSite = true;
456 }
457 gPublishSiteData[siteIndex].siteName = siteName;
458 gPublishSiteData[siteIndex].publishUrl = publishUrl;
459 gPublishSiteData[siteIndex].browseUrl = browseUrl;
460 gPublishSiteData[siteIndex].username = username;
461 // Don't save password in data that will be saved in prefs
462 gPublishSiteData[siteIndex].password = savePassword ? password : "";
463 gPublishSiteData[siteIndex].savePassword = savePassword;
464
465 if (publishOtherFiles != gPublishSiteData[siteIndex].publishOtherFiles)
466 gSettingsChanged = true;
467
468 gPublishSiteData[siteIndex].publishOtherFiles = publishOtherFiles;
469
470 gDialog.SiteList.selectedIndex = siteIndex;
471 if (siteIndex == gDefaultSiteIndex)
472 gDefaultSiteName = siteName;
473
474 // Should never be empty, but be sure we have a default site
475 if (!gDefaultSiteName)
476 {
477 gDefaultSiteName = gPublishSiteData[0].siteName;
478 gDefaultSiteIndex = 0;
479 }
480
481 // Rebuild the site menulist if we added a new site
482 if (newSite)
483 {
484 FillSiteList();
485 gDialog.SiteList.selectedIndex = siteIndex;
486 }
487 else
488 {
489 // Update selected item if sitename changed
490 var selectedItem = gDialog.SiteList.selectedItem;
491 if (selectedItem)
492 {
493 var oldName = selectedItem.getAttribute("label");
494 if (oldName != siteName)
495 {
496 selectedItem.setAttribute("label", siteName);
497 gDialog.SiteList.setAttribute("label", siteName);
498 gSettingsChanged = true;
499 if (oldName == gDefaultSiteName)
500 gDefaultSiteName = siteName;
501 }
502 }
503 }
504
505 // Get the directory name in site to publish to
506 var docDir = GetDocDirInput();
507
508 gPublishSiteData[siteIndex].docDir = docDir;
509
510 // And directory for images and other files
511 var otherDir = GetOtherDirInput();
512 if (gDialog.SameLocationRadio.selected)
513 otherDir = docDir;
514 else
515 otherDir = GetOtherDirInput();
516
517 gPublishSiteData[siteIndex].otherDir = otherDir;
518
519 // Fill return data object
520 gReturnData.siteName = siteName;
521 gReturnData.previousSiteName = gPublishSiteData[siteIndex].previousSiteName;
522 gReturnData.publishUrl = publishUrl;
523 gReturnData.browseUrl = browseUrl;
524 gReturnData.username = username;
525 // Note that we use the password for the next publish action
526 // even if savePassword is false; but we won't save it in PasswordManager database
527 gReturnData.password = password;
528 gReturnData.savePassword = savePassword;
529 gReturnData.docDir = gPublishSiteData[siteIndex].docDir;
530 gReturnData.otherDir = gPublishSiteData[siteIndex].otherDir;
531 gReturnData.publishOtherFiles = publishOtherFiles;
532 gReturnData.dirList = gPublishSiteData[siteIndex].dirList;
533 return true;
534 }
535
ValidateData
536 function ValidateData()
537 {
538 if (!ValidateSettings())
539 return false;
540
541 var siteIndex = gDialog.SiteList.selectedIndex;
542 if (siteIndex == -1)
543 return false;
544
545 var filename = TrimString(gDialog.FilenameInput.value);
546 if (!filename)
547 {
548 ShowErrorInPanel(gDialog.PublishTab, "MissingPublishFilename", gDialog.FilenameInput);
549 return false;
550 }
551 gReturnData.filename = filename;
552
553 return true;
554 }
555
ShowErrorInPanel
556 function ShowErrorInPanel(tab, errorMsgId, widgetWithError)
557 {
558 SwitchPanel(tab);
559 ShowInputErrorMessage(GetString(errorMsgId));
560 if (widgetWithError)
561 SetTextboxFocus(widgetWithError);
562 }
563
onAccept
564 function onAccept()
565 {
566 if (ValidateData())
567 {
568 // DON'T save the docDir and otherDir before trying to publish
569 gReturnData.saveDirs = false;
570
571 // We save new site data to prefs only if we are attempting to publish
572 if (gSettingsChanged)
573 SavePublishDataToPrefs(gReturnData);
574
575 // Set flag to resave data after publishing
576 // so we save docDir and otherDir if we published successfully
577 gReturnData.savePublishData = true;
578
579 var title = TrimString(gDialog.PageTitleInput.value);
580 if (title != gPreviousTitle)
581 SetDocumentTitle(title);
582
583 SaveWindowLocation();
584 window.opener.ok = true;
585 return true;
586 }
587
588 return false;
589 }