3 * Provides utility functions for Quick Edit.
6 (function ($, Drupal) {
10 Drupal.quickedit.util = Drupal.quickedit.util || {};
15 Drupal.quickedit.util.constants = {};
21 Drupal.quickedit.util.constants.transitionEnd = 'transitionEnd.quickedit webkitTransitionEnd.quickedit transitionend.quickedit msTransitionEnd.quickedit oTransitionEnd.quickedit';
24 * Converts a field id into a formatted url path.
27 * Drupal.quickedit.util.buildUrl(
28 * 'node/1/body/und/full',
29 * '/quickedit/form/!entity_type/!id/!field_name/!langcode/!view_mode'
33 * The id of an editable field.
34 * @param {string} urlFormat
35 * The Controller route for field processing.
40 Drupal.quickedit.util.buildUrl = function (id, urlFormat) {
41 const parts = id.split('/');
42 return Drupal.formatString(decodeURIComponent(urlFormat), {
43 '!entity_type': parts[0],
45 '!field_name': parts[2],
46 '!langcode': parts[3],
47 '!view_mode': parts[4],
52 * Shows a network error modal dialog.
54 * @param {string} title
55 * The title to use in the modal dialog.
56 * @param {string} message
57 * The message to use in the modal dialog.
59 Drupal.quickedit.util.networkErrorModal = function (title, message) {
60 const $message = $(`<div>${message}</div>`);
61 var networkErrorModal = Drupal.dialog($message.get(0), {
63 dialogClass: 'quickedit-network-error',
68 networkErrorModal.close();
74 $(this).parent().find('.ui-dialog-titlebar-close').remove();
77 // Automatically destroy the DOM element that was used for the dialog.
78 $(event.target).remove();
81 networkErrorModal.showModal();
87 Drupal.quickedit.util.form = {
90 * Loads a form, calls a callback to insert.
92 * Leverages {@link Drupal.Ajax}' ability to have scoped (per-instance)
93 * command implementations to be able to call a callback.
95 * @param {object} options
96 * An object with the following keys:
97 * @param {string} options.fieldID
98 * The field ID that uniquely identifies the field for which this form
100 * @param {bool} options.nocssjs
101 * Boolean indicating whether no CSS and JS should be returned (necessary
102 * when the form is invisible to the user).
103 * @param {bool} options.reset
104 * Boolean indicating whether the data stored for this field's entity in
105 * PrivateTempStore should be used or reset.
106 * @param {function} callback
107 * A callback function that will receive the form to be inserted, as well
108 * as the ajax object, necessary if the callback wants to perform other
111 load(options, callback) {
112 const fieldID = options.fieldID;
114 // Create a Drupal.ajax instance to load the form.
115 const formLoaderAjax = Drupal.ajax({
116 url: Drupal.quickedit.util.buildUrl(fieldID, Drupal.url('quickedit/form/!entity_type/!id/!field_name/!langcode/!view_mode')),
118 nocssjs: options.nocssjs,
119 reset: options.reset,
122 // Show a modal to inform the user of the network error.
123 const fieldLabel = Drupal.quickedit.metadata.get(fieldID, 'label');
124 const message = Drupal.t('Could not load the form for <q>@field-label</q>, either due to a website problem or a network connection problem.<br>Please try again.', { '@field-label': fieldLabel });
125 Drupal.quickedit.util.networkErrorModal(Drupal.t('Network problem!'), message);
127 // Change the state back to "candidate", to allow the user to start
128 // in-place editing of the field again.
129 const fieldModel = Drupal.quickedit.app.model.get('activeField');
130 fieldModel.set('state', 'candidate');
133 // Implement a scoped quickeditFieldForm AJAX command: calls the callback.
134 formLoaderAjax.commands.quickeditFieldForm = function (ajax, response, status) {
135 callback(response.data, ajax);
136 Drupal.ajax.instances[this.instanceIndex] = null;
138 // This will ensure our scoped quickeditFieldForm AJAX command gets
140 formLoaderAjax.execute();
144 * Creates a {@link Drupal.Ajax} instance that is used to save a form.
146 * @param {object} options
147 * Submit options to the form.
148 * @param {bool} options.nocssjs
149 * Boolean indicating whether no CSS and JS should be returned (necessary
150 * when the form is invisible to the user).
151 * @param {Array.<string>} options.other_view_modes
152 * Array containing view mode IDs (of other instances of this field on the
154 * @param {jQuery} $submit
155 * The submit element.
157 * @return {Drupal.Ajax}
158 * A {@link Drupal.Ajax} instance.
160 ajaxifySaving(options, $submit) {
161 // Re-wire the form to handle submit.
163 url: $submit.closest('form').attr('action'),
165 event: 'click.quickedit',
168 nocssjs: options.nocssjs,
169 other_view_modes: options.other_view_modes,
173 * Reimplement the success handler.
175 * Ensure {@link Drupal.attachBehaviors} does not get called on the
178 * @param {Drupal.AjaxCommands~commandDefinition} response
179 * The Drupal AJAX response.
180 * @param {number} [status]
181 * The HTTP status code.
183 success(response, status) {
184 for (const i in response) {
185 if (response.hasOwnProperty(i) && response[i].command && this.commands[response[i].command]) {
186 this.commands[response[i].command](this, response[i], status);
190 base: $submit.attr('id'),
194 return Drupal.ajax(settings);
198 * Cleans up the {@link Drupal.Ajax} instance that is used to save the form.
200 * @param {Drupal.Ajax} ajax
201 * A {@link Drupal.Ajax} instance that was returned by
202 * {@link Drupal.quickedit.form.ajaxifySaving}.
204 unajaxifySaving(ajax) {
205 $(ajax.element).off('click.quickedit');