ckeditor-init.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* global CKEDITOR */
  2. ;(function() {
  3. var el = document.getElementById('ckeditor-init-script');
  4. if (el && !window.CKEDITOR_BASEPATH) {
  5. window.CKEDITOR_BASEPATH = el.getAttribute('data-ckeditor-basepath');
  6. }
  7. // Polyfill from https://developer.mozilla.org/en/docs/Web/API/Element/matches
  8. if (!Element.prototype.matches) {
  9. Element.prototype.matches =
  10. Element.prototype.matchesSelector ||
  11. Element.prototype.mozMatchesSelector ||
  12. Element.prototype.msMatchesSelector ||
  13. Element.prototype.oMatchesSelector ||
  14. Element.prototype.webkitMatchesSelector ||
  15. function(s) {
  16. var matches = (this.document || this.ownerDocument).querySelectorAll(s),
  17. i = matches.length;
  18. while (--i >= 0 && matches.item(i) !== this) {}
  19. return i > -1;
  20. };
  21. }
  22. function runInitialisers() {
  23. initialiseCKEditor();
  24. initialiseCKEditorInInlinedForms();
  25. }
  26. if (document.readyState != 'loading') {
  27. runInitialisers();
  28. } else {
  29. document.addEventListener('DOMContentLoaded', runInitialisers);
  30. }
  31. function initialiseCKEditor() {
  32. var textareas = Array.prototype.slice.call(document.querySelectorAll('textarea[data-type=ckeditortype]'));
  33. for (var i=0; i<textareas.length; ++i) {
  34. var t = textareas[i];
  35. if (t.getAttribute('data-processed') == '0' && t.id.indexOf('__prefix__') == -1) {
  36. t.setAttribute('data-processed', '1');
  37. var ext = JSON.parse(t.getAttribute('data-external-plugin-resources'));
  38. for (var j=0; j<ext.length; ++j) {
  39. CKEDITOR.plugins.addExternal(ext[j][0], ext[j][1], ext[j][2]);
  40. }
  41. CKEDITOR.replace(t.id, JSON.parse(t.getAttribute('data-config')));
  42. }
  43. }
  44. }
  45. function initialiseCKEditorInInlinedForms() {
  46. document.body.addEventListener('click', function(e) {
  47. if (e.target && (
  48. e.target.matches('.add-row a') ||
  49. e.target.matches('.grp-add-handler')
  50. )) {
  51. initialiseCKEditor();
  52. }
  53. });
  54. }
  55. }());