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 / EdListProps.js < prev    next >
Text File  |  2003-06-08  |  14KB  |  464 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) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  * 
  20.  * Contributor(s):
  21.  *   Charles Manske (cmanske@netscape.com)
  22.  *   Ryan Cassin (rcassin@supernova.org)
  23.  *   David Turley (dturley@pobox.com) contributed Roman Numeral conversion code.
  24.  *   Neil Rashbrook <neil@parkwaycc.co.uk>
  25.  */
  26.  
  27. //Cancel() is in EdDialogCommon.js
  28. var gBulletStyleType = "";
  29. var gNumberStyleType = "";
  30. var gListElement;
  31. var gOriginalListType = "";
  32. var gListType = "";
  33. var gMixedListSelection = false;
  34. var gStyleType = "";
  35. var gOriginalStyleType = "";
  36. const gOnesArray = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];
  37. const gTensArray = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"];
  38. const gHundredsArray = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"];
  39. const gThousandsArray = ["", "M", "MM", "MMM", "MMMM", "MMMMM", "MMMMMM", "MMMMMMM", "MMMMMMMM", "MMMMMMMMM"];
  40. const gRomanDigits = {I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000};
  41. const A = "A".charCodeAt(0);
  42. const gArabic = "1";
  43. const gUpperRoman = "I";
  44. const gLowerRoman = "i";
  45. const gUpperLetters = "A";
  46. const gLowerLetters = "a";
  47. const gDecimalCSS = "decimal";
  48. const gUpperRomanCSS = "upper-roman";
  49. const gLowerRomanCSS = "lower-roman";
  50. const gUpperAlphaCSS = "upper-alpha";
  51. const gLowerAlphaCSS = "lower-alpha";
  52.  
  53. // dialog initialization code
  54. function Startup()
  55. {
  56.   var editor = GetCurrentEditor();
  57.   if (!editor)
  58.   {
  59.     window.close();
  60.     return;
  61.   }
  62.   gDialog.ListTypeList = document.getElementById("ListType");
  63.   gDialog.BulletStyleList = document.getElementById("BulletStyle");
  64.   gDialog.BulletStyleLabel = document.getElementById("BulletStyleLabel");
  65.   gDialog.StartingNumberInput = document.getElementById("StartingNumber");
  66.   gDialog.StartingNumberLabel = document.getElementById("StartingNumberLabel");
  67.   gDialog.AdvancedEditButton = document.getElementById("AdvancedEditButton1");
  68.   gDialog.RadioGroup = document.getElementById("RadioGroup");
  69.   gDialog.ChangeAllRadio = document.getElementById("ChangeAll");
  70.   gDialog.ChangeSelectedRadio = document.getElementById("ChangeSelected");
  71.   
  72.   // Try to get an existing list(s)
  73.   var mixedObj = { value: null };
  74.   try {
  75.     gListType = editor.getListState(mixedObj, {}, {}, {} );
  76.  
  77.     // We may have mixed list and non-list, or > 1 list type in selection
  78.     gMixedListSelection = mixedObj.value;
  79.  
  80.     // Get the list element at the anchor node
  81.     gListElement = editor.getElementOrParentByTagName("list", null);
  82.   } catch (e) {}
  83.  
  84.   // The copy to use in AdvancedEdit
  85.   if (gListElement)
  86.     globalElement = gListElement.cloneNode(false);
  87.  
  88.   // Show extra options for changing entire list if we have one already.
  89.   gDialog.RadioGroup.collapsed = !gListElement;
  90.   if (gListElement)
  91.   {
  92.     // Radio button index is persistant
  93.     if (gDialog.RadioGroup.getAttribute("index") == "1")
  94.       gDialog.RadioGroup.selectedItem = gDialog.ChangeSelectedRadio;
  95.     else
  96.       gDialog.RadioGroup.selectedItem = gDialog.ChangeAllRadio;
  97.   }
  98.  
  99.   InitDialog();
  100.  
  101.   gOriginalListType = gListType;
  102.  
  103.   gDialog.ListTypeList.focus();
  104.  
  105.   SetWindowLocation();
  106. }
  107.  
  108. function InitDialog()
  109. {
  110.   // Note that if mixed, we we pay attention 
  111.   //   only to the anchor node's list type
  112.   // (i.e., don't confuse user with "mixed" designation)
  113.   if (gListElement)
  114.     gListType = gListElement.nodeName.toLowerCase();
  115.   else
  116.     gListType = "";
  117.   
  118.   gDialog.ListTypeList.value = gListType;
  119.   gDialog.StartingNumberInput.value = "";
  120.   
  121.   // Last param = true means attribute value is case-sensitive
  122.   var type = globalElement ? GetHTMLOrCSSStyleValue(globalElement, "type", "list-style-type") : null;
  123.  
  124.   var index = 0;
  125.   if (gListType == "ul")
  126.   {
  127.     if (type)
  128.     {
  129.       type = type.toLowerCase();
  130.       gBulletStyleType = type;
  131.       gOriginalStyleType = type;
  132.     }
  133.   }
  134.   else if (gListType == "ol")
  135.   {
  136.     // Translate CSS property strings
  137.     switch (type.toLowerCase())
  138.     {
  139.       case gDecimalCSS:
  140.         type = gArabic;
  141.         break;
  142.       case gUpperRomanCSS:
  143.         type = gUpperRoman;
  144.         break;
  145.       case gLowerRomanCSS:
  146.         type = gLowerRoman;
  147.         break;
  148.       case gUpperAlphaCSS:
  149.         type = gUpperLetters;
  150.         break;
  151.       case gLowerAlphaCSS:
  152.         type = gLowerLetters;
  153.         break;
  154.     }
  155.     if (type)
  156.     {
  157.       gNumberStyleType = type;
  158.       gOriginalStyleType = type;
  159.     }
  160.  
  161.     // Convert attribute number to appropriate letter or roman numeral
  162.     gDialog.StartingNumberInput.value = 
  163.       ConvertStartAttrToUserString(globalElement.getAttribute("start"), type);
  164.   }
  165.   BuildBulletStyleList();
  166. }
  167.  
  168. // Convert attribute number to appropriate letter or roman numeral
  169. function ConvertStartAttrToUserString(startAttr, type)
  170. {
  171.   switch (type)
  172.   {
  173.     case gUpperRoman:
  174.       startAttr = ConvertArabicToRoman(startAttr);
  175.       break;
  176.     case gLowerRoman:
  177.       startAttr = ConvertArabicToRoman(startAttr).toLowerCase();
  178.       break;
  179.     case gUpperLetters:
  180.       startAttr = ConvertArabicToLetters(startAttr);
  181.       break;
  182.     case gLowerLetters:
  183.       startAttr = ConvertArabicToLetters(startAttr).toLowerCase();
  184.       break;
  185.   }
  186.   return startAttr;
  187. }
  188.  
  189. function BuildBulletStyleList()
  190. {
  191.   gDialog.BulletStyleList.removeAllItems();
  192.   var label;
  193.  
  194.   if (gListType == "ul")
  195.   {
  196.     gDialog.BulletStyleList.removeAttribute("disabled");
  197.     gDialog.BulletStyleLabel.removeAttribute("disabled");
  198.     gDialog.StartingNumberInput.setAttribute("disabled", "true");
  199.     gDialog.StartingNumberLabel.setAttribute("disabled", "true");
  200.  
  201.     label = GetString("BulletStyle");
  202.  
  203.     gDialog.BulletStyleList.appendItem(GetString("Automatic"), "");
  204.     gDialog.BulletStyleList.appendItem(GetString("SolidCircle"), "disc");
  205.     gDialog.BulletStyleList.appendItem(GetString("OpenCircle"), "circle");
  206.     gDialog.BulletStyleList.appendItem(GetString("SolidSquare"), "square");
  207.  
  208.     gDialog.BulletStyleList.value = gBulletStyleType;
  209.   }
  210.   else if (gListType == "ol")
  211.   {
  212.     gDialog.BulletStyleList.removeAttribute("disabled");
  213.     gDialog.BulletStyleLabel.removeAttribute("disabled");
  214.     gDialog.StartingNumberInput.removeAttribute("disabled");
  215.     gDialog.StartingNumberLabel.removeAttribute("disabled");
  216.     label = GetString("NumberStyle");
  217.  
  218.     gDialog.BulletStyleList.appendItem(GetString("Automatic"), "");
  219.     gDialog.BulletStyleList.appendItem(GetString("Style_1"), gArabic);
  220.     gDialog.BulletStyleList.appendItem(GetString("Style_I"), gUpperRoman);
  221.     gDialog.BulletStyleList.appendItem(GetString("Style_i"), gLowerRoman);
  222.     gDialog.BulletStyleList.appendItem(GetString("Style_A"), gUpperLetters);
  223.     gDialog.BulletStyleList.appendItem(GetString("Style_a"), gLowerLetters);
  224.  
  225.     gDialog.BulletStyleList.value = gNumberStyleType;
  226.   } 
  227.   else 
  228.   {
  229.     gDialog.BulletStyleList.setAttribute("disabled", "true");
  230.     gDialog.BulletStyleLabel.setAttribute("disabled", "true");
  231.     gDialog.StartingNumberInput.setAttribute("disabled", "true");
  232.     gDialog.StartingNumberLabel.setAttribute("disabled", "true");
  233.   }
  234.   
  235.   // Disable advanced edit button if changing to "normal"
  236.   if (gListType)
  237.     gDialog.AdvancedEditButton.removeAttribute("disabled");
  238.   else
  239.     gDialog.AdvancedEditButton.setAttribute("disabled", "true");
  240.  
  241.   if (label)
  242.     gDialog.BulletStyleLabel.setAttribute("label",label);
  243. }
  244.  
  245. function SelectListType()
  246. {
  247.   // Each list type is stored in the "value" of each menuitem
  248.   var NewType = gDialog.ListTypeList.value;
  249.  
  250.   if (NewType == "ol")
  251.     SetTextboxFocus(gDialog.StartingNumberInput);
  252.  
  253.   if (gListType != NewType)
  254.   {
  255.     gListType = NewType;
  256.     
  257.     // Create a newlist object for Advanced Editing
  258.     try {
  259.       if (gListType)
  260.         globalElement = GetCurrentEditor().createElementWithDefaults(gListType);
  261.     } catch (e) {}
  262.  
  263.     BuildBulletStyleList();
  264.   }
  265. }
  266.  
  267. function SelectBulletStyle()
  268. {
  269.   // Save the selected index so when user changes
  270.   //   list style, restore index to associated list
  271.   // Each bullet or number type is stored in the "value" of each menuitem
  272.   if (gListType == "ul")
  273.     gBulletStyleType = gDialog.BulletStyleList.value;
  274.   else if (gListType == "ol")
  275.   {
  276.     var type = gDialog.BulletStyleList.value;
  277.     if (gNumberStyleType != type)
  278.     {
  279.       // Convert existing input value to attr number first,
  280.       //   then convert to the appropriate format for the newly-selected
  281.       gDialog.StartingNumberInput.value = 
  282.         ConvertStartAttrToUserString( ConvertUserStringToStartAttr(gNumberStyleType), type);
  283.  
  284.       gNumberStyleType = type;
  285.       SetTextboxFocus(gDialog.StartingNumberInput);
  286.     }
  287.   }
  288. }
  289.  
  290. function ValidateData()
  291. {
  292.   gBulletStyleType = gDialog.BulletStyleList.value;
  293.   // globalElement should already be of the correct type 
  294.  
  295.   if (globalElement)
  296.   {
  297.     var editor = GetCurrentEditor();
  298.     if (gListType == "ul")
  299.     {
  300.       if (gBulletStyleType && gDialog.ChangeAllRadio.selected)
  301.         globalElement.setAttribute("type", gBulletStyleType);
  302.       else
  303.         try {
  304.           editor.removeAttributeOrEquivalent(globalElement, "type", true);
  305.         } catch (e) {}
  306.  
  307.     } 
  308.     else if (gListType == "ol")
  309.     {
  310.       if (gBulletStyleType)
  311.         globalElement.setAttribute("type", gBulletStyleType);
  312.       else
  313.         try {
  314.           editor.removeAttributeOrEquivalent(globalElement, "type", true);
  315.         } catch (e) {}
  316.       
  317.       var startingNumber = ConvertUserStringToStartAttr(gBulletStyleType);
  318.       if (startingNumber)
  319.         globalElement.setAttribute("start", startingNumber);
  320.       else
  321.         globalElement.removeAttribute("start");
  322.     }
  323.   }
  324.   return true;
  325. }
  326.  
  327. function ConvertUserStringToStartAttr(type)
  328. {
  329.   var startingNumber = TrimString(gDialog.StartingNumberInput.value);
  330.  
  331.   switch (type)
  332.   {
  333.     case gUpperRoman:
  334.     case gLowerRoman:
  335.       // If the input isn't an integer, assume it's a roman numeral. Convert it.
  336.       if (!Number(startingNumber))
  337.         startingNumber = ConvertRomanToArabic(startingNumber);
  338.       break;
  339.     case gUpperLetters:
  340.     case gLowerLetters:
  341.       // Get the number equivalent of the letters
  342.       if (!Number(startingNumber))
  343.         startingNumber = ConvertLettersToArabic(startingNumber);
  344.       break;
  345.   }
  346.   return startingNumber;
  347. }
  348.  
  349. function ConvertRomanToArabic(num)
  350. {
  351.   num = num.toUpperCase();
  352.   if (num && !/[^MDCLXVI]/i.test(num))
  353.   {
  354.     var Arabic = 0;
  355.     var last_digit = 1000;
  356.     for (var i=0; i < num.length; i++)
  357.     {
  358.       var digit = gRomanDigits[num.charAt(i)];
  359.       if (last_digit < digit)
  360.         Arabic -= 2 * last_digit;
  361.  
  362.       last_digit = digit;
  363.       Arabic += last_digit;
  364.     }
  365.     return Arabic;
  366.   }
  367.  
  368.   return "";
  369. }
  370.  
  371. function ConvertArabicToRoman(num)
  372. {
  373.   if (/^\d{1,4}$/.test(num))
  374.   {
  375.     var digits = ("000" + num).substr(-4);
  376.     return gThousandsArray[digits.charAt(0)] +
  377.            gHundredsArray[digits.charAt(1)] +
  378.            gTensArray[digits.charAt(2)] +
  379.            gOnesArray[digits.charAt(3)];
  380.   }
  381.   return "";
  382. }
  383.  
  384. function ConvertLettersToArabic(letters)
  385. {
  386.   letters = letters.toUpperCase();
  387.   if (!letters || /[^A-Z]/.test(letters))
  388.     return "";
  389.  
  390.   var num = 0;
  391.   for (var i = 0; i < letters.length; i++)
  392.     num = num * 26 + letters.charCodeAt(i) - A + 1;
  393.   return num;
  394. }
  395.  
  396. function ConvertArabicToLetters(num)
  397. {
  398.   var letters = "";
  399.   while (num) {
  400.     num--;
  401.     letters = String.fromCharCode(A + (num % 26)) + letters;
  402.     num = Math.floor(num / 26);
  403.   }
  404.   return letters;
  405. }
  406.  
  407. function onAccept()
  408. {
  409.   if (ValidateData())
  410.   {
  411.     // Coalesce into one undo transaction
  412.     var editor = GetCurrentEditor();
  413.  
  414.     editor.beginTransaction();
  415.  
  416.     var changeEntireList = gDialog.RadioGroup.selectedItem == gDialog.ChangeAllRadio;
  417.  
  418.     // Remember which radio button was selected
  419.     if (gListElement)
  420.       gDialog.RadioGroup.setAttribute("index", changeEntireList ? "0" : "1");
  421.  
  422.     var changeList;
  423.     if (gListElement && gDialog.ChangeAllRadio.selected)
  424.     {
  425.       changeList = true;
  426.     }
  427.     else
  428.       changeList = gMixedListSelection || gListType != gOriginalListType || 
  429.                    gBulletStyleType != gOriginalStyleType;
  430.     if (changeList)
  431.     {
  432.       try {
  433.         if (gListType)
  434.         {
  435.           editor.makeOrChangeList(gListType, changeEntireList,
  436.                      (gBulletStyleType != gOriginalStyleType) ? gBulletStyleType : null);
  437.  
  438.           // Get the new list created:
  439.           gListElement = editor.getElementOrParentByTagName(gListType, null);
  440.  
  441.           editor.cloneAttributes(gListElement, globalElement);
  442.         }
  443.         else
  444.         {
  445.           // Remove all existing lists
  446.           if (gListElement && changeEntireList)
  447.             editor.selectElement(gListElement);
  448.  
  449.           editor.removeList("ol");
  450.           editor.removeList("ul");
  451.           editor.removeList("dl");
  452.         }
  453.       } catch (e) {}
  454.     }
  455.  
  456.     editor.endTransaction();
  457.     
  458.     SaveWindowLocation();
  459.  
  460.     return true;
  461.   }
  462.   return false;
  463. }
  464.