2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
8 (function ($, Drupal, drupalSettings) {
9 function findFieldForFormatSelector($formatSelector) {
10 var fieldId = $formatSelector.attr('data-editor-for');
12 return $('#' + fieldId).get(0);
15 function filterXssWhenSwitching(field, format, originalFormatID, callback) {
16 if (format.editor.isXssSafe) {
17 callback(field, format);
20 url: Drupal.url('editor/filter_xss/' + format.format),
24 original_format_id: originalFormatID
27 success: function success(xssFilteredValue) {
28 if (xssFilteredValue !== false) {
29 field.value = xssFilteredValue;
31 callback(field, format);
37 function changeTextEditor(field, newFormatID) {
38 var previousFormatID = field.getAttribute('data-editor-active-text-format');
40 if (drupalSettings.editor.formats[previousFormatID]) {
41 Drupal.editorDetach(field, drupalSettings.editor.formats[previousFormatID]);
43 $(field).off('.editor');
46 if (drupalSettings.editor.formats[newFormatID]) {
47 var format = drupalSettings.editor.formats[newFormatID];
48 filterXssWhenSwitching(field, format, previousFormatID, Drupal.editorAttach);
51 field.setAttribute('data-editor-active-text-format', newFormatID);
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();
60 if (newFormatID === activeFormatID) {
64 var supportContentFiltering = drupalSettings.editor.formats[newFormatID] && drupalSettings.editor.formats[newFormatID].editorSupportsContentFiltering;
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()
71 var confirmationDialog = Drupal.dialog('<div>' + message + '</div>', {
72 title: Drupal.t('Change text format?'),
73 dialogClass: 'editor-change-text-format-modal',
76 text: Drupal.t('Continue'),
77 class: 'button button--primary',
78 click: function click() {
79 changeTextEditor(field, newFormatID);
80 confirmationDialog.close();
83 text: Drupal.t('Cancel'),
85 click: function click() {
86 $select.val(activeFormatID);
87 confirmationDialog.close();
92 create: function create() {
93 $(this).parent().find('.ui-dialog-titlebar-close').remove();
97 close: function close(event) {
98 $(event.target).remove();
102 confirmationDialog.showModal();
104 changeTextEditor(field, newFormatID);
110 Drupal.behaviors.editor = {
111 attach: function attach(context, settings) {
112 if (!settings.editor) {
116 $(context).find('[data-editor-for]').once('editor').each(function () {
118 var field = findFieldForFormatSelector($this);
124 var activeFormatID = $this.val();
125 field.setAttribute('data-editor-active-text-format', activeFormatID);
127 if (settings.editor.formats[activeFormatID]) {
128 Drupal.editorAttach(field, settings.editor.formats[activeFormatID]);
131 $(field).on('change.editor keypress.editor', function () {
132 field.setAttribute('data-editor-value-is-changed', 'true');
134 $(field).off('.editor');
137 if ($this.is('select')) {
138 $this.on('change.editorAttach', { field: field }, onTextFormatChange);
141 $this.parents('form').on('submit', function (event) {
142 if (event.isDefaultPrevented()) {
146 if (settings.editor.formats[activeFormatID]) {
147 Drupal.editorDetach(field, settings.editor.formats[activeFormatID], 'serialize');
152 detach: function detach(context, settings, trigger) {
153 var editors = void 0;
155 if (trigger === 'serialize') {
156 editors = $(context).find('[data-editor-for]').findOnce('editor');
158 editors = $(context).find('[data-editor-for]').removeOnce('editor');
161 editors.each(function () {
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);
172 Drupal.editorAttach = function (field, format) {
174 Drupal.editors[format.editor].attach(field, format);
176 Drupal.editors[format.editor].onChange(field, function () {
177 $(field).trigger('formUpdated');
179 field.setAttribute('data-editor-value-is-changed', 'true');
184 Drupal.editorDetach = function (field, format, trigger) {
186 Drupal.editors[format.editor].detach(field, format, trigger);
188 if (field.getAttribute('data-editor-value-is-changed') === 'false') {
189 field.value = field.getAttribute('data-editor-value-original');
193 })(jQuery, Drupal, drupalSettings);