From 6fa7b160faff53c0e83835e088c4ec6ea58b809f Mon Sep 17 00:00:00 2001 From: "brade%netscape.com" Date: Thu, 19 Aug 1999 14:25:32 +0000 Subject: [PATCH] reformatting by pete@postpagan.com, pixel/percent handling by Brian King --- editor/ui/dialogs/content/EdDialogCommon.js | 247 +++++++++++++++----- 1 file changed, 185 insertions(+), 62 deletions(-) diff --git a/editor/ui/dialogs/content/EdDialogCommon.js b/editor/ui/dialogs/content/EdDialogCommon.js index 0411b052d198..e07ca5cce7a4 100644 --- a/editor/ui/dialogs/content/EdDialogCommon.js +++ b/editor/ui/dialogs/content/EdDialogCommon.js @@ -1,25 +1,52 @@ +/* + * 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): + * Pete Collins + * Brian King + */ + // Each editor window must include this file // Variables shared by all dialogs: var editorShell; -var SelectionOnly=1; -var FormatedWithDoctype=2; -var FormatedWithoutDoctype=6; -var maxPixels = 10000; +var SelectionOnly = 1; +var FormatedWithDoctype = 2; +var FormatedWithoutDoctype = 6; +var maxPixels = 10000; function InitEditorShell() { // get the editor shell from the parent window - editorShell = window.opener.editorShell; + + editorShell = window.opener.editorShell; if (editorShell) { - editorShell = editorShell.QueryInterface(Components.interfaces.nsIEditorShell); + editorShell = editorShell.QueryInterface(Components.interfaces.nsIEditorShell); } if (!editorShell) { dump("EditorShell not found!!!\n"); window.close(); return false; } + // Save as a property of the window so it can be used by child dialogs - window.editorShell = editorShell; + + window.editorShell = editorShell; return true; } @@ -47,26 +74,26 @@ function ReplaceStringInList(list, index, string) if (index < list.options.length) { /* - node = list.options[index]; + node = list.options[index]; dump("BEFORE Option node text: "+node.text+" Value: "+node.value+"\n"); - node.text = string; - node.value = string; + node.text = string; + node.value = string; dump("AFTER Option node text: "+node.text+" Value: "+node.value+"\n"); */ // Save and remove selection else we have trouble below! // (This must be a bug!) - selIndex = list.selectedIndex; - list.selectedIndex = -1; + selIndex = list.selectedIndex; + list.selectedIndex = -1; - optionNode = new Option(string, string); + optionNode = new Option(string, string); // Remove existing option node //list.remove(index); - list.options[index] = null; + list.options[index] = null; // Insert the new node - list.options[index] = optionNode; + list.options[index] = optionNode; // NOTE: If we insert, then remove, we crash! // Reset the selected item - list.selectedIndex = selIndex; + list.selectedIndex = selIndex; } } @@ -74,12 +101,13 @@ function AppendStringToList(list, string) { // THIS DOESN'T WORK! Result is a XULElement -- namespace problem - //optionNode1 = document.createElement("option"); + //optionNode1 = document.createElement("option"); // "Unsanctioned method from Vidur: // createElementWithNamespace("http://... [the HTML4 URL], "option); // This works - Thanks to Vidur! Params = name, value - optionNode = new Option(string, string); + + optionNode = new Option(string, string); if (optionNode) { list.add(optionNode, null); } else { @@ -88,15 +116,19 @@ function AppendStringToList(list, string) } // "value" may be a number or string type + function ValidateNumberString(value, minValue, maxValue) { // Get the number version (strip out non-numbers) - var pat = /\D/g; - value = value.replace(pat, ""); - number = value - 0; + + var pat = /\D/g; + value = value.replace(pat, ""); + number = value - 0; if ((value+"") != "") { if (number && number >= minValue && number <= maxValue ){ + // Return string version of the number + return number + ""; } } @@ -104,12 +136,14 @@ function ValidateNumberString(value, minValue, maxValue) ShowInputErrorMessage(message); // Return an empty string to indicate error + return ""; } function ShowInputErrorMessage(message) { // This is NOT MODAL as of 7/16/99! + window.openDialog("chrome://editor/content/EdMessage.xul", "MsgDlg", "chrome", "", message, "Input Error"); } @@ -118,7 +152,7 @@ function TrimStringLeft(string) if(!StringExists(string)) return ""; if( IsWhitespace(string.charAt(0))) - string = string.replace(/\s+/, ""); + string = string.replace(/\s+/, ""); return string; } @@ -126,23 +160,26 @@ function TrimStringRight(string) { if(!StringExists(string)) return ""; - var lastCharIndex = string.length-1; + var lastCharIndex = string.length-1; var result; - var done = false; + var done = false; while (!done && lastCharIndex >= 0) { + // Find the last non-whitespace char + if (!IsWhitespace(string.charAt(lastCharIndex))) break; lastCharIndex--; } if (lastCharIndex < 0) { - string = ""; + string = ""; } else { - string = string.slice(0, lastCharIndex+1); + string = string.slice(0, lastCharIndex+1); } return string; } // Remove whitespace from both ends of a string + function TrimString(string) { return TrimStringRight(TrimStringLeft(string)); @@ -150,7 +187,7 @@ function TrimString(string) function IsWhitespace(character) { - var result = character.match(/\s/); + var result = character.match(/\s/); if (result == null) return false; return true; @@ -159,49 +196,56 @@ function IsWhitespace(character) function TruncateStringAtWordEnd(string, maxLength, addEllipses) { // We assume they probably don't want whitespace at the beginning - var string = TrimStringLeft(string); - var len = string.length; + var string = TrimStringLeft(string); + + var len = string.length; if (len > maxLength) { + // We need to truncate the string + var max; if (addEllipses) { // Make room for ellipses - max = maxLength - 3; + max = maxLength - 3; } else { - max = maxLength; + max = maxLength; } var lastCharIndex = 0; // Start search just past max if there's enough characters + if (len >= (max+1)) { - lastCharIndex = max; + lastCharIndex = max; } else { - lastCharIndex = len-1; + lastCharIndex = len-1; } dump("Len="+len+" lastCharIndex="+lastCharIndex+" max="+max+"\n"); // Find the last whitespace char from the end + dump("Skip to first whitspace from end: "); while (lastCharIndex > 0) { - var lastChar = string.charAt(lastCharIndex); + var lastChar = string.charAt(lastCharIndex); dump(lastChar); if (IsWhitespace(lastChar)) break; - lastCharIndex = lastCharIndex -1; + lastCharIndex = lastCharIndex -1; } dump("[space found]\nlastCharIndex="+lastCharIndex+"\nSkip over whitespace:"); while (lastCharIndex > 0) { + // Find the last non-whitespace char - lastChar = string.charAt(lastCharIndex); + + lastChar = string.charAt(lastCharIndex); dump(lastChar); if (!IsWhitespace(lastChar)) break; - lastCharIndex = lastCharIndex -1; + lastCharIndex = lastCharIndex -1; } dump("[non-space found]\nlastCharIndex="+lastCharIndex+"\n"); - string = string.slice(0, lastCharIndex+1); + string = string.slice(0, lastCharIndex+1); if (addEllipses) { string = string+"..."; dump(string+"\n"); @@ -216,9 +260,9 @@ function TruncateStringAtWordEnd(string, maxLength, addEllipses) function ReplaceWhitespace(string, charReplace) { if (string.length > 0 ) { - string = TrimString(string); + string = TrimString(string); // This replaces a run of whitespace with just one character - string = string.replace(/\s+/g, charReplace); + string = string.replace(/\s+/g, charReplace); } dump(string+"\n"); return string; @@ -229,7 +273,7 @@ function ReplaceWhitespace(string, charReplace) { // or disabled (setAttribute) as specified in the "doEnable" parameter function SetElementEnabledByID( elementID, doEnable ) { - element = document.getElementById(elementID); + element = document.getElementById(elementID); if ( element ) { if ( doEnable ) @@ -243,44 +287,50 @@ function SetElementEnabledByID( elementID, doEnable ) } } -// this function takes an ID for a label and a flag +// This function relies on css classes for enabling and disabling +// This function takes an ID for a label and a flag // if an element can be found by its ID, then it is either enabled or disabled // The class is set to either "enabled" or "disabled" depending on the flag passed in. // This function relies on css having a special appearance for these two classes. -function SetLabelEnabledByID( labelID, doEnable ) + +function SetClassEnabledByID( elementID, doEnable ) { - label = document.getElementById(labelID); - if ( label ) + element = document.getElementById(elementID); + if ( element ) { if ( doEnable ) { - label.setAttribute( "class", "enabled" ); + element.setAttribute( "class", "enabled" ); } else { - label.setAttribute( "class", "disabled" ); + element.setAttribute( "class", "disabled" ); } } else { - dump( "not changing label"+labelID+"\n" ); + dump( "not changing element "+elementlID+"\n" ); } } // Next two methods assume caller has a "percentChar" variable // to hold an empty string (no % used) or "%" (percent is used) + function InitPixelOrPercentPopupButton(element, attribute, buttonID) { - size = element.getAttribute(attribute); - btn = document.getElementById(buttonID); + size = element.getAttribute(attribute); + btn = document.getElementById(buttonID); // Search for a "%" character - percentIndex = size.search(/%/); + + percentIndex = size.search(/%/); if (percentIndex > 0) { - percentChar = "%"; + percentChar = "%"; // Strip out the % - size = size.substr(0, percentIndex); + size = size.substr(0, percentIndex); + // TODO: USE ENTITIES FOR TEXT VALUES + if (btn) btn.setAttribute("value","percent"); } else { @@ -291,17 +341,27 @@ function InitPixelOrPercentPopupButton(element, attribute, buttonID) } // Input string is "" for pixel, or "%" for percent + function SetPixelOrPercentByID(elementID, percentString) { - percentChar = percentString; + percentChar = percentString; dump("SetPixelOrPercent. PercentChar="+percentChar+"\n"); - btn = document.getElementById( elementID ); + btn = document.getElementById( elementID ); if ( btn ) { if ( percentChar == "%" ) { - btn.setAttribute( "value", "percent" ); + var containing = getContainer(); + if (containing != null) + { + dump("Containing Element = " + containing + "\n"); + if (containing.nodeName == "TD") + btn.setAttribute( "value", "% of cell" ); + else + btn.setAttribute( "value", "% of window" ); + } + // need error handling } else { @@ -313,27 +373,31 @@ function SetPixelOrPercentByID(elementID, percentString) // USE onkeyup! // forceInteger by petejc (pete@postpagan.com) -var sysBeep = Components.classes["component://netscape/sound"].createInstance(); -sysBeep = sysBeep.QueryInterface(Components.interfaces.nsISound); +var sysBeep = Components.classes["component://netscape/sound"].createInstance(); +sysBeep = sysBeep.QueryInterface(Components.interfaces.nsISound); + function forceInteger(elementID) { - editfield = document.getElementById( elementID ); + editfield = document.getElementById( elementID ); if ( !editfield ) return; - var stringIn = editfield.value; + var stringIn = editfield.value; var pat = /\D/g; - result = stringIn.match(pat, ""); + result = stringIn.match(pat, ""); if (result) { - editfield.value = stringIn.replace(pat,""); + editfield.value = stringIn.replace(pat,""); + // hopefully we can remove the following line for blur() once xp widgets land + editfield.blur(); sysBeep.Beep(); } } // All dialogs share this simple method + function onCancel() { dump("Cancel button clicked: closing window\n"); @@ -344,3 +408,62 @@ function GetSelectionAsText() { return editorShell.GetContentsAs("text/plain", SelectionOnly); } + + +// This is here so to stop the annoying JS error that keeps poping up +// from the advanced edit button which points to a non existant onAdvanced() function +// Well now it exists -pete + +function onAdvanced(){ + + +dump("\n\ncomming soon . . .\nthe \"onAdvanced\" function\n\n"); + + + +} + + +// ** getSelection () +// ** This function checks for existence of table around the focus node +// ** Brian King - XML Workshop + +function getContainer () +{ + tagName = "img"; + selImage = editorShell.GetSelectedElement(tagName); + if (selImage) // existing image + { + dump("Is an image element\n"); + oneup = selImage.parentNode; + dump("Parent = " + oneup.nodeName + "\n"); + //twoup = oneup.parentNode; + //dump("Grand Parent is " + twoup.nodeName + "\n"); + return oneup; + } + else if (!selImage) // new image insertion + { + dump("Not an image element\n"); + dump("Trying for caret selection\n"); + var selection = window.editorShell.editorSelection; + if (selection) + { + dump("Got selection\n"); + //dump("Selection is " + selection.nodeName + "\n"); + var focusN = selection.focusNode; + dump("FOCUS is now on " + focusN.nodeName + "\n"); + if (focusN.nodeName == "TD") + return focusN + else + { + oneup = focusN.parentNode; + dump("PARENT is " + oneup.nodeName + "\n"); + return oneup; + } + } + else + return null; + } + else + return null; +} \ No newline at end of file