Updated to Drupal 8.5. Core Media not yet in use.
[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           {
77             var textElement = this.$textElement.get(0);
78             var toolbarView = fieldModel.toolbarView;
79             this.textEditor.attachInlineEditor(textElement, this.textFormat, toolbarView.getMainWysiwygToolgroupId(), toolbarView.getFloatedWysiwygToolgroupId());
80
81             this.textEditor.onChange(textElement, function (htmlText) {
82               editorModel.set('currentValue', htmlText);
83               fieldModel.set('state', 'changed');
84             });
85             break;
86           }
87
88         case 'changed':
89           break;
90
91         case 'saving':
92           if (from === 'invalid') {
93             this.removeValidationErrors();
94           }
95           this.save();
96           break;
97
98         case 'saved':
99           break;
100
101         case 'invalid':
102           this.showValidationErrors();
103           break;
104       }
105     },
106     getQuickEditUISettings: function getQuickEditUISettings() {
107       return { padding: true, unifiedToolbar: true, fullWidthToolbar: true, popup: false };
108     },
109     revert: function revert() {
110       this.$textElement.html(this.model.get('originalValue'));
111     },
112     _getUntransformedText: function _getUntransformedText(callback) {
113       var fieldID = this.fieldModel.get('fieldID');
114
115       var textLoaderAjax = Drupal.ajax({
116         url: Drupal.quickedit.util.buildUrl(fieldID, Drupal.url('editor/!entity_type/!id/!field_name/!langcode/!view_mode')),
117         submit: { nocssjs: true }
118       });
119
120       textLoaderAjax.commands.editorGetUntransformedText = function (ajax, response, status) {
121         callback(response.data);
122       };
123
124       textLoaderAjax.execute();
125     }
126   });
127 })(jQuery, Drupal, drupalSettings, _);