ckeditor.utils.js 13 KB

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