ckeditor.utils.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
  3. For licensing, see LICENSE.html or http://ckeditor.com/license
  4. */
  5. window.CKEDITOR_BASEPATH = Drupal.settings.ckeditor.editor_path;
  6. (function ($) {
  7. Drupal.ckeditor = (typeof(CKEDITOR) != 'undefined');
  8. Drupal.ckeditor_ver = false;
  9. Drupal.ckeditorToggle = function(textarea_ids, TextTextarea, TextRTE){
  10. if (!CKEDITOR.env.isCompatible) {
  11. return;
  12. }
  13. for (i=0; i<textarea_ids.length; i++){
  14. if (typeof(CKEDITOR.instances) != 'undefined' && typeof(CKEDITOR.instances[textarea_ids[i]]) != 'undefined'){
  15. Drupal.ckeditorOff(textarea_ids[i]);
  16. $('#switch_' + textarea_ids[0]).text(TextRTE);
  17. }
  18. else {
  19. Drupal.ckeditorOn(textarea_ids[i]);
  20. $('#switch_' + textarea_ids[0]).text(TextTextarea);
  21. }
  22. }
  23. };
  24. /**
  25. * CKEditor starting function
  26. *
  27. * @param string textarea_id
  28. */
  29. Drupal.ckeditorInit = function(textarea_id) {
  30. var ckeditor_obj = Drupal.settings.ckeditor;
  31. $("#" + textarea_id).next(".grippie").css("display", "none");
  32. $("#" + textarea_id).addClass("ckeditor-processed");
  33. var textarea_settings = false;
  34. ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]].toolbar = eval(ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]].toolbar);
  35. textarea_settings = ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]];
  36. var drupalTopToolbar = $('#toolbar, #admin-menu', Drupal.overlayChild ? window.parent.document : document);
  37. textarea_settings['on'] =
  38. {
  39. configLoaded : function(ev)
  40. {
  41. Drupal.ckeditor_ver = CKEDITOR.version.split('.')[0];
  42. if (Drupal.ckeditor_ver == 3) {
  43. ev.editor.addCss(ev.editor.config.extraCss);
  44. }
  45. else {
  46. CKEDITOR.addCss(ev.editor.config.extraCss);
  47. }
  48. },
  49. instanceReady : function(ev)
  50. {
  51. var body = $(ev.editor.document.$.body);
  52. if (typeof(ev.editor.dataProcessor.writer.setRules) != 'undefined') {
  53. ev.editor.dataProcessor.writer.setRules('p', {
  54. breakAfterOpen: false
  55. });
  56. if (typeof(ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]].custom_formatting) != 'undefined') {
  57. var dtd = CKEDITOR.dtd;
  58. for ( var e in CKEDITOR.tools.extend( {}, dtd.$block, dtd.$listItem, dtd.$tableContent ) ) {
  59. ev.editor.dataProcessor.writer.setRules( e, ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]].custom_formatting);
  60. }
  61. ev.editor.dataProcessor.writer.setRules( 'pre',
  62. {
  63. indent: ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]].output_pre_indent
  64. });
  65. }
  66. }
  67. if (ev.editor.config.bodyClass)
  68. body.addClass(ev.editor.config.bodyClass);
  69. if (ev.editor.config.bodyId)
  70. body.attr('id', ev.editor.config.bodyId);
  71. if (typeof(Drupal.smileysAttach) != 'undefined' && typeof(ev.editor.dataProcessor.writer) != 'undefined')
  72. ev.editor.dataProcessor.writer.indentationChars = ' ';
  73. },
  74. focus : function(ev)
  75. {
  76. Drupal.ckeditorInstance = ev.editor;
  77. Drupal.ckeditorActiveId = ev.editor.name;
  78. }
  79. ,
  80. afterCommandExec: function(ev)
  81. {
  82. if (ev.data.name != 'maximize') {
  83. return;
  84. }
  85. if (ev.data.command.state == CKEDITOR.TRISTATE_ON) {
  86. drupalTopToolbar.hide();
  87. } else {
  88. drupalTopToolbar.show();
  89. }
  90. }
  91. };
  92. if (typeof Drupal.settings.ckeditor.scayt_language != 'undefined'){
  93. textarea_settings['scayt_sLang'] = Drupal.settings.ckeditor.scayt_language;
  94. }
  95. if (typeof textarea_settings['js_conf'] != 'undefined'){
  96. for (var add_conf in textarea_settings['js_conf']){
  97. textarea_settings[add_conf] = eval(textarea_settings['js_conf'][add_conf]);
  98. }
  99. }
  100. //remove width 100% from settings because this may cause problems with theme css
  101. if (textarea_settings.width == '100%') textarea_settings.width = '';
  102. if (CKEDITOR.loadFullCore) {
  103. CKEDITOR.on('loaded', function() {
  104. textarea_settings = Drupal.ckeditorLoadPlugins(textarea_settings);
  105. Drupal.ckeditorInstance = CKEDITOR.replace(textarea_id, textarea_settings);
  106. });
  107. CKEDITOR.loadFullCore();
  108. }
  109. else {
  110. textarea_settings = Drupal.ckeditorLoadPlugins(textarea_settings);
  111. Drupal.ckeditorInstance = CKEDITOR.replace(textarea_id, textarea_settings);
  112. }
  113. }
  114. Drupal.ckeditorOn = function(textarea_id, run_filter) {
  115. run_filter = typeof(run_filter) != 'undefined' ? run_filter : true;
  116. if (typeof(textarea_id) == 'undefined' || textarea_id.length == 0 || $("#" + textarea_id).length == 0) {
  117. return;
  118. }
  119. if ((typeof(Drupal.settings.ckeditor.load_timeout) == 'undefined') && (typeof(CKEDITOR.instances[textarea_id]) != 'undefined')) {
  120. return;
  121. }
  122. if (typeof(Drupal.settings.ckeditor.elements[textarea_id]) == 'undefined') {
  123. return;
  124. }
  125. var ckeditor_obj = Drupal.settings.ckeditor;
  126. if (!CKEDITOR.env.isCompatible) {
  127. return;
  128. }
  129. if (run_filter && ($("#" + textarea_id).val().length > 0) && typeof(ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]]) != 'undefined' && ((ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]]['ss'] == 1 && typeof(Drupal.settings.ckeditor.autostart) != 'undefined' && typeof(Drupal.settings.ckeditor.autostart[textarea_id]) != 'undefined') || ckeditor_obj.input_formats[ckeditor_obj.elements[textarea_id]]['ss'] == 2)) {
  130. $.ajax({
  131. type: 'POST',
  132. url: Drupal.settings.ckeditor.xss_url,
  133. async: false,
  134. data: {
  135. text: $('#' + textarea_id).val(),
  136. input_format: ckeditor_obj.textarea_default_format[textarea_id],
  137. token: Drupal.settings.ckeditor.ajaxToken
  138. },
  139. success: function(text){
  140. $("#" + textarea_id).val(text);
  141. Drupal.ckeditorInit(textarea_id);
  142. }
  143. })
  144. }
  145. else {
  146. Drupal.ckeditorInit(textarea_id);
  147. }
  148. };
  149. /**
  150. * CKEditor destroy function
  151. *
  152. * @param string textarea_id
  153. */
  154. Drupal.ckeditorOff = function(textarea_id) {
  155. if (!CKEDITOR.instances || typeof(CKEDITOR.instances[textarea_id]) == 'undefined') {
  156. return;
  157. }
  158. if (!CKEDITOR.env.isCompatible) {
  159. return;
  160. }
  161. if (Drupal.ckeditorInstance && Drupal.ckeditorInstance.name == textarea_id)
  162. delete Drupal.ckeditorInstance;
  163. $("#" + textarea_id).val(CKEDITOR.instances[textarea_id].getData());
  164. CKEDITOR.instances[textarea_id].destroy(true);
  165. $("#" + textarea_id).next(".grippie").css("display", "block");
  166. };
  167. /**
  168. * Loading selected CKEditor plugins
  169. *
  170. * @param object textarea_settings
  171. */
  172. Drupal.ckeditorLoadPlugins = function(textarea_settings) {
  173. if (typeof(textarea_settings.extraPlugins) == 'undefined') {
  174. textarea_settings.extraPlugins = '';
  175. }
  176. if (typeof CKEDITOR.plugins != 'undefined') {
  177. for (var plugin in textarea_settings['loadPlugins']) {
  178. if (typeof(textarea_settings['loadPlugins'][plugin]['active']) == 'undefined' || textarea_settings['loadPlugins'][plugin]['active'] == 1) {
  179. textarea_settings.extraPlugins += (textarea_settings.extraPlugins) ? ',' + textarea_settings['loadPlugins'][plugin]['name'] : textarea_settings['loadPlugins'][plugin]['name'];
  180. CKEDITOR.plugins.addExternal(textarea_settings['loadPlugins'][plugin]['name'], textarea_settings['loadPlugins'][plugin]['path']);
  181. }
  182. }
  183. }
  184. return textarea_settings;
  185. }
  186. /**
  187. * Returns true if CKEDITOR.version >= version
  188. */
  189. Drupal.ckeditorCompareVersion = function (version){
  190. var ckver = CKEDITOR.version;
  191. ckver = ckver.match(/(([\d]\.)+[\d]+)/i);
  192. version = version.match(/((\d+\.)+[\d]+)/i);
  193. ckver = ckver[0].split('.');
  194. version = version[0].split('.');
  195. for (var x in ckver) {
  196. if (ckver[x]<version[x]) {
  197. return false;
  198. }
  199. else if (ckver[x]>version[x]) {
  200. return true;
  201. }
  202. }
  203. return true;
  204. };
  205. Drupal.ckeditorInsertHtml = function(html) {
  206. if (!Drupal.ckeditorInstance)
  207. return false;
  208. if (Drupal.ckeditorInstance.mode == 'wysiwyg') {
  209. Drupal.ckeditorInstance.insertHtml(html);
  210. return true;
  211. }
  212. else {
  213. alert(Drupal.t('Content can only be inserted into CKEditor in the WYSIWYG mode.'));
  214. return false;
  215. }
  216. };
  217. /**
  218. * Ajax support
  219. */
  220. if (typeof(Drupal.Ajax) != 'undefined' && typeof(Drupal.Ajax.plugins) != 'undefined') {
  221. Drupal.Ajax.plugins.CKEditor = function(hook, args) {
  222. if (hook === 'submit' && typeof(CKEDITOR.instances) != 'undefined') {
  223. for (var i in CKEDITOR.instances)
  224. CKEDITOR.instances[i].updateElement();
  225. }
  226. return true;
  227. };
  228. }
  229. //Support for Panels [#679976]
  230. Drupal.ckeditorSubmitAjaxForm = function () {
  231. if (typeof(CKEDITOR.instances) != 'undefined' && typeof(CKEDITOR.instances['edit-body']) != 'undefined') {
  232. Drupal.ckeditorOff('edit-body');
  233. }
  234. };
  235. /**
  236. * Drupal behaviors
  237. */
  238. Drupal.behaviors.ckeditor = {
  239. attach:
  240. function (context) {
  241. if ((typeof(CKEDITOR) == 'undefined') || !CKEDITOR.env.isCompatible) {
  242. return;
  243. }
  244. // make sure the textarea behavior is run first, to get a correctly sized grippie
  245. if (Drupal.behaviors.textarea && Drupal.behaviors.textarea.attach) {
  246. Drupal.behaviors.textarea.attach(context);
  247. }
  248. $(context).find("textarea.ckeditor-mod:not(.ckeditor-processed)").each(function () {
  249. var ta_id=$(this).attr("id");
  250. if (CKEDITOR.instances && typeof(CKEDITOR.instances[ta_id]) != 'undefined'){
  251. Drupal.ckeditorOff(ta_id);
  252. }
  253. if ((typeof(Drupal.settings.ckeditor.autostart) != 'undefined') && (typeof(Drupal.settings.ckeditor.autostart[ta_id]) != 'undefined')) {
  254. Drupal.ckeditorOn(ta_id);
  255. }
  256. if (typeof(Drupal.settings.ckeditor.input_formats[Drupal.settings.ckeditor.elements[ta_id]]) != 'undefined') {
  257. $('.ckeditor_links').show();
  258. }
  259. var sel_format = $("#" + ta_id.substr(0, ta_id.lastIndexOf("-")) + "-format--2");
  260. if (sel_format && sel_format.not('.ckeditor-processed')) {
  261. sel_format.addClass('ckeditor-processed').change(function() {
  262. Drupal.settings.ckeditor.elements[ta_id] = $(this).val();
  263. if (CKEDITOR.instances && typeof(CKEDITOR.instances[ta_id]) != 'undefined') {
  264. $('#'+ta_id).val(CKEDITOR.instances[ta_id].getData());
  265. }
  266. Drupal.ckeditorOff(ta_id);
  267. if (typeof(Drupal.settings.ckeditor.input_formats[$(this).val()]) != 'undefined'){
  268. if ($('#'+ta_id).hasClass('ckeditor-processed')) {
  269. Drupal.ckeditorOn(ta_id, false);
  270. }
  271. else {
  272. Drupal.ckeditorOn(ta_id);
  273. }
  274. $('#switch_'+ta_id).show();
  275. }
  276. else {
  277. $('#switch_'+ta_id).hide();
  278. }
  279. });
  280. }
  281. });
  282. },
  283. detach:
  284. function(context, settings, trigger){
  285. $(context).find("textarea.ckeditor-mod.ckeditor-processed").each(function () {
  286. var ta_id=$(this).attr("id");
  287. if (CKEDITOR.instances[ta_id])
  288. $('#'+ta_id).val(CKEDITOR.instances[ta_id].getData());
  289. if(trigger != 'serialize') {
  290. Drupal.ckeditorOff(ta_id);
  291. $(this).removeClass('ckeditor-processed');
  292. }
  293. });
  294. }
  295. };
  296. })(jQuery);
  297. /**
  298. * IMCE support
  299. */
  300. var ckeditor_imceSendTo = function (file, win){
  301. var cfunc = win.location.href.split('&');
  302. for (var x in cfunc) {
  303. if (cfunc[x].match(/^CKEditorFuncNum=\d+$/)) {
  304. cfunc = cfunc[x].split('=');
  305. break;
  306. }
  307. }
  308. CKEDITOR.tools.callFunction(cfunc[1], file.url);
  309. win.close();
  310. }