]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/compat3x/plugin.js
WordPress 4.0
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / compat3x / plugin.js
1 /**
2  * plugin.js
3  *
4  * Copyright, Moxiecode Systems AB
5  * Released under LGPL License.
6  *
7  * License: http://www.tinymce.com/license
8  * Contributing: http://www.tinymce.com/contributing
9  */
10
11 /*global tinymce:true, console:true */
12 /*eslint no-console:0, new-cap:0 */
13
14 /**
15  * This plugin adds missing events form the 4.x API back. Not every event is
16  * properly supported but most things should work.
17  *
18  * Unsupported things:
19  *  - No editor.onEvent
20  *  - Can't cancel execCommands with beforeExecCommand
21  */
22 (function(tinymce) {
23         var reported;
24
25         function noop() {
26         }
27
28         function log(apiCall) {
29                 if (!reported && window && window.console) {
30                         reported = true;
31                         console.log("Deprecated TinyMCE API call: " + apiCall);
32                 }
33         }
34
35         function Dispatcher(target, newEventName, argsMap, defaultScope) {
36                 target = target || this;
37
38                 if (!newEventName) {
39                         this.add = this.addToTop = this.remove = this.dispatch = noop;
40                         return;
41                 }
42
43                 this.add = function(callback, scope, prepend) {
44                         log('<target>.on' + newEventName + ".add(..)");
45
46                         // Convert callback({arg1:x, arg2:x}) -> callback(arg1, arg2)
47                         function patchedEventCallback(e) {
48                                 var callbackArgs = [];
49
50                                 if (typeof argsMap == "string") {
51                                         argsMap = argsMap.split(" ");
52                                 }
53
54                                 if (argsMap && typeof argsMap != "function") {
55                                         for (var i = 0; i < argsMap.length; i++) {
56                                                 callbackArgs.push(e[argsMap[i]]);
57                                         }
58                                 }
59
60                                 if (typeof argsMap == "function") {
61                                         callbackArgs = argsMap(newEventName, e, target);
62                                         if (!callbackArgs) {
63                                                 return;
64                                         }
65                                 }
66
67                                 if (!argsMap) {
68                                         callbackArgs = [e];
69                                 }
70
71                                 callbackArgs.unshift(defaultScope || target);
72
73                                 if (callback.apply(scope || defaultScope || target, callbackArgs) === false) {
74                                         e.stopImmediatePropagation();
75                                 }
76                         }
77
78                         target.on(newEventName, patchedEventCallback, prepend);
79
80                         return patchedEventCallback;
81                 };
82
83                 this.addToTop = function(callback, scope) {
84                         this.add(callback, scope, true);
85                 };
86
87                 this.remove = function(callback) {
88                         return target.off(newEventName, callback);
89                 };
90
91                 this.dispatch = function() {
92                         target.fire(newEventName);
93
94                         return true;
95                 };
96         }
97
98         tinymce.util.Dispatcher = Dispatcher;
99         tinymce.onBeforeUnload = new Dispatcher(tinymce, "BeforeUnload");
100         tinymce.onAddEditor = new Dispatcher(tinymce, "AddEditor", "editor");
101         tinymce.onRemoveEditor = new Dispatcher(tinymce, "RemoveEditor", "editor");
102
103         tinymce.util.Cookie = {
104                 get: noop, getHash: noop, remove: noop, set: noop, setHash: noop
105         };
106
107         function patchEditor(editor) {
108                 function patchEditorEvents(oldEventNames, argsMap) {
109                         tinymce.each(oldEventNames.split(" "), function(oldName) {
110                                 editor["on" + oldName] = new Dispatcher(editor, oldName, argsMap);
111                         });
112                 }
113
114                 function convertUndoEventArgs(type, event, target) {
115                         return [
116                                 event.level,
117                                 target
118                         ];
119                 }
120
121                 function filterSelectionEvents(needsSelection) {
122                         return function(type, e) {
123                                 if ((!e.selection && !needsSelection) || e.selection == needsSelection) {
124                                         return [e];
125                                 }
126                         };
127                 }
128
129                 if (editor.controlManager) {
130                         return;
131                 }
132
133                 function cmNoop() {
134                         var obj = {}, methods = 'add addMenu addSeparator collapse createMenu destroy displayColor expand focus ' +
135                                 'getLength hasMenus hideMenu isActive isCollapsed isDisabled isRendered isSelected mark ' +
136                                 'postRender remove removeAll renderHTML renderMenu renderNode renderTo select selectByIndex ' +
137                                 'setActive setAriaProperty setColor setDisabled setSelected setState showMenu update';
138
139                         log('editor.controlManager.*');
140
141                         function _noop() {
142                                 return cmNoop();
143                         }
144
145                         tinymce.each(methods.split(' '), function(method) {
146                                 obj[method] = _noop;
147                         });
148
149                         return obj;
150                 }
151
152                 editor.controlManager = {
153                         buttons: {},
154
155                         setDisabled: function(name, state) {
156                                 log("controlManager.setDisabled(..)");
157
158                                 if (this.buttons[name]) {
159                                         this.buttons[name].disabled(state);
160                                 }
161                         },
162
163                         setActive: function(name, state) {
164                                 log("controlManager.setActive(..)");
165
166                                 if (this.buttons[name]) {
167                                         this.buttons[name].active(state);
168                                 }
169                         },
170
171                         onAdd: new Dispatcher(),
172                         onPostRender: new Dispatcher(),
173
174                         add: function(obj) {
175                                 return obj;
176                         },
177                         createButton: cmNoop,
178                         createColorSplitButton: cmNoop,
179                         createControl: cmNoop,
180                         createDropMenu: cmNoop,
181                         createListBox: cmNoop,
182                         createMenuButton: cmNoop,
183                         createSeparator: cmNoop,
184                         createSplitButton: cmNoop,
185                         createToolbar: cmNoop,
186                         createToolbarGroup: cmNoop,
187                         destroy: noop,
188                         get: noop,
189                         setControlType: cmNoop
190                 };
191
192                 patchEditorEvents("PreInit BeforeRenderUI PostRender Load Init Remove Activate Deactivate", "editor");
193                 patchEditorEvents("Click MouseUp MouseDown DblClick KeyDown KeyUp KeyPress ContextMenu Paste Submit Reset");
194                 patchEditorEvents("BeforeExecCommand ExecCommand", "command ui value args"); // args.terminate not supported
195                 patchEditorEvents("PreProcess PostProcess LoadContent SaveContent Change");
196                 patchEditorEvents("BeforeSetContent BeforeGetContent SetContent GetContent", filterSelectionEvents(false));
197                 patchEditorEvents("SetProgressState", "state time");
198                 patchEditorEvents("VisualAid", "element hasVisual");
199                 patchEditorEvents("Undo Redo", convertUndoEventArgs);
200
201                 patchEditorEvents("NodeChange", function(type, e) {
202                         return [
203                                 editor.controlManager,
204                                 e.element,
205                                 editor.selection.isCollapsed(),
206                                 e
207                         ];
208                 });
209
210                 var originalAddButton = editor.addButton;
211                 editor.addButton = function(name, settings) {
212                         var originalOnPostRender, string, translated;
213
214                         function patchedPostRender() {
215                                 editor.controlManager.buttons[name] = this;
216
217                                 if (originalOnPostRender) {
218                                         return originalOnPostRender.call(this);
219                                 }
220                         }
221
222                         for (var key in settings) {
223                                 if (key.toLowerCase() === "onpostrender") {
224                                         originalOnPostRender = settings[key];
225                                         settings.onPostRender = patchedPostRender;
226                                 }
227                         }
228
229                         if (!originalOnPostRender) {
230                                 settings.onPostRender = patchedPostRender;
231                         }
232
233                         if ( settings.title ) {
234                                 // WP
235                                 string = (editor.settings.language || "en") + "." + settings.title;
236                                 translated = tinymce.i18n.translate(string);
237
238                                 if ( string !== translated ) {
239                                         settings.title = translated;
240                                 }
241                                 // WP end
242                         }
243
244                         return originalAddButton.call(this, name, settings);
245                 };
246
247                 editor.on('init', function() {
248                         var undoManager = editor.undoManager, selection = editor.selection;
249
250                         undoManager.onUndo = new Dispatcher(editor, "Undo", convertUndoEventArgs, null, undoManager);
251                         undoManager.onRedo = new Dispatcher(editor, "Redo", convertUndoEventArgs, null, undoManager);
252                         undoManager.onBeforeAdd = new Dispatcher(editor, "BeforeAddUndo", null, undoManager);
253                         undoManager.onAdd = new Dispatcher(editor, "AddUndo", null, undoManager);
254
255                         selection.onBeforeGetContent = new Dispatcher(editor, "BeforeGetContent", filterSelectionEvents(true), selection);
256                         selection.onGetContent = new Dispatcher(editor, "GetContent", filterSelectionEvents(true), selection);
257                         selection.onBeforeSetContent = new Dispatcher(editor, "BeforeSetContent", filterSelectionEvents(true), selection);
258                         selection.onSetContent = new Dispatcher(editor, "SetContent", filterSelectionEvents(true), selection);
259                 });
260
261                 editor.on('BeforeRenderUI', function() {
262                         var windowManager = editor.windowManager;
263
264                         windowManager.onOpen = new Dispatcher();
265                         windowManager.onClose = new Dispatcher();
266                         windowManager.createInstance = function(className, a, b, c, d, e) {
267                                 log("windowManager.createInstance(..)");
268
269                                 var constr = tinymce.resolve(className);
270                                 return new constr(a, b, c, d, e);
271                         };
272                 });
273         }
274
275         tinymce.on('SetupEditor', patchEditor);
276         tinymce.PluginManager.add("compat3x", patchEditor);
277
278         tinymce.addI18n = function(prefix, o) {
279                 var I18n = tinymce.util.I18n, each = tinymce.each;
280
281                 if (typeof(prefix) == "string" && prefix.indexOf('.') === -1) {
282                         I18n.add(prefix, o);
283                         return;
284                 }
285
286                 if (!tinymce.is(prefix, 'string')) {
287                         each(prefix, function(o, lc) {
288                                 each(o, function(o, g) {
289                                         each(o, function(o, k) {
290                                                 if (g === 'common') {
291                                                         I18n.data[lc + '.' + k] = o;
292                                                 } else {
293                                                         I18n.data[lc + '.' + g + '.' + k] = o;
294                                                 }
295                                         });
296                                 });
297                         });
298                 } else {
299                         each(o, function(o, k) {
300                                 I18n.data[prefix + '.' + k] = o;
301                         });
302                 }
303         };
304 })(tinymce);