Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refs #312. Refactored the anchor dialog handler to be testable

  • Loading branch information...
commit dab23fb3fa5e1caf1608f3022e33980a70bb161a 1 parent fd3c141
@winhamwr winhamwr authored
Showing with 93 additions and 23 deletions.
  1. +46 −23 src/wymeditor/core.js
  2. +47 −0 src/wymeditor/editor/base.js
View
69 src/wymeditor/core.js
@@ -804,7 +804,8 @@ WYMeditor.INIT_DIALOG = function (index) {
sStamp = wym.uniqueStamp(),
styles,
aCss,
- tableOnClick;
+ tableOnClick,
+ insertAnchorOnClick;
if (dialogType === WYMeditor.DIALOG_LINK) {
// ensure that we select the link to populate the fields
@@ -846,27 +847,6 @@ WYMeditor.INIT_DIALOG = function (index) {
jQuery(wym._options.dialogImageSelector + " " + wym._options.altSelector).val(jQuery(wym._selected_image).attr(WYMeditor.ALT));
}
- jQuery(wym._options.dialogLinkSelector + " " +
- wym._options.submitSelector).submit(function () {
-
- var sUrl = jQuery(wym._options.hrefSelector).val(),
- link;
- if (sUrl.length > 0) {
-
- if (selected[0] && selected[0].tagName.toLowerCase() === WYMeditor.A) {
- link = selected;
- } else {
- wym._exec(WYMeditor.CREATE_LINK, sStamp);
- link = jQuery("a[href=" + sStamp + "]", wym._doc.body);
- }
-
- link.attr(WYMeditor.HREF, sUrl);
- link.attr(WYMeditor.TITLE, jQuery(wym._options.titleSelector).val());
- link.attr(WYMeditor.REL, jQuery(wym._options.relSelector).val());
- }
- window.close();
- });
-
jQuery(wym._options.dialogImageSelector + " " +
wym._options.submitSelector).submit(function () {
@@ -884,6 +864,12 @@ WYMeditor.INIT_DIALOG = function (index) {
window.close();
});
+ // Link insertion
+ insertAnchorOnClick = WYMeditor.MAKE_ANCHOR_ONCLICK(wym);
+ jQuery(wym._options.dialogLinkSelector + " " + wym._options.submitSelector)
+ .submit(insertAnchorOnClick);
+
+ // Table insertion
tableOnClick = WYMeditor.MAKE_TABLE_ONCLICK(wym);
jQuery(wym._options.dialogTableSelector + " " + wym._options.submitSelector)
.submit(tableOnClick);
@@ -911,7 +897,7 @@ WYMeditor.INIT_DIALOG = function (index) {
};
-/********** TABLE DIALOG ONCLICK **********/
+/********** DIALOG OnClick Handlers **********/
WYMeditor.MAKE_TABLE_ONCLICK = function (wym) {
var tableOnClick = function () {
@@ -928,6 +914,43 @@ WYMeditor.MAKE_TABLE_ONCLICK = function (wym) {
return tableOnClick;
};
+WYMeditor.MAKE_ANCHOR_ONCLICK = function (wym) {
+ var makeAnchorOnClick = function () {
+ // Used to find the a tag we create via exec
+ var uniqueStamp = wym.uniqueStamp(),
+ // User-input href, title and rel values from the dialog form
+ inputHref,
+ inputTitle,
+ inputRel,
+ selected,
+ anchorToEdit;
+
+ selected = wym.selected();
+
+ // Get the inputs from the dialog form
+ inputHref = jQuery(wym._options.hrefSelector).val();
+ inputTitle = jQuery(wym._options.titleSelector).val();
+ inputRel = jQuery(wym._options.relSelector).val();
+
+ if (inputHref.length > 0) {
+ // We have a valid href, so go ahead and make changes
+
+ if (selected[0] && selected[0].tagName.toLowerCase() === WYMeditor.A) {
+ anchorToEdit = selected;
+ } else {
+ wym._exec(WYMeditor.CREATE_LINK, uniqueStamp);
+ anchorToEdit = jQuery("a[href=" + uniqueStamp + "]", wym._doc.body);
+ }
+
+ anchorToEdit.attr(WYMeditor.HREF, inputHref);
+ anchorToEdit.attr(WYMeditor.TITLE, inputTitle);
+ anchorToEdit.attr(WYMeditor.REL, inputRel);
+ }
+ window.close();
+ };
+
+ return makeAnchorOnClick;
+};
/********** HELPERS **********/
View
47 src/wymeditor/editor/base.js
@@ -1505,6 +1505,53 @@ WYMeditor.editor.prototype.outdent = function () {
};
/**
+ editor.insertAnchor
+ ===================
+
+ Insert an anchor tag at the current selection with the given `href`,
+ `title` and `rel` attributes.
+*/
+WYMeditor.editor.prototype.insertAnchor = function (href, title, rel) {
+ var wym = this,
+ selected,
+ uniqueStamp,
+ anchorToEdit;
+
+ if (href.length === 0) {
+ // href is required, so don't do anything
+ return;
+ }
+ // ensure that we select the link to populate the fields
+ selected = wym.selected();
+ if (selected && selected.tagName &&
+ selected.tagName.toLowerCase !== WYMeditor.A) {
+ selected = jQuery(selected).parentsOrSelf(WYMeditor.A);
+ }
+
+ // fix MSIE selection if link image has been clicked
+ if (!selected && wym._selected_image) {
+ selected = jQuery(wym._selected_image).parentsOrSelf(WYMeditor.A);
+ }
+
+ if (selected[0] && selected[0].tagName.toLowerCase() === WYMeditor.A) {
+ anchorToEdit = selected;
+ console.log("existing anchor");
+ console.log(anchorToEdit);
+ } else {
+ console.log("new anchor");
+ uniqueStamp = wym.uniqueStamp();
+ wym._exec(WYMeditor.CREATE_LINK, uniqueStamp);
+ anchorToEdit = jQuery("a[href=" + uniqueStamp + "]", wym._doc.body);
+ }
+
+ anchorToEdit.attr(WYMeditor.HREF, inputHref);
+ anchorToEdit.attr(WYMeditor.TITLE, inputTitle);
+ anchorToEdit.attr(WYMeditor.REL, inputRel);
+
+ return anchorToEdit;
+};
+
+/**
editor.insertTable
==================

0 comments on commit dab23fb

Please sign in to comment.
Something went wrong with that request. Please try again.