AddOnManager.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**
  2. * AddOnManager.js
  3. *
  4. * Copyright 2009, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://tinymce.moxiecode.com/license
  8. * Contributing: http://tinymce.moxiecode.com/contributing
  9. */
  10. (function(tinymce) {
  11. var Dispatcher = tinymce.util.Dispatcher, each = tinymce.each;
  12. /**
  13. * This class handles the loading of themes/plugins or other add-ons and their language packs.
  14. *
  15. * @class tinymce.AddOnManager
  16. */
  17. tinymce.create('tinymce.AddOnManager', {
  18. AddOnManager : function() {
  19. var self = this;
  20. self.items = [];
  21. self.urls = {};
  22. self.lookup = {};
  23. self.onAdd = new Dispatcher(self);
  24. },
  25. /**
  26. * Fires when a item is added.
  27. *
  28. * @event onAdd
  29. */
  30. /**
  31. * Returns the specified add on by the short name.
  32. *
  33. * @method get
  34. * @param {String} n Add-on to look for.
  35. * @return {tinymce.Theme/tinymce.Plugin} Theme or plugin add-on instance or undefined.
  36. */
  37. get : function(n) {
  38. if (this.lookup[n]) {
  39. return this.lookup[n].instance;
  40. } else {
  41. return undefined;
  42. }
  43. },
  44. dependencies : function(n) {
  45. var result;
  46. if (this.lookup[n]) {
  47. result = this.lookup[n].dependencies;
  48. }
  49. return result || [];
  50. },
  51. /**
  52. * Loads a language pack for the specified add-on.
  53. *
  54. * @method requireLangPack
  55. * @param {String} n Short name of the add-on.
  56. */
  57. requireLangPack : function(n) {
  58. var s = tinymce.settings;
  59. if (s && s.language && s.language_load !== false)
  60. tinymce.ScriptLoader.add(this.urls[n] + '/langs/' + s.language + '.js');
  61. },
  62. /**
  63. * Adds a instance of the add-on by it's short name.
  64. *
  65. * @method add
  66. * @param {String} id Short name/id for the add-on.
  67. * @param {tinymce.Theme/tinymce.Plugin} o Theme or plugin to add.
  68. * @return {tinymce.Theme/tinymce.Plugin} The same theme or plugin instance that got passed in.
  69. * @example
  70. * // Create a simple plugin
  71. * tinymce.create('tinymce.plugins.TestPlugin', {
  72. * TestPlugin : function(ed, url) {
  73. * ed.onClick.add(function(ed, e) {
  74. * ed.windowManager.alert('Hello World!');
  75. * });
  76. * }
  77. * });
  78. *
  79. * // Register plugin using the add method
  80. * tinymce.PluginManager.add('test', tinymce.plugins.TestPlugin);
  81. *
  82. * // Initialize TinyMCE
  83. * tinyMCE.init({
  84. * ...
  85. * plugins : '-test' // Init the plugin but don't try to load it
  86. * });
  87. */
  88. add : function(id, o, dependencies) {
  89. this.items.push(o);
  90. this.lookup[id] = {instance:o, dependencies:dependencies};
  91. this.onAdd.dispatch(this, id, o);
  92. return o;
  93. },
  94. createUrl: function(baseUrl, dep) {
  95. if (typeof dep === "object") {
  96. return dep
  97. } else {
  98. return {prefix: baseUrl.prefix, resource: dep, suffix: baseUrl.suffix};
  99. }
  100. },
  101. /**
  102. * Add a set of components that will make up the add-on. Using the url of the add-on name as the base url.
  103. * This should be used in development mode. A new compressor/javascript munger process will ensure that the
  104. * components are put together into the editor_plugin.js file and compressed correctly.
  105. * @param pluginName {String} name of the plugin to load scripts from (will be used to get the base url for the plugins).
  106. * @param scripts {Array} Array containing the names of the scripts to load.
  107. */
  108. addComponents: function(pluginName, scripts) {
  109. var pluginUrl = this.urls[pluginName];
  110. tinymce.each(scripts, function(script){
  111. tinymce.ScriptLoader.add(pluginUrl+"/"+script);
  112. });
  113. },
  114. /**
  115. * Loads an add-on from a specific url.
  116. *
  117. * @method load
  118. * @param {String} n Short name of the add-on that gets loaded.
  119. * @param {String} u URL to the add-on that will get loaded.
  120. * @param {function} cb Optional callback to execute ones the add-on is loaded.
  121. * @param {Object} s Optional scope to execute the callback in.
  122. * @example
  123. * // Loads a plugin from an external URL
  124. * tinymce.PluginManager.load('myplugin', '/some/dir/someplugin/editor_plugin.js');
  125. *
  126. * // Initialize TinyMCE
  127. * tinyMCE.init({
  128. * ...
  129. * plugins : '-myplugin' // Don't try to load it again
  130. * });
  131. */
  132. load : function(n, u, cb, s) {
  133. var t = this, url = u;
  134. function loadDependencies() {
  135. var dependencies = t.dependencies(n);
  136. tinymce.each(dependencies, function(dep) {
  137. var newUrl = t.createUrl(u, dep);
  138. t.load(newUrl.resource, newUrl, undefined, undefined);
  139. });
  140. if (cb) {
  141. if (s) {
  142. cb.call(s);
  143. } else {
  144. cb.call(tinymce.ScriptLoader);
  145. }
  146. }
  147. }
  148. if (t.urls[n])
  149. return;
  150. if (typeof u === "object")
  151. url = u.prefix + u.resource + u.suffix;
  152. if (url.indexOf('/') != 0 && url.indexOf('://') == -1)
  153. url = tinymce.baseURL + '/' + url;
  154. t.urls[n] = url.substring(0, url.lastIndexOf('/'));
  155. if (t.lookup[n]) {
  156. loadDependencies();
  157. } else {
  158. tinymce.ScriptLoader.add(url, loadDependencies, s);
  159. }
  160. }
  161. });
  162. // Create plugin and theme managers
  163. tinymce.PluginManager = new tinymce.AddOnManager();
  164. tinymce.ThemeManager = new tinymce.AddOnManager();
  165. }(tinymce));
  166. /**
  167. * TinyMCE theme class.
  168. *
  169. * @class tinymce.Theme
  170. */
  171. /**
  172. * Initializes the theme.
  173. *
  174. * @method init
  175. * @param {tinymce.Editor} editor Editor instance that created the theme instance.
  176. * @param {String} url Absolute URL where the theme is located.
  177. */
  178. /**
  179. * Meta info method, this method gets executed when TinyMCE wants to present information about the theme for example in the about/help dialog.
  180. *
  181. * @method getInfo
  182. * @return {Object} Returns an object with meta information about the theme the current items are longname, author, authorurl, infourl and version.
  183. */
  184. /**
  185. * This method is responsible for rendering/generating the overall user interface with toolbars, buttons, iframe containers etc.
  186. *
  187. * @method renderUI
  188. * @param {Object} obj Object parameter containing the targetNode DOM node that will be replaced visually with an editor instance.
  189. * @return {Object} an object with items like iframeContainer, editorContainer, sizeContainer, deltaWidth, deltaHeight.
  190. */
  191. /**
  192. * 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.
  193. *
  194. * @class tinymce.Plugin
  195. * @example
  196. * // Create a new plugin class
  197. * tinymce.create('tinymce.plugins.ExamplePlugin', {
  198. * init : function(ed, url) {
  199. * // Register an example button
  200. * ed.addButton('example', {
  201. * title : 'example.desc',
  202. * onclick : function() {
  203. * // Display an alert when the user clicks the button
  204. * ed.windowManager.alert('Hello world!');
  205. * },
  206. * 'class' : 'bold' // Use the bold icon from the theme
  207. * });
  208. * }
  209. * });
  210. *
  211. * // Register plugin with a short name
  212. * tinymce.PluginManager.add('example', tinymce.plugins.ExamplePlugin);
  213. *
  214. * // Initialize TinyMCE with the new plugin and button
  215. * tinyMCE.init({
  216. * ...
  217. * plugins : '-example', // - means TinyMCE will not try to load it
  218. * theme_advanced_buttons1 : 'example' // Add the new example button to the toolbar
  219. * });
  220. */
  221. /**
  222. * Initialization function for the plugin. This will be called when the plugin is created.
  223. *
  224. * @method init
  225. * @param {tinymce.Editor} editor Editor instance that created the plugin instance.
  226. * @param {String} url Absolute URL where the plugin is located.
  227. * @example
  228. * // Creates a new plugin class
  229. * tinymce.create('tinymce.plugins.ExamplePlugin', {
  230. * init : function(ed, url) {
  231. * // Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('mceExample');
  232. * ed.addCommand('mceExample', function() {
  233. * ed.windowManager.open({
  234. * file : url + '/dialog.htm',
  235. * width : 320 + ed.getLang('example.delta_width', 0),
  236. * height : 120 + ed.getLang('example.delta_height', 0),
  237. * inline : 1
  238. * }, {
  239. * plugin_url : url, // Plugin absolute URL
  240. * some_custom_arg : 'custom arg' // Custom argument
  241. * });
  242. * });
  243. *
  244. * // Register example button
  245. * ed.addButton('example', {
  246. * title : 'example.desc',
  247. * cmd : 'mceExample',
  248. * image : url + '/img/example.gif'
  249. * });
  250. *
  251. * // Add a node change handler, selects the button in the UI when a image is selected
  252. * ed.onNodeChange.add(function(ed, cm, n) {
  253. * cm.setActive('example', n.nodeName == 'IMG');
  254. * });
  255. * }
  256. * });
  257. *
  258. * // Register plugin
  259. * tinymce.PluginManager.add('example', tinymce.plugins.ExamplePlugin);
  260. */
  261. /**
  262. * Meta info method, this method gets executed when TinyMCE wants to present information about the plugin for example in the about/help dialog.
  263. *
  264. * @method getInfo
  265. * @return {Object} Returns an object with meta information about the plugin the current items are longname, author, authorurl, infourl and version.
  266. * @example
  267. * // Creates a new plugin class
  268. * tinymce.create('tinymce.plugins.ExamplePlugin', {
  269. * // Meta info method
  270. * getInfo : function() {
  271. * return {
  272. * longname : 'Example plugin',
  273. * author : 'Some author',
  274. * authorurl : 'http://tinymce.moxiecode.com',
  275. * infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/example',
  276. * version : "1.0"
  277. * };
  278. * }
  279. * });
  280. *
  281. * // Register plugin
  282. * tinymce.PluginManager.add('example', tinymce.plugins.ExamplePlugin);
  283. *
  284. * // Initialize TinyMCE with the new plugin
  285. * tinyMCE.init({
  286. * ...
  287. * plugins : '-example' // - means TinyMCE will not try to load it
  288. * });
  289. */
  290. /**
  291. * Gets called when a new control instance is created.
  292. *
  293. * @method createControl
  294. * @param {String} name Control name to create for example "mylistbox"
  295. * @param {tinymce.ControlManager} controlman Control manager/factory to use to create the control.
  296. * @return {tinymce.ui.Control} Returns a new control instance or null.
  297. * @example
  298. * // Creates a new plugin class
  299. * tinymce.create('tinymce.plugins.ExamplePlugin', {
  300. * createControl: function(n, cm) {
  301. * switch (n) {
  302. * case 'mylistbox':
  303. * var mlb = cm.createListBox('mylistbox', {
  304. * title : 'My list box',
  305. * onselect : function(v) {
  306. * tinyMCE.activeEditor.windowManager.alert('Value selected:' + v);
  307. * }
  308. * });
  309. *
  310. * // Add some values to the list box
  311. * mlb.add('Some item 1', 'val1');
  312. * mlb.add('some item 2', 'val2');
  313. * mlb.add('some item 3', 'val3');
  314. *
  315. * // Return the new listbox instance
  316. * return mlb;
  317. * }
  318. *
  319. * return null;
  320. * }
  321. * });
  322. *
  323. * // Register plugin
  324. * tinymce.PluginManager.add('example', tinymce.plugins.ExamplePlugin);
  325. *
  326. * // Initialize TinyMCE with the new plugin and button
  327. * tinyMCE.init({
  328. * ...
  329. * plugins : '-example', // - means TinyMCE will not try to load it
  330. * theme_advanced_buttons1 : 'mylistbox' // Add the new mylistbox control to the toolbar
  331. * });
  332. */