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