1999-05-06 03:29:18 +04:00
|
|
|
var anchorElement = null;
|
1999-07-03 02:52:34 +04:00
|
|
|
var imageElement = null;
|
1999-05-06 03:29:18 +04:00
|
|
|
var insertNew = true;
|
|
|
|
var needLinkText = false;
|
1999-05-08 02:26:23 +04:00
|
|
|
var insertLinkAroundSelection = false;
|
1999-07-25 00:27:53 +04:00
|
|
|
var linkTextInput;
|
|
|
|
var hrefInput;
|
|
|
|
var linkMessage;
|
1999-05-06 03:29:18 +04:00
|
|
|
|
1999-07-03 02:52:34 +04:00
|
|
|
// NOTE: Use "href" instead of "a" to distinguish from Named Anchor
|
|
|
|
// The returned node is has an "a" tagName
|
|
|
|
var tagName = "href";
|
1999-05-06 03:29:18 +04:00
|
|
|
|
|
|
|
// dialog initialization code
|
1999-06-11 22:58:32 +04:00
|
|
|
function Startup()
|
|
|
|
{
|
1999-07-14 19:24:33 +04:00
|
|
|
if (!InitEditorShell())
|
|
|
|
return;
|
1999-05-06 03:29:18 +04:00
|
|
|
|
1999-07-25 00:27:53 +04:00
|
|
|
linkTextInput = document.getElementById("linkTextInput");
|
|
|
|
hrefInput = document.getElementById("hrefInput");
|
1999-05-07 04:44:46 +04:00
|
|
|
|
1999-07-25 00:27:53 +04:00
|
|
|
// Message was wrapped in a <p>, so actual message is a child text node
|
|
|
|
linkMessage = (document.getElementById("linkMessage")).firstChild;
|
1999-05-06 03:29:18 +04:00
|
|
|
|
1999-07-25 00:27:53 +04:00
|
|
|
if (null == linkTextInput ||
|
|
|
|
null == hrefInput ||
|
|
|
|
null == linkMessage )
|
1999-05-06 03:29:18 +04:00
|
|
|
{
|
|
|
|
dump("Not all dialog controls were found!!!\n");
|
|
|
|
}
|
1999-05-07 04:44:46 +04:00
|
|
|
|
|
|
|
// Set data for the dialog controls
|
|
|
|
initDialog();
|
1999-04-21 22:53:55 +04:00
|
|
|
|
1999-05-07 04:44:46 +04:00
|
|
|
// Set initial focus
|
|
|
|
|
|
|
|
if (insertNew) {
|
1999-06-11 22:58:32 +04:00
|
|
|
dump("Setting focus to linkTextInput\n");
|
1999-07-25 00:27:53 +04:00
|
|
|
linkTextInput.focus();
|
1999-05-07 04:44:46 +04:00
|
|
|
} else {
|
1999-06-11 22:58:32 +04:00
|
|
|
dump("Setting focus to linkTextInput\n");
|
1999-07-25 00:27:53 +04:00
|
|
|
hrefInput.focus();
|
1999-05-07 04:44:46 +04:00
|
|
|
|
|
|
|
// We will not insert a new link at caret, so remove link text input field
|
1999-07-25 00:27:53 +04:00
|
|
|
parentNode = linkTextInput.parentNode;
|
1999-05-07 04:44:46 +04:00
|
|
|
if (parentNode) {
|
|
|
|
dump("Removing link text input field.\n");
|
1999-07-25 00:27:53 +04:00
|
|
|
parentNode.removeChild(linkTextInput);
|
|
|
|
linkTextInput = null;
|
1999-04-21 22:53:55 +04:00
|
|
|
}
|
1999-05-06 03:29:18 +04:00
|
|
|
}
|
|
|
|
}
|
1999-04-21 22:53:55 +04:00
|
|
|
|
1999-06-11 22:58:32 +04:00
|
|
|
function initDialog()
|
|
|
|
{
|
1999-05-06 03:29:18 +04:00
|
|
|
// Get a single selected anchor element
|
1999-06-11 22:58:32 +04:00
|
|
|
anchorElement = editorShell.GetSelectedElement(tagName);
|
1999-05-06 03:29:18 +04:00
|
|
|
|
1999-07-25 05:24:51 +04:00
|
|
|
var selection = editorShell.editorSelection;
|
1999-05-07 04:44:46 +04:00
|
|
|
if (selection) {
|
|
|
|
dump("There is a selection: collapsed = "+selection.isCollapsed+"\n");
|
1999-05-06 03:29:18 +04:00
|
|
|
} else {
|
|
|
|
dump("Failed to get selection\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (anchorElement) {
|
|
|
|
// We found an element and don't need to insert one
|
1999-07-25 05:24:51 +04:00
|
|
|
dump("found anchor element\n");
|
1999-05-06 03:29:18 +04:00
|
|
|
insertNew = false;
|
1999-07-25 05:24:51 +04:00
|
|
|
|
|
|
|
// We get the anchor if any of the selection (or just caret)
|
|
|
|
// is enclosed by the link. Select the entire link
|
|
|
|
// so we can show the selection text
|
|
|
|
editorShell.SelectElement(anchorElement);
|
|
|
|
selection = editorShell.editorSelection;
|
|
|
|
|
|
|
|
hrefInput.value = anchorElement.getAttribute("href");
|
|
|
|
dump("Current HREF: "+hrefInput.value+"\n");
|
1999-05-06 03:29:18 +04:00
|
|
|
} else {
|
1999-07-03 02:52:34 +04:00
|
|
|
// See if we have a selected image instead of text
|
|
|
|
imageElement = editorShell.GetSelectedElement("img");
|
|
|
|
if (imageElement) {
|
|
|
|
// See if the image is a child of a link
|
|
|
|
dump("Image element found - check if its a link...\n");
|
|
|
|
dump("Image Parent="+parent);
|
|
|
|
parent = imageElement.parentNode;
|
|
|
|
dump("Parent="+parent+" nodeName="+parent.nodeName+"\n");
|
|
|
|
if (parent) {
|
|
|
|
anchorElement = parent;
|
|
|
|
insertNew = false;
|
|
|
|
// Link source string is the source URL of image
|
|
|
|
// TODO: THIS STILL DOESN'T HANDLE MULTIPLE SELECTED IMAGES!
|
1999-07-25 00:27:53 +04:00
|
|
|
linkMessage.data = imageElement.getAttribute("src");;
|
1999-07-03 02:52:34 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We don't have an element selected,
|
|
|
|
// so create one with default attributes
|
|
|
|
dump("Element not selected - calling createElementWithDefaults\n");
|
|
|
|
anchorElement = editorShell.CreateElementWithDefaults(tagName);
|
|
|
|
|
|
|
|
// We will insert a new link at caret location if there's no selection
|
|
|
|
// TODO: This isn't entirely correct. If selection doesn't have any text
|
|
|
|
// or an image, then shouldn't we clear the selection and insert new text?
|
|
|
|
insertNew = selection.isCollapsed;
|
|
|
|
dump("insertNew is " + insertNew + "\n");
|
|
|
|
}
|
1999-05-07 04:44:46 +04:00
|
|
|
}
|
1999-05-06 03:29:18 +04:00
|
|
|
if(!anchorElement)
|
|
|
|
{
|
|
|
|
dump("Failed to get selected element or create a new one!\n");
|
1999-05-27 01:40:51 +04:00
|
|
|
window.close();
|
1999-07-03 02:52:34 +04:00
|
|
|
} else if (!insertNew && !imageElement) {
|
1999-05-07 04:44:46 +04:00
|
|
|
|
|
|
|
// Replace the link message with the link source string
|
1999-07-16 22:04:03 +04:00
|
|
|
selectedText = GetSelectionAsText();
|
1999-07-03 02:52:34 +04:00
|
|
|
if (selectedText.length > 0) {
|
|
|
|
// Use just the first 50 characters and add "..."
|
|
|
|
selectedText = TruncateStringAtWordEnd(selectedText, 50, true);
|
|
|
|
} else {
|
|
|
|
dump("Selected text for link source not found. Non-text elements selected?\n");
|
|
|
|
}
|
1999-07-25 00:27:53 +04:00
|
|
|
linkMessage.data = selectedText;
|
1999-05-06 03:29:18 +04:00
|
|
|
}
|
1999-05-08 02:26:23 +04:00
|
|
|
|
|
|
|
if (!selection.isCollapsed)
|
|
|
|
{
|
|
|
|
// HREF is a weird case: If selection extends beyond
|
|
|
|
// the link, user probably wants to extend link to
|
|
|
|
// entire selection.
|
|
|
|
// TODO: If there was already a link,
|
|
|
|
// we need to know if selection extends beyond existing
|
|
|
|
// link text before we should do this
|
|
|
|
insertLinkAroundSelection = true;
|
|
|
|
dump("insertLinkAroundSelection is TRUE\n");
|
|
|
|
}
|
1999-05-06 03:29:18 +04:00
|
|
|
}
|
1999-04-21 22:53:55 +04:00
|
|
|
|
1999-05-27 01:40:51 +04:00
|
|
|
function chooseFile()
|
|
|
|
{
|
|
|
|
// Get a local file, converted into URL format
|
1999-06-11 22:58:32 +04:00
|
|
|
fileName = editorShell.GetLocalFileURL(window, "html");
|
1999-05-27 01:40:51 +04:00
|
|
|
if (fileName != "") {
|
1999-07-25 00:27:53 +04:00
|
|
|
hrefInput.value = fileName;
|
1999-05-27 01:40:51 +04:00
|
|
|
}
|
|
|
|
// Put focus into the input field
|
1999-07-25 00:27:53 +04:00
|
|
|
hrefInput.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
function RemoveLink()
|
|
|
|
{
|
|
|
|
// Simple clear the input field!
|
|
|
|
hrefInput.value = "";
|
1999-05-27 01:40:51 +04:00
|
|
|
}
|
|
|
|
|
1999-06-09 05:27:08 +04:00
|
|
|
function onOK()
|
1999-05-06 03:29:18 +04:00
|
|
|
{
|
1999-06-09 05:27:08 +04:00
|
|
|
// TODO: VALIDATE FIELDS BEFORE COMMITING CHANGES
|
|
|
|
|
1999-07-25 00:27:53 +04:00
|
|
|
href = TrimString(hrefInput.value);
|
1999-07-16 22:04:03 +04:00
|
|
|
if (href.length > 0) {
|
|
|
|
// Coalesce into one undo transaction
|
|
|
|
editorShell.BeginBatchChanges();
|
|
|
|
|
|
|
|
// Set the HREF directly on the editor document's anchor node
|
|
|
|
// or on the newly-created node if insertNew is true
|
|
|
|
anchorElement.setAttribute("href",href);
|
|
|
|
|
|
|
|
// Get text to use for a new link
|
|
|
|
if (insertNew) {
|
|
|
|
// Append the link text as the last child node
|
|
|
|
// of the anchor node
|
|
|
|
dump("Creating text node\n");
|
1999-07-25 00:27:53 +04:00
|
|
|
newText = TrimString(linkTextInput.value);
|
1999-07-23 04:52:17 +04:00
|
|
|
if (newText.length == 0) {
|
|
|
|
ShowInputErrorMessage("You must enter some text for this link.");
|
1999-07-25 00:27:53 +04:00
|
|
|
linkTextInput.focus();
|
1999-07-23 04:52:17 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
textNode = editorShell.editorDocument.createTextNode(newText);
|
1999-07-16 22:04:03 +04:00
|
|
|
if (textNode) {
|
|
|
|
anchorElement.appendChild(textNode);
|
|
|
|
}
|
|
|
|
dump("Inserting\n");
|
|
|
|
editorShell.InsertElement(anchorElement, false);
|
|
|
|
} else if (insertLinkAroundSelection) {
|
1999-07-23 04:52:17 +04:00
|
|
|
// Text was supplied by the selection,
|
|
|
|
// so insert a link node as parent of this text
|
1999-07-16 22:04:03 +04:00
|
|
|
dump("Setting link around selected text\n");
|
|
|
|
editorShell.InsertLinkAroundSelection(anchorElement);
|
1999-04-21 22:53:55 +04:00
|
|
|
}
|
1999-07-16 22:04:03 +04:00
|
|
|
editorShell.EndBatchChanges();
|
|
|
|
} else if (!insertNew) {
|
|
|
|
// We already had a link, but empty HREF means remove it
|
1999-07-23 04:52:17 +04:00
|
|
|
editorShell.RemoveTextProperty("a", "");
|
1999-05-06 03:29:18 +04:00
|
|
|
}
|
1999-07-23 04:52:17 +04:00
|
|
|
// Note: if HREF is empty and we were inserting a new link, do nothing
|
|
|
|
|
1999-06-09 05:27:08 +04:00
|
|
|
window.close();
|
1999-05-06 03:29:18 +04:00
|
|
|
}
|
1999-06-09 05:27:08 +04:00
|
|
|
|