* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* @module moodle-atto_media-button
*/
/**
* Atto media selection tool.
*
* @namespace M.atto_media
* @class Button
* @extends M.editor_atto.EditorPlugin
*/
var COMPONENTNAME = 'atto_media',
CSS = {
URLINPUT: 'atto_media_urlentry',
NAMEINPUT: 'atto_media_nameentry'
},
SELECTORS = {
URLINPUT: '.' + CSS.URLINPUT,
NAMEINPUT: '.' + CSS.NAMEINPUT
},
TEMPLATE = '' +
'';
Y.namespace('M.atto_media').Button = Y.Base.create('button', Y.M.editor_atto.EditorPlugin, [], {
/**
* A reference to the current selection at the time that the dialogue
* was opened.
*
* @property _currentSelection
* @type Range
* @private
*/
_currentSelection: null,
/**
* A reference to the dialogue content.
*
* @property _content
* @type Node
* @private
*/
_content: null,
initializer: function() {
if (this.get('host').canShowFilepicker('media')) {
this.addButton({
icon: 'e/insert_edit_video',
callback: this._displayDialogue
});
}
},
/**
* Display the media editing tool.
*
* @method _displayDialogue
* @private
*/
_displayDialogue: function() {
// Store the current selection.
this._currentSelection = this.get('host').getSelection();
if (this._currentSelection === false) {
return;
}
var dialogue = this.getDialogue({
headerContent: M.util.get_string('createmedia', COMPONENTNAME),
focusAfterHide: true,
focusOnShowSelector: SELECTORS.URLINPUT
});
// Set the dialogue content, and then show the dialogue.
dialogue.set('bodyContent', this._getDialogueContent())
.show();
},
/**
* Return the dialogue content for the tool, attaching any required
* events.
*
* @method _getDialogueContent
* @return {Node} The content to place in the dialogue.
* @private
*/
_getDialogueContent: function() {
var template = Y.Handlebars.compile(TEMPLATE);
this._content = Y.Node.create(template({
component: COMPONENTNAME,
elementid: this.get('host').get('elementid'),
CSS: CSS
}));
this._content.one('.submit').on('click', this._setMedia, this);
this._content.one('.openmediabrowser').on('click', function(e) {
e.preventDefault();
this.get('host').showFilepicker('media', this._filepickerCallback, this);
}, this);
return this._content;
},
/**
* Update the dialogue after an media was selected in the File Picker.
*
* @method _filepickerCallback
* @param {object} params The parameters provided by the filepicker
* containing information about the image.
* @private
*/
_filepickerCallback: function(params) {
if (params.url !== '') {
this._content.one(SELECTORS.URLINPUT)
.set('value', params.url);
this._content.one(SELECTORS.NAMEINPUT)
.set('value', params.file);
}
},
/**
* Update the media in the contenteditable.
*
* @method setMedia
* @param {EventFacade} e
* @private
*/
_setMedia: function(e) {
e.preventDefault();
this.getDialogue({
focusAfterHide: null
}).hide();
var form = e.currentTarget.ancestor('.atto_form'),
url = form.one(SELECTORS.URLINPUT).get('value'),
name = form.one(SELECTORS.NAMEINPUT).get('value'),
host = this.get('host');
if (url !== '' && name !== '') {
host.setSelection(this._currentSelection);
var mediahtml = '' + name + '';
host.insertContentAtFocusPoint(mediahtml);
this.markUpdated();
}
}
});
}, '@VERSION@', {"requires": ["moodle-editor_atto-plugin"]});