6448953f190acd372154c2ec9d194fb1c189984d
[yaffs-website] / web / core / modules / editor / js / editor.js
1 /**
2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
5 * @preserve
6 **/
7
8 (function ($, Drupal, drupalSettings) {
9   function findFieldForFormatSelector($formatSelector) {
10     var fieldId = $formatSelector.attr('data-editor-for');
11
12     return $('#' + fieldId).get(0);
13   }
14
15   function filterXssWhenSwitching(field, format, originalFormatID, callback) {
16     if (format.editor.isXssSafe) {
17       callback(field, format);
18     } else {
19         $.ajax({
20           url: Drupal.url('editor/filter_xss/' + format.format),
21           type: 'POST',
22           data: {
23             value: field.value,
24             original_format_id: originalFormatID
25           },
26           dataType: 'json',
27           success: function success(xssFilteredValue) {
28             if (xssFilteredValue !== false) {
29               field.value = xssFilteredValue;
30             }
31             callback(field, format);
32           }
33         });
34       }
35   }
36
37   function changeTextEditor(field, newFormatID) {
38     var previousFormatID = field.getAttribute('data-editor-active-text-format');
39
40     if (drupalSettings.editor.formats[previousFormatID]) {
41       Drupal.editorDetach(field, drupalSettings.editor.formats[previousFormatID]);
42     } else {
43         $(field).off('.editor');
44       }
45
46     if (drupalSettings.editor.formats[newFormatID]) {
47       var format = drupalSettings.editor.formats[newFormatID];
48       filterXssWhenSwitching(field, format, previousFormatID, Drupal.editorAttach);
49     }
50
51     field.setAttribute('data-editor-active-text-format', newFormatID);
52   }
53
54   function onTextFormatChange(event) {
55     var $select = $(event.target);
56     var field = event.data.field;
57     var activeFormatID = field.getAttribute('data-editor-active-text-format');
58     var newFormatID = $select.val();
59
60     if (newFormatID === activeFormatID) {
61       return;
62     }
63
64     var supportContentFiltering = drupalSettings.editor.formats[newFormatID] && drupalSettings.editor.formats[newFormatID].editorSupportsContentFiltering;
65
66     var hasContent = field.value !== '';
67     if (hasContent && supportContentFiltering) {
68       var message = Drupal.t('Changing the text format to %text_format will permanently remove content that is not allowed in that text format.<br><br>Save your changes before switching the text format to avoid losing data.', {
69         '%text_format': $select.find('option:selected').text()
70       });
71       var confirmationDialog = Drupal.dialog('<div>' + message + '</div>', {
72         title: Drupal.t('Change text format?'),
73         dialogClass: 'editor-change-text-format-modal',
74         resizable: false,
75         buttons: [{
76           text: Drupal.t('Continue'),
77           class: 'button button--primary',
78           click: function click() {
79             changeTextEditor(field, newFormatID);
80             confirmationDialog.close();
81           }
82         }, {
83           text: Drupal.t('Cancel'),
84           class: 'button',
85           click: function click() {
86             $select.val(activeFormatID);
87             confirmationDialog.close();
88           }
89         }],
90
91         closeOnEscape: false,
92         create: function create() {
93           $(this).parent().find('.ui-dialog-titlebar-close').remove();
94         },
95
96         beforeClose: false,
97         close: function close(event) {
98           $(event.target).remove();
99         }
100       });
101
102       confirmationDialog.showModal();
103     } else {
104       changeTextEditor(field, newFormatID);
105     }
106   }
107
108   Drupal.editors = {};
109
110   Drupal.behaviors.editor = {
111     attach: function attach(context, settings) {
112       if (!settings.editor) {
113         return;
114       }
115
116       $(context).find('[data-editor-for]').once('editor').each(function () {
117         var $this = $(this);
118         var field = findFieldForFormatSelector($this);
119
120         if (!field) {
121           return;
122         }
123
124         var activeFormatID = $this.val();
125         field.setAttribute('data-editor-active-text-format', activeFormatID);
126
127         if (settings.editor.formats[activeFormatID]) {
128           Drupal.editorAttach(field, settings.editor.formats[activeFormatID]);
129         }
130
131         $(field).on('change.editor keypress.editor', function () {
132           field.setAttribute('data-editor-value-is-changed', 'true');
133
134           $(field).off('.editor');
135         });
136
137         if ($this.is('select')) {
138           $this.on('change.editorAttach', { field: field }, onTextFormatChange);
139         }
140
141         $this.parents('form').on('submit', function (event) {
142           if (event.isDefaultPrevented()) {
143             return;
144           }
145
146           if (settings.editor.formats[activeFormatID]) {
147             Drupal.editorDetach(field, settings.editor.formats[activeFormatID], 'serialize');
148           }
149         });
150       });
151     },
152     detach: function detach(context, settings, trigger) {
153       var editors = void 0;
154
155       if (trigger === 'serialize') {
156         editors = $(context).find('[data-editor-for]').findOnce('editor');
157       } else {
158         editors = $(context).find('[data-editor-for]').removeOnce('editor');
159       }
160
161       editors.each(function () {
162         var $this = $(this);
163         var activeFormatID = $this.val();
164         var field = findFieldForFormatSelector($this);
165         if (field && activeFormatID in settings.editor.formats) {
166           Drupal.editorDetach(field, settings.editor.formats[activeFormatID], trigger);
167         }
168       });
169     }
170   };
171
172   Drupal.editorAttach = function (field, format) {
173     if (format.editor) {
174       Drupal.editors[format.editor].attach(field, format);
175
176       Drupal.editors[format.editor].onChange(field, function () {
177         $(field).trigger('formUpdated');
178
179         field.setAttribute('data-editor-value-is-changed', 'true');
180       });
181     }
182   };
183
184   Drupal.editorDetach = function (field, format, trigger) {
185     if (format.editor) {
186       Drupal.editors[format.editor].detach(field, format, trigger);
187
188       if (field.getAttribute('data-editor-value-is-changed') === 'false') {
189         field.value = field.getAttribute('data-editor-value-original');
190       }
191     }
192   };
193 })(jQuery, Drupal, drupalSettings);