AddOnManager.js 7.4 KB

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