home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / chrome / comm.jar / content / editor / pref-composer.js < prev    next >
Encoding:
JavaScript  |  2001-04-11  |  10.0 KB  |  302 lines

  1. // This is mostly a modified version of code in EdColorProps.xul
  2. // TODO: Factor out common code to reduce code size
  3. //       and make a new utility.js to eliminate duplication
  4. //       in editor.js and EdDialogCommon.js
  5.  
  6. // Initialize in case we can't get them from prefs???
  7. var defaultTextColor="#000000";
  8. var defaultLinkColor="#000099";
  9. var defaultActiveColor="#000099";
  10. var defaultVisitedColor="#990099";
  11. var defaultBackgroundColor="#FFFFFF";
  12.  
  13. var customTextColor;
  14. var customLinkColor;
  15. var customActiveColor;
  16. var customVisitedColor;
  17. var customBackgroundColor;
  18. var previewBGColor;
  19. var backgroundImage = "";
  20.  
  21. // Strings we use often
  22. var styleStr =       "style";
  23. var textStr =        "text";
  24. var linkStr =        "link";
  25. var vlinkStr =       "vlink";
  26. var alinkStr =       "alink";
  27. var bgcolorStr =     "bgcolor";
  28. var backgroundStr =  "background";
  29. var colorStyle =     "color: ";
  30. var backColorStyle = "background-color: ";
  31. var backImageStyle = "; background-image: url(";
  32.  
  33. var browserColors;
  34. var dialog;
  35.  
  36. function Startup()
  37. {
  38.   dialog = new Object;
  39.   if (!dialog)
  40.     return false;
  41.  
  42.   dialog.ColorPreview = document.getElementById("ColorPreview");
  43.   dialog.NormalText = document.getElementById("NormalText");
  44.   dialog.LinkText = document.getElementById("LinkText");
  45.   dialog.ActiveLinkText = document.getElementById("ActiveLinkText");
  46.   dialog.VisitedLinkText = document.getElementById("VisitedLinkText");
  47.   dialog.DefaultColorsRadio = document.getElementById("DefaultColorsRadio");
  48.   dialog.CustomColorsRadio = document.getElementById("CustomColorsRadio");
  49.   dialog.BackgroundImageInput = document.getElementById("BackgroundImageInput");
  50.  
  51.   // The data elements that hold the pref values
  52.   dialog.NormalData = document.getElementById("textData");
  53.   dialog.LinkData = document.getElementById("linkData");
  54.   dialog.ActiveLinkData = document.getElementById("aLinkData");
  55.   dialog.VisitedLinkData = document.getElementById("fLinkData");
  56.   dialog.BackgroundColorData = document.getElementById("backgroundColorData");
  57.   dialog.BackgroundImageData = document.getElementById("backgroundImageData");
  58.  
  59.   browserColors = GetDefaultBrowserColors();
  60.  
  61.   // Use author's browser pref colors passed into dialog
  62.   defaultTextColor = browserColors.TextColor;
  63.   defaultLinkColor = browserColors.LinkColor;
  64.   // Note: Browser doesn't store a value for ActiveLinkColor
  65.   defaultActiveColor = defaultLinkColor;
  66.   defaultVisitedColor =  browserColors.VisitedLinkColor;
  67.   defaultBackgroundColor=  browserColors.BackgroundColor;
  68.  
  69.   // Get the colors and image set by prefs init code 
  70.   customTextColor = dialog.NormalData.getAttribute("value"); 
  71.   customLinkColor = dialog.LinkData.getAttribute("value");
  72.   customActiveColor = dialog.ActiveLinkData.getAttribute("value");
  73.   customVisitedColor = dialog.VisitedLinkData.getAttribute("value");
  74.   customBackgroundColor = dialog.BackgroundColorData.getAttribute("value");
  75.   backgroundImage = dialog.BackgroundImageData.getAttribute("value");
  76.   if (backgroundImage)
  77.     dialog.BackgroundImageInput.value = backgroundImage;
  78.  
  79.   // "value" attribute value is a string conversion of boolean!
  80.   if( document.getElementById( "useCustomColors" ).value == "true" )
  81.     UseCustomColors();
  82.   else
  83.     UseDefaultColors();
  84.  
  85.   return true;
  86. }                   
  87.  
  88. function GetColorAndUpdate(ColorWellID)
  89. {
  90.   // Only allow selecting when in custom mode
  91.   if (!dialog.CustomColorsRadio.checked) return;
  92.  
  93.   var colorObj = new Object;
  94.   var colorWell = document.getElementById(ColorWellID);
  95.   if (!colorWell) return;
  96.  
  97.   // Don't allow a blank color, i.e., using the "default"
  98.   colorObj.NoDefault = true;
  99.  
  100.   switch( ColorWellID )
  101.   {
  102.     case "textCW":
  103.       colorObj.Type = "Text";
  104.       colorObj.TextColor = customTextColor;
  105.       break;
  106.     case "linkCW":
  107.       colorObj.Type = "Link";
  108.       colorObj.TextColor = customLinkColor;
  109.       break;
  110.     case "activeCW":
  111.       colorObj.Type = "ActiveLink";
  112.       colorObj.TextColor = customActiveColor;
  113.       break;
  114.     case "visitedCW":
  115.       colorObj.Type = "VisitedLink";
  116.       colorObj.TextColor = customVisitedColor;
  117.       break;
  118.     case "backgroundCW":
  119.       colorObj.Type = "Page";
  120.       colorObj.PageColor = customBackgroundColor;
  121.       break;
  122.   }
  123.  
  124.   window.openDialog("chrome://editor/content/EdColorPicker.xul", "_blank", "chrome,close,titlebar,modal", "", colorObj);
  125.  
  126.   // User canceled the dialog
  127.   if (colorObj.Cancel)
  128.     return;
  129.  
  130.   var color = "";
  131.   switch( ColorWellID )
  132.   {
  133.     case "textCW":
  134.       color = customTextColor = colorObj.TextColor;
  135.       dialog.NormalData.setAttribute("value", color); 
  136.       break;
  137.     case "linkCW":
  138.       color = customLinkColor = colorObj.TextColor;
  139.       dialog.LinkData.setAttribute("value", color);
  140.       break;
  141.     case "activeCW":
  142.       color = customActiveColor = colorObj.TextColor;
  143.       dialog.ActiveLinkData.setAttribute("value", color);
  144.       break;
  145.     case "visitedCW":
  146.       color = customVisitedColor = colorObj.TextColor;
  147.       dialog.VisitedLinkData.setAttribute("value", color);
  148.       break;
  149.     case "backgroundCW":
  150.       color = customBackgroundColor = colorObj.BackgroundColor;
  151.       dialog.BackgroundColorData.setAttribute("value", color);
  152.       break;
  153.   }
  154.   setColorWell(ColorWellID, color); 
  155.   SetColorPreview(ColorWellID, color);
  156. }
  157.  
  158. function SetColorPreview(ColorWellID, color)
  159. {
  160.   switch( ColorWellID )
  161.   {
  162.     case "textCW":
  163.       dialog.NormalText.setAttribute(styleStr,colorStyle+color);
  164.       break;
  165.     case "linkCW":
  166.       dialog.LinkText.setAttribute(styleStr,colorStyle+color);
  167.       break;
  168.     case "activeCW":
  169.       dialog.ActiveLinkText.setAttribute(styleStr,colorStyle+color);
  170.       break;
  171.     case "visitedCW":
  172.       dialog.VisitedLinkText.setAttribute(styleStr,colorStyle+color);
  173.       break;
  174.     case "backgroundCW":
  175.       // Must combine background color and image style values
  176.       var styleValue = backColorStyle+color;
  177.       if (backgroundImage)
  178.         styleValue += ";"+backImageStyle+backgroundImage+");";
  179.  
  180.       dialog.ColorPreview.setAttribute(styleStr,styleValue);
  181.       previewBGColor = color;
  182.       break;
  183.   }
  184. }
  185.  
  186. function UseCustomColors()
  187. {
  188.   SetElementEnabledById("TextButton", true);
  189.   SetElementEnabledById("LinkButton", true);
  190.   SetElementEnabledById("ActiveLinkButton", true);
  191.   SetElementEnabledById("VisitedLinkButton", true);
  192.   SetElementEnabledById("BackgroundButton", true);
  193.   SetElementEnabledById("Text", true);
  194.   SetElementEnabledById("Link", true);
  195.   SetElementEnabledById("Active", true);
  196.   SetElementEnabledById("Visited", true);
  197.   SetElementEnabledById("Background", true);
  198.  
  199.   SetColorPreview("textCW",       customTextColor);
  200.   SetColorPreview("linkCW",       customLinkColor);
  201.   SetColorPreview("activeCW",     customActiveColor);
  202.   SetColorPreview("visitedCW",    customVisitedColor);
  203.   SetColorPreview("backgroundCW", customBackgroundColor);
  204.  
  205.   setColorWell("textCW",          customTextColor);
  206.   setColorWell("linkCW",          customLinkColor);
  207.   setColorWell("activeCW",        customActiveColor);
  208.   setColorWell("visitedCW",       customVisitedColor);
  209.   setColorWell("backgroundCW",    customBackgroundColor);
  210.  
  211.   dialog.NormalData.setAttribute("value",          customTextColor); 
  212.   dialog.LinkData.setAttribute("value",            customLinkColor);
  213.   dialog.ActiveLinkData.setAttribute("value",      customActiveColor);
  214.   dialog.VisitedLinkData.setAttribute("value",     customVisitedColor);
  215.   dialog.BackgroundColorData.setAttribute("value", customBackgroundColor);
  216. }
  217.  
  218. function UseDefaultColors()
  219. {
  220.   SetColorPreview("textCW",       defaultTextColor);
  221.   SetColorPreview("linkCW",       defaultLinkColor);
  222.   SetColorPreview("activeCW",     defaultActiveColor);
  223.   SetColorPreview("visitedCW",    defaultVisitedColor);
  224.   SetColorPreview("backgroundCW", defaultBackgroundColor);
  225.  
  226.   // Setting to blank color will remove color from buttons,
  227.   setColorWell("textCW",       "");
  228.   setColorWell("linkCW",       "");
  229.   setColorWell("activeCW",     "");
  230.   setColorWell("visitedCW",    "");
  231.   setColorWell("backgroundCW", "");
  232.  
  233.   // Disable color buttons and labels
  234.   SetElementEnabledById("TextButton", false);
  235.   SetElementEnabledById("LinkButton", false);
  236.   SetElementEnabledById("ActiveLinkButton", false);
  237.   SetElementEnabledById("VisitedLinkButton", false);
  238.   SetElementEnabledById("BackgroundButton", false);
  239.   SetElementEnabledById("Text", false);
  240.   SetElementEnabledById("Link", false);
  241.   SetElementEnabledById("Active", false);
  242.   SetElementEnabledById("Visited", false);
  243.   SetElementEnabledById("Background", false);
  244.   
  245.   // Note that we leave custom colors set even if 
  246.   //  custom colors pref is false (we just ignore the colors)
  247. }
  248.   
  249. function ChooseImageFile()
  250. {
  251.   // Get a local image file, converted into URL format
  252.   var fileName = GetLocalFileURL("img");
  253.   if (fileName)
  254.   {
  255.     dialog.BackgroundImageInput.value = fileName;
  256.     ValidateAndPreviewImage(true);
  257.   }
  258.   SetTextboxFocus(dialog.BackgroundImageInput);
  259. }
  260.  
  261. function ChangeBackgroundImage()
  262. {
  263.   // Don't show error message for image while user is typing
  264.   ValidateAndPreviewImage(false);
  265. }
  266.  
  267. function ValidateAndPreviewImage(ShowErrorMessage)
  268. {
  269.   // First make a string with just background color
  270.   var styleValue = backColorStyle+previewBGColor+";";
  271.  
  272.   var image = dialog.BackgroundImageInput.value.trimString();
  273.   if (image)
  274.   {
  275.     if (IsValidImage(image))
  276.     {
  277.       backgroundImage = image;
  278.       // Append image style
  279.       styleValue += backImageStyle+backgroundImage+");";
  280.     }
  281.     else
  282.     {
  283.       backgroundImage = "";
  284.       if (ShowErrorMessage)
  285.       {
  286.         SetTextboxFocus(dialog.BackgroundImageInput);
  287.         // Tell user about bad image
  288.         ShowInputErrorMessage(GetString("MissingImageError"));
  289.       }
  290.     }
  291.   }
  292.   else
  293.     backgroundImage = "";
  294.  
  295.   // Set style on preview (removes image if not valid)
  296.   dialog.ColorPreview.setAttribute(styleStr, styleValue);
  297.   
  298.   // Set the pref data so pref code saves it 
  299.   dialog.BackgroundImageData.setAttribute("value", backgroundImage ? backgroundImage : "");
  300. }
  301.  
  302.