2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
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);
13 this.model.set('originalValue', this.$el.html().trim());
15 this.model.set('currentValue', function (index, value) {
16 var matches = $(this).attr('name').match(/(alt|title)]$/);
18 var name = matches[1];
19 var $toolgroup = $('#' + options.fieldModel.toolbarView.getMainWysiwygToolgroupId());
20 var $input = $toolgroup.find('.quickedit-image-field-info input[name="' + name + '"]');
27 stateChange: function stateChange(fieldModel, state, options) {
28 var from = fieldModel.previous('state');
34 if (from !== 'inactive') {
35 this.$el.find('.quickedit-image-dropzone').remove();
36 this.$el.removeClass('quickedit-image-element');
38 if (from === 'invalid') {
39 this.removeValidationErrors();
48 fieldModel.set('state', 'active');
56 this.$el.addClass('quickedit-image-element');
58 var $dropzone = this.renderDropzone('upload', Drupal.t('Drop file here or click to upload'));
60 $dropzone.on('dragenter', function (e) {
61 $(this).addClass('hover');
63 $dropzone.on('dragleave', function (e) {
64 $(this).removeClass('hover');
67 $dropzone.on('drop', function (e) {
68 if (e.originalEvent.dataTransfer && e.originalEvent.dataTransfer.files.length) {
69 $(this).removeClass('hover');
70 self.uploadImage(e.originalEvent.dataTransfer.files[0]);
74 $dropzone.on('click', function (e) {
75 $('<input type="file">').trigger('click').on('change', function () {
76 if (this.files.length) {
77 self.uploadImage(this.files[0]);
82 $dropzone.on('dragover dragenter dragleave drop click', function (e) {
87 this.renderToolbar(fieldModel);
95 if (from === 'invalid') {
96 this.removeValidationErrors();
106 this.showValidationErrors();
110 uploadImage: function uploadImage(file) {
111 this.renderDropzone('upload loading', Drupal.t('Uploading <i>@file</i>…', { '@file': file.name }));
113 var fieldID = this.fieldModel.get('fieldID');
114 var url = Drupal.quickedit.util.buildUrl(fieldID, Drupal.url('quickedit/image/upload/!entity_type/!id/!field_name/!langcode/!view_mode'));
116 var data = new FormData();
117 data.append('files[image]', file);
124 success: function success(response) {
125 var $el = $(self.fieldModel.get('el'));
127 self.fieldModel.set('state', 'changed');
128 self.fieldModel.get('entity').set('inTempStore', true);
129 self.removeValidationErrors();
131 var $content = $(response.html).closest('[data-quickedit-field-id]').children();
132 $el.empty().append($content);
136 ajax: function ajax(options) {
137 var defaultOptions = {
143 error: function error() {
144 this.renderDropzone('error', Drupal.t('A server error has occurred.'));
148 var ajaxOptions = $.extend(defaultOptions, options);
149 var successCallback = ajaxOptions.success;
151 ajaxOptions.success = function (response) {
152 if (response.main_error) {
153 this.renderDropzone('error', response.main_error);
154 if (response.errors.length) {
155 this.model.set('validationErrors', response.errors);
157 this.showValidationErrors();
159 successCallback(response);
165 renderToolbar: function renderToolbar(fieldModel) {
166 var $toolgroup = $('#' + fieldModel.toolbarView.getMainWysiwygToolgroupId());
167 var $toolbar = $toolgroup.find('.quickedit-image-field-info');
168 if ($toolbar.length === 0) {
169 var fieldID = fieldModel.get('fieldID');
170 var url = Drupal.quickedit.util.buildUrl(fieldID, Drupal.url('quickedit/image/info/!entity_type/!id/!field_name/!langcode/!view_mode'));
175 success: function success(response) {
176 $toolbar = $(Drupal.theme.quickeditImageToolbar(response));
177 $toolgroup.append($toolbar);
178 $toolbar.on('keyup paste', function () {
179 fieldModel.set('state', 'changed');
182 fieldModel.get('entity').toolbarView.position();
187 renderDropzone: function renderDropzone(state, text) {
188 var $dropzone = this.$el.find('.quickedit-image-dropzone');
190 if ($dropzone.length) {
191 $dropzone.removeClass('upload error hover loading').addClass('.quickedit-image-dropzone ' + state).children('.quickedit-image-text').html(text);
193 $dropzone = $(Drupal.theme('quickeditImageDropzone', {
197 this.$el.append($dropzone);
202 revert: function revert() {
203 this.$el.html(this.model.get('originalValue'));
205 getQuickEditUISettings: function getQuickEditUISettings() {
208 unifiedToolbar: true,
209 fullWidthToolbar: true,
213 showValidationErrors: function showValidationErrors() {
214 var errors = Drupal.theme('quickeditImageErrors', {
215 errors: this.model.get('validationErrors')
217 $('#' + this.fieldModel.toolbarView.getMainWysiwygToolgroupId()).append(errors);
218 this.getEditedElement().addClass('quickedit-validation-error');
220 this.fieldModel.get('entity').toolbarView.position();
222 removeValidationErrors: function removeValidationErrors() {
223 $('#' + this.fieldModel.toolbarView.getMainWysiwygToolgroupId()).find('.quickedit-image-errors').remove();
224 this.getEditedElement().removeClass('quickedit-validation-error');
227 })(jQuery, _, Drupal);