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 / EditorPublish.js < prev    next >
Text File  |  2003-06-08  |  18KB  |  588 lines

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