1999-05-07 04:44:46 +04:00
|
|
|
// OnOK(), Undo(), and Cancel() are in EdDialogCommon.js
|
|
|
|
// applyChanges() must be implemented here
|
|
|
|
|
1999-05-06 03:29:18 +04:00
|
|
|
var appCore;
|
|
|
|
var toolkitCore;
|
|
|
|
var anchorElement = null;
|
|
|
|
var insertNew = true;
|
|
|
|
var needLinkText = false;
|
1999-05-07 04:44:46 +04:00
|
|
|
var selection;
|
1999-05-06 03:29:18 +04:00
|
|
|
var undoCount = 0;
|
1999-05-07 04:44:46 +04:00
|
|
|
var makeLinkWithSelection = false;
|
1999-05-06 03:29:18 +04:00
|
|
|
|
|
|
|
// NOTE: Use "HREF" instead of "A" to distinguish from Named Anchor
|
|
|
|
// The returned node is has an "A" tagName
|
|
|
|
var tagName = "HREF";
|
|
|
|
var data;
|
|
|
|
var dialog;
|
|
|
|
|
|
|
|
// dialog initialization code
|
|
|
|
function Startup() {
|
|
|
|
dump("Doing Startup...\n");
|
|
|
|
toolkitCore = XPAppCoresManager.Find("ToolkitCore");
|
|
|
|
if (!toolkitCore) {
|
|
|
|
toolkitCore = new ToolkitCore();
|
|
|
|
if (toolkitCore)
|
|
|
|
toolkitCore.Init("ToolkitCore");
|
|
|
|
}
|
|
|
|
if(!toolkitCore) {
|
|
|
|
dump("toolkitCore not found!!! And we can't close the dialog!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// NEVER create an appcore here - we must find parent editor's
|
|
|
|
appCore = XPAppCoresManager.Find("EditorAppCoreHTML");
|
|
|
|
if(!appCore || !toolkitCore) {
|
|
|
|
dump("EditorAppCore not found!!!\n");
|
|
|
|
toolkitCore.CloseWindow(window);
|
|
|
|
}
|
|
|
|
dump("EditorAppCore found for Link Properties dialog\n");
|
|
|
|
|
|
|
|
// Create dialog object to store controls for easy access
|
|
|
|
dialog = new Object;
|
|
|
|
dialog.linkTextInput = document.getElementById("linkTextInput");
|
|
|
|
dialog.hrefInput = document.getElementById("hrefInput");
|
1999-05-07 04:44:46 +04:00
|
|
|
|
|
|
|
// Kinda clunky: Message was wrapped in a <p>, so actual message is a child text node
|
|
|
|
dialog.linkMessage = (document.getElementById("linkMessage")).firstChild;
|
1999-05-06 03:29:18 +04:00
|
|
|
|
|
|
|
if (null == dialog.linkTextInput ||
|
|
|
|
null == dialog.hrefInput ||
|
1999-05-07 04:44:46 +04:00
|
|
|
null == dialog.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) {
|
|
|
|
dialog.linkTextInput.focus();
|
|
|
|
} else {
|
|
|
|
dialog.hrefInput.focus();
|
|
|
|
|
|
|
|
// We will not insert a new link at caret, so remove link text input field
|
|
|
|
parentNode = dialog.linkTextInput.parentNode;
|
|
|
|
if (parentNode) {
|
|
|
|
dump("Removing link text input field.\n");
|
|
|
|
parentNode.removeChild(dialog.linkTextInput);
|
1999-05-06 03:29:18 +04:00
|
|
|
dialog.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-05-06 03:29:18 +04:00
|
|
|
function initDialog() {
|
|
|
|
// Get a single selected anchor element
|
|
|
|
anchorElement = appCore.getSelectedElement(tagName);
|
|
|
|
|
|
|
|
selection = appCore.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
|
|
|
|
insertNew = false;
|
|
|
|
|
1999-05-07 04:44:46 +04:00
|
|
|
if (!selection.isCollapsed)
|
1999-05-06 03:29:18 +04:00
|
|
|
{
|
1999-05-07 04:44:46 +04:00
|
|
|
// HREF is a weird case: If selection extends beyond
|
|
|
|
// the link, user probably wants to extend link to
|
|
|
|
// entire selection.
|
|
|
|
// TODO: We need to know if selection extends beyond existing
|
|
|
|
// link text before we should do this
|
|
|
|
//makeLinkWithSelection = true;
|
1999-04-21 22:53:55 +04:00
|
|
|
}
|
1999-05-06 03:29:18 +04:00
|
|
|
} else {
|
|
|
|
// We don't have an element selected,
|
|
|
|
// so create one with default attributes
|
|
|
|
dump("Element not selected - calling createElementWithDefaults\n");
|
|
|
|
anchorElement = appCore.createElementWithDefaults(tagName);
|
|
|
|
|
1999-05-07 04:44:46 +04:00
|
|
|
// 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;
|
|
|
|
}
|
1999-05-06 03:29:18 +04:00
|
|
|
if(!anchorElement)
|
|
|
|
{
|
|
|
|
dump("Failed to get selected element or create a new one!\n");
|
|
|
|
toolkitCore.CloseWindow(window);
|
1999-05-07 04:44:46 +04:00
|
|
|
} else if (!insertNew) {
|
|
|
|
dump("Need to get selected text\n");
|
|
|
|
|
|
|
|
// Replace the link message with the link source string
|
|
|
|
// TODO: Get the text of the selection WHAT ABOUT IMAGES?
|
|
|
|
// Maybe have a special method "GetLinkSource" that resolves images as
|
|
|
|
// their URL? E.g.: "Link source [image:http://myimage.gif]"
|
|
|
|
dialog.linkMessage.data = "[Link source text or image URL goes here]";
|
1999-05-06 03:29:18 +04:00
|
|
|
}
|
|
|
|
}
|
1999-04-21 22:53:55 +04:00
|
|
|
|
1999-05-06 03:29:18 +04:00
|
|
|
function applyChanges()
|
|
|
|
{
|
|
|
|
// Coalesce into one undo transaction
|
|
|
|
appCore.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",dialog.hrefInput.value);
|
|
|
|
|
|
|
|
// Get text to use for a new link
|
1999-05-07 04:44:46 +04:00
|
|
|
if (insertNew) {
|
1999-05-06 03:29:18 +04:00
|
|
|
// Append the link text as the last child node
|
|
|
|
// of the anchor node
|
|
|
|
textNode = appCore.editorDocument.createTextNode(dialog.linkTextInput.value);
|
1999-05-07 04:44:46 +04:00
|
|
|
if (textNode) {
|
1999-05-06 03:29:18 +04:00
|
|
|
anchorElement.appendChild(textNode);
|
1999-04-21 22:53:55 +04:00
|
|
|
}
|
1999-05-06 03:29:18 +04:00
|
|
|
newElement = appCore.insertElement(anchorElement, true);
|
1999-05-07 04:44:46 +04:00
|
|
|
if (newElement != anchorElement) {
|
1999-05-06 03:29:18 +04:00
|
|
|
dump("Returned element from insertElement is different from orginal element.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
undoCount = undoCount + 1;
|
|
|
|
appCore.endBatchChanges();
|
|
|
|
|
|
|
|
// Reinitialize dialog data
|
|
|
|
initDialog();
|
|
|
|
}
|