MDL-64573 output: ajax form event
[moodle.git] / lib / editor / tinymce / module.js
CommitLineData
4c508047
PS
1// This file is part of Moodle - http://moodle.org/
2//
3// Moodle is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// Moodle is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
15
16/**
de6a6512 17 * TinyMCE helper javascript functions.
4c508047 18 *
34a30b5c 19 * @package editor_tinymce
de6a6512 20 * @copyright 2010 Petr Skoda (http://skodak.org)
4c508047
PS
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24M.editor_tinymce = M.editor_tinymce || {};
25
26M.editor_tinymce.editor_options = M.editor_tinymce.options || {};
27M.editor_tinymce.filepicker_options = M.editor_tinymce.filepicker_options || {};
5ba3a966 28M.editor_tinymce.initialised = false;
4c508047
PS
29
30M.editor_tinymce.init_editor = function(Y, editorid, options) {
29149f98 31
5ba3a966
PS
32 if (!M.editor_tinymce.initialised) {
33 // Load all language strings for all plugins - we do not use standard TinyMCE lang pack loading!
29149f98 34 tinymce.ScriptLoader.add(M.cfg.wwwroot + '/lib/editor/tinymce/all_strings.php?elanguage=' + options.language + '&rev=' + options.langrev);
5ba3a966
PS
35
36 // Monkey patch for MDL-35284 - this hack ignores empty toolbars.
37 tinymce.ui.Toolbar.prototype.oldRenderHTML = tinymce.ui.Toolbar.prototype.renderHTML;
38 tinymce.ui.Toolbar.prototype.renderHTML = function() {
39 if (this.controls.length == 0) {
40 return;
41 }
42 return tinymce.ui.Toolbar.prototype.oldRenderHTML.call(this);
43 };
44
45 M.editor_tinymce.initialised = true;
519449c5
DW
46 M.util.js_pending('editors');
47 options.oninit = "M.editor_tinymce.init_callback";
29149f98
PS
48 }
49
4c508047 50 M.editor_tinymce.editor_options[editorid] = options;
fae91170 51
de6a6512 52 // Load necessary Moodle plugins into editor.
fae91170 53 if (options.moodle_init_plugins) {
54 var extraplugins = options.moodle_init_plugins.split(',');
55 for (var i=0; i<extraplugins.length; i++) {
56 var filedetails = extraplugins[i].split(':');
57 tinyMCE.PluginManager.load(filedetails[0],
58 M.cfg.wwwroot + '/lib/editor/tinymce/plugins/' + filedetails[1]);
59 }
60 }
be94c6d8 61
1bcc54ef
SL
62 // Retain any setup which is already defined.
63 options.originalSetupFunction = options.setup || function(){};
64 options.setup = function(editor) {
65 options.originalSetupFunction();
66 editor.onChange.add(function(ed) {
67 ed.save();
68 });
260565e3
DW
69 Y.use('event', 'moodle-core-event', function(Y) {
70 var form = Y.one(document.getElementById(editor.id)).ancestor('form');
71 form.on(M.core.event.FORM_SUBMIT_AJAX, function() {
72 editor.save();
73 }, this)
74 });
1bcc54ef
SL
75 };
76
4c508047
PS
77 tinyMCE.init(options);
78
79 var item = document.getElementById(editorid+'_filemanager');
80 if (item) {
81 item.parentNode.removeChild(item);
82 }
b6976f1a
JH
83
84 document.getElementById(editorid).addEventListener('form:editorUpdated', function() {
85 M.editor_tinymce.updateEditorState(editorid);
86 });
4c508047
PS
87};
88
519449c5
DW
89M.editor_tinymce.init_callback = function() {
90 M.util.js_complete('editors');
91}
92
4c508047
PS
93M.editor_tinymce.init_filepicker = function(Y, editorid, options) {
94 M.editor_tinymce.filepicker_options[editorid] = options;
95};
96
97M.editor_tinymce.toggle = function(id) {
98 tinyMCE.execCommand('mceToggleEditor', false, id);
99};
100
b6976f1a
JH
101/**
102 * Update the state of the editor.
103 * @param {String} id
104 */
105M.editor_tinymce.updateEditorState = function(id) {
106 var instance = window.tinyMCE.get(id),
107 content = instance.getBody(),
108 controls = instance.controlManager.controls,
109 disabled = instance.getElement().readOnly;
110 // Enable/Disable all plugins.
111 for (var key in controls) {
112 if (controls.hasOwnProperty(key)) {
113 controls[key].setDisabled(disabled);
114 }
115 }
116 // Enable/Disable body content.
117 content.setAttribute('contenteditable', !disabled);
118};
119
4c508047
PS
120M.editor_tinymce.filepicker_callback = function(args) {
121};
122
123M.editor_tinymce.filepicker = function(target_id, url, type, win) {
e0a5793d 124 YUI().use('core_filepicker', function (Y) {
4c508047 125 var editor_id = tinyMCE.selectedInstance.editorId;
5a254328
DC
126 if (editor_id == 'mce_fullscreen') {
127 editor_id = tinyMCE.selectedInstance.settings.elements;
128 }
875f4e7b
DC
129 var options = null;
130 if (type == 'media') {
de6a6512 131 // When media button clicked.
875f4e7b 132 options = M.editor_tinymce.filepicker_options[editor_id]['media'];
766514a0 133 } else if (type == 'file') {
de6a6512 134 // When link button clicked.
766514a0
DC
135 options = M.editor_tinymce.filepicker_options[editor_id]['link'];
136 } else if (type == 'image') {
de6a6512 137 // When image button clicked.
875f4e7b 138 options = M.editor_tinymce.filepicker_options[editor_id]['image'];
e5e50970 139 }
4c508047
PS
140
141 options.formcallback = M.editor_tinymce.filepicker_callback;
142 options.editor_target = win.document.getElementById(target_id);
143
144 M.core_filepicker.show(Y, options);
145 });
146};
147
ab978b38 148M.editor_tinymce.onblur_event = function(ed) {
de6a6512 149 // Attach event only after tinymce is initialized.
ab978b38
RT
150 if (ed.onInit != undefined) {
151 var s = ed.settings;
de6a6512
PS
152 // Save before event is attached, so that if this event is not generated then textarea should
153 // have loaded contents and submitting form should not throw error.
ab978b38
RT
154 ed.save();
155
de6a6512 156 // Attach blur event for tinymce to save contents to textarea.
ab978b38
RT
157 var doc = s.content_editable ? ed.getBody() : (tinymce.isGecko ? ed.getDoc() : ed.getWin());
158 tinymce.dom.Event.add(doc, 'blur', function() {
de6a6512 159 // Save contents to textarea before calling validation script.
ab978b38 160 ed.save();
ab978b38
RT
161 });
162 };
163};