Chris@0: /**
Chris@0: * DO NOT EDIT THIS FILE.
Chris@0: * See the following change record for more information,
Chris@0: * https://www.drupal.org/node/2815083
Chris@0: * @preserve
Chris@0: **/
Chris@0:
Chris@0: (function ($, Drupal, drupalSettings) {
Chris@0: function findFieldForFormatSelector($formatSelector) {
Chris@14: var fieldId = $formatSelector.attr('data-editor-for');
Chris@0:
Chris@14: return $('#' + fieldId).get(0);
Chris@0: }
Chris@0:
Chris@17: function filterXssWhenSwitching(field, format, originalFormatID, callback) {
Chris@17: if (format.editor.isXssSafe) {
Chris@17: callback(field, format);
Chris@17: } else {
Chris@17: $.ajax({
Chris@17: url: Drupal.url('editor/filter_xss/' + format.format),
Chris@17: type: 'POST',
Chris@17: data: {
Chris@17: value: field.value,
Chris@17: original_format_id: originalFormatID
Chris@17: },
Chris@17: dataType: 'json',
Chris@17: success: function success(xssFilteredValue) {
Chris@17: if (xssFilteredValue !== false) {
Chris@17: field.value = xssFilteredValue;
Chris@17: }
Chris@17: callback(field, format);
Chris@17: }
Chris@17: });
Chris@17: }
Chris@17: }
Chris@17:
Chris@0: function changeTextEditor(field, newFormatID) {
Chris@0: var previousFormatID = field.getAttribute('data-editor-active-text-format');
Chris@0:
Chris@0: if (drupalSettings.editor.formats[previousFormatID]) {
Chris@0: Drupal.editorDetach(field, drupalSettings.editor.formats[previousFormatID]);
Chris@0: } else {
Chris@0: $(field).off('.editor');
Chris@0: }
Chris@0:
Chris@0: if (drupalSettings.editor.formats[newFormatID]) {
Chris@0: var format = drupalSettings.editor.formats[newFormatID];
Chris@0: filterXssWhenSwitching(field, format, previousFormatID, Drupal.editorAttach);
Chris@0: }
Chris@0:
Chris@0: field.setAttribute('data-editor-active-text-format', newFormatID);
Chris@0: }
Chris@0:
Chris@0: function onTextFormatChange(event) {
Chris@0: var $select = $(event.target);
Chris@0: var field = event.data.field;
Chris@0: var activeFormatID = field.getAttribute('data-editor-active-text-format');
Chris@0: var newFormatID = $select.val();
Chris@0:
Chris@0: if (newFormatID === activeFormatID) {
Chris@0: return;
Chris@0: }
Chris@0:
Chris@0: var supportContentFiltering = drupalSettings.editor.formats[newFormatID] && drupalSettings.editor.formats[newFormatID].editorSupportsContentFiltering;
Chris@0:
Chris@0: var hasContent = field.value !== '';
Chris@0: if (hasContent && supportContentFiltering) {
Chris@0: var message = Drupal.t('Changing the text format to %text_format will permanently remove content that is not allowed in that text format.
Save your changes before switching the text format to avoid losing data.', {
Chris@0: '%text_format': $select.find('option:selected').text()
Chris@0: });
Chris@0: var confirmationDialog = Drupal.dialog('
' + message + '
', {
Chris@0: title: Drupal.t('Change text format?'),
Chris@0: dialogClass: 'editor-change-text-format-modal',
Chris@0: resizable: false,
Chris@0: buttons: [{
Chris@0: text: Drupal.t('Continue'),
Chris@0: class: 'button button--primary',
Chris@0: click: function click() {
Chris@0: changeTextEditor(field, newFormatID);
Chris@0: confirmationDialog.close();
Chris@0: }
Chris@0: }, {
Chris@0: text: Drupal.t('Cancel'),
Chris@0: class: 'button',
Chris@0: click: function click() {
Chris@0: $select.val(activeFormatID);
Chris@0: confirmationDialog.close();
Chris@0: }
Chris@0: }],
Chris@0:
Chris@0: closeOnEscape: false,
Chris@0: create: function create() {
Chris@0: $(this).parent().find('.ui-dialog-titlebar-close').remove();
Chris@0: },
Chris@0:
Chris@0: beforeClose: false,
Chris@0: close: function close(event) {
Chris@0: $(event.target).remove();
Chris@0: }
Chris@0: });
Chris@0:
Chris@0: confirmationDialog.showModal();
Chris@0: } else {
Chris@0: changeTextEditor(field, newFormatID);
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: Drupal.editors = {};
Chris@0:
Chris@0: Drupal.behaviors.editor = {
Chris@0: attach: function attach(context, settings) {
Chris@0: if (!settings.editor) {
Chris@0: return;
Chris@0: }
Chris@0:
Chris@0: $(context).find('[data-editor-for]').once('editor').each(function () {
Chris@0: var $this = $(this);
Chris@0: var field = findFieldForFormatSelector($this);
Chris@0:
Chris@0: if (!field) {
Chris@0: return;
Chris@0: }
Chris@0:
Chris@0: var activeFormatID = $this.val();
Chris@0: field.setAttribute('data-editor-active-text-format', activeFormatID);
Chris@0:
Chris@0: if (settings.editor.formats[activeFormatID]) {
Chris@0: Drupal.editorAttach(field, settings.editor.formats[activeFormatID]);
Chris@0: }
Chris@0:
Chris@0: $(field).on('change.editor keypress.editor', function () {
Chris@0: field.setAttribute('data-editor-value-is-changed', 'true');
Chris@0:
Chris@0: $(field).off('.editor');
Chris@0: });
Chris@0:
Chris@0: if ($this.is('select')) {
Chris@0: $this.on('change.editorAttach', { field: field }, onTextFormatChange);
Chris@0: }
Chris@0:
Chris@0: $this.parents('form').on('submit', function (event) {
Chris@0: if (event.isDefaultPrevented()) {
Chris@0: return;
Chris@0: }
Chris@0:
Chris@0: if (settings.editor.formats[activeFormatID]) {
Chris@0: Drupal.editorDetach(field, settings.editor.formats[activeFormatID], 'serialize');
Chris@0: }
Chris@0: });
Chris@0: });
Chris@0: },
Chris@0: detach: function detach(context, settings, trigger) {
Chris@0: var editors = void 0;
Chris@0:
Chris@0: if (trigger === 'serialize') {
Chris@0: editors = $(context).find('[data-editor-for]').findOnce('editor');
Chris@0: } else {
Chris@0: editors = $(context).find('[data-editor-for]').removeOnce('editor');
Chris@0: }
Chris@0:
Chris@0: editors.each(function () {
Chris@0: var $this = $(this);
Chris@0: var activeFormatID = $this.val();
Chris@0: var field = findFieldForFormatSelector($this);
Chris@0: if (field && activeFormatID in settings.editor.formats) {
Chris@0: Drupal.editorDetach(field, settings.editor.formats[activeFormatID], trigger);
Chris@0: }
Chris@0: });
Chris@0: }
Chris@0: };
Chris@0:
Chris@0: Drupal.editorAttach = function (field, format) {
Chris@0: if (format.editor) {
Chris@0: Drupal.editors[format.editor].attach(field, format);
Chris@0:
Chris@0: Drupal.editors[format.editor].onChange(field, function () {
Chris@0: $(field).trigger('formUpdated');
Chris@0:
Chris@0: field.setAttribute('data-editor-value-is-changed', 'true');
Chris@0: });
Chris@0: }
Chris@0: };
Chris@0:
Chris@0: Drupal.editorDetach = function (field, format, trigger) {
Chris@0: if (format.editor) {
Chris@0: Drupal.editors[format.editor].detach(field, format, trigger);
Chris@0:
Chris@0: if (field.getAttribute('data-editor-value-is-changed') === 'false') {
Chris@0: field.value = field.getAttribute('data-editor-value-original');
Chris@0: }
Chris@0: }
Chris@0: };
Chris@0: })(jQuery, Drupal, drupalSettings);