Skip to content

Commit

Permalink
Merge branch 'MDL-49107-27' of git://github.com/FMCorz/moodle into MO…
Browse files Browse the repository at this point in the history
…ODLE_27_STABLE
  • Loading branch information
David Monllao committed Mar 10, 2015
2 parents 154356e + ac5e89c commit 28a3365
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
_displayDialogue: function() {
// Store the current selection.
this._currentSelection = this.get('host').getSelection();
if (this._currentSelection === false || this._currentSelection.collapsed) {
if (this._currentSelection === false) {
return;
}

Expand Down Expand Up @@ -167,11 +167,11 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
},

/**
* Update the dialogue after an image was selected in the File Picker.
* Update the dialogue after a link was selected in the File Picker.
*
* @method _filepickerCallback
* @param {object} params The parameters provided by the filepicker
* containing information about the image.
* containing information about the link.
* @private
*/
_filepickerCallback: function(params) {
Expand All @@ -180,9 +180,9 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
.hide();

if (params.url !== '') {
this.get('host').setSelection(this._currentSelection);
document.execCommand('unlink', false, null);
document.execCommand('createLink', false, params.url);
// Add the link.
this._setLinkOnSelection(params.url);

// And mark the text area as updated.
this.markUpdated();
}
Expand All @@ -202,8 +202,6 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
anchornodes,
value;

var host = this.get('host');

e.preventDefault();
this.getDialogue({
focusAfterHide: null
Expand All @@ -213,13 +211,9 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit

value = input.get('value');
if (value !== '') {
this.editor.focus();
host.setSelection(this._currentSelection);
document.execCommand('unlink', false, null);
document.execCommand('createLink', false, value);

// Now set the target.
selectednode = host.getSelectionParentNode();
// Add the link.
selectednode = this._setLinkOnSelection(value);

// Note this is a document fragment and YUI doesn't like them.
if (!selectednode) {
Expand All @@ -240,6 +234,41 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
}
},

/**
* Final step setting the anchor on the selection.
*
* @private
* @method _setLinkOnSelection
* @param {String} url URL the link will point to.
* @return {Node} The added Node.
*/
_setLinkOnSelection: function(url) {
var host = this.get('host'),
link,
selectednode;

this.editor.focus();
host.setSelection(this._currentSelection);

if (this._currentSelection[0].collapsed) {
// Firefox cannot add links when the selection is empty so we will add it manually.
link = Y.Node.create('<a>' + url + '</a>');
link.setAttribute('href', url);

// Add the node and select it to replicate the behaviour of execCommand.
selectednode = host.insertContentAtFocusPoint(link.get('outerHTML'));
host.setSelection(host.getSelectionFromNode(selectednode));
} else {
document.execCommand('unlink', false, null);
document.execCommand('createLink', false, url);

// Now set the target.
selectednode = host.getSelectionParentNode();
}

return selectednode;
},

/**
* Look up and down for the nearest anchor tags that are least partly contained in the selection.
*
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
_displayDialogue: function() {
// Store the current selection.
this._currentSelection = this.get('host').getSelection();
if (this._currentSelection === false || this._currentSelection.collapsed) {
if (this._currentSelection === false) {
return;
}

Expand Down Expand Up @@ -167,11 +167,11 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
},

/**
* Update the dialogue after an image was selected in the File Picker.
* Update the dialogue after a link was selected in the File Picker.
*
* @method _filepickerCallback
* @param {object} params The parameters provided by the filepicker
* containing information about the image.
* containing information about the link.
* @private
*/
_filepickerCallback: function(params) {
Expand All @@ -180,9 +180,9 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
.hide();

if (params.url !== '') {
this.get('host').setSelection(this._currentSelection);
document.execCommand('unlink', false, null);
document.execCommand('createLink', false, params.url);
// Add the link.
this._setLinkOnSelection(params.url);

// And mark the text area as updated.
this.markUpdated();
}
Expand All @@ -202,8 +202,6 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
anchornodes,
value;

var host = this.get('host');

e.preventDefault();
this.getDialogue({
focusAfterHide: null
Expand All @@ -213,13 +211,9 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit

value = input.get('value');
if (value !== '') {
this.editor.focus();
host.setSelection(this._currentSelection);
document.execCommand('unlink', false, null);
document.execCommand('createLink', false, value);

// Now set the target.
selectednode = host.getSelectionParentNode();
// Add the link.
selectednode = this._setLinkOnSelection(value);

// Note this is a document fragment and YUI doesn't like them.
if (!selectednode) {
Expand All @@ -240,6 +234,41 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
}
},

/**
* Final step setting the anchor on the selection.
*
* @private
* @method _setLinkOnSelection
* @param {String} url URL the link will point to.
* @return {Node} The added Node.
*/
_setLinkOnSelection: function(url) {
var host = this.get('host'),
link,
selectednode;

this.editor.focus();
host.setSelection(this._currentSelection);

if (this._currentSelection[0].collapsed) {
// Firefox cannot add links when the selection is empty so we will add it manually.
link = Y.Node.create('<a>' + url + '</a>');
link.setAttribute('href', url);

// Add the node and select it to replicate the behaviour of execCommand.
selectednode = host.insertContentAtFocusPoint(link.get('outerHTML'));
host.setSelection(host.getSelectionFromNode(selectednode));
} else {
document.execCommand('unlink', false, null);
document.execCommand('createLink', false, url);

// Now set the target.
selectednode = host.getSelectionParentNode();
}

return selectednode;
},

/**
* Look up and down for the nearest anchor tags that are least partly contained in the selection.
*
Expand Down
57 changes: 43 additions & 14 deletions lib/editor/atto/plugins/link/yui/src/button/js/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
_displayDialogue: function() {
// Store the current selection.
this._currentSelection = this.get('host').getSelection();
if (this._currentSelection === false || this._currentSelection.collapsed) {
if (this._currentSelection === false) {
return;
}

Expand Down Expand Up @@ -165,11 +165,11 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
},

/**
* Update the dialogue after an image was selected in the File Picker.
* Update the dialogue after a link was selected in the File Picker.
*
* @method _filepickerCallback
* @param {object} params The parameters provided by the filepicker
* containing information about the image.
* containing information about the link.
* @private
*/
_filepickerCallback: function(params) {
Expand All @@ -178,9 +178,9 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
.hide();

if (params.url !== '') {
this.get('host').setSelection(this._currentSelection);
document.execCommand('unlink', false, null);
document.execCommand('createLink', false, params.url);
// Add the link.
this._setLinkOnSelection(params.url);

// And mark the text area as updated.
this.markUpdated();
}
Expand All @@ -200,8 +200,6 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
anchornodes,
value;

var host = this.get('host');

e.preventDefault();
this.getDialogue({
focusAfterHide: null
Expand All @@ -211,13 +209,9 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit

value = input.get('value');
if (value !== '') {
this.editor.focus();
host.setSelection(this._currentSelection);
document.execCommand('unlink', false, null);
document.execCommand('createLink', false, value);

// Now set the target.
selectednode = host.getSelectionParentNode();
// Add the link.
selectednode = this._setLinkOnSelection(value);

// Note this is a document fragment and YUI doesn't like them.
if (!selectednode) {
Expand All @@ -238,6 +232,41 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
}
},

/**
* Final step setting the anchor on the selection.
*
* @private
* @method _setLinkOnSelection
* @param {String} url URL the link will point to.
* @return {Node} The added Node.
*/
_setLinkOnSelection: function(url) {
var host = this.get('host'),
link,
selectednode;

this.editor.focus();
host.setSelection(this._currentSelection);

if (this._currentSelection[0].collapsed) {
// Firefox cannot add links when the selection is empty so we will add it manually.
link = Y.Node.create('<a>' + url + '</a>');
link.setAttribute('href', url);

// Add the node and select it to replicate the behaviour of execCommand.
selectednode = host.insertContentAtFocusPoint(link.get('outerHTML'));
host.setSelection(host.getSelectionFromNode(selectednode));
} else {
document.execCommand('unlink', false, null);
document.execCommand('createLink', false, url);

// Now set the target.
selectednode = host.getSelectionParentNode();
}

return selectednode;
},

/**
* Look up and down for the nearest anchor tags that are least partly contained in the selection.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,7 @@ EditorSelection.prototype = {
*
* @method insertContentAtFocusPoint
* @param {String} html
* @return {Node} The YUI Node object added to the DOM.
*/
insertContentAtFocusPoint: function(html) {
var selection = rangy.getSelection(),
Expand All @@ -1373,6 +1374,7 @@ EditorSelection.prototype = {
range.deleteContents();
range.insertNode(node.getDOMNode());
}
return node;
}

};
Expand Down
Loading

0 comments on commit 28a3365

Please sign in to comment.