1 YUI.add('moodle-editor_atto-editor', function (Y, NAME) {
3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /* eslint-disable no-unused-vars */
20 * The Atto WYSIWG pluggable editor, written for Moodle.
22 * @module moodle-editor_atto-editor
23 * @package editor_atto
24 * @copyright 2013 Damyon Wiese <damyon@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 * @main moodle-editor_atto-editor
30 * @module moodle-editor_atto-editor
31 * @submodule editor-base
34 var LOGNAME = 'moodle-editor_atto-editor';
36 CONTENT: 'editor_atto_content',
37 CONTENTWRAPPER: 'editor_atto_content_wrap',
38 TOOLBAR: 'editor_atto_toolbar',
39 WRAPPER: 'editor_atto',
40 HIGHLIGHT: 'highlight'
45 * The Atto editor for Moodle.
47 * @namespace M.editor_atto
50 * @uses M.editor_atto.EditorClean
51 * @uses M.editor_atto.EditorFilepicker
52 * @uses M.editor_atto.EditorSelection
53 * @uses M.editor_atto.EditorStyling
54 * @uses M.editor_atto.EditorTextArea
55 * @uses M.editor_atto.EditorToolbar
56 * @uses M.editor_atto.EditorToolbarNav
60 Editor.superclass.constructor.apply(this, arguments);
63 Y.extend(Editor, Y.Base, {
66 * List of known block level tags.
67 * Taken from "https://developer.mozilla.org/en-US/docs/HTML/Block-level_elements".
69 * @property BLOCK_TAGS
108 PLACEHOLDER_CLASS: 'atto-tmp-class',
109 ALL_NODES_SELECTOR: '[style],font[face]',
110 FONT_FAMILY: 'fontFamily',
113 * The wrapper containing the editor.
122 * A reference to the content editable Node.
130 * A reference to the original text area.
138 * A reference to the label associated with the original text area.
140 * @property textareaLabel
146 * A reference to the list of plugins.
154 * Event Handles to clear on editor destruction.
156 * @property _eventHandles
161 initializer: function() {
164 // Note - it is not safe to use a CSS selector like '#' + elementid because the id
165 // may have colons in it - e.g. quiz.
166 this.textarea = Y.one(document.getElementById(this.get('elementid')));
168 if (!this.textarea) {
169 // No text area found.
170 Y.log('Text area not found - unable to setup editor for ' + this.get('elementid'),
175 this._eventHandles = [];
177 this._wrapper = Y.Node.create('<div class="' + CSS.WRAPPER + '" />');
178 template = Y.Handlebars.compile('<div id="{{elementid}}editable" ' +
179 'contenteditable="true" ' +
181 'spellcheck="true" ' +
183 'class="{{CSS.CONTENT}}" ' +
185 this.editor = Y.Node.create(template({
186 elementid: this.get('elementid'),
190 // Add a labelled-by attribute to the contenteditable.
191 this.textareaLabel = Y.one('[for="' + this.get('elementid') + '"]');
192 if (this.textareaLabel) {
193 this.textareaLabel.generateID();
194 this.editor.setAttribute('aria-labelledby', this.textareaLabel.get("id"));
197 // Add everything to the wrapper.
200 // Editable content wrapper.
201 var content = Y.Node.create('<div class="' + CSS.CONTENTWRAPPER + '" />');
202 content.appendChild(this.editor);
203 this._wrapper.appendChild(content);
205 // Style the editor. According to the styles.css: 20 is the line-height, 8 is padding-top + padding-bottom.
206 this.editor.setStyle('minHeight', ((20 * this.textarea.getAttribute('rows')) + 8) + 'px');
209 // We set a height here to force the overflow because decent browsers allow the CSS property resize.
210 this.editor.setStyle('height', ((20 * this.textarea.getAttribute('rows')) + 8) + 'px');
213 // Disable odd inline CSS styles.
214 this.disableCssStyling();
216 // Use paragraphs not divs.
217 if (document.queryCommandSupported('DefaultParagraphSeparator')) {
218 document.execCommand('DefaultParagraphSeparator', false, 'p');
221 // Add the toolbar and editable zone to the page.
222 this.textarea.get('parentNode').insert(this._wrapper, this.textarea).
223 setAttribute('class', 'editor_atto_wrap');
225 // Hide the old textarea.
226 this.textarea.hide();
228 // Copy the text to the contenteditable div.
229 this.updateFromTextArea();
231 // Publish the events that are defined by this editor.
232 this.publishEvents();
234 // Add handling for saving and restoring selections on cursor/focus changes.
235 this.setupSelectionWatchers();
237 // Add polling to update the textarea periodically when typing long content.
238 this.setupAutomaticPolling();
243 // Initialize the auto-save timer.
244 this.setupAutosave();
245 // Preload the icons for the notifications.
246 this.setupNotifications();
250 * Focus on the editable area for this editor.
262 * Publish events for this editor instance.
264 * @method publishEvents
268 publishEvents: function() {
270 * Fired when changes are made within the editor.
274 this.publish('change', {
280 * Fired when all plugins have completed loading.
282 * @event pluginsloaded
284 this.publish('pluginsloaded', {
288 this.publish('atto:selectionchanged', {
296 * Set up automated polling of the text area to update the textarea.
298 * @method setupAutomaticPolling
301 setupAutomaticPolling: function() {
302 this._registerEventHandle(this.editor.on(['keyup', 'cut'], this.updateOriginal, this));
303 this._registerEventHandle(this.editor.on('paste', this.pasteCleanup, this));
305 // Call this.updateOriginal after dropped content has been processed.
306 this._registerEventHandle(this.editor.on('drop', this.updateOriginalDelayed, this));
312 * Calls updateOriginal on a short timer to allow native event handlers to run first.
314 * @method updateOriginalDelayed
317 updateOriginalDelayed: function() {
318 Y.soon(Y.bind(this.updateOriginal, this));
323 setupPlugins: function() {
324 // Clear the list of plugins.
327 var plugins = this.get('plugins');
335 for (groupIndex in plugins) {
336 group = plugins[groupIndex];
337 if (!group.plugins) {
338 // No plugins in this group - skip it.
341 for (pluginIndex in group.plugins) {
342 plugin = group.plugins[pluginIndex];
344 pluginConfig = Y.mix({
348 toolbar: this.toolbar,
352 // Add a reference to the current editor.
353 if (typeof Y.M['atto_' + plugin.name] === "undefined") {
354 Y.log("Plugin '" + plugin.name + "' could not be found - skipping initialisation", "warn", LOGNAME);
357 this.plugins[plugin.name] = new Y.M['atto_' + plugin.name].Button(pluginConfig);
361 // Some plugins need to perform actions once all plugins have loaded.
362 this.fire('pluginsloaded');
367 enablePlugins: function(plugin) {
368 this._setPluginState(true, plugin);
371 disablePlugins: function(plugin) {
372 this._setPluginState(false, plugin);
375 _setPluginState: function(enable, plugin) {
376 var target = 'disableButtons';
378 target = 'enableButtons';
382 this.plugins[plugin][target]();
384 Y.Object.each(this.plugins, function(currentPlugin) {
385 currentPlugin[target]();
391 * Register an event handle for disposal in the destructor.
393 * @method _registerEventHandle
394 * @param {EventHandle} The Event Handle as returned by Y.on, and Y.delegate.
397 _registerEventHandle: function(handle) {
398 this._eventHandles.push(handle);
405 * The unique identifier for the form element representing the editor.
407 * @attribute elementid
417 * The contextid of the form.
419 * @attribute contextid
429 * Plugins with their configuration.
431 * The plugins structure is:
435 * "group": "groupName",
438 * "configKey": "configValue"
441 * "configKey": "configValue"
446 * "group": "groupName",
449 * "configKey": "configValue"
466 // The Editor publishes custom events that can be subscribed to.
467 Y.augment(Editor, Y.EventTarget);
469 Y.namespace('M.editor_atto').Editor = Editor;
471 // Function for Moodle's initialisation.
472 Y.namespace('M.editor_atto.Editor').init = function(config) {
473 return new Y.M.editor_atto.Editor(config);
475 // This file is part of Moodle - http://moodle.org/
477 // Moodle is free software: you can redistribute it and/or modify
478 // it under the terms of the GNU General Public License as published by
479 // the Free Software Foundation, either version 3 of the License, or
480 // (at your option) any later version.
482 // Moodle is distributed in the hope that it will be useful,
483 // but WITHOUT ANY WARRANTY; without even the implied warranty of
484 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
485 // GNU General Public License for more details.
487 // You should have received a copy of the GNU General Public License
488 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
491 * A notify function for the Atto editor.
493 * @module moodle-editor_atto-notify
495 * @package editor_atto
496 * @copyright 2014 Damyon Wiese
497 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
500 var LOGNAME_NOTIFY = 'moodle-editor_atto-editor-notify',
501 NOTIFY_INFO = 'info',
502 NOTIFY_WARNING = 'warning';
504 function EditorNotify() {}
506 EditorNotify.ATTRS= {
509 EditorNotify.prototype = {
512 * A single Y.Node for this editor. There is only ever one - it is replaced if a new message comes in.
514 * @property messageOverlay
517 messageOverlay: null,
520 * A single timer object that can be used to cancel the hiding behaviour.
522 * @property hideTimer
528 * Initialize the notifications.
530 * @method setupNotifications
533 setupNotifications: function() {
534 var preload1 = new Image(),
535 preload2 = new Image();
537 preload1.src = M.util.image_url('i/warning', 'moodle');
538 preload2.src = M.util.image_url('i/info', 'moodle');
544 * Show a notification in a floaty overlay somewhere in the atto editor text area.
546 * @method showMessage
547 * @param {String} message The translated message (use get_string)
548 * @param {String} type Must be either "info" or "warning"
549 * @param {Number} timeout Time in milliseconds to show this message for.
552 showMessage: function(message, type, timeout) {
553 var messageTypeIcon = '',
557 if (this.messageOverlay === null) {
558 this.messageOverlay = Y.Node.create('<div class="editor_atto_notification"></div>');
560 this.messageOverlay.hide(true);
561 this.textarea.get('parentNode').append(this.messageOverlay);
563 this.messageOverlay.on('click', function() {
564 this.messageOverlay.hide(true);
568 if (this.hideTimer !== null) {
569 this.hideTimer.cancel();
572 if (type === NOTIFY_WARNING) {
573 messageTypeIcon = '<img src="' +
574 M.util.image_url('i/warning', 'moodle') +
575 '" alt="' + M.util.get_string('warning', 'moodle') + '"/>';
576 } else if (type === NOTIFY_INFO) {
577 messageTypeIcon = '<img src="' +
578 M.util.image_url('i/info', 'moodle') +
579 '" alt="' + M.util.get_string('info', 'moodle') + '"/>';
581 Y.log('Invalid message type specified: ' + type + '. Must be either "info" or "warning".', 'debug', LOGNAME_NOTIFY);
584 // Parse the timeout value.
585 intTimeout = parseInt(timeout, 10);
586 if (intTimeout <= 0) {
590 // Convert class to atto_info (for example).
591 type = 'atto_' + type;
593 bodyContent = Y.Node.create('<div class="' + type + '" role="alert" aria-live="assertive">' +
594 messageTypeIcon + ' ' +
595 Y.Escape.html(message) +
597 this.messageOverlay.empty();
598 this.messageOverlay.append(bodyContent);
599 this.messageOverlay.show(true);
601 this.hideTimer = Y.later(intTimeout, this, function() {
602 Y.log('Hide Atto notification.', 'debug', LOGNAME_NOTIFY);
603 this.hideTimer = null;
604 if (this.messageOverlay.inDoc()) {
605 this.messageOverlay.hide(true);
614 Y.Base.mix(Y.M.editor_atto.Editor, [EditorNotify]);
615 // This file is part of Moodle - http://moodle.org/
617 // Moodle is free software: you can redistribute it and/or modify
618 // it under the terms of the GNU General Public License as published by
619 // the Free Software Foundation, either version 3 of the License, or
620 // (at your option) any later version.
622 // Moodle is distributed in the hope that it will be useful,
623 // but WITHOUT ANY WARRANTY; without even the implied warranty of
624 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
625 // GNU General Public License for more details.
627 // You should have received a copy of the GNU General Public License
628 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
631 * @module moodle-editor_atto-editor
632 * @submodule textarea
636 * Textarea functions for the Atto editor.
638 * See {{#crossLink "M.editor_atto.Editor"}}{{/crossLink}} for details.
640 * @namespace M.editor_atto
641 * @class EditorTextArea
644 function EditorTextArea() {}
646 EditorTextArea.ATTRS= {
649 EditorTextArea.prototype = {
652 * Return the appropriate empty content value for the current browser.
654 * Different browsers use a different content when they are empty and
655 * we must set this reliable across the board.
657 * @method _getEmptyContent
658 * @return String The content to use representing no user-provided content
661 _getEmptyContent: function() {
662 if (Y.UA.ie && Y.UA.ie < 10) {
665 return '<p><br></p>';
670 * Copy and clean the text from the textarea into the contenteditable div.
672 * If the text is empty, provide a default paragraph tag to hold the content.
674 * @method updateFromTextArea
677 updateFromTextArea: function() {
679 this.editor.setHTML('');
681 // Copy cleaned HTML to editable div.
682 this.editor.append(this._cleanHTML(this.textarea.get('value')));
684 // Insert a paragraph in the empty contenteditable div.
685 if (this.editor.getHTML() === '') {
686 this.editor.setHTML(this._getEmptyContent());
693 * Copy the text from the contenteditable to the textarea which it replaced.
695 * @method updateOriginal
698 updateOriginal : function() {
699 // Get the previous and current value to compare them.
700 var oldValue = this.textarea.get('value'),
701 newValue = this.getCleanHTML();
703 if (newValue === "" && this.isActive()) {
704 // The content was entirely empty so get the empty content placeholder.
705 newValue = this._getEmptyContent();
708 // Only call this when there has been an actual change to reduce processing.
709 if (oldValue !== newValue) {
710 // Insert the cleaned content.
711 this.textarea.set('value', newValue);
713 // Trigger the onchange callback on the textarea, essentially to notify moodle-core-formchangechecker.
714 this.textarea.simulate('change');
716 // Trigger handlers for this action.
724 Y.Base.mix(Y.M.editor_atto.Editor, [EditorTextArea]);
725 // This file is part of Moodle - http://moodle.org/
727 // Moodle is free software: you can redistribute it and/or modify
728 // it under the terms of the GNU General Public License as published by
729 // the Free Software Foundation, either version 3 of the License, or
730 // (at your option) any later version.
732 // Moodle is distributed in the hope that it will be useful,
733 // but WITHOUT ANY WARRANTY; without even the implied warranty of
734 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
735 // GNU General Public License for more details.
737 // You should have received a copy of the GNU General Public License
738 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
739 /* global NOTIFY_WARNING, NOTIFY_INFO */
740 /* eslint-disable no-unused-vars */
743 * A autosave function for the Atto editor.
745 * @module moodle-editor_atto-autosave
746 * @submodule autosave-base
747 * @package editor_atto
748 * @copyright 2014 Damyon Wiese
749 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
752 var SUCCESS_MESSAGE_TIMEOUT = 5000,
753 RECOVER_MESSAGE_TIMEOUT = 60000,
754 LOGNAME_AUTOSAVE = 'moodle-editor_atto-editor-autosave';
756 function EditorAutosave() {}
758 EditorAutosave.ATTRS= {
760 * Enable/Disable auto save for this instance.
762 * @attribute autosaveEnabled
772 * The time between autosaves (in seconds).
774 * @attribute autosaveFrequency
785 * Unique hash for this page instance. Calculated from $PAGE->url in php.
787 * @attribute pageHash
797 EditorAutosave.prototype = {
800 * The text that was auto saved in the last request.
810 * @property autosaveInstance
813 autosaveInstance: null,
818 * @property autosaveTimer
824 * Initialize the autosave process
826 * @method setupAutosave
829 setupAutosave: function() {
833 options = this.get('filepickeroptions'),
836 if (!this.get('autosaveEnabled')) {
837 // Autosave disabled for this instance.
841 this.autosaveInstance = Y.stamp(this);
842 for (optiontype in options) {
843 if (typeof options[optiontype].itemid !== "undefined") {
844 draftid = options[optiontype].itemid;
848 // First see if there are any saved drafts.
849 // Make an ajax request.
851 contextid: this.get('contextid'),
854 elementid: this.get('elementid'),
855 pageinstance: this.autosaveInstance,
856 pagehash: this.get('pageHash')
859 this.autosaveIo(params, this, {
860 success: function(response) {
861 if (response === null) {
862 // This can happen when there is nothing to resume from.
864 } else if (!response) {
865 Y.log('Invalid response received.', 'debug', LOGNAME_AUTOSAVE);
869 // Revert untouched editor contents to an empty string.
870 // Check for FF and Chrome.
871 if (response.result === '<p></p>' || response.result === '<p><br></p>' ||
872 response.result === '<br>') {
873 response.result = '';
876 // Check for IE 9 and 10.
877 if (response.result === '<p> </p>' || response.result === '<p><br> </p>') {
878 response.result = '';
881 if (response.error || typeof response.result === 'undefined') {
882 Y.log('Error occurred recovering draft text: ' + response.error, 'debug', LOGNAME_AUTOSAVE);
883 this.showMessage(M.util.get_string('errortextrecovery', 'editor_atto'),
884 NOTIFY_WARNING, RECOVER_MESSAGE_TIMEOUT);
885 } else if (response.result !== this.textarea.get('value') &&
886 response.result !== '') {
887 Y.log('Autosave text found - recover it.', 'debug', LOGNAME_AUTOSAVE);
888 this.recoverText(response.result);
890 this._fireSelectionChanged();
893 failure: function() {
894 this.showMessage(M.util.get_string('errortextrecovery', 'editor_atto'),
895 NOTIFY_WARNING, RECOVER_MESSAGE_TIMEOUT);
899 // Now setup the timer for periodic saves.
900 var delay = parseInt(this.get('autosaveFrequency'), 10) * 1000;
901 this.autosaveTimer = Y.later(delay, this, this.saveDraft, false, true);
903 // Now setup the listener for form submission.
904 form = this.textarea.ancestor('form');
906 this.autosaveIoOnSubmit(form, {
908 contextid: this.get('contextid'),
909 elementid: this.get('elementid'),
910 pageinstance: this.autosaveInstance,
911 pagehash: this.get('pageHash')
918 * Recover a previous version of this text and show a message.
920 * @method recoverText
921 * @param {String} text
924 recoverText: function(text) {
925 this.editor.setHTML(text);
926 this.saveSelection();
927 this.updateOriginal();
928 this.lastText = text;
930 this.showMessage(M.util.get_string('textrecovered', 'editor_atto'),
931 NOTIFY_INFO, RECOVER_MESSAGE_TIMEOUT);
937 * Save a single draft via ajax.
942 saveDraft: function() {
945 if (!this.editor.getDOMNode()) {
946 // Stop autosaving if the editor was removed from the page.
947 this.autosaveTimer.cancel();
950 // Only copy the text from the div to the textarea if the textarea is not currently visible.
951 if (!this.editor.get('hidden')) {
952 this.updateOriginal();
954 var newText = this.textarea.get('value');
956 if (newText !== this.lastText) {
957 Y.log('Autosave text', 'debug', LOGNAME_AUTOSAVE);
959 // Make an ajax request.
960 url = M.cfg.wwwroot + this.get('autosaveAjaxScript');
962 sesskey: M.cfg.sesskey,
963 contextid: this.get('contextid'),
966 elementid: this.get('elementid'),
967 pagehash: this.get('pageHash'),
968 pageinstance: this.autosaveInstance
971 // Reusable error handler - must be passed the correct context.
972 var ajaxErrorFunction = function(response) {
973 var errorDuration = parseInt(this.get('autosaveFrequency'), 10) * 1000;
974 Y.log('Error while autosaving text', 'warn', LOGNAME_AUTOSAVE);
975 Y.log(response, 'warn', LOGNAME_AUTOSAVE);
976 this.showMessage(M.util.get_string('autosavefailed', 'editor_atto'), NOTIFY_WARNING, errorDuration);
979 this.autosaveIo(params, this, {
980 failure: ajaxErrorFunction,
981 success: function(response) {
982 if (response && response.error) {
983 Y.soon(Y.bind(ajaxErrorFunction, this, [response]));
986 this.lastText = newText;
987 this.showMessage(M.util.get_string('autosavesucceeded', 'editor_atto'),
988 NOTIFY_INFO, SUCCESS_MESSAGE_TIMEOUT);
997 Y.Base.mix(Y.M.editor_atto.Editor, [EditorAutosave]);
998 // This file is part of Moodle - http://moodle.org/
1000 // Moodle is free software: you can redistribute it and/or modify
1001 // it under the terms of the GNU General Public License as published by
1002 // the Free Software Foundation, either version 3 of the License, or
1003 // (at your option) any later version.
1005 // Moodle is distributed in the hope that it will be useful,
1006 // but WITHOUT ANY WARRANTY; without even the implied warranty of
1007 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1008 // GNU General Public License for more details.
1010 // You should have received a copy of the GNU General Public License
1011 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1014 * A autosave function for the Atto editor.
1016 * @module moodle-editor_atto-autosave-io
1017 * @submodule autosave-io
1018 * @package editor_atto
1019 * @copyright 2016 Frédéric Massart
1020 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1023 var EditorAutosaveIoDispatcherInstance = null;
1025 function EditorAutosaveIoDispatcher() {
1026 EditorAutosaveIoDispatcher.superclass.constructor.apply(this, arguments);
1027 this._submitEvents = {};
1029 this._throttle = null;
1031 EditorAutosaveIoDispatcher.NAME = 'EditorAutosaveIoDispatcher';
1032 EditorAutosaveIoDispatcher.ATTRS = {
1035 * The relative path to the ajax script.
1037 * @attribute autosaveAjaxScript
1039 * @default '/lib/editor/atto/autosave-ajax.php'
1042 autosaveAjaxScript: {
1043 value: '/lib/editor/atto/autosave-ajax.php',
1048 * The time buffer for the throttled requested.
1061 Y.extend(EditorAutosaveIoDispatcher, Y.Base, {
1064 * Dispatch an IO request.
1066 * This method will put the requests in a queue in order to attempt to bulk them.
1068 * @param {Object} params The parameters of the request.
1069 * @param {Object} context The context in which the callbacks are called.
1070 * @param {Object} callbacks Object with 'success', 'complete', 'end', 'failure' and 'start' as
1071 * optional keys defining the callbacks to call. Success and Complete
1072 * functions will receive the response as parameter. Success and Complete
1073 * may receive an object containing the error key, use this to confirm
1074 * that no errors occured.
1077 dispatch: function(params, context, callbacks) {
1078 if (this._throttle) {
1079 this._throttle.cancel();
1082 this._throttle = Y.later(this.get('delay'), this, this._processDispatchQueue);
1083 this._queue.push([params, context, callbacks]);
1087 * Dispatches the requests in the queue.
1091 _processDispatchQueue: function() {
1092 var queue = this._queue,
1096 if (queue.length < 1) {
1100 Y.Array.each(queue, function(item, index) {
1101 data[index] = item[0];
1104 Y.io(M.cfg.wwwroot + this.get('autosaveAjaxScript'), {
1106 data: Y.QueryString.stringify({
1108 sesskey: M.cfg.sesskey
1111 start: this._makeIoEventCallback('start', queue),
1112 complete: this._makeIoEventCallback('complete', queue),
1113 failure: this._makeIoEventCallback('failure', queue),
1114 end: this._makeIoEventCallback('end', queue),
1115 success: this._makeIoEventCallback('success', queue)
1121 * Creates a function that dispatches an IO response to callbacks.
1123 * @param {String} event The type of event.
1124 * @param {Array} queue The queue.
1125 * @return {Function}
1127 _makeIoEventCallback: function(event, queue) {
1128 var noop = function() {};
1130 var response = arguments[1],
1133 if ((event == 'complete' || event == 'success') && (typeof response !== 'undefined'
1134 && typeof response.responseText !== 'undefined' && response.responseText !== '')) {
1136 // Success and complete events need to parse the response.
1137 parsed = JSON.parse(response.responseText) || {};
1140 Y.Array.each(queue, function(item, index) {
1141 var context = item[1],
1142 cb = (item[2] && item[2][event]) || noop,
1145 if (parsed && parsed.error) {
1146 // The response is an error, we send it to everyone.
1148 } else if (parsed) {
1149 // The response was parsed, we only communicate the relevant portion of the response.
1150 arg = parsed[index];
1153 cb.apply(context, [arg]);
1159 * Form submit handler.
1161 * @param {EventFacade} e The event.
1164 _onSubmit: function(e) {
1166 id = e.currentTarget.generateID(),
1167 params = this._submitEvents[id];
1169 if (!params || params.ios.length < 1) {
1173 Y.Array.each(params.ios, function(param, index) {
1174 data[index] = param;
1177 Y.io(M.cfg.wwwroot + this.get('autosaveAjaxScript'), {
1179 data: Y.QueryString.stringify({
1181 sesskey: M.cfg.sesskey
1188 * Registers a request to be made on form submission.
1190 * @param {Node} node The forum node we will listen to.
1191 * @param {Object} params Parameters for the IO request.
1194 whenSubmit: function(node, params) {
1195 if (typeof this._submitEvents[node.generateID()] === 'undefined') {
1196 this._submitEvents[node.generateID()] = {
1197 event: node.on('submit', this._onSubmit, this),
1201 this._submitEvents[node.get('id')].ios.push([params]);
1205 EditorAutosaveIoDispatcherInstance = new EditorAutosaveIoDispatcher();
1208 function EditorAutosaveIo() {}
1209 EditorAutosaveIo.prototype = {
1212 * Dispatch an IO request.
1214 * This method will put the requests in a queue in order to attempt to bulk them.
1216 * @param {Object} params The parameters of the request.
1217 * @param {Object} context The context in which the callbacks are called.
1218 * @param {Object} callbacks Object with 'success', 'complete', 'end', 'failure' and 'start' as
1219 * optional keys defining the callbacks to call. Success and Complete
1220 * functions will receive the response as parameter. Success and Complete
1221 * may receive an object containing the error key, use this to confirm
1222 * that no errors occured.
1225 autosaveIo: function(params, context, callbacks) {
1226 EditorAutosaveIoDispatcherInstance.dispatch(params, context, callbacks);
1230 * Registers a request to be made on form submission.
1232 * @param {Node} form The forum node we will listen to.
1233 * @param {Object} params Parameters for the IO request.
1236 autosaveIoOnSubmit: function(form, params) {
1237 EditorAutosaveIoDispatcherInstance.whenSubmit(form, params);
1241 Y.Base.mix(Y.M.editor_atto.Editor, [EditorAutosaveIo]);
1242 // This file is part of Moodle - http://moodle.org/
1244 // Moodle is free software: you can redistribute it and/or modify
1245 // it under the terms of the GNU General Public License as published by
1246 // the Free Software Foundation, either version 3 of the License, or
1247 // (at your option) any later version.
1249 // Moodle is distributed in the hope that it will be useful,
1250 // but WITHOUT ANY WARRANTY; without even the implied warranty of
1251 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1252 // GNU General Public License for more details.
1254 // You should have received a copy of the GNU General Public License
1255 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1256 /* global LOGNAME */
1259 * @module moodle-editor_atto-editor
1264 * Functions for the Atto editor to clean the generated content.
1266 * See {{#crossLink "M.editor_atto.Editor"}}{{/crossLink}} for details.
1268 * @namespace M.editor_atto
1269 * @class EditorClean
1272 function EditorClean() {}
1274 EditorClean.ATTRS= {
1277 EditorClean.prototype = {
1279 * Clean the generated HTML content without modifying the editor content.
1281 * This includes removes all YUI ids from the generated content.
1283 * @return {string} The cleaned HTML content.
1285 getCleanHTML: function() {
1286 // Clone the editor so that we don't actually modify the real content.
1287 var editorClone = this.editor.cloneNode(true),
1290 // Remove all YUI IDs.
1291 Y.each(editorClone.all('[id^="yui"]'), function(node) {
1292 node.removeAttribute('id');
1295 editorClone.all('.atto_control').remove(true);
1296 html = editorClone.get('innerHTML');
1298 // Revert untouched editor contents to an empty string.
1299 if (html === '<p></p>' || html === '<p><br></p>') {
1303 // Remove any and all nasties from source.
1304 return this._cleanHTML(html);
1308 * Clean the HTML content of the editor.
1310 * @method cleanEditorHTML
1313 cleanEditorHTML: function() {
1314 var startValue = this.editor.get('innerHTML');
1315 this.editor.set('innerHTML', this._cleanHTML(startValue));
1321 * Clean the specified HTML content and remove any content which could cause issues.
1323 * @method _cleanHTML
1325 * @param {String} content The content to clean
1326 * @return {String} The cleaned HTML
1328 _cleanHTML: function(content) {
1329 // Removing limited things that can break the page or a disallowed, like unclosed comments, style blocks, etc.
1332 // Remove any style blocks. Some browsers do not work well with them in a contenteditable.
1333 // Plus style blocks are not allowed in body html, except with "scoped", which most browsers don't support as of 2015.
1334 // Reference: "http://stackoverflow.com/questions/1068280/javascript-regex-multiline-flag-doesnt-work"
1335 {regex: /<style[^>]*>[\s\S]*?<\/style>/gi, replace: ""},
1337 // Remove any open HTML comment opens that are not followed by a close. This can completely break page layout.
1338 {regex: /<!--(?![\s\S]*?-->)/gi, replace: ""},
1340 // Source: "http://www.codinghorror.com/blog/2006/01/cleaning-words-nasty-html.html"
1341 // Remove forbidden tags for content, title, meta, style, st0-9, head, font, html, body, link.
1342 {regex: /<\/?(?:title|meta|style|st\d|head|font|html|body|link)[^>]*?>/gi, replace: ""}
1345 return this._filterContentWithRules(content, rules);
1349 * Take the supplied content and run on the supplied regex rules.
1351 * @method _filterContentWithRules
1353 * @param {String} content The content to clean
1354 * @param {Array} rules An array of structures: [ {regex: /something/, replace: "something"}, {...}, ...]
1355 * @return {String} The cleaned content
1357 _filterContentWithRules: function(content, rules) {
1359 for (i = 0; i < rules.length; i++) {
1360 content = content.replace(rules[i].regex, rules[i].replace);
1367 * Intercept and clean html paste events.
1369 * @method pasteCleanup
1370 * @param {Object} sourceEvent The YUI EventFacade object
1371 * @return {Boolean} True if the passed event should continue, false if not.
1373 pasteCleanup: function(sourceEvent) {
1374 // We only expect paste events, but we will check anyways.
1375 if (sourceEvent.type === 'paste') {
1376 // The YUI event wrapper doesn't provide paste event info, so we need the underlying event.
1377 var event = sourceEvent._event;
1378 // Check if we have a valid clipboardData object in the event.
1379 // IE has a clipboard object at window.clipboardData, but as of IE 11, it does not provide HTML content access.
1380 if (event && event.clipboardData && event.clipboardData.getData && event.clipboardData.types) {
1381 // Check if there is HTML type to be pasted, if we can get it, we want to scrub before insert.
1382 var types = event.clipboardData.types;
1384 // Different browsers use different containers to hold the types, so test various functions.
1385 if (typeof types.contains === 'function') {
1386 isHTML = types.contains('text/html');
1387 } else if (typeof types.indexOf === 'function') {
1388 isHTML = (types.indexOf('text/html') > -1);
1392 // Get the clipboard content.
1395 content = event.clipboardData.getData('text/html');
1397 // Something went wrong. Fallback.
1398 this.fallbackPasteCleanupDelayed();
1402 // Stop the original paste.
1403 sourceEvent.preventDefault();
1405 // Scrub the paste content.
1406 content = this._cleanPasteHTML(content);
1408 // Save the current selection.
1409 // Using saveSelection as it produces a more consistent experience.
1410 var selection = window.rangy.saveSelection();
1412 // Insert the content.
1413 this.insertContentAtFocusPoint(content);
1415 // Restore the selection, and collapse to end.
1416 window.rangy.restoreSelection(selection);
1417 window.rangy.getSelection().collapseToEnd();
1419 // Update the text area.
1420 this.updateOriginal();
1423 // Due to poor cross browser clipboard compatibility, the failure to find html doesn't mean it isn't there.
1424 // Wait for the clipboard event to finish then fallback clean the entire editor.
1425 this.fallbackPasteCleanupDelayed();
1429 // If we reached a here, this probably means the browser has limited (or no) clipboard support.
1430 // Wait for the clipboard event to finish then fallback clean the entire editor.
1431 this.fallbackPasteCleanupDelayed();
1436 // We should never get here - we must have received a non-paste event for some reason.
1437 // Um, just call updateOriginalDelayed() - it's safe.
1438 this.updateOriginalDelayed();
1443 * Cleanup code after a paste event if we couldn't intercept the paste content.
1445 * @method fallbackPasteCleanup
1448 fallbackPasteCleanup: function() {
1449 Y.log('Using fallbackPasteCleanup for atto cleanup', 'debug', LOGNAME);
1451 // Save the current selection (cursor position).
1452 var selection = window.rangy.saveSelection();
1454 // Get, clean, and replace the content in the editable.
1455 var content = this.editor.get('innerHTML');
1456 this.editor.set('innerHTML', this._cleanPasteHTML(content));
1458 // Update the textarea.
1459 this.updateOriginal();
1461 // Restore the selection (cursor position).
1462 window.rangy.restoreSelection(selection);
1468 * Calls fallbackPasteCleanup on a short timer to allow the paste event handlers to complete.
1470 * @method fallbackPasteCleanupDelayed
1473 fallbackPasteCleanupDelayed: function() {
1474 Y.soon(Y.bind(this.fallbackPasteCleanup, this));
1480 * Cleanup html that comes from WYSIWYG paste events. These are more likely to contain messy code that we should strip.
1482 * @method _cleanPasteHTML
1484 * @param {String} content The html content to clean
1485 * @return {String} The cleaned HTML
1487 _cleanPasteHTML: function(content) {
1488 // Return an empty string if passed an invalid or empty object.
1489 if (!content || content.length === 0) {
1493 // Rules that get rid of the real-nasties and don't care about normalize code (correct quotes, white spaces, etc).
1495 // Stuff that is specifically from MS Word and similar office packages.
1496 // Remove all garbage after closing html tag.
1497 {regex: /<\s*\/html\s*>([\s\S]+)$/gi, replace: ""},
1498 // Remove if comment blocks.
1499 {regex: /<!--\[if[\s\S]*?endif\]-->/gi, replace: ""},
1500 // Remove start and end fragment comment blocks.
1501 {regex: /<!--(Start|End)Fragment-->/gi, replace: ""},
1502 // Remove any xml blocks.
1503 {regex: /<xml[^>]*>[\s\S]*?<\/xml>/gi, replace: ""},
1504 // Remove any <?xml><\?xml> blocks.
1505 {regex: /<\?xml[^>]*>[\s\S]*?<\\\?xml>/gi, replace: ""},
1506 // Remove <o:blah>, <\o:blah>.
1507 {regex: /<\/?\w+:[^>]*>/gi, replace: ""}
1510 // Apply the first set of harsher rules.
1511 content = this._filterContentWithRules(content, rules);
1513 // Apply the standard rules, which mainly cleans things like headers, links, and style blocks.
1514 content = this._cleanHTML(content);
1516 // Check if the string is empty or only contains whitespace.
1517 if (content.length === 0 || !content.match(/\S/)) {
1521 // Now we let the browser normalize the code by loading it into the DOM and then get the html back.
1522 // This gives us well quoted, well formatted code to continue our work on. Word may provide very poorly formatted code.
1523 var holder = document.createElement('div');
1524 holder.innerHTML = content;
1525 content = holder.innerHTML;
1526 // Free up the DOM memory.
1527 holder.innerHTML = "";
1529 // Run some more rules that care about quotes and whitespace.
1531 // Get all class attributes so we can work on them.
1532 {regex: /(<[^>]*?class\s*?=\s*?")([^>"]*)(")/gi, replace: function(match, group1, group2, group3) {
1533 // Remove MSO classes.
1534 group2 = group2.replace(/(?:^|[\s])[\s]*MSO[_a-zA-Z0-9\-]*/gi,"");
1535 // Remove Apple- classes.
1536 group2 = group2.replace(/(?:^|[\s])[\s]*Apple-[_a-zA-Z0-9\-]*/gi,"");
1537 return group1 + group2 + group3;
1539 // Remove OLE_LINK# anchors that may litter the code.
1540 {regex: /<a [^>]*?name\s*?=\s*?"OLE_LINK\d*?"[^>]*?>\s*?<\/a>/gi, replace: ""}
1543 // Clean all style attributes from the text.
1544 content = this._cleanStyles(content);
1547 content = this._filterContentWithRules(content, rules);
1549 // Reapply the standard cleaner to the content.
1550 content = this._cleanHTML(content);
1552 // Clean unused spans out of the content.
1553 content = this._cleanSpans(content);
1559 * Clean all inline styles from pasted text.
1561 * This code intentionally doesn't use YUI Nodes. YUI was quite a bit slower at this, so using raw DOM objects instead.
1563 * @method _cleanStyles
1565 * @param {String} content The content to clean
1566 * @return {String} The cleaned HTML
1568 _cleanStyles: function(content) {
1569 var holder = document.createElement('div');
1570 holder.innerHTML = content;
1571 var elementsWithStyle = holder.querySelectorAll('[style]');
1574 for (i = 0; i < elementsWithStyle.length; i++) {
1575 elementsWithStyle[i].removeAttribute('style');
1578 var elementsWithClass = holder.querySelectorAll('[class]');
1579 for (i = 0; i < elementsWithClass.length; i++) {
1580 elementsWithClass[i].removeAttribute('class');
1583 return holder.innerHTML;
1586 * Clean empty or un-unused spans from passed HTML.
1588 * This code intentionally doesn't use YUI Nodes. YUI was quite a bit slower at this, so using raw DOM objects instead.
1590 * @method _cleanSpans
1592 * @param {String} content The content to clean
1593 * @return {String} The cleaned HTML
1595 _cleanSpans: function(content) {
1596 // Return an empty string if passed an invalid or empty object.
1597 if (!content || content.length === 0) {
1600 // Check if the string is empty or only contains whitespace.
1601 if (content.length === 0 || !content.match(/\S/)) {
1606 // Remove unused class, style, or id attributes. This will make empty tag detection easier later.
1607 {regex: /(<[^>]*?)(?:[\s]*(?:class|style|id)\s*?=\s*?"\s*?")+/gi, replace: "$1"}
1610 content = this._filterContentWithRules(content, rules);
1612 // Reference: "http://stackoverflow.com/questions/8131396/remove-nested-span-without-id"
1614 // This is better to run detached from the DOM, so the browser doesn't try to update on each change.
1615 var holder = document.createElement('div');
1616 holder.innerHTML = content;
1617 var spans = holder.getElementsByTagName('span');
1619 // Since we will be removing elements from the list, we should copy it to an array, making it static.
1620 var spansarr = Array.prototype.slice.call(spans, 0);
1622 spansarr.forEach(function(span) {
1623 if (!span.hasAttributes()) {
1624 // If no attributes (id, class, style, etc), this span is has no effect.
1625 // Move each child (if they exist) to the parent in place of this span.
1626 while (span.firstChild) {
1627 span.parentNode.insertBefore(span.firstChild, span);
1630 // Remove the now empty span.
1631 span.parentNode.removeChild(span);
1635 return holder.innerHTML;
1639 Y.Base.mix(Y.M.editor_atto.Editor, [EditorClean]);
1640 // This file is part of Moodle - http://moodle.org/
1642 // Moodle is free software: you can redistribute it and/or modify
1643 // it under the terms of the GNU General Public License as published by
1644 // the Free Software Foundation, either version 3 of the License, or
1645 // (at your option) any later version.
1647 // Moodle is distributed in the hope that it will be useful,
1648 // but WITHOUT ANY WARRANTY; without even the implied warranty of
1649 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1650 // GNU General Public License for more details.
1652 // You should have received a copy of the GNU General Public License
1653 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1656 * @module moodle-editor_atto-editor
1657 * @submodule commands
1661 * Selection functions for the Atto editor.
1663 * See {{#crossLink "M.editor_atto.Editor"}}{{/crossLink}} for details.
1665 * @namespace M.editor_atto
1666 * @class EditorCommand
1669 function EditorCommand() {}
1671 EditorCommand.ATTRS= {
1674 EditorCommand.prototype = {
1676 * Applies a callback method to editor if selection is uncollapsed or waits for input to select first.
1677 * @method applyFormat
1678 * @param e EventTarget Event to be passed to callback if selection is uncollapsed
1679 * @param method callback A callback method which changes editor when text is selected.
1680 * @param object context Context to be used for callback method
1681 * @param array args Array of arguments to pass to callback
1683 applyFormat: function(e, callback, context, args) {
1684 function handleInsert(e, callback, context, args, anchorNode, anchorOffset) {
1685 // After something is inputed, select it and apply the formating function.
1686 Y.soon(Y.bind(function(e, callback, context, args, anchorNode, anchorOffset) {
1687 var selection = window.rangy.getSelection();
1689 // Set the start of the selection to where it was when the method was first called.
1690 var range = selection.getRangeAt(0);
1691 range.setStart(anchorNode, anchorOffset);
1692 selection.setSingleRange(range);
1694 // Now apply callback to the new text that is selected.
1695 callback.apply(context, [e, args]);
1697 // Collapse selection so cursor is at end of inserted material.
1698 selection.collapseToEnd();
1700 // Save save selection and editor contents.
1701 this.saveSelection();
1702 this.updateOriginal();
1703 }, this, e, callback, context, args, anchorNode, anchorOffset));
1706 // Set default context for the method.
1707 context = context || this;
1709 // Check whether range is collapsed.
1710 var selection = window.rangy.getSelection();
1712 if (selection.isCollapsed) {
1713 // Selection is collapsed so listen for input into editor.
1714 var handle = this.editor.once('input', handleInsert, this, callback, context, args,
1715 selection.anchorNode, selection.anchorOffset);
1717 // Cancel if selection changes before input.
1718 this.editor.onceAfter(['click', 'selectstart'], handle.detach, handle);
1723 // The range is not collapsed; so apply callback method immediately.
1724 callback.apply(context, [e, args]);
1726 // Save save selection and editor contents.
1727 this.saveSelection();
1728 this.updateOriginal();
1732 * Replaces all the tags in a node list with new type.
1733 * @method replaceTags
1734 * @param NodeList nodelist
1737 replaceTags: function(nodelist, tag) {
1738 // We mark elements in the node list for iterations.
1739 nodelist.setAttribute('data-iterate', true);
1740 var node = this.editor.one('[data-iterate="true"]');
1742 var clone = Y.Node.create('<' + tag + ' />')
1743 .setAttrs(node.getAttrs())
1744 .removeAttribute('data-iterate');
1745 // Copy class and style if not blank.
1746 if (node.getAttribute('style')) {
1747 clone.setAttribute('style', node.getAttribute('style'));
1749 if (node.getAttribute('class')) {
1750 clone.setAttribute('class', node.getAttribute('class'));
1752 // We use childNodes here because we are interested in both type 1 and 3 child nodes.
1753 var children = node.getDOMNode().childNodes, child;
1754 child = children[0];
1755 while (typeof child !== "undefined") {
1756 clone.append(child);
1757 child = children[0];
1759 node.replace(clone);
1760 node = this.editor.one('[data-iterate="true"]');
1765 * Change all tags with given type to a span with CSS class attribute.
1766 * @method changeToCSS
1767 * @param String tag Tag type to be changed to span
1768 * @param String markerClass CSS class that corresponds to desired tag
1770 changeToCSS: function(tag, markerClass) {
1771 // Save the selection.
1772 var selection = window.rangy.saveSelection();
1774 // Remove display:none from rangy markers so browser doesn't delete them.
1775 this.editor.all('.rangySelectionBoundary').setStyle('display', null);
1777 // Replace tags with CSS classes.
1778 this.editor.all(tag).addClass(markerClass);
1779 this.replaceTags(this.editor.all('.' + markerClass), 'span');
1781 // Restore selection and toggle class.
1782 window.rangy.restoreSelection(selection);
1786 * Change spans with CSS classes in editor into elements with given tag.
1787 * @method changeToCSS
1788 * @param String markerClass CSS class that corresponds to desired tag
1789 * @param String tag New tag type to be created
1791 changeToTags: function(markerClass, tag) {
1792 // Save the selection.
1793 var selection = window.rangy.saveSelection();
1795 // Remove display:none from rangy markers so browser doesn't delete them.
1796 this.editor.all('.rangySelectionBoundary').setStyle('display', null);
1798 // Replace spans with given tag.
1799 this.replaceTags(this.editor.all('span[class="' + markerClass + '"]'), tag);
1800 this.editor.all(tag + '[class="' + markerClass + '"]').removeAttribute('class');
1801 this.editor.all('.' + markerClass).each(function(n) {
1802 n.wrap('<' + tag + '/>');
1803 n.removeClass(markerClass);
1806 // Remove CSS classes.
1807 this.editor.all('[class="' + markerClass + '"]').removeAttribute('class');
1808 this.editor.all(tag).removeClass(markerClass);
1810 // Restore selection.
1811 window.rangy.restoreSelection(selection);
1815 Y.Base.mix(Y.M.editor_atto.Editor, [EditorCommand]);
1816 // This file is part of Moodle - http://moodle.org/
1818 // Moodle is free software: you can redistribute it and/or modify
1819 // it under the terms of the GNU General Public License as published by
1820 // the Free Software Foundation, either version 3 of the License, or
1821 // (at your option) any later version.
1823 // Moodle is distributed in the hope that it will be useful,
1824 // but WITHOUT ANY WARRANTY; without even the implied warranty of
1825 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1826 // GNU General Public License for more details.
1828 // You should have received a copy of the GNU General Public License
1829 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1832 * @module moodle-editor_atto-editor
1833 * @submodule toolbar
1837 * Toolbar functions for the Atto editor.
1839 * See {{#crossLink "M.editor_atto.Editor"}}{{/crossLink}} for details.
1841 * @namespace M.editor_atto
1842 * @class EditorToolbar
1845 function EditorToolbar() {}
1847 EditorToolbar.ATTRS= {
1850 EditorToolbar.prototype = {
1852 * A reference to the toolbar Node.
1860 * A reference to any currently open menus in the toolbar.
1862 * @property openMenus
1868 * Setup the toolbar on the editor.
1870 * @method setupToolbar
1873 setupToolbar: function() {
1874 this.toolbar = Y.Node.create('<div class="' + CSS.TOOLBAR + '" role="toolbar" aria-live="off"/>');
1875 this.openMenus = [];
1876 this._wrapper.appendChild(this.toolbar);
1878 if (this.textareaLabel) {
1879 this.toolbar.setAttribute('aria-labelledby', this.textareaLabel.get("id"));
1882 // Add keyboard navigation for the toolbar.
1883 this.setupToolbarNavigation();
1889 Y.Base.mix(Y.M.editor_atto.Editor, [EditorToolbar]);
1890 // This file is part of Moodle - http://moodle.org/
1892 // Moodle is free software: you can redistribute it and/or modify
1893 // it under the terms of the GNU General Public License as published by
1894 // the Free Software Foundation, either version 3 of the License, or
1895 // (at your option) any later version.
1897 // Moodle is distributed in the hope that it will be useful,
1898 // but WITHOUT ANY WARRANTY; without even the implied warranty of
1899 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1900 // GNU General Public License for more details.
1902 // You should have received a copy of the GNU General Public License
1903 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1904 /* global LOGNAME */
1907 * @module moodle-editor_atto-editor
1908 * @submodule toolbarnav
1912 * Toolbar Navigation functions for the Atto editor.
1914 * See {{#crossLink "M.editor_atto.Editor"}}{{/crossLink}} for details.
1916 * @namespace M.editor_atto
1917 * @class EditorToolbarNav
1920 function EditorToolbarNav() {}
1922 EditorToolbarNav.ATTRS= {
1925 EditorToolbarNav.prototype = {
1927 * The current focal point for tabbing.
1929 * @property _tabFocus
1937 * Set up the watchers for toolbar navigation.
1939 * @method setupToolbarNavigation
1942 setupToolbarNavigation: function() {
1943 // Listen for Arrow left and Arrow right keys.
1944 this._wrapper.delegate('key',
1945 this.toolbarKeyboardNavigation,
1949 this._wrapper.delegate('focus',
1951 this._setTabFocus(e.currentTarget);
1952 }, '.' + CSS.TOOLBAR + ' button', this);
1958 * Implement arrow key navigation for the buttons in the toolbar.
1960 * @method toolbarKeyboardNavigation
1961 * @param {EventFacade} e - the keyboard event.
1963 toolbarKeyboardNavigation: function(e) {
1964 // Prevent the default browser behaviour.
1967 // On cursor moves we loops through the buttons.
1968 var buttons = this.toolbar.all('button'),
1971 current = e.target.ancestor('button', true);
1973 if (e.keyCode === 37) {
1974 // Moving left so reverse the direction.
1978 button = this._findFirstFocusable(buttons, current, direction);
1981 this._setTabFocus(button);
1983 Y.log("Unable to find a button to focus on", 'debug', LOGNAME);
1988 * Find the first focusable button.
1990 * @param {NodeList} buttons A list of nodes.
1991 * @param {Node} startAt The node in the list to start the search from.
1992 * @param {Number} direction The direction in which to search (1 or -1).
1993 * @return {Node | Undefined} The Node or undefined.
1994 * @method _findFirstFocusable
1997 _findFirstFocusable: function(buttons, startAt, direction) {
2004 // Determine which button to start the search from.
2005 index = buttons.indexOf(startAt);
2007 Y.log("Unable to find the button in the list of buttons", 'debug', LOGNAME);
2011 // Try to find the next.
2012 while (checkCount < buttons.size()) {
2015 index = buttons.size() - 1;
2016 } else if (index >= buttons.size()) {
2021 candidate = buttons.item(index);
2023 // Add a counter to ensure we don't get stuck in a loop if there's only one visible menu item.
2027 // * we haven't checked every button;
2028 // * the button is hidden or disabled;
2029 // * the group is hidden.
2030 if (candidate.hasAttribute('hidden') || candidate.hasAttribute('disabled')) {
2033 group = candidate.ancestor('.atto_group');
2034 if (group.hasAttribute('hidden')) {
2046 * Check the tab focus.
2048 * When we disable or hide a button, we should call this method to ensure that the
2049 * focus is not currently set on an inaccessible button, otherwise tabbing to the toolbar
2050 * would be impossible.
2052 * @method checkTabFocus
2055 checkTabFocus: function() {
2056 if (this._tabFocus) {
2057 if (this._tabFocus.hasAttribute('disabled') || this._tabFocus.hasAttribute('hidden')
2058 || this._tabFocus.ancestor('.atto_group').hasAttribute('hidden')) {
2059 // Find first available button.
2060 var button = this._findFirstFocusable(this.toolbar.all('button'), this._tabFocus, -1);
2062 if (this._tabFocus.compareTo(document.activeElement)) {
2063 // We should also move the focus, because the inaccessible button also has the focus.
2066 this._setTabFocus(button);
2074 * Sets tab focus for the toolbar to the specified Node.
2076 * @method _setTabFocus
2077 * @param {Node} button The node that focus should now be set to
2081 _setTabFocus: function(button) {
2082 if (this._tabFocus) {
2083 // Unset the previous entry.
2084 this._tabFocus.setAttribute('tabindex', '-1');
2087 // Set up the new entry.
2088 this._tabFocus = button;
2089 this._tabFocus.setAttribute('tabindex', 0);
2091 // And update the activedescendant to point at the currently selected button.
2092 this.toolbar.setAttribute('aria-activedescendant', this._tabFocus.generateID());
2098 Y.Base.mix(Y.M.editor_atto.Editor, [EditorToolbarNav]);
2099 // This file is part of Moodle - http://moodle.org/
2101 // Moodle is free software: you can redistribute it and/or modify
2102 // it under the terms of the GNU General Public License as published by
2103 // the Free Software Foundation, either version 3 of the License, or
2104 // (at your option) any later version.
2106 // Moodle is distributed in the hope that it will be useful,
2107 // but WITHOUT ANY WARRANTY; without even the implied warranty of
2108 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2109 // GNU General Public License for more details.
2111 // You should have received a copy of the GNU General Public License
2112 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
2116 * @module moodle-editor_atto-editor
2117 * @submodule selection
2121 * Selection functions for the Atto editor.
2123 * See {{#crossLink "M.editor_atto.Editor"}}{{/crossLink}} for details.
2125 * @namespace M.editor_atto
2126 * @class EditorSelection
2129 function EditorSelection() {}
2131 EditorSelection.ATTRS= {
2134 EditorSelection.prototype = {
2137 * List of saved selections per editor instance.
2139 * @property _selections
2145 * A unique identifier for the last selection recorded.
2147 * @property _lastSelection
2148 * @param lastselection
2152 _lastSelection: null,
2155 * Whether focus came from a click event.
2157 * This is used to determine whether to restore the selection or not.
2159 * @property _focusFromClick
2164 _focusFromClick: false,
2167 * Whether if the last gesturemovestart event target was contained in this editor or not.
2169 * @property _gesturestartededitor
2174 _gesturestartededitor: false,
2177 * Set up the watchers for selection save and restoration.
2179 * @method setupSelectionWatchers
2182 setupSelectionWatchers: function() {
2183 // Save the selection when a change was made.
2184 this.on('atto:selectionchanged', this.saveSelection, this);
2186 this.editor.on('focus', this.restoreSelection, this);
2188 // Do not restore selection when focus is from a click event.
2189 this.editor.on('mousedown', function() {
2190 this._focusFromClick = true;
2193 // Copy the current value back to the textarea when focus leaves us and save the current selection.
2194 this.editor.on('blur', function() {
2195 // Clear the _focusFromClick value.
2196 this._focusFromClick = false;
2198 // Update the original text area.
2199 this.updateOriginal();
2202 this.editor.on(['keyup', 'focus'], function(e) {
2203 Y.soon(Y.bind(this._hasSelectionChanged, this, e));
2206 Y.one(document.body).on('gesturemovestart', function(e) {
2207 if (this._wrapper.contains(e.target._node)) {
2208 this._gesturestartededitor = true;
2210 this._gesturestartededitor = false;
2214 Y.one(document.body).on('gesturemoveend', function(e) {
2215 if (!this._gesturestartededitor) {
2216 // Ignore the event if movestart target was not contained in the editor.
2219 Y.soon(Y.bind(this._hasSelectionChanged, this, e));
2221 // Standalone will make sure all editors receive the end event.
2229 * Work out if the cursor is in the editable area for this editor instance.
2234 isActive: function() {
2235 var range = rangy.createRange(),
2236 selection = rangy.getSelection();
2238 if (!selection.rangeCount) {
2239 // If there was no range count, then there is no selection.
2243 // We can't be active if the editor doesn't have focus at the moment.
2244 if (!document.activeElement ||
2245 !(this.editor.compareTo(document.activeElement) || this.editor.contains(document.activeElement))) {
2249 // Check whether the range intersects the editor selection.
2250 range.selectNode(this.editor.getDOMNode());
2251 return range.intersectsRange(selection.getRangeAt(0));
2255 * Create a cross browser selection object that represents a YUI node.
2257 * @method getSelectionFromNode
2258 * @param {Node} YUI Node to base the selection upon.
2259 * @return {[rangy.Range]}
2261 getSelectionFromNode: function(node) {
2262 var range = rangy.createRange();
2263 range.selectNode(node.getDOMNode());
2268 * Save the current selection to an internal property.
2270 * This allows more reliable return focus, helping improve keyboard navigation.
2272 * Should be used in combination with {{#crossLink "M.editor_atto.EditorSelection/restoreSelection"}}{{/crossLink}}.
2274 * @method saveSelection
2276 saveSelection: function() {
2277 if (this.isActive()) {
2278 this._selections = this.getSelection();
2283 * Restore any stored selection when the editor gets focus again.
2285 * Should be used in combination with {{#crossLink "M.editor_atto.EditorSelection/saveSelection"}}{{/crossLink}}.
2287 * @method restoreSelection
2289 restoreSelection: function() {
2290 if (!this._focusFromClick) {
2291 if (this._selections) {
2292 this.setSelection(this._selections);
2295 this._focusFromClick = false;
2299 * Get the selection object that can be passed back to setSelection.
2301 * @method getSelection
2302 * @return {array} An array of rangy ranges.
2304 getSelection: function() {
2305 return rangy.getSelection().getAllRanges();
2309 * Check that a YUI node it at least partly contained by the current selection.
2311 * @method selectionContainsNode
2312 * @param {Node} The node to check.
2315 selectionContainsNode: function(node) {
2316 return rangy.getSelection().containsNode(node.getDOMNode(), true);
2320 * Runs a filter on each node in the selection, and report whether the
2321 * supplied selector(s) were found in the supplied Nodes.
2323 * By default, all specified nodes must match the selection, but this
2324 * can be controlled with the requireall property.
2326 * @method selectionFilterMatches
2327 * @param {String} selector
2328 * @param {NodeList} [selectednodes] For performance this should be passed. If not passed, this will be looked up each time.
2329 * @param {Boolean} [requireall=true] Used to specify that "any" match is good enough.
2332 selectionFilterMatches: function(selector, selectednodes, requireall) {
2333 if (typeof requireall === 'undefined') {
2336 if (!selectednodes) {
2337 // Find this because it was not passed as a param.
2338 selectednodes = this.getSelectedNodes();
2340 var allmatch = selectednodes.size() > 0,
2343 var editor = this.editor,
2344 stopFn = function(node) {
2345 // The function getSelectedNodes only returns nodes within the editor, so this test is safe.
2346 return node === editor;
2349 // If we do not find at least one match in the editor, no point trying to find them in the selection.
2350 if (!editor.one(selector)) {
2354 selectednodes.each(function(node){
2355 // Check each node, if it doesn't match the tags AND is not within the specified tags then fail this thing.
2357 // Check for at least one failure.
2358 if (!allmatch || !node.ancestor(selector, true, stopFn)) {
2362 // Check for at least one match.
2363 if (!anymatch && node.ancestor(selector, true, stopFn)) {
2376 * Get the deepest possible list of nodes in the current selection.
2378 * @method getSelectedNodes
2379 * @return {NodeList}
2381 getSelectedNodes: function() {
2382 var results = new Y.NodeList(),
2389 selection = rangy.getSelection();
2391 if (selection.rangeCount) {
2392 range = selection.getRangeAt(0);
2395 range = rangy.createRange();
2398 if (range.collapsed) {
2399 // We do not want to select all the nodes in the editor if we managed to
2400 // have a collapsed selection directly in the editor.
2401 // It's also possible for the commonAncestorContainer to be the document, which selectNode does not handle
2402 // so we must filter that out here too.
2403 if (range.commonAncestorContainer !== this.editor.getDOMNode()
2404 && range.commonAncestorContainer !== Y.config.doc) {
2405 range = range.cloneRange();
2406 range.selectNode(range.commonAncestorContainer);
2410 nodes = range.getNodes();
2412 for (i = 0; i < nodes.length; i++) {
2413 node = Y.one(nodes[i]);
2414 if (this.editor.contains(node)) {
2422 * Check whether the current selection has changed since this method was last called.
2424 * If the selection has changed, the atto:selectionchanged event is also fired.
2426 * @method _hasSelectionChanged
2428 * @param {EventFacade} e
2431 _hasSelectionChanged: function(e) {
2432 var selection = rangy.getSelection(),
2436 if (selection.rangeCount) {
2437 range = selection.getRangeAt(0);
2440 range = rangy.createRange();
2443 if (this._lastSelection) {
2444 if (!this._lastSelection.equals(range)) {
2446 return this._fireSelectionChanged(e);
2449 this._lastSelection = range;
2454 * Fires the atto:selectionchanged event.
2456 * When the selectionchanged event is fired, the following arguments are provided:
2457 * - event : the original event that lead to this event being fired.
2458 * - selectednodes : an array containing nodes that are entirely selected of contain partially selected content.
2460 * @method _fireSelectionChanged
2462 * @param {EventFacade} e
2464 _fireSelectionChanged: function(e) {
2465 this.fire('atto:selectionchanged', {
2467 selectedNodes: this.getSelectedNodes()
2472 * Get the DOM node representing the common anscestor of the selection nodes.
2474 * @method getSelectionParentNode
2475 * @return {Element|boolean} The DOM Node for this parent, or false if no seletion was made.
2477 getSelectionParentNode: function() {
2478 var selection = rangy.getSelection();
2479 if (selection.rangeCount) {
2480 return selection.getRangeAt(0).commonAncestorContainer;
2486 * Set the current selection. Used to restore a selection.
2489 * @param {array} ranges A list of rangy.range objects in the selection.
2491 setSelection: function(ranges) {
2492 var selection = rangy.getSelection();
2493 selection.setRanges(ranges);
2497 * Inserts the given HTML into the editable content at the currently focused point.
2499 * @method insertContentAtFocusPoint
2500 * @param {String} html
2501 * @return {Node} The YUI Node object added to the DOM.
2503 insertContentAtFocusPoint: function(html) {
2504 var selection = rangy.getSelection(),
2506 node = Y.Node.create(html);
2507 if (selection.rangeCount) {
2508 range = selection.getRangeAt(0);
2511 range.deleteContents();
2512 range.insertNode(node.getDOMNode());
2519 Y.Base.mix(Y.M.editor_atto.Editor, [EditorSelection]);
2520 // This file is part of Moodle - http://moodle.org/
2522 // Moodle is free software: you can redistribute it and/or modify
2523 // it under the terms of the GNU General Public License as published by
2524 // the Free Software Foundation, either version 3 of the License, or
2525 // (at your option) any later version.
2527 // Moodle is distributed in the hope that it will be useful,
2528 // but WITHOUT ANY WARRANTY; without even the implied warranty of
2529 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2530 // GNU General Public License for more details.
2532 // You should have received a copy of the GNU General Public License
2533 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
2537 * @module moodle-editor_atto-editor
2538 * @submodule styling
2542 * Editor styling functions for the Atto editor.
2544 * See {{#crossLink "M.editor_atto.Editor"}}{{/crossLink}} for details.
2546 * @namespace M.editor_atto
2547 * @class EditorStyling
2550 function EditorStyling() {}
2552 EditorStyling.ATTRS= {
2555 EditorStyling.prototype = {
2557 * Disable CSS styling.
2559 * @method disableCssStyling
2561 disableCssStyling: function() {
2563 document.execCommand("styleWithCSS", 0, false);
2566 document.execCommand("useCSS", 0, true);
2569 document.execCommand('styleWithCSS', false, false);
2578 * Enable CSS styling.
2580 * @method enableCssStyling
2582 enableCssStyling: function() {
2584 document.execCommand("styleWithCSS", 0, true);
2587 document.execCommand("useCSS", 0, false);
2590 document.execCommand('styleWithCSS', false, true);
2599 * Change the formatting for the current selection.
2601 * This will wrap the selection in span tags, adding the provided classes.
2603 * If the selection covers multiple block elements, multiple spans will be inserted to preserve the original structure.
2605 * @method toggleInlineSelectionClass
2606 * @param {Array} toggleclasses - Class names to be toggled on or off.
2608 toggleInlineSelectionClass: function(toggleclasses) {
2609 var classname = toggleclasses.join(" ");
2610 var cssApplier = rangy.createClassApplier(classname, {normalize: true});
2612 cssApplier.toggleSelection();
2616 * Change the formatting for the current selection.
2618 * This will set inline styles on the current selection.
2620 * @method formatSelectionInlineStyle
2621 * @param {Array} styles - Style attributes to set on the nodes.
2623 formatSelectionInlineStyle: function(styles) {
2624 var classname = this.PLACEHOLDER_CLASS;
2625 var cssApplier = rangy.createClassApplier(classname, {normalize: true});
2627 cssApplier.applyToSelection();
2629 this.editor.all('.' + classname).each(function (node) {
2630 node.removeClass(classname).setStyles(styles);
2636 * Change the formatting for the current selection.
2638 * Also changes the selection to the newly formatted block (allows applying multiple styles to a block).
2640 * @method formatSelectionBlock
2641 * @param {String} [blocktag] Change the block level tag to this. Empty string, means do not change the tag.
2642 * @param {Object} [attributes] The keys and values for attributes to be added/changed in the block tag.
2643 * @return {Node|boolean} The Node that was formatted if a change was made, otherwise false.
2645 formatSelectionBlock: function(blocktag, attributes) {
2646 // First find the nearest ancestor of the selection that is a block level element.
2647 var selectionparentnode = this.getSelectionParentNode(),
2655 if (!selectionparentnode) {
2656 // No selection, nothing to format.
2660 boundary = this.editor;
2662 selectionparentnode = Y.one(selectionparentnode);
2664 // If there is a table cell in between the selectionparentnode and the boundary,
2665 // move the boundary to the table cell.
2666 // This is because we might have a table in a div, and we select some text in a cell,
2667 // want to limit the change in style to the table cell, not the entire table (via the outer div).
2668 cell = selectionparentnode.ancestor(function (node) {
2669 var tagname = node.get('tagName');
2671 tagname = tagname.toLowerCase();
2673 return (node === boundary) ||
2674 (tagname === 'td') ||
2679 // Limit the scope to the table cell.
2683 nearestblock = selectionparentnode.ancestor(this.BLOCK_TAGS.join(', '), true);
2685 // Check that the block is contained by the boundary.
2686 match = nearestblock.ancestor(function (node) {
2687 return node === boundary;
2691 nearestblock = false;
2695 // No valid block element - make one.
2696 if (!nearestblock) {
2697 // There is no block node in the content, wrap the content in a p and use that.
2698 newcontent = Y.Node.create('<p></p>');
2699 boundary.get('childNodes').each(function (child) {
2700 newcontent.append(child.remove());
2702 boundary.append(newcontent);
2703 nearestblock = newcontent;
2706 // Guaranteed to have a valid block level element contained in the contenteditable region.
2707 // Change the tag to the new block level tag.
2708 if (blocktag && blocktag !== '') {
2709 // Change the block level node for a new one.
2710 replacement = Y.Node.create('<' + blocktag + '></' + blocktag + '>');
2711 // Copy all attributes.
2712 replacement.setAttrs(nearestblock.getAttrs());
2713 // Copy all children.
2714 nearestblock.get('childNodes').each(function (child) {
2716 replacement.append(child);
2719 nearestblock.replace(replacement);
2720 nearestblock = replacement;
2723 // Set the attributes on the block level tag.
2725 nearestblock.setAttrs(attributes);
2728 // Change the selection to the modified block. This makes sense when we might apply multiple styles
2730 var selection = this.getSelectionFromNode(nearestblock);
2731 this.setSelection(selection);
2733 return nearestblock;
2738 Y.Base.mix(Y.M.editor_atto.Editor, [EditorStyling]);
2739 // This file is part of Moodle - http://moodle.org/
2741 // Moodle is free software: you can redistribute it and/or modify
2742 // it under the terms of the GNU General Public License as published by
2743 // the Free Software Foundation, either version 3 of the License, or
2744 // (at your option) any later version.
2746 // Moodle is distributed in the hope that it will be useful,
2747 // but WITHOUT ANY WARRANTY; without even the implied warranty of
2748 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2749 // GNU General Public License for more details.
2751 // You should have received a copy of the GNU General Public License
2752 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
2755 * @module moodle-editor_atto-editor
2756 * @submodule filepicker
2760 * Filepicker options for the Atto editor.
2762 * See {{#crossLink "M.editor_atto.Editor"}}{{/crossLink}} for details.
2764 * @namespace M.editor_atto
2765 * @class EditorFilepicker
2768 function EditorFilepicker() {}
2770 EditorFilepicker.ATTRS= {
2772 * The options for the filepicker.
2774 * @attribute filepickeroptions
2778 filepickeroptions: {
2783 EditorFilepicker.prototype = {
2785 * Should we show the filepicker for this filetype?
2787 * @method canShowFilepicker
2788 * @param string type The media type for the file picker.
2791 canShowFilepicker: function(type) {
2792 return (typeof this.get('filepickeroptions')[type] !== 'undefined');
2796 * Show the filepicker.
2798 * This depends on core_filepicker, and then call that modules show function.
2800 * @method showFilepicker
2801 * @param {string} type The media type for the file picker.
2802 * @param {function} callback The callback to use when selecting an item of media.
2803 * @param {object} [context] The context from which to call the callback.
2805 showFilepicker: function(type, callback, context) {
2807 Y.use('core_filepicker', function (Y) {
2808 var options = Y.clone(self.get('filepickeroptions')[type], true);
2809 options.formcallback = callback;
2811 options.magicscope = context;
2814 M.core_filepicker.show(Y, options);
2819 Y.Base.mix(Y.M.editor_atto.Editor, [EditorFilepicker]);
2834 "moodle-core-notification-dialogue",
2835 "moodle-core-notification-confirm",
2836 "moodle-editor_atto-rangy",
2839 "querystring-stringify"