annotate core/modules/editor/js/editor.js @ 19:fa3358dc1485 tip

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