2
* The contents of this file are subject to the Netscape Public
3
* License Version 1.1 (the "License"); you may not use this file
4
* except in compliance with the License. You may obtain a copy of
5
* the License at http://www.mozilla.org/NPL/
7
* Software distributed under the License is distributed on an "AS
8
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9
* implied. See the License for the specific language governing
10
* rights and limitations under the License.
12
* The Original Code is Mozilla Communicator client code, released
15
* The Initial Developer of the Original Code is Netscape
16
* Communications Corporation. Portions created by Netscape are
17
* Copyright (C) 2001 Netscape Communications Corporation. All
21
* Charley Manske (cmanske@netscape.com)
22
* Ryan Cassin (rcassin@supernova.org)
27
var gDefaultSiteIndex = -1;
29
var gPreviousDefaultDir;
31
var gSettingsChanged = false;
33
var gInitialSiteIndex = -1;
34
var gPasswordManagerOn = true;
36
// Dialog initialization code
39
window.opener.ok = false;
41
// Element to edit is passed in
42
gInitialSiteName = window.arguments[1];
43
gReturnData = window.arguments[2];
44
if (!gReturnData || !GetCurrentEditor())
46
dump("Publish: No editor or return data object not supplied\n");
51
gDialog.TabBox = document.getElementById("TabBox");
52
gDialog.PublishTab = document.getElementById("PublishTab");
53
gDialog.SettingsTab = document.getElementById("SettingsTab");
56
gDialog.PageTitleInput = document.getElementById("PageTitleInput");
57
gDialog.FilenameInput = document.getElementById("FilenameInput");
58
gDialog.SiteList = document.getElementById("SiteList");
59
gDialog.DocDirList = document.getElementById("DocDirList");
60
gDialog.OtherDirCheckbox = document.getElementById("OtherDirCheckbox");
61
gDialog.OtherDirRadiogroup = document.getElementById("OtherDirRadiogroup");
62
gDialog.SameLocationRadio = document.getElementById("SameLocationRadio");
63
gDialog.UseSubdirRadio = document.getElementById("UseSubdirRadio");
64
gDialog.OtherDirList = document.getElementById("OtherDirList");
67
gDialog.SiteNameInput = document.getElementById("SiteNameInput");
68
gDialog.PublishUrlInput = document.getElementById("PublishUrlInput");
69
gDialog.BrowseUrlInput = document.getElementById("BrowseUrlInput");
70
gDialog.UsernameInput = document.getElementById("UsernameInput");
71
gDialog.PasswordInput = document.getElementById("PasswordInput");
72
gDialog.SavePassword = document.getElementById("SavePassword");
74
gDialog.PublishButton = document.documentElement.getButton("accept");
76
// Change 'Ok' button to 'Publish'
77
gDialog.PublishButton.setAttribute("label", GetString("Publish"));
79
gPasswordManagerOn = GetBoolPref("signon.rememberSignons");
80
gDialog.SavePassword.disabled = !gPasswordManagerOn;
82
gPublishSiteData = GetPublishSiteData();
83
gDefaultSiteName = GetDefaultPublishSiteName();
85
var addNewSite = false;
92
// No current site data, start a new item in the Settings panel
97
var docUrl = GetDocumentUrl();
98
var scheme = GetScheme(docUrl);
103
filename = GetFilename(docUrl);
105
if (scheme != "file")
107
var siteFound = false;
109
// Editing a remote URL.
110
// Attempt to find doc URL in Site Data
111
if (gPublishSiteData)
114
var siteIndex = FindSiteIndexAndDocDir(gPublishSiteData, docUrl, dirObj);
116
// Select this site only if the same as user's intended site, or there wasn't one
117
if (siteIndex != -1 && (gInitialSiteIndex == -1 || siteIndex == gInitialSiteIndex))
121
// Select the site we found
122
gDialog.SiteList.selectedIndex = siteIndex;
123
var docDir = dirObj.value;
125
// Use the directory within site in the editable menulist
126
gPublishSiteData[siteIndex].docDir = docDir;
128
//XXX HOW DO WE DECIDE WHAT "OTHER" DIR TO USE?
129
//gPublishSiteData[siteIndex].otherDir = docDir;
134
// Not found in site database
135
// Setup for a new site and use data from a remote URL
141
var publishData = CreatePublishDataFromUrl(docUrl);
144
filename = publishData.filename;
145
gDialog.SiteNameInput.value = publishData.siteName;
146
gDialog.PublishUrlInput.value = publishData.publishUrl;
147
gDialog.BrowseUrlInput.value = publishData.browseUrl;
148
gDialog.UsernameInput.value = publishData.username;
149
gDialog.PasswordInput.value = publishData.password;
150
gDialog.SavePassword.checked = false;
156
gPreviousTitle = GetDocumentTitle();
159
gDialog.PageTitleInput.value = gPreviousTitle;
160
gDialog.FilenameInput.value = decodeURIComponent(filename);
164
// If not adding a site and we haven't selected a site -- use initial or default site
165
if (gDialog.SiteList.selectedIndex == -1)
166
gDialog.SiteList.selectedIndex = (gInitialSiteIndex != -1) ? gInitialSiteIndex : gDefaultSiteIndex;
168
// Fill in all the site data for currently-selected site
170
SetTextboxFocus(gDialog.PageTitleInput);
173
if (gDialog.SiteList.selectedIndex == -1)
175
// No selected site -- assume same directory
176
gDialog.OtherDirRadiogroup.selectedItem = gDialog.SameLocationRadio;
178
else if (gPublishSiteData[gDialog.SiteList.selectedIndex].docDir ==
179
gPublishSiteData[gDialog.SiteList.selectedIndex].otherDir)
181
// For now, check "same location" if dirs are already set to same directory
182
gDialog.OtherDirRadiogroup.selectedItem = gDialog.SameLocationRadio;
186
gDialog.OtherDirRadiogroup.selectedItem = gDialog.UseSubdirRadio;
194
function FillSiteList()
196
gDialog.SiteList.removeAllItems();
197
gDefaultSiteIndex = -1;
199
// Fill the site lists
200
var count = gPublishSiteData.length;
203
for (i = 0; i < count; i++)
205
var name = gPublishSiteData[i].siteName;
206
var menuitem = gDialog.SiteList.appendItem(name);
207
// Highlight the default site
208
if (name == gDefaultSiteName)
210
gDefaultSiteIndex = i;
213
menuitem.setAttribute("class", "menuitem-highlight-1");
214
menuitem.setAttribute("default", "true");
217
// Find initial site location
218
if (name == gInitialSiteName)
219
gInitialSiteIndex = i;
223
function doEnabling()
225
var disableOther = !gDialog.OtherDirCheckbox.checked;
226
gDialog.SameLocationRadio.disabled = disableOther;
227
gDialog.UseSubdirRadio.disabled = disableOther;
228
gDialog.OtherDirList.disabled = (disableOther || gDialog.SameLocationRadio.selected);
231
function SelectSiteList()
233
var selectedSiteIndex = gDialog.SiteList.selectedIndex;
240
var savePassword = false;
241
var publishOtherFiles = true;
243
gDialog.DocDirList.removeAllItems();
244
gDialog.OtherDirList.removeAllItems();
246
if (gPublishSiteData && selectedSiteIndex != -1)
248
siteName = gPublishSiteData[selectedSiteIndex].siteName;
249
publishUrl = gPublishSiteData[selectedSiteIndex].publishUrl;
250
browseUrl = gPublishSiteData[selectedSiteIndex].browseUrl;
251
username = gPublishSiteData[selectedSiteIndex].username;
252
savePassword = gPasswordManagerOn ? gPublishSiteData[selectedSiteIndex].savePassword : false;
254
password = gPublishSiteData[selectedSiteIndex].password;
256
// Fill the directory menulists
257
if (gPublishSiteData[selectedSiteIndex].dirList.length)
259
for (var i = 0; i < gPublishSiteData[selectedSiteIndex].dirList.length; i++)
261
gDialog.DocDirList.appendItem(gPublishSiteData[selectedSiteIndex].dirList[i]);
262
gDialog.OtherDirList.appendItem(gPublishSiteData[selectedSiteIndex].dirList[i]);
265
gDialog.DocDirList.value = FormatDirForPublishing(gPublishSiteData[selectedSiteIndex].docDir);
266
gDialog.OtherDirList.value = FormatDirForPublishing(gPublishSiteData[selectedSiteIndex].otherDir);
267
publishOtherFiles = gPublishSiteData[selectedSiteIndex].publishOtherFiles;
272
gDialog.DocDirList.value = "";
273
gDialog.OtherDirList.value = "";
276
gDialog.SiteNameInput.value = siteName;
277
gDialog.PublishUrlInput.value = publishUrl;
278
gDialog.BrowseUrlInput.value = browseUrl;
279
gDialog.UsernameInput.value = username;
280
gDialog.PasswordInput.value = password;
281
gDialog.SavePassword.checked = savePassword;
282
gDialog.OtherDirCheckbox.checked = publishOtherFiles;
287
function AddNewSite()
289
// Button in Publish panel allows user
290
// to automatically switch to "Settings" panel
291
// to enter data for new site
292
SwitchPanel(gDialog.SettingsTab);
294
gDialog.SiteList.selectedIndex = -1;
298
gSettingsChanged = true;
300
SetTextboxFocus(gDialog.SiteNameInput);
303
function SelectPublishTab()
305
if (gSettingsChanged && !ValidateSettings())
308
SwitchPanel(gDialog.PublishTab);
309
SetTextboxFocus(gDialog.PageTitleInput);
312
function SelectSettingsTab()
314
SwitchPanel(gDialog.SettingsTab);
315
SetTextboxFocus(gDialog.SiteNameInput);
318
function SwitchPanel(tab)
320
if (gDialog.TabBox.selectedTab != tab)
321
gDialog.TabBox.selectedTab = tab;
324
function onInputSettings()
326
// TODO: Save current data during SelectSite and compare here
327
// to detect if real change has occured?
328
gSettingsChanged = true;
331
function GetPublishUrlInput()
333
gDialog.PublishUrlInput.value = FormatUrlForPublishing(gDialog.PublishUrlInput.value);
334
return gDialog.PublishUrlInput.value;
337
function GetBrowseUrlInput()
339
gDialog.BrowseUrlInput.value = FormatUrlForPublishing(gDialog.BrowseUrlInput.value);
340
return gDialog.BrowseUrlInput.value;
343
function GetDocDirInput()
345
gDialog.DocDirList.value = FormatDirForPublishing(gDialog.DocDirList.value);
346
return gDialog.DocDirList.value;
349
function GetOtherDirInput()
351
gDialog.OtherDirList.value = FormatDirForPublishing(gDialog.OtherDirList.value);
352
return gDialog.OtherDirList.value;
355
function ChooseDir(menulist)
357
//TODO: For FTP publish destinations, get file listing of just dirs
358
// and build a tree to let user select dir
361
function ValidateSettings()
363
var siteName = TrimString(gDialog.SiteNameInput.value);
366
ShowErrorInPanel(gDialog.SettingsTab, "MissingSiteNameError", gDialog.SiteNameInput);
369
if (PublishSiteNameExists(siteName, gPublishSiteData, gDialog.SiteList.selectedIndex))
371
SwitchPanel(gDialog.SettingsTab);
372
ShowInputErrorMessage(GetString("DuplicateSiteNameError").replace(/%name%/, siteName));
373
SetTextboxFocus(gDialog.SiteNameInput);
377
// Extract username and password while removing them from publishingUrl
380
var publishUrl = StripUsernamePassword(gDialog.PublishUrlInput.value, urlUserObj, urlPassObj);
383
publishUrl = FormatUrlForPublishing(publishUrl);
385
// Assume scheme = "ftp://" if missing
386
// This compensates when user enters hostname w/o scheme (as most ISPs provide)
387
if (!GetScheme(publishUrl))
388
publishUrl = "ftp://" + publishUrl;
390
gDialog.PublishUrlInput.value = publishUrl;
394
ShowErrorInPanel(gDialog.SettingsTab, "MissingPublishUrlError", gDialog.PublishUrlInput);
397
var browseUrl = GetBrowseUrlInput();
399
var username = TrimString(gDialog.UsernameInput.value);
400
var savePassword = gDialog.SavePassword.checked;
401
var password = gDialog.PasswordInput.value;
402
var publishOtherFiles = gDialog.OtherDirCheckbox.checked;
404
//XXX If there was a username and/or password in the publishUrl
405
// AND in the input field, which do we use?
406
// Let's use those in url only if input is empty
409
username = urlUserObj.value;
410
gDialog.UsernameInput.value = username;
411
gSettingsChanged = true;
415
password = urlPassObj.value;
416
gDialog.PasswordInput.value = password;
417
gSettingsChanged = true;
420
// Update or add data for a site
421
var siteIndex = gDialog.SiteList.selectedIndex;
426
// No site is selected, add a new site at the end
427
if (gPublishSiteData)
429
siteIndex = gPublishSiteData.length;
433
// First time: start entire site array
434
gPublishSiteData = new Array(1);
436
gDefaultSiteIndex = 0;
437
gDefaultSiteName = siteName;
439
gPublishSiteData[siteIndex] = {};
440
gPublishSiteData[siteIndex].docDir = "";
441
gPublishSiteData[siteIndex].otherDir = "";
442
gPublishSiteData[siteIndex].dirList = [""];
443
gPublishSiteData[siteIndex].publishOtherFiles = true;
444
gPublishSiteData[siteIndex].previousSiteName = siteName;
447
gPublishSiteData[siteIndex].siteName = siteName;
448
gPublishSiteData[siteIndex].publishUrl = publishUrl;
449
gPublishSiteData[siteIndex].browseUrl = browseUrl;
450
gPublishSiteData[siteIndex].username = username;
451
// Don't save password in data that will be saved in prefs
452
gPublishSiteData[siteIndex].password = savePassword ? password : "";
453
gPublishSiteData[siteIndex].savePassword = savePassword;
455
if (publishOtherFiles != gPublishSiteData[siteIndex].publishOtherFiles)
456
gSettingsChanged = true;
458
gPublishSiteData[siteIndex].publishOtherFiles = publishOtherFiles;
460
gDialog.SiteList.selectedIndex = siteIndex;
461
if (siteIndex == gDefaultSiteIndex)
462
gDefaultSiteName = siteName;
464
// Should never be empty, but be sure we have a default site
465
if (!gDefaultSiteName)
467
gDefaultSiteName = gPublishSiteData[0].siteName;
468
gDefaultSiteIndex = 0;
471
// Rebuild the site menulist if we added a new site
475
gDialog.SiteList.selectedIndex = siteIndex;
479
// Update selected item if sitename changed
480
var selectedItem = gDialog.SiteList.selectedItem;
483
var oldName = selectedItem.getAttribute("label");
484
if (oldName != siteName)
486
selectedItem.setAttribute("label", siteName);
487
gDialog.SiteList.setAttribute("label", siteName);
488
gSettingsChanged = true;
489
if (oldName == gDefaultSiteName)
490
gDefaultSiteName = siteName;
495
// Get the directory name in site to publish to
496
var docDir = GetDocDirInput();
498
gPublishSiteData[siteIndex].docDir = docDir;
500
// And directory for images and other files
501
var otherDir = GetOtherDirInput();
502
if (gDialog.SameLocationRadio.selected)
505
otherDir = GetOtherDirInput();
507
gPublishSiteData[siteIndex].otherDir = otherDir;
509
// Fill return data object
510
gReturnData.siteName = siteName;
511
gReturnData.previousSiteName = gPublishSiteData[siteIndex].previousSiteName;
512
gReturnData.publishUrl = publishUrl;
513
gReturnData.browseUrl = browseUrl;
514
gReturnData.username = username;
515
// Note that we use the password for the next publish action
516
// even if savePassword is false; but we won't save it in PasswordManager database
517
gReturnData.password = password;
518
gReturnData.savePassword = savePassword;
519
gReturnData.docDir = gPublishSiteData[siteIndex].docDir;
520
gReturnData.otherDir = gPublishSiteData[siteIndex].otherDir;
521
gReturnData.publishOtherFiles = publishOtherFiles;
522
gReturnData.dirList = gPublishSiteData[siteIndex].dirList;
526
function ValidateData()
528
if (!ValidateSettings())
531
var siteIndex = gDialog.SiteList.selectedIndex;
535
var filename = TrimString(gDialog.FilenameInput.value);
538
ShowErrorInPanel(gDialog.PublishTab, "MissingPublishFilename", gDialog.FilenameInput);
541
gReturnData.filename = filename;
546
function ShowErrorInPanel(tab, errorMsgId, widgetWithError)
549
ShowInputErrorMessage(GetString(errorMsgId));
551
SetTextboxFocus(widgetWithError);
554
function doHelpButton()
556
if (gDialog.TabBox.selectedTab == gDialog.PublishTab)
557
openHelp('comp-doc-publish-document','chrome://help/locale/nvu.rdf');
559
openHelp('comp-doc-publish-settings','chrome://help/locale/nvu.rdf');
566
// DON'T save the docDir and otherDir before trying to publish
567
gReturnData.saveDirs = false;
569
// We save new site data to prefs only if we are attempting to publish
570
if (gSettingsChanged)
571
SavePublishDataToPrefs(gReturnData);
573
// Set flag to resave data after publishing
574
// so we save docDir and otherDir if we published successfully
575
gReturnData.savePublishData = true;
577
var title = TrimString(gDialog.PageTitleInput.value);
578
if (title != gPreviousTitle)
579
SetDocumentTitle(title);
581
SaveWindowLocation();
582
window.opener.ok = true;