jquery.tinymce.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /**
  2. * jquery.tinymce.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. /*global tinymce:true, jQuery */
  11. (function($) {
  12. var undef,
  13. lazyLoading,
  14. delayedInits = [],
  15. win = window;
  16. $.fn.tinymce = function(settings) {
  17. var self = this, url, base, lang, suffix = "", patchApplied;
  18. // No match then just ignore the call
  19. if (!self.length) {
  20. return self;
  21. }
  22. // Get editor instance
  23. if (!settings) {
  24. return window.tinymce ? tinymce.get(self[0].id) : null;
  25. }
  26. self.css('visibility', 'hidden'); // Hide textarea to avoid flicker
  27. function init() {
  28. var editors = [], initCount = 0;
  29. // Apply patches to the jQuery object, only once
  30. if (!patchApplied) {
  31. applyPatch();
  32. patchApplied = true;
  33. }
  34. // Create an editor instance for each matched node
  35. self.each(function(i, node) {
  36. var ed, id = node.id, oninit = settings.oninit;
  37. // Generate unique id for target element if needed
  38. if (!id) {
  39. node.id = id = tinymce.DOM.uniqueId();
  40. }
  41. // Only init the editor once
  42. if (tinymce.get(id)) {
  43. return;
  44. }
  45. // Create editor instance and render it
  46. ed = new tinymce.Editor(id, settings, tinymce.EditorManager);
  47. editors.push(ed);
  48. ed.on('init', function() {
  49. var scope, func = oninit;
  50. self.css('visibility', '');
  51. // Run this if the oninit setting is defined
  52. // this logic will fire the oninit callback ones each
  53. // matched editor instance is initialized
  54. if (oninit) {
  55. // Fire the oninit event ones each editor instance is initialized
  56. if (++initCount == editors.length) {
  57. if (typeof(func) === "string") {
  58. scope = (func.indexOf(".") === -1) ? null : tinymce.resolve(func.replace(/\.\w+$/, ""));
  59. func = tinymce.resolve(func);
  60. }
  61. // Call the oninit function with the object
  62. func.apply(scope || tinymce, editors);
  63. }
  64. }
  65. });
  66. });
  67. // Render the editor instances in a separate loop since we
  68. // need to have the full editors array used in the onInit calls
  69. $.each(editors, function(i, ed) {
  70. ed.render();
  71. });
  72. }
  73. // Load TinyMCE on demand, if we need to
  74. if (!win.tinymce && !lazyLoading && (url = settings.script_url)) {
  75. lazyLoading = 1;
  76. base = url.substring(0, url.lastIndexOf("/"));
  77. // Check if it's a dev/src version they want to load then
  78. // make sure that all plugins, themes etc are loaded in source mode as well
  79. if (url.indexOf('.min') != -1) {
  80. suffix = ".min";
  81. }
  82. // Setup tinyMCEPreInit object this will later be used by the TinyMCE
  83. // core script to locate other resources like CSS files, dialogs etc
  84. // You can also predefined a tinyMCEPreInit object and then it will use that instead
  85. win.tinymce = win.tinyMCEPreInit || {
  86. base: base,
  87. suffix: suffix
  88. };
  89. // url contains gzip then we assume it's a compressor
  90. if (url.indexOf('gzip') != -1) {
  91. lang = settings.language || "en";
  92. url = url + (/\?/.test(url) ? '&' : '?') + "js=true&core=true&suffix=" + escape(suffix) +
  93. "&themes=" + escape(settings.theme || 'modern') + "&plugins=" +
  94. escape(settings.plugins || '') + "&languages=" + (lang || '');
  95. // Check if compressor script is already loaded otherwise setup a basic one
  96. if (!win.tinyMCE_GZ) {
  97. win.tinyMCE_GZ = {
  98. start: function() {
  99. function load(url) {
  100. tinymce.ScriptLoader.markDone(tinymce.baseURI.toAbsolute(url));
  101. }
  102. // Add core languages
  103. load("langs/" + lang + ".js");
  104. // Add themes with languages
  105. load("themes/" + settings.theme + "/theme" + suffix + ".js");
  106. load("themes/" + settings.theme + "/langs/" + lang + ".js");
  107. // Add plugins with languages
  108. $.each(settings.plugins.split(","), function(i, name) {
  109. if (name) {
  110. load("plugins/" + name + "/plugin" + suffix + ".js");
  111. load("plugins/" + name + "/langs/" + lang + ".js");
  112. }
  113. });
  114. },
  115. end: function() {
  116. }
  117. };
  118. }
  119. }
  120. var script = document.createElement('script');
  121. script.type = 'text/javascript';
  122. script.onload = script.onreadystatechange = function(e) {
  123. e = e || window.event;
  124. if (lazyLoading !== 2 && (e.type == 'load' || /complete|loaded/.test(script.readyState))) {
  125. tinymce.dom.Event.domLoaded = 1;
  126. lazyLoading = 2;
  127. // Execute callback after mainscript has been loaded and before the initialization occurs
  128. if (settings.script_loaded) {
  129. settings.script_loaded();
  130. }
  131. init();
  132. $.each(delayedInits, function(i, init) {
  133. init();
  134. });
  135. }
  136. };
  137. script.src = url;
  138. document.body.appendChild(script);
  139. } else {
  140. // Delay the init call until tinymce is loaded
  141. if (lazyLoading === 1) {
  142. delayedInits.push(init);
  143. } else {
  144. init();
  145. }
  146. }
  147. return self;
  148. };
  149. // Add :tinymce psuedo selector this will select elements that has been converted into editor instances
  150. // it's now possible to use things like $('*:tinymce') to get all TinyMCE bound elements.
  151. $.extend($.expr[":"], {
  152. tinymce: function(e) {
  153. return !!(e.id && "tinymce" in window && tinymce.get(e.id));
  154. }
  155. });
  156. // This function patches internal jQuery functions so that if
  157. // you for example remove an div element containing an editor it's
  158. // automatically destroyed by the TinyMCE API
  159. function applyPatch() {
  160. // Removes any child editor instances by looking for editor wrapper elements
  161. function removeEditors(name) {
  162. // If the function is remove
  163. if (name === "remove") {
  164. this.each(function(i, node) {
  165. var ed = tinyMCEInstance(node);
  166. if (ed) {
  167. ed.remove();
  168. }
  169. });
  170. }
  171. this.find("span.mceEditor,div.mceEditor").each(function(i, node) {
  172. var ed = tinymce.get(node.id.replace(/_parent$/, ""));
  173. if (ed) {
  174. ed.remove();
  175. }
  176. });
  177. }
  178. // Loads or saves contents from/to textarea if the value
  179. // argument is defined it will set the TinyMCE internal contents
  180. function loadOrSave(value) {
  181. var self = this, ed;
  182. // Handle set value
  183. /*jshint eqnull:true */
  184. if (value != null) {
  185. removeEditors.call(self);
  186. // Saves the contents before get/set value of textarea/div
  187. self.each(function(i, node) {
  188. var ed;
  189. if ((ed = tinymce.get(node.id))) {
  190. ed.setContent(value);
  191. }
  192. });
  193. } else if (self.length > 0) {
  194. // Handle get value
  195. if ((ed = tinymce.get(self[0].id))) {
  196. return ed.getContent();
  197. }
  198. }
  199. }
  200. // Returns tinymce instance for the specified element or null if it wasn't found
  201. function tinyMCEInstance(element) {
  202. var ed = null;
  203. if (element && element.id && win.tinymce) {
  204. ed = tinymce.get(element.id);
  205. }
  206. return ed;
  207. }
  208. // Checks if the specified set contains tinymce instances
  209. function containsTinyMCE(matchedSet) {
  210. return !!((matchedSet) && (matchedSet.length) && (win.tinymce) && (matchedSet.is(":tinymce")));
  211. }
  212. // Patch various jQuery functions
  213. var jQueryFn = {};
  214. // Patch some setter/getter functions these will
  215. // now be able to set/get the contents of editor instances for
  216. // example $('#editorid').html('Content'); will update the TinyMCE iframe instance
  217. $.each(["text", "html", "val"], function(i, name) {
  218. var origFn = jQueryFn[name] = $.fn[name],
  219. textProc = (name === "text");
  220. $.fn[name] = function(value) {
  221. var self = this;
  222. if (!containsTinyMCE(self)) {
  223. return origFn.apply(self, arguments);
  224. }
  225. if (value !== undef) {
  226. loadOrSave.call(self.filter(":tinymce"), value);
  227. origFn.apply(self.not(":tinymce"), arguments);
  228. return self; // return original set for chaining
  229. } else {
  230. var ret = "";
  231. var args = arguments;
  232. (textProc ? self : self.eq(0)).each(function(i, node) {
  233. var ed = tinyMCEInstance(node);
  234. if (ed) {
  235. ret += textProc ? ed.getContent().replace(/<(?:"[^"]*"|'[^']*'|[^'">])*>/g, "") : ed.getContent({save: true});
  236. } else {
  237. ret += origFn.apply($(node), args);
  238. }
  239. });
  240. return ret;
  241. }
  242. };
  243. });
  244. // Makes it possible to use $('#id').append("content"); to append contents to the TinyMCE editor iframe
  245. $.each(["append", "prepend"], function(i, name) {
  246. var origFn = jQueryFn[name] = $.fn[name],
  247. prepend = (name === "prepend");
  248. $.fn[name] = function(value) {
  249. var self = this;
  250. if (!containsTinyMCE(self)) {
  251. return origFn.apply(self, arguments);
  252. }
  253. if (value !== undef) {
  254. self.filter(":tinymce").each(function(i, node) {
  255. var ed = tinyMCEInstance(node);
  256. if (ed) {
  257. ed.setContent(prepend ? value + ed.getContent() : ed.getContent() + value);
  258. }
  259. });
  260. origFn.apply(self.not(":tinymce"), arguments);
  261. return self; // return original set for chaining
  262. }
  263. };
  264. });
  265. // Makes sure that the editor instance gets properly destroyed when the parent element is removed
  266. $.each(["remove", "replaceWith", "replaceAll", "empty"], function(i, name) {
  267. var origFn = jQueryFn[name] = $.fn[name];
  268. $.fn[name] = function() {
  269. removeEditors.call(this, name);
  270. return origFn.apply(this, arguments);
  271. };
  272. });
  273. jQueryFn.attr = $.fn.attr;
  274. // Makes sure that $('#tinymce_id').attr('value') gets the editors current HTML contents
  275. $.fn.attr = function(name, value) {
  276. var self = this, args = arguments;
  277. if ((!name) || (name !== "value") || (!containsTinyMCE(self))) {
  278. if (value !== undef) {
  279. return jQueryFn.attr.apply(self, args);
  280. } else {
  281. return jQueryFn.attr.apply(self, args);
  282. }
  283. }
  284. if (value !== undef) {
  285. loadOrSave.call(self.filter(":tinymce"), value);
  286. jQueryFn.attr.apply(self.not(":tinymce"), args);
  287. return self; // return original set for chaining
  288. } else {
  289. var node = self[0], ed = tinyMCEInstance(node);
  290. return ed ? ed.getContent({save: true}) : jQueryFn.attr.apply($(node), args);
  291. }
  292. };
  293. }
  294. })(jQuery);