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