MDL-41328 Editor: Work around another iOS issue with TinyMCE
[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;
29149f98
PS
46 }
47
4c508047 48 M.editor_tinymce.editor_options[editorid] = options;
fae91170 49
de6a6512 50 // Load necessary Moodle plugins into editor.
fae91170 51 if (options.moodle_init_plugins) {
52 var extraplugins = options.moodle_init_plugins.split(',');
53 for (var i=0; i<extraplugins.length; i++) {
54 var filedetails = extraplugins[i].split(':');
55 tinyMCE.PluginManager.load(filedetails[0],
56 M.cfg.wwwroot + '/lib/editor/tinymce/plugins/' + filedetails[1]);
57 }
58 }
be94c6d8
AN
59
60 // We have to override the editor setup to work around a bug in iOS browsers - MDL-36803.
61 if (Y.UA.ios) {
62 // Retain any setup which is already defined.
63 options.originalSetupFunction = options.setup || function(){};
64 options.setup = function(editor) {
65 options.originalSetupFunction();
66 editor.onPostRender.add(function(ed) {
67 // Whenever there is a keydown event, ensure that the contentWindow still have focus.
68 ed.contentDocument.addEventListener('keydown', function() {
69 ed.contentWindow.focus();
70 });
ff73a6d1
AN
71
72 // Whenever a touch event is registered against the content document,
73 // reapply focus. This works around an issue with the location caret not
74 // being focusable without use of the Loupe.
75 ed.contentDocument.addEventListener('touchend', function() {
76 ed.contentWindow.focus();
77 });
be94c6d8
AN
78 });
79 };
80 }
4c508047
PS
81 tinyMCE.init(options);
82
83 var item = document.getElementById(editorid+'_filemanager');
84 if (item) {
85 item.parentNode.removeChild(item);
86 }
87};
88
89M.editor_tinymce.init_filepicker = function(Y, editorid, options) {
90 M.editor_tinymce.filepicker_options[editorid] = options;
91};
92
93M.editor_tinymce.toggle = function(id) {
94 tinyMCE.execCommand('mceToggleEditor', false, id);
95};
96
97M.editor_tinymce.filepicker_callback = function(args) {
98};
99
100M.editor_tinymce.filepicker = function(target_id, url, type, win) {
e0a5793d 101 YUI().use('core_filepicker', function (Y) {
4c508047 102 var editor_id = tinyMCE.selectedInstance.editorId;
5a254328
DC
103 if (editor_id == 'mce_fullscreen') {
104 editor_id = tinyMCE.selectedInstance.settings.elements;
105 }
875f4e7b
DC
106 var options = null;
107 if (type == 'media') {
de6a6512 108 // When media button clicked.
875f4e7b 109 options = M.editor_tinymce.filepicker_options[editor_id]['media'];
766514a0 110 } else if (type == 'file') {
de6a6512 111 // When link button clicked.
766514a0
DC
112 options = M.editor_tinymce.filepicker_options[editor_id]['link'];
113 } else if (type == 'image') {
de6a6512 114 // When image button clicked.
875f4e7b 115 options = M.editor_tinymce.filepicker_options[editor_id]['image'];
e5e50970 116 }
4c508047
PS
117
118 options.formcallback = M.editor_tinymce.filepicker_callback;
119 options.editor_target = win.document.getElementById(target_id);
120
121 M.core_filepicker.show(Y, options);
122 });
123};
124
ab978b38 125M.editor_tinymce.onblur_event = function(ed) {
de6a6512 126 // Attach event only after tinymce is initialized.
ab978b38
RT
127 if (ed.onInit != undefined) {
128 var s = ed.settings;
de6a6512
PS
129 // Save before event is attached, so that if this event is not generated then textarea should
130 // have loaded contents and submitting form should not throw error.
ab978b38
RT
131 ed.save();
132
de6a6512 133 // Attach blur event for tinymce to save contents to textarea.
ab978b38
RT
134 var doc = s.content_editable ? ed.getBody() : (tinymce.isGecko ? ed.getDoc() : ed.getWin());
135 tinymce.dom.Event.add(doc, 'blur', function() {
de6a6512 136 // Save contents to textarea before calling validation script.
ab978b38 137 ed.save();
ab978b38
RT
138 });
139 };
140};