Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / image / js / editors / image.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) {
9   Drupal.quickedit.editors.image = Drupal.quickedit.EditorView.extend({
10     initialize: function initialize(options) {
11       Drupal.quickedit.EditorView.prototype.initialize.call(this, options);
12
13       this.model.set('originalValue', this.$el.html().trim());
14
15       this.model.set('currentValue', function (index, value) {
16         var matches = $(this).attr('name').match(/(alt|title)]$/);
17         if (matches) {
18           var name = matches[1];
19           var $toolgroup = $('#' + options.fieldModel.toolbarView.getMainWysiwygToolgroupId());
20           var $input = $toolgroup.find('.quickedit-image-field-info input[name="' + name + '"]');
21           if ($input.length) {
22             return $input.val();
23           }
24         }
25       });
26     },
27     stateChange: function stateChange(fieldModel, state, options) {
28       var from = fieldModel.previous('state');
29       switch (state) {
30         case 'inactive':
31           break;
32
33         case 'candidate':
34           if (from !== 'inactive') {
35             this.$el.find('.quickedit-image-dropzone').remove();
36             this.$el.removeClass('quickedit-image-element');
37           }
38           if (from === 'invalid') {
39             this.removeValidationErrors();
40           }
41           break;
42
43         case 'highlighted':
44           break;
45
46         case 'activating':
47           _.defer(function () {
48             fieldModel.set('state', 'active');
49           });
50           break;
51
52         case 'active':
53           var self = this;
54
55           this.$el.addClass('quickedit-image-element');
56
57           var $dropzone = this.renderDropzone('upload', Drupal.t('Drop file here or click to upload'));
58
59           $dropzone.on('dragenter', function (e) {
60             $(this).addClass('hover');
61           });
62           $dropzone.on('dragleave', function (e) {
63             $(this).removeClass('hover');
64           });
65
66           $dropzone.on('drop', function (e) {
67             if (e.originalEvent.dataTransfer && e.originalEvent.dataTransfer.files.length) {
68               $(this).removeClass('hover');
69               self.uploadImage(e.originalEvent.dataTransfer.files[0]);
70             }
71           });
72
73           $dropzone.on('click', function (e) {
74             $('<input type="file">').trigger('click').on('change', function () {
75               if (this.files.length) {
76                 self.uploadImage(this.files[0]);
77               }
78             });
79           });
80
81           $dropzone.on('dragover dragenter dragleave drop click', function (e) {
82             e.preventDefault();
83             e.stopPropagation();
84           });
85
86           this.renderToolbar(fieldModel);
87           break;
88
89         case 'changed':
90           break;
91
92         case 'saving':
93           if (from === 'invalid') {
94             this.removeValidationErrors();
95           }
96
97           this.save(options);
98           break;
99
100         case 'saved':
101           break;
102
103         case 'invalid':
104           this.showValidationErrors();
105           break;
106       }
107     },
108     uploadImage: function uploadImage(file) {
109       this.renderDropzone('upload loading', Drupal.t('Uploading <i>@file</i>…', { '@file': file.name }));
110
111       var fieldID = this.fieldModel.get('fieldID');
112       var url = Drupal.quickedit.util.buildUrl(fieldID, Drupal.url('quickedit/image/upload/!entity_type/!id/!field_name/!langcode/!view_mode'));
113
114       var data = new FormData();
115       data.append('files[image]', file);
116
117       var self = this;
118       this.ajax({
119         type: 'POST',
120         url: url,
121         data: data,
122         success: function success(response) {
123           var $el = $(self.fieldModel.get('el'));
124
125           self.fieldModel.set('state', 'changed');
126           self.fieldModel.get('entity').set('inTempStore', true);
127           self.removeValidationErrors();
128
129           var $content = $(response.html).closest('[data-quickedit-field-id]').children();
130           $el.empty().append($content);
131         }
132       });
133     },
134     ajax: function ajax(options) {
135       var defaultOptions = {
136         context: this,
137         dataType: 'json',
138         cache: false,
139         contentType: false,
140         processData: false,
141         error: function error() {
142           this.renderDropzone('error', Drupal.t('A server error has occurred.'));
143         }
144       };
145
146       var ajaxOptions = $.extend(defaultOptions, options);
147       var successCallback = ajaxOptions.success;
148
149       ajaxOptions.success = function (response) {
150         if (response.main_error) {
151           this.renderDropzone('error', response.main_error);
152           if (response.errors.length) {
153             this.model.set('validationErrors', response.errors);
154           }
155           this.showValidationErrors();
156         } else {
157           successCallback(response);
158         }
159       };
160
161       $.ajax(ajaxOptions);
162     },
163     renderToolbar: function renderToolbar(fieldModel) {
164       var $toolgroup = $('#' + fieldModel.toolbarView.getMainWysiwygToolgroupId());
165       var $toolbar = $toolgroup.find('.quickedit-image-field-info');
166       if ($toolbar.length === 0) {
167         var fieldID = fieldModel.get('fieldID');
168         var url = Drupal.quickedit.util.buildUrl(fieldID, Drupal.url('quickedit/image/info/!entity_type/!id/!field_name/!langcode/!view_mode'));
169         var self = this;
170         self.ajax({
171           type: 'GET',
172           url: url,
173           success: function success(response) {
174             $toolbar = $(Drupal.theme.quickeditImageToolbar(response));
175             $toolgroup.append($toolbar);
176             $toolbar.on('keyup paste', function () {
177               fieldModel.set('state', 'changed');
178             });
179
180             fieldModel.get('entity').toolbarView.position();
181           }
182         });
183       }
184     },
185     renderDropzone: function renderDropzone(state, text) {
186       var $dropzone = this.$el.find('.quickedit-image-dropzone');
187
188       if ($dropzone.length) {
189         $dropzone.removeClass('upload error hover loading').addClass('.quickedit-image-dropzone ' + state).children('.quickedit-image-text').html(text);
190       } else {
191         $dropzone = $(Drupal.theme('quickeditImageDropzone', {
192           state: state,
193           text: text
194         }));
195         this.$el.append($dropzone);
196       }
197
198       return $dropzone;
199     },
200     revert: function revert() {
201       this.$el.html(this.model.get('originalValue'));
202     },
203     getQuickEditUISettings: function getQuickEditUISettings() {
204       return { padding: false, unifiedToolbar: true, fullWidthToolbar: true, popup: false };
205     },
206     showValidationErrors: function showValidationErrors() {
207       var errors = Drupal.theme('quickeditImageErrors', {
208         errors: this.model.get('validationErrors')
209       });
210       $('#' + this.fieldModel.toolbarView.getMainWysiwygToolgroupId()).append(errors);
211       this.getEditedElement().addClass('quickedit-validation-error');
212
213       this.fieldModel.get('entity').toolbarView.position();
214     },
215     removeValidationErrors: function removeValidationErrors() {
216       $('#' + this.fieldModel.toolbarView.getMainWysiwygToolgroupId()).find('.quickedit-image-errors').remove();
217       this.getEditedElement().removeClass('quickedit-validation-error');
218     }
219   });
220 })(jQuery, _, Drupal);