Added error-checking JS method and used it for named anchor properties dialog

This commit is contained in:
cmanske%netscape.com 1999-07-16 20:34:16 +00:00
parent fe0aea2102
commit a69f0ba9f3
2 changed files with 40 additions and 16 deletions

View File

@ -81,7 +81,9 @@ function AppendStringToList(list, string)
// "value" may be a number or string type
function ValidateNumberString(value, minValue, maxValue)
{
// Get the number version
// Get the number version (strip out non-numbers)
var pat = /\D/g;
value = value.replace(pat, "");
number = value - 0;
if ((value+"") != "") {
if (number && number >= minValue && number <= maxValue ){
@ -90,20 +92,18 @@ function ValidateNumberString(value, minValue, maxValue)
}
}
message = "The number you entered ("+number+") is outside of allowed range.\nPlease enter a number between "+minValue+" and "+maxValue;
// Initialize where we place result from generic EdMessage dialog
window.msgResult = 0;
// This is NOT MODAL!
ShowInputErrorMessage(message);
window.openDialog("chrome://editordlgs/content/EdMessage.xul", "MsgDlg", "chrome", "", message, "Input Error", "OK");
// We could do something like this if we could call
// a method that pumps the message system
//while (window.msgResult == 0);
dump("Message button pressed: "+window.msgResult+"\n");
// Return an empty string to indicate error
return "";
}
function ShowInputErrorMessage(message)
{
// This is NOT MODAL as of 7/16/99!
window.openDialog("chrome://editordlgs/content/EdMessage.xul", "MsgDlg", "chrome", "", message, "Input Error", "OK");
}
function TrimStringLeft(string)
{
@ -335,4 +335,4 @@ function onCancel()
function GetSelectionAsText()
{
return editorShell.GetContentsAs("text/plain", SelectionOnly);
}
}

View File

@ -1,5 +1,5 @@
var insertNew = true;
var tagName = "anchor"
var tagName = "anchor";
var anchorElement = null;
var nameInput;
@ -9,9 +9,9 @@ function Startup()
if (!InitEditorShell())
return;
dump("EditorShell found for NamedAnchor Properties dialog\n");
dump(document+"\n");
nameInput = document.getElementById("nameInput");
dump(nameInput+"\n");
dump("tagName = "+tagName+"\n");
// Get a single selected element of the desired type
anchorElement = editorShell.GetSelectedElement(tagName);
@ -33,7 +33,11 @@ function Startup()
name = TruncateStringAtWordEnd(name, 40, false);
// Replace whitespace with "_"
name = ReplaceWhitespace(name, "_");
dump("Selection text for name: "+name+"\n");
//Be sure the name is unique to the document
if (AnchorNameExists(name))
name += "_"
nameInput.value = name;
}
@ -46,16 +50,36 @@ function Startup()
nameInput.focus();
}
function AnchorNameExists(name)
{
anchorList = editorShell.editorDocument.anchors; // getElementsByTagName("A");
if (anchorList) {
dump("We have an anchor list\n");
for (i=0; i < anchorList.length; i++) {
dump("Anchor name: "+anchorList[i].name+"\n");
if (anchorList[i].name == name)
return true;
}
}
return false;
}
function onOK()
{
name = nameInput.value;
name = TrimString(name);
if (name.length == 0) {
dump("EMPTY ANCHOR STRING\n");
//TODO: POPUP ERROR DIALOG HERE
ShowInputErrorMessage("You must enter a name for this anchor.");
nameInput.focus();
return;
} else {
// Replace spaces with "_" else it causes trouble in URL parsing
name = ReplaceWhitespace(name, "_");
if (AnchorNameExists(name)) {
ShowInputErrorMessage("\""+name+"\" already exists in this page.\nPlease enter a different name.");
nameInput.focus();
return;
}
anchorElement.setAttribute("name",name);
if (insertNew) {
// Don't delete selected text when inserting