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