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 changeTextEditor(field, newFormatID) {
16 var previousFormatID = field.getAttribute('data-editor-active-text-format');
18 if (drupalSettings.editor.formats[previousFormatID]) {
19 Drupal.editorDetach(field, drupalSettings.editor.formats[previousFormatID]);
21 $(field).off('.editor');
24 if (drupalSettings.editor.formats[newFormatID]) {
25 var format = drupalSettings.editor.formats[newFormatID];
26 filterXssWhenSwitching(field, format, previousFormatID, Drupal.editorAttach);
29 field.setAttribute('data-editor-active-text-format', newFormatID);
32 function onTextFormatChange(event) {
33 var $select = $(event.target);
34 var field = event.data.field;
35 var activeFormatID = field.getAttribute('data-editor-active-text-format');
36 var newFormatID = $select.val();
38 if (newFormatID === activeFormatID) {
42 var supportContentFiltering = drupalSettings.editor.formats[newFormatID] && drupalSettings.editor.formats[newFormatID].editorSupportsContentFiltering;
44 var hasContent = field.value !== '';
45 if (hasContent && supportContentFiltering) {
46 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.', {
47 '%text_format': $select.find('option:selected').text()
49 var confirmationDialog = Drupal.dialog('<div>' + message + '</div>', {
50 title: Drupal.t('Change text format?'),
51 dialogClass: 'editor-change-text-format-modal',
54 text: Drupal.t('Continue'),
55 class: 'button button--primary',
56 click: function click() {
57 changeTextEditor(field, newFormatID);
58 confirmationDialog.close();
61 text: Drupal.t('Cancel'),
63 click: function click() {
64 $select.val(activeFormatID);
65 confirmationDialog.close();
70 create: function create() {
71 $(this).parent().find('.ui-dialog-titlebar-close').remove();
75 close: function close(event) {
76 $(event.target).remove();
80 confirmationDialog.showModal();
82 changeTextEditor(field, newFormatID);
88 Drupal.behaviors.editor = {
89 attach: function attach(context, settings) {
90 if (!settings.editor) {
94 $(context).find('[data-editor-for]').once('editor').each(function () {
96 var field = findFieldForFormatSelector($this);
102 var activeFormatID = $this.val();
103 field.setAttribute('data-editor-active-text-format', activeFormatID);
105 if (settings.editor.formats[activeFormatID]) {
106 Drupal.editorAttach(field, settings.editor.formats[activeFormatID]);
109 $(field).on('change.editor keypress.editor', function () {
110 field.setAttribute('data-editor-value-is-changed', 'true');
112 $(field).off('.editor');
115 if ($this.is('select')) {
116 $this.on('change.editorAttach', { field: field }, onTextFormatChange);
119 $this.parents('form').on('submit', function (event) {
120 if (event.isDefaultPrevented()) {
124 if (settings.editor.formats[activeFormatID]) {
125 Drupal.editorDetach(field, settings.editor.formats[activeFormatID], 'serialize');
130 detach: function detach(context, settings, trigger) {
131 var editors = void 0;
133 if (trigger === 'serialize') {
134 editors = $(context).find('[data-editor-for]').findOnce('editor');
136 editors = $(context).find('[data-editor-for]').removeOnce('editor');
139 editors.each(function () {
141 var activeFormatID = $this.val();
142 var field = findFieldForFormatSelector($this);
143 if (field && activeFormatID in settings.editor.formats) {
144 Drupal.editorDetach(field, settings.editor.formats[activeFormatID], trigger);
150 Drupal.editorAttach = function (field, format) {
152 Drupal.editors[format.editor].attach(field, format);
154 Drupal.editors[format.editor].onChange(field, function () {
155 $(field).trigger('formUpdated');
157 field.setAttribute('data-editor-value-is-changed', 'true');
162 Drupal.editorDetach = function (field, format, trigger) {
164 Drupal.editors[format.editor].detach(field, format, trigger);
166 if (field.getAttribute('data-editor-value-is-changed') === 'false') {
167 field.value = field.getAttribute('data-editor-value-original');
172 function filterXssWhenSwitching(field, format, originalFormatID, callback) {
173 if (format.editor.isXssSafe) {
174 callback(field, format);
177 url: Drupal.url('editor/filter_xss/' + format.format),
181 original_format_id: originalFormatID
184 success: function success(xssFilteredValue) {
185 if (xssFilteredValue !== false) {
186 field.value = xssFilteredValue;
188 callback(field, format);
193 })(jQuery, Drupal, drupalSettings);