gecko-dev/editor/ui/dialogs/content/EdHLineProps.js

210 lines
5.8 KiB
JavaScript
Raw Normal View History

1999-08-19 14:28:44 +00:00
/*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998-1999 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
var tagName = "hr";
var hLineElement;
var width;
var height;
var align;
var shading;
// dialog initialization code
function Startup()
{
dump("HLine Properties startup...\n");
if (!InitEditorShell())
return;
1999-08-25 14:36:13 +00:00
doSetOKCancel(onOK, null);
// Get the selected horizontal line
hLineElement = editorShell.GetSelectedElement(tagName);
if (!hLineElement) {
// We should never be here if not editing an existing HLine
dump("HLine is not selected! Shouldn't be here!\n");
window.close();
return;
}
// Create dialog object to store controls for easy access
dialog = new Object;
dialog.heightInput = document.getElementById("height");
dialog.widthInput = document.getElementById("width");
dialog.leftAlign = document.getElementById("leftAlign");
dialog.centerAlign = document.getElementById("centerAlign");
dialog.rightAlign = document.getElementById("rightAlign");
dialog.shading = document.getElementById("3dShading");
dialog.pixelOrPercentSelect = document.getElementById("pixelOrPercentSelect");
// Make a copy to use for AdvancedEdit and onSaveDefault
globalElement = hLineElement.cloneNode(false);
// Initialize control values based on existing attributes
InitDialog()
// SET FOCUS TO FIRST CONTROL
dialog.heightInput.focus();
// Resize window
window.sizeToContent();
}
// Set dialog widgets with attribute data
// We get them from globalElement copy so this can be used
// by AdvancedEdit(), which is shared by all property dialogs
function InitDialog()
{
// Just to be confusing, "size" is used instead of height
var height = globalElement.getAttribute("size");
if(!height) {
dump("NO SIZE FOUND FOR HLINE");
height = 2; //Default value
}
// We will use "height" here and in UI
dialog.heightInput.value = height;
1999-06-15 04:03:22 +00:00
// Get the width attribute of the element, stripping out "%"
// This sets contents of combobox (adds pixel and percent option elements)
dialog.widthInput.value = InitPixelOrPercentCombobox(globalElement,"width","pixelOrPercentSelect");
align = globalElement.getAttribute("align");
if (align == "center") {
dialog.centerAlign.checked = true;
} else if (align == "right") {
dialog.rightAlign.checked = true;
} else {
dialog.leftAlign.checked = true;
}
noshade = globalElement.getAttribute("noshade");
dialog.shading.checked = (noshade == "");
}
function onSaveDefault()
{
// "false" means set attributes on the globalElement,
// not the real element being edited
if (ValidateData()) {
var prefs = GetPrefs();
if (prefs) {
dump("Setting HLine prefs\n");
var alignInt;
if (align == "left") {
alignInt = 0;
} else if (align == "right") {
alignInt = 2;
} else {
alignInt = 1;
}
prefs.SetIntPref("editor.hrule.align", alignInt);
var percentIndex = width.search(/%/);
var percent;
var widthInt;
if (percentIndex > 0) {
percent = true;
widthInt = Number(width.substr(0, percentIndex));
} else {
percent = false;
widthInt = Number(width);
}
prefs.SetIntPref("editor.hrule.width", widthInt);
prefs.SetBoolPref("editor.hrule.width_percent", percent);
// Convert string to number
prefs.SetIntPref("editor.hrule.height", Number(height));
prefs.SetBoolPref("editor.hrule.shading", shading);
// Write the prefs out NOW!
prefs.SavePrefFile();
}
}
}
// Get and validate data from widgets.
// Set attributes on globalElement so they can be accessed by AdvancedEdit()
function ValidateData()
{
// Height is always pixels
height = ValidateNumberString(dialog.heightInput.value, 1, maxPixels);
if (height == "") {
// Set focus to the offending control
dump("Height is empty\n");
dialog.heightInput.focus();
return false;
}
1999-06-15 04:03:22 +00:00
dump("Setting height="+height+"\n");
globalElement.setAttribute("size", height);
var isPercent = (dialog.pixelOrPercentSelect.selectedIndex == 1);
var maxLimit;
if (isPercent) {
maxLimit = 100;
} else {
// Upper limit when using pixels
maxLimit = maxPixels;
}
width = ValidateNumberString(dialog.widthInput.value, 1, maxLimit);
if (width == "") {
dump("Width is empty\n");
dialog.widthInput.focus();
return false;
}
if (isPercent)
width = width + "%";
dump("Height="+height+" Width="+width+"\n");
globalElement.setAttribute("width", width);
align = "left";
if (dialog.centerAlign.checked) {
align = "center";
} else if (dialog.rightAlign.checked) {
align = "right";
}
globalElement.setAttribute("align", align);
if (dialog.shading.checked) {
shading = true;
globalElement.removeAttribute("noshade");
} else {
shading = false;
globalElement.setAttribute("noshade", "");
}
return true;
}
1999-06-15 04:03:22 +00:00
function onOK()
{
if (ValidateData())
{
// Copy attributes from the globalElement to the document element
editorShell.CloneAttributes(hLineElement, globalElement);
return true;
}
return false;
}