Patched to Drupal 8.4.8 level. See https://www.drupal.org/sa-core-2018-004 and patch...
[yaffs-website] / web / core / modules / editor / js / editor.formattedTextEditor.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   Drupal.quickedit.editors.editor = Drupal.quickedit.EditorView.extend({
10     textFormat: null,
11
12     textFormatHasTransformations: null,
13
14     textEditor: null,
15
16     $textElement: null,
17
18     initialize: function initialize(options) {
19       Drupal.quickedit.EditorView.prototype.initialize.call(this, options);
20
21       var metadata = Drupal.quickedit.metadata.get(this.fieldModel.get('fieldID'), 'custom');
22       this.textFormat = drupalSettings.editor.formats[metadata.format];
23       this.textFormatHasTransformations = metadata.formatHasTransformations;
24       this.textEditor = Drupal.editors[this.textFormat.editor];
25
26       var $fieldItems = this.$el.find('.quickedit-field');
27       if ($fieldItems.length) {
28         this.$textElement = $fieldItems.eq(0);
29       } else {
30         this.$textElement = this.$el;
31       }
32       this.model.set('originalValue', this.$textElement.html());
33     },
34     getEditedElement: function getEditedElement() {
35       return this.$textElement;
36     },
37     stateChange: function stateChange(fieldModel, state) {
38       var editorModel = this.model;
39       var from = fieldModel.previous('state');
40       var to = state;
41       switch (to) {
42         case 'inactive':
43           break;
44
45         case 'candidate':
46           if (from !== 'inactive' && from !== 'highlighted') {
47             this.textEditor.detach(this.$textElement.get(0), this.textFormat);
48           }
49
50           if (from === 'active' && this.textFormatHasTransformations) {
51             this.revert();
52           }
53           if (from === 'invalid') {
54             this.removeValidationErrors();
55           }
56           break;
57
58         case 'highlighted':
59           break;
60
61         case 'activating':
62           if (this.textFormatHasTransformations) {
63             var $textElement = this.$textElement;
64             this._getUntransformedText(function (untransformedText) {
65               $textElement.html(untransformedText);
66               fieldModel.set('state', 'active');
67             });
68           } else {
69               _.defer(function () {
70                 fieldModel.set('state', 'active');
71               });
72             }
73           break;
74
75         case 'active':
76           var textElement = this.$textElement.get(0);
77           var toolbarView = fieldModel.toolbarView;
78           this.textEditor.attachInlineEditor(textElement, this.textFormat, toolbarView.getMainWysiwygToolgroupId(), toolbarView.getFloatedWysiwygToolgroupId());
79
80           this.textEditor.onChange(textElement, function (htmlText) {
81             editorModel.set('currentValue', htmlText);
82             fieldModel.set('state', 'changed');
83           });
84           break;
85
86         case 'changed':
87           break;
88
89         case 'saving':
90           if (from === 'invalid') {
91             this.removeValidationErrors();
92           }
93           this.save();
94           break;
95
96         case 'saved':
97           break;
98
99         case 'invalid':
100           this.showValidationErrors();
101           break;
102       }
103     },
104     getQuickEditUISettings: function getQuickEditUISettings() {
105       return { padding: true, unifiedToolbar: true, fullWidthToolbar: true, popup: false };
106     },
107     revert: function revert() {
108       this.$textElement.html(this.model.get('originalValue'));
109     },
110     _getUntransformedText: function _getUntransformedText(callback) {
111       var fieldID = this.fieldModel.get('fieldID');
112
113       var textLoaderAjax = Drupal.ajax({
114         url: Drupal.quickedit.util.buildUrl(fieldID, Drupal.url('editor/!entity_type/!id/!field_name/!langcode/!view_mode')),
115         submit: { nocssjs: true }
116       });
117
118       textLoaderAjax.commands.editorGetUntransformedText = function (ajax, response, status) {
119         callback(response.data);
120       };
121
122       textLoaderAjax.execute();
123     }
124   });
125 })(jQuery, Drupal, drupalSettings, _);