AddOnManager.js
7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* AddOnManager.js
*
* Released under LGPL License.
* Copyright (c) 1999-2015 Ephox Corp. All rights reserved
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
/**
* This class handles the loading of themes/plugins or other add-ons and their language packs.
*
* @class tinymce.AddOnManager
*/
define("tinymce/AddOnManager", [
"tinymce/dom/ScriptLoader",
"tinymce/util/Tools"
], function(ScriptLoader, Tools) {
var each = Tools.each;
function AddOnManager() {
var self = this;
self.items = [];
self.urls = {};
self.lookup = {};
}
AddOnManager.prototype = {
/**
* Returns the specified add on by the short name.
*
* @method get
* @param {String} name Add-on to look for.
* @return {tinymce.Theme/tinymce.Plugin} Theme or plugin add-on instance or undefined.
*/
get: function(name) {
if (this.lookup[name]) {
return this.lookup[name].instance;
}
return undefined;
},
dependencies: function(name) {
var result;
if (this.lookup[name]) {
result = this.lookup[name].dependencies;
}
return result || [];
},
/**
* Loads a language pack for the specified add-on.
*
* @method requireLangPack
* @param {String} name Short name of the add-on.
* @param {String} languages Optional comma or space separated list of languages to check if it matches the name.
*/
requireLangPack: function(name, languages) {
var language = AddOnManager.language;
if (language && AddOnManager.languageLoad !== false) {
if (languages) {
languages = ',' + languages + ',';
// Load short form sv.js or long form sv_SE.js
if (languages.indexOf(',' + language.substr(0, 2) + ',') != -1) {
language = language.substr(0, 2);
} else if (languages.indexOf(',' + language + ',') == -1) {
return;
}
}
ScriptLoader.ScriptLoader.add(this.urls[name] + '/langs/' + language + '.js');
}
},
/**
* Adds a instance of the add-on by it's short name.
*
* @method add
* @param {String} id Short name/id for the add-on.
* @param {tinymce.Theme/tinymce.Plugin} addOn Theme or plugin to add.
* @return {tinymce.Theme/tinymce.Plugin} The same theme or plugin instance that got passed in.
* @example
* // Create a simple plugin
* tinymce.create('tinymce.plugins.TestPlugin', {
* TestPlugin: function(ed, url) {
* ed.on('click', function(e) {
* ed.windowManager.alert('Hello World!');
* });
* }
* });
*
* // Register plugin using the add method
* tinymce.PluginManager.add('test', tinymce.plugins.TestPlugin);
*
* // Initialize TinyMCE
* tinymce.init({
* ...
* plugins: '-test' // Init the plugin but don't try to load it
* });
*/
add: function(id, addOn, dependencies) {
this.items.push(addOn);
this.lookup[id] = {instance: addOn, dependencies: dependencies};
return addOn;
},
remove: function(name) {
delete this.urls[name];
delete this.lookup[name];
},
createUrl: function(baseUrl, dep) {
if (typeof dep === "object") {
return dep;
}
return {prefix: baseUrl.prefix, resource: dep, suffix: baseUrl.suffix};
},
/**
* Add a set of components that will make up the add-on. Using the url of the add-on name as the base url.
* This should be used in development mode. A new compressor/javascript munger process will ensure that the
* components are put together into the plugin.js file and compressed correctly.
*
* @method addComponents
* @param {String} pluginName name of the plugin to load scripts from (will be used to get the base url for the plugins).
* @param {Array} scripts Array containing the names of the scripts to load.
*/
addComponents: function(pluginName, scripts) {
var pluginUrl = this.urls[pluginName];
each(scripts, function(script) {
ScriptLoader.ScriptLoader.add(pluginUrl + "/" + script);
});
},
/**
* Loads an add-on from a specific url.
*
* @method load
* @param {String} name Short name of the add-on that gets loaded.
* @param {String} addOnUrl URL to the add-on that will get loaded.
* @param {function} callback Optional callback to execute ones the add-on is loaded.
* @param {Object} scope Optional scope to execute the callback in.
* @example
* // Loads a plugin from an external URL
* tinymce.PluginManager.load('myplugin', '/some/dir/someplugin/plugin.js');
*
* // Initialize TinyMCE
* tinymce.init({
* ...
* plugins: '-myplugin' // Don't try to load it again
* });
*/
load: function(name, addOnUrl, callback, scope) {
var self = this, url = addOnUrl;
function loadDependencies() {
var dependencies = self.dependencies(name);
each(dependencies, function(dep) {
var newUrl = self.createUrl(addOnUrl, dep);
self.load(newUrl.resource, newUrl, undefined, undefined);
});
if (callback) {
if (scope) {
callback.call(scope);
} else {
callback.call(ScriptLoader);
}
}
}
if (self.urls[name]) {
return;
}
if (typeof addOnUrl === "object") {
url = addOnUrl.prefix + addOnUrl.resource + addOnUrl.suffix;
}
if (url.indexOf('/') !== 0 && url.indexOf('://') == -1) {
url = AddOnManager.baseURL + '/' + url;
}
self.urls[name] = url.substring(0, url.lastIndexOf('/'));
if (self.lookup[name]) {
loadDependencies();
} else {
ScriptLoader.ScriptLoader.add(url, loadDependencies, scope);
}
}
};
AddOnManager.PluginManager = new AddOnManager();
AddOnManager.ThemeManager = new AddOnManager();
return AddOnManager;
});
/**
* TinyMCE theme class.
*
* @class tinymce.Theme
*/
/**
* This method is responsible for rendering/generating the overall user interface with toolbars, buttons, iframe containers etc.
*
* @method renderUI
* @param {Object} obj Object parameter containing the targetNode DOM node that will be replaced visually with an editor instance.
* @return {Object} an object with items like iframeContainer, editorContainer, sizeContainer, deltaWidth, deltaHeight.
*/
/**
* Plugin base class, this is a pseudo class that describes how a plugin is to be created for TinyMCE. The methods below are all optional.
*
* @class tinymce.Plugin
* @example
* tinymce.PluginManager.add('example', function(editor, url) {
* // Add a button that opens a window
* editor.addButton('example', {
* text: 'My button',
* icon: false,
* onclick: function() {
* // Open window
* editor.windowManager.open({
* title: 'Example plugin',
* body: [
* {type: 'textbox', name: 'title', label: 'Title'}
* ],
* onsubmit: function(e) {
* // Insert content when the window form is submitted
* editor.insertContent('Title: ' + e.data.title);
* }
* });
* }
* });
*
* // Adds a menu item to the tools menu
* editor.addMenuItem('example', {
* text: 'Example plugin',
* context: 'tools',
* onclick: function() {
* // Open window with a specific url
* editor.windowManager.open({
* title: 'TinyMCE site',
* url: 'http://www.tinymce.com',
* width: 800,
* height: 600,
* buttons: [{
* text: 'Close',
* onclick: 'close'
* }]
* });
* }
* });
* });
*/