3 * Drag+drop based in-place editor for images.
6 (function ($, _, Drupal) {
7 Drupal.quickedit.editors.image = Drupal.quickedit.EditorView.extend(/** @lends Drupal.quickedit.editors.image# */{
12 * @augments Drupal.quickedit.EditorView
14 * @param {object} options
15 * Options for the image editor.
18 Drupal.quickedit.EditorView.prototype.initialize.call(this, options);
19 // Set our original value to our current HTML (for reverting).
20 this.model.set('originalValue', this.$el.html().trim());
21 // $.val() callback function for copying input from our custom form to
22 // the Quick Edit Field Form.
23 this.model.set('currentValue', function (index, value) {
24 const matches = $(this).attr('name').match(/(alt|title)]$/);
26 const name = matches[1];
27 const $toolgroup = $(`#${options.fieldModel.toolbarView.getMainWysiwygToolgroupId()}`);
28 const $input = $toolgroup.find(`.quickedit-image-field-info input[name="${name}"]`);
39 * @param {Drupal.quickedit.FieldModel} fieldModel
40 * The field model that holds the state.
41 * @param {string} state
42 * The state to change to.
43 * @param {object} options
44 * State options, if needed by the state change.
46 stateChange(fieldModel, state, options) {
47 const from = fieldModel.previous('state');
53 if (from !== 'inactive') {
54 this.$el.find('.quickedit-image-dropzone').remove();
55 this.$el.removeClass('quickedit-image-element');
57 if (from === 'invalid') {
58 this.removeValidationErrors();
66 // Defer updating the field model until the current state change has
67 // propagated, to not trigger a nested state change event.
69 fieldModel.set('state', 'active');
76 // Indicate that this element is being edited by Quick Edit Image.
77 this.$el.addClass('quickedit-image-element');
79 // Render our initial dropzone element. Once the user reverts changes
80 // or saves a new image, this element is removed.
81 const $dropzone = this.renderDropzone('upload', Drupal.t('Drop file here or click to upload'));
83 $dropzone.on('dragenter', function (e) {
84 $(this).addClass('hover');
86 $dropzone.on('dragleave', function (e) {
87 $(this).removeClass('hover');
90 $dropzone.on('drop', function (e) {
91 // Only respond when a file is dropped (could be another element).
92 if (e.originalEvent.dataTransfer && e.originalEvent.dataTransfer.files.length) {
93 $(this).removeClass('hover');
94 self.uploadImage(e.originalEvent.dataTransfer.files[0]);
98 $dropzone.on('click', (e) => {
99 // Create an <input> element without appending it to the DOM, and
100 // trigger a click event. This is the easiest way to arbitrarily
101 // open the browser's upload dialog.
102 $('<input type="file">')
104 .on('change', function () {
105 if (this.files.length) {
106 self.uploadImage(this.files[0]);
111 // Prevent the browser's default behavior when dragging files onto
112 // the document (usually opens them in the same tab).
113 $dropzone.on('dragover dragenter dragleave drop click', (e) => {
118 this.renderToolbar(fieldModel);
126 if (from === 'invalid') {
127 this.removeValidationErrors();
137 this.showValidationErrors();
143 * Validates/uploads a given file.
146 * The file to upload.
149 // Indicate loading by adding a special class to our icon.
150 this.renderDropzone('upload loading', Drupal.t('Uploading <i>@file</i>…', { '@file': file.name }));
152 // Build a valid URL for our endpoint.
153 const fieldID = this.fieldModel.get('fieldID');
154 const url = Drupal.quickedit.util.buildUrl(fieldID, Drupal.url('quickedit/image/upload/!entity_type/!id/!field_name/!langcode/!view_mode'));
156 // Construct form data that our endpoint can consume.
157 const data = new FormData();
158 data.append('files[image]', file);
160 // Construct a POST request to our endpoint.
167 const $el = $(self.fieldModel.get('el'));
168 // Indicate that the field has changed - this enables the
170 self.fieldModel.set('state', 'changed');
171 self.fieldModel.get('entity').set('inTempStore', true);
172 self.removeValidationErrors();
174 // Replace our html with the new image. If we replaced our entire
175 // element with data.html, we would have to implement complicated logic
176 // like what's in Drupal.quickedit.AppView.renderUpdatedField.
177 const $content = $(response.html).closest('[data-quickedit-field-id]').children();
178 $el.empty().append($content);
184 * Utility function to make an AJAX request to the server.
186 * In addition to formatting the correct request, this also handles error
187 * codes and messages by displaying them visually inline with the image.
189 * Drupal.ajax is not called here as the Form API is unused by this
190 * in-place editor, and our JSON requests/responses try to be
191 * editor-agnostic. Ideally similar logic and routes could be used by
192 * modules like CKEditor for drag+drop file uploads as well.
194 * @param {object} options
196 * @param {string} options.type
197 * The type of request (i.e. GET, POST, PUT, DELETE, etc.)
198 * @param {string} options.url
199 * The URL for the request.
200 * @param {*} options.data
201 * The data to send to the server.
202 * @param {function} options.success
203 * A callback function used when a request is successful, without errors.
206 const defaultOptions = {
213 this.renderDropzone('error', Drupal.t('A server error has occurred.'));
217 const ajaxOptions = $.extend(defaultOptions, options);
218 const successCallback = ajaxOptions.success;
220 // Handle the success callback.
221 ajaxOptions.success = function (response) {
222 if (response.main_error) {
223 this.renderDropzone('error', response.main_error);
224 if (response.errors.length) {
225 this.model.set('validationErrors', response.errors);
227 this.showValidationErrors();
230 successCallback(response);
238 * Renders our toolbar form for editing metadata.
240 * @param {Drupal.quickedit.FieldModel} fieldModel
241 * The current Field Model.
243 renderToolbar(fieldModel) {
244 const $toolgroup = $(`#${fieldModel.toolbarView.getMainWysiwygToolgroupId()}`);
245 let $toolbar = $toolgroup.find('.quickedit-image-field-info');
246 if ($toolbar.length === 0) {
247 // Perform an AJAX request for extra image info (alt/title).
248 const fieldID = fieldModel.get('fieldID');
249 const url = Drupal.quickedit.util.buildUrl(fieldID, Drupal.url('quickedit/image/info/!entity_type/!id/!field_name/!langcode/!view_mode'));
255 $toolbar = $(Drupal.theme.quickeditImageToolbar(response));
256 $toolgroup.append($toolbar);
257 $toolbar.on('keyup paste', () => {
258 fieldModel.set('state', 'changed');
260 // Re-position the toolbar, which could have changed size.
261 fieldModel.get('entity').toolbarView.position();
268 * Renders our dropzone element.
270 * @param {string} state
271 * The current state of our editor. Only used for visual styling.
272 * @param {string} text
273 * The text to display in the dropzone area.
276 * The rendered dropzone.
278 renderDropzone(state, text) {
279 let $dropzone = this.$el.find('.quickedit-image-dropzone');
280 // If the element already exists, modify its contents.
281 if ($dropzone.length) {
283 .removeClass('upload error hover loading')
284 .addClass(`.quickedit-image-dropzone ${state}`)
285 .children('.quickedit-image-text')
289 $dropzone = $(Drupal.theme('quickeditImageDropzone', {
293 this.$el.append($dropzone);
303 this.$el.html(this.model.get('originalValue'));
309 getQuickEditUISettings() {
310 return { padding: false, unifiedToolbar: true, fullWidthToolbar: true, popup: false };
316 showValidationErrors() {
317 const errors = Drupal.theme('quickeditImageErrors', {
318 errors: this.model.get('validationErrors'),
320 $(`#${this.fieldModel.toolbarView.getMainWysiwygToolgroupId()}`)
322 this.getEditedElement()
323 .addClass('quickedit-validation-error');
324 // Re-position the toolbar, which could have changed size.
325 this.fieldModel.get('entity').toolbarView.position();
331 removeValidationErrors() {
332 $(`#${this.fieldModel.toolbarView.getMainWysiwygToolgroupId()}`)
333 .find('.quickedit-image-errors').remove();
334 this.getEditedElement()
335 .removeClass('quickedit-validation-error');
339 }(jQuery, _, Drupal));