mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 05:58:34 +01:00
MDL-41241 Atto: Make searching for the anchor tag more reliable
This commit is contained in:
parent
a929e5e83d
commit
30509335dd
@ -105,7 +105,7 @@ M.atto_link = M.atto_link || {
|
||||
return;
|
||||
}
|
||||
|
||||
anchornode = Y.one(selectednode).ancestor('a');
|
||||
anchornode = M.atto_link.find_nearest_anchor(Y.one(selectednode));
|
||||
|
||||
if (anchornode) {
|
||||
M.atto_link.selection = M.editor_atto.get_selection_from_node(anchornode);
|
||||
@ -167,7 +167,6 @@ M.atto_link = M.atto_link || {
|
||||
M.atto_link.dialogue.hide();
|
||||
|
||||
input = e.currentTarget.get('parentNode').one('input[type=url]');
|
||||
target = e.currentTarget.get('parentNode').one('input[type=checkbox]');
|
||||
|
||||
value = input.get('value');
|
||||
if (value !== '') {
|
||||
@ -183,7 +182,7 @@ M.atto_link = M.atto_link || {
|
||||
return;
|
||||
}
|
||||
|
||||
anchornode = Y.one(selectednode).ancestor('a');
|
||||
anchornode = M.atto_link.find_nearest_anchor(Y.one(selectednode));
|
||||
if (anchornode) {
|
||||
target = e.currentTarget.get('parentNode').one('input[type=checkbox]');
|
||||
if (target.get('checked')) {
|
||||
@ -195,6 +194,32 @@ M.atto_link = M.atto_link || {
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Look up and down for the nearest anchor tag.
|
||||
*
|
||||
* @method find_nearest_anchor
|
||||
* @param Node node
|
||||
* @return Node|false
|
||||
*/
|
||||
find_nearest_anchor : function(node) {
|
||||
var tagname = node.get('tagName'), hit;
|
||||
// Direct hit.
|
||||
if (tagname && tagname.toLowerCase() === 'a') {
|
||||
return node;
|
||||
}
|
||||
// Search down.
|
||||
hit = node.one('a');
|
||||
if (hit) {
|
||||
return hit;
|
||||
}
|
||||
// Search up.
|
||||
hit = node.ancestor('a');
|
||||
if (hit) {
|
||||
return hit;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
/**
|
||||
* Return the HTML of the form to show in the dialogue.
|
||||
*
|
||||
|
@ -1 +1 @@
|
||||
YUI.add("moodle-atto_link-button",function(e,t){M.atto_link=M.atto_link||{dialogue:null,selection:null,display_chooser:function(e,t){e.preventDefault(),M.editor_atto.is_active(t)||M.editor_atto.focus(t),M.atto_link.selection=M.editor_atto.get_selection();if(M.atto_link.selection!==!1&&!M.atto_link.selection.collapsed){var n;M.atto_link.dialogue?n=M.atto_link.dialogue:n=new M.core.dialogue({visible:!1,modal:!0,close:!0,draggable:!0}),n.render(),n.set("bodyContent",M.atto_link.get_form_content(t)),n.set("headerContent",M.util.get_string("createlink","atto_link")),M.atto_link.resolve_anchors(),n.show(),M.atto_link.dialogue=n}},init:function(e){M.editor_atto.add_toolbar_button(e.elementid,"link",e.icon,e.group,this.display_chooser,this)},resolve_anchors:function(){var t=M.editor_atto.get_selection_parent_node(),n,r;if(!t)return;n=e.one(t).ancestor("a"),n&&(M.atto_link.selection=M.editor_atto.get_selection_from_node(n),r=n.getAttribute("href"),target=n.getAttribute("target"),r!==""&&e.one("#atto_link_urlentry").set("value",r),target==="_blank"?e.one("#atto_link_openinnewwindow").set("checked","checked"):e.one("#atto_link_openinnewwindow").set("checked",""))},open_filepicker:function(e){var t=this.getAttribute("data-editor");e.preventDefault(),M.editor_atto.show_filepicker(t,"link",M.atto_link.filepicker_callback)},filepicker_callback:function(e){M.atto_link.dialogue.hide(),e.url!==""&&(M.editor_atto.set_selection(M.atto_link.selection),document.execCommand("unlink",!1,null),document.execCommand("createLink",!1,e.url))},set_link:function(t){var n,r,i,s,o;t.preventDefault(),M.atto_link.dialogue.hide(),n=t.currentTarget.get("parentNode").one("input[type=url]"),r=t.currentTarget.get("parentNode").one("input[type=checkbox]"),o=n.get("value");if(o!==""){M.editor_atto.set_selection(M.atto_link.selection),document.execCommand("unlink",!1,null),document.execCommand("createLink",!1,o),i=M.editor_atto.get_selection_parent_node();if(!i)return;s=e.one(i).ancestor("a"),s&&(r=t.currentTarget.get("parentNode").one("input[type=checkbox]"),r.get("checked")?s.setAttribute("target","_blank"):s.removeAttribute("target"))}},get_form_content:function(t){var n=e.Node.create('<form><label for="atto_link_urlentry">'+M.util.get_string("enterurl","atto_link")+"</label><br/>"+'<input type="url" value="" id="atto_link_urlentry" size="32"/>'+"<br/>"+'<label for="atto_link_openinnewwindow">'+M.util.get_string("openinnewwindow","atto_link")+"</label><br/>"+'<input type="checkbox" id="atto_link_openinnewwindow"/>'+"<br/>"+'<button id="openlinkbrowser" data-editor="'+e.Escape.html(t)+'">'+M.util.get_string("browserepositories","atto_link")+"</button>"+"<hr/>"+'<button id="atto_link_urlentrysubmit">'+M.util.get_string("createlink","atto_link")+"</button>"+"</form>"+"<hr/>"+M.util.get_string("accessibilityhint","atto_link"));return n.one("#atto_link_urlentrysubmit").on("click",M.atto_link.set_link),n.one("#openlinkbrowser").on("click",M.atto_link.open_filepicker),n}}},"@VERSION@",{requires:["node","escape"]});
|
||||
YUI.add("moodle-atto_link-button",function(e,t){M.atto_link=M.atto_link||{dialogue:null,selection:null,display_chooser:function(e,t){e.preventDefault(),M.editor_atto.is_active(t)||M.editor_atto.focus(t),M.atto_link.selection=M.editor_atto.get_selection();if(M.atto_link.selection!==!1&&!M.atto_link.selection.collapsed){var n;M.atto_link.dialogue?n=M.atto_link.dialogue:n=new M.core.dialogue({visible:!1,modal:!0,close:!0,draggable:!0}),n.render(),n.set("bodyContent",M.atto_link.get_form_content(t)),n.set("headerContent",M.util.get_string("createlink","atto_link")),M.atto_link.resolve_anchors(),n.show(),M.atto_link.dialogue=n}},init:function(e){M.editor_atto.add_toolbar_button(e.elementid,"link",e.icon,e.group,this.display_chooser,this)},resolve_anchors:function(){var t=M.editor_atto.get_selection_parent_node(),n,r;if(!t)return;n=M.atto_link.find_nearest_anchor(e.one(t)),n&&(M.atto_link.selection=M.editor_atto.get_selection_from_node(n),r=n.getAttribute("href"),target=n.getAttribute("target"),r!==""&&e.one("#atto_link_urlentry").set("value",r),target==="_blank"?e.one("#atto_link_openinnewwindow").set("checked","checked"):e.one("#atto_link_openinnewwindow").set("checked",""))},open_filepicker:function(e){var t=this.getAttribute("data-editor");e.preventDefault(),M.editor_atto.show_filepicker(t,"link",M.atto_link.filepicker_callback)},filepicker_callback:function(e){M.atto_link.dialogue.hide(),e.url!==""&&(M.editor_atto.set_selection(M.atto_link.selection),document.execCommand("unlink",!1,null),document.execCommand("createLink",!1,e.url))},set_link:function(t){var n,r,i,s,o;t.preventDefault(),M.atto_link.dialogue.hide(),n=t.currentTarget.get("parentNode").one("input[type=url]"),o=n.get("value");if(o!==""){M.editor_atto.set_selection(M.atto_link.selection),document.execCommand("unlink",!1,null),document.execCommand("createLink",!1,o),i=M.editor_atto.get_selection_parent_node();if(!i)return;s=M.atto_link.find_nearest_anchor(e.one(i)),s&&(r=t.currentTarget.get("parentNode").one("input[type=checkbox]"),r.get("checked")?s.setAttribute("target","_blank"):s.removeAttribute("target"))}},find_nearest_anchor:function(e){var t=e.get("tagName"),n;return t&&t.toLowerCase()==="a"?e:(n=e.one("a"),n?n:(n=e.ancestor("a"),n?n:!1))},get_form_content:function(t){var n=e.Node.create('<form><label for="atto_link_urlentry">'+M.util.get_string("enterurl","atto_link")+"</label><br/>"+'<input type="url" value="" id="atto_link_urlentry" size="32"/>'+"<br/>"+'<label for="atto_link_openinnewwindow">'+M.util.get_string("openinnewwindow","atto_link")+"</label><br/>"+'<input type="checkbox" id="atto_link_openinnewwindow"/>'+"<br/>"+'<button id="openlinkbrowser" data-editor="'+e.Escape.html(t)+'">'+M.util.get_string("browserepositories","atto_link")+"</button>"+"<hr/>"+'<button id="atto_link_urlentrysubmit">'+M.util.get_string("createlink","atto_link")+"</button>"+"</form>"+"<hr/>"+M.util.get_string("accessibilityhint","atto_link"));return n.one("#atto_link_urlentrysubmit").on("click",M.atto_link.set_link),n.one("#openlinkbrowser").on("click",M.atto_link.open_filepicker),n}}},"@VERSION@",{requires:["node","escape"]});
|
||||
|
@ -105,7 +105,7 @@ M.atto_link = M.atto_link || {
|
||||
return;
|
||||
}
|
||||
|
||||
anchornode = Y.one(selectednode).ancestor('a');
|
||||
anchornode = M.atto_link.find_nearest_anchor(Y.one(selectednode));
|
||||
|
||||
if (anchornode) {
|
||||
M.atto_link.selection = M.editor_atto.get_selection_from_node(anchornode);
|
||||
@ -167,7 +167,6 @@ M.atto_link = M.atto_link || {
|
||||
M.atto_link.dialogue.hide();
|
||||
|
||||
input = e.currentTarget.get('parentNode').one('input[type=url]');
|
||||
target = e.currentTarget.get('parentNode').one('input[type=checkbox]');
|
||||
|
||||
value = input.get('value');
|
||||
if (value !== '') {
|
||||
@ -183,7 +182,7 @@ M.atto_link = M.atto_link || {
|
||||
return;
|
||||
}
|
||||
|
||||
anchornode = Y.one(selectednode).ancestor('a');
|
||||
anchornode = M.atto_link.find_nearest_anchor(Y.one(selectednode));
|
||||
if (anchornode) {
|
||||
target = e.currentTarget.get('parentNode').one('input[type=checkbox]');
|
||||
if (target.get('checked')) {
|
||||
@ -195,6 +194,32 @@ M.atto_link = M.atto_link || {
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Look up and down for the nearest anchor tag.
|
||||
*
|
||||
* @method find_nearest_anchor
|
||||
* @param Node node
|
||||
* @return Node|false
|
||||
*/
|
||||
find_nearest_anchor : function(node) {
|
||||
var tagname = node.get('tagName'), hit;
|
||||
// Direct hit.
|
||||
if (tagname && tagname.toLowerCase() === 'a') {
|
||||
return node;
|
||||
}
|
||||
// Search down.
|
||||
hit = node.one('a');
|
||||
if (hit) {
|
||||
return hit;
|
||||
}
|
||||
// Search up.
|
||||
hit = node.ancestor('a');
|
||||
if (hit) {
|
||||
return hit;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
/**
|
||||
* Return the HTML of the form to show in the dialogue.
|
||||
*
|
||||
|
@ -103,7 +103,7 @@ M.atto_link = M.atto_link || {
|
||||
return;
|
||||
}
|
||||
|
||||
anchornode = Y.one(selectednode).ancestor('a');
|
||||
anchornode = M.atto_link.find_nearest_anchor(Y.one(selectednode));
|
||||
|
||||
if (anchornode) {
|
||||
M.atto_link.selection = M.editor_atto.get_selection_from_node(anchornode);
|
||||
@ -165,7 +165,6 @@ M.atto_link = M.atto_link || {
|
||||
M.atto_link.dialogue.hide();
|
||||
|
||||
input = e.currentTarget.get('parentNode').one('input[type=url]');
|
||||
target = e.currentTarget.get('parentNode').one('input[type=checkbox]');
|
||||
|
||||
value = input.get('value');
|
||||
if (value !== '') {
|
||||
@ -181,7 +180,7 @@ M.atto_link = M.atto_link || {
|
||||
return;
|
||||
}
|
||||
|
||||
anchornode = Y.one(selectednode).ancestor('a');
|
||||
anchornode = M.atto_link.find_nearest_anchor(Y.one(selectednode));
|
||||
if (anchornode) {
|
||||
target = e.currentTarget.get('parentNode').one('input[type=checkbox]');
|
||||
if (target.get('checked')) {
|
||||
@ -193,6 +192,32 @@ M.atto_link = M.atto_link || {
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Look up and down for the nearest anchor tag.
|
||||
*
|
||||
* @method find_nearest_anchor
|
||||
* @param Node node
|
||||
* @return Node|false
|
||||
*/
|
||||
find_nearest_anchor : function(node) {
|
||||
var tagname = node.get('tagName'), hit;
|
||||
// Direct hit.
|
||||
if (tagname && tagname.toLowerCase() === 'a') {
|
||||
return node;
|
||||
}
|
||||
// Search down.
|
||||
hit = node.one('a');
|
||||
if (hit) {
|
||||
return hit;
|
||||
}
|
||||
// Search up.
|
||||
hit = node.ancestor('a');
|
||||
if (hit) {
|
||||
return hit;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
/**
|
||||
* Return the HTML of the form to show in the dialogue.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user