Chris@0
|
1 /**
|
Chris@0
|
2 * @file
|
Chris@0
|
3 * Provides utility functions for Quick Edit.
|
Chris@0
|
4 */
|
Chris@0
|
5
|
Chris@0
|
6 (function ($, Drupal) {
|
Chris@0
|
7 /**
|
Chris@0
|
8 * @namespace
|
Chris@0
|
9 */
|
Chris@0
|
10 Drupal.quickedit.util = Drupal.quickedit.util || {};
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * @namespace
|
Chris@0
|
14 */
|
Chris@0
|
15 Drupal.quickedit.util.constants = {};
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 *
|
Chris@0
|
19 * @type {string}
|
Chris@0
|
20 */
|
Chris@0
|
21 Drupal.quickedit.util.constants.transitionEnd = 'transitionEnd.quickedit webkitTransitionEnd.quickedit transitionend.quickedit msTransitionEnd.quickedit oTransitionEnd.quickedit';
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * Converts a field id into a formatted url path.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @example
|
Chris@0
|
27 * Drupal.quickedit.util.buildUrl(
|
Chris@0
|
28 * 'node/1/body/und/full',
|
Chris@0
|
29 * '/quickedit/form/!entity_type/!id/!field_name/!langcode/!view_mode'
|
Chris@0
|
30 * );
|
Chris@0
|
31 *
|
Chris@0
|
32 * @param {string} id
|
Chris@0
|
33 * The id of an editable field.
|
Chris@0
|
34 * @param {string} urlFormat
|
Chris@0
|
35 * The Controller route for field processing.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @return {string}
|
Chris@0
|
38 * The formatted URL.
|
Chris@0
|
39 */
|
Chris@0
|
40 Drupal.quickedit.util.buildUrl = function (id, urlFormat) {
|
Chris@0
|
41 const parts = id.split('/');
|
Chris@0
|
42 return Drupal.formatString(decodeURIComponent(urlFormat), {
|
Chris@0
|
43 '!entity_type': parts[0],
|
Chris@0
|
44 '!id': parts[1],
|
Chris@0
|
45 '!field_name': parts[2],
|
Chris@0
|
46 '!langcode': parts[3],
|
Chris@0
|
47 '!view_mode': parts[4],
|
Chris@0
|
48 });
|
Chris@0
|
49 };
|
Chris@0
|
50
|
Chris@0
|
51 /**
|
Chris@0
|
52 * Shows a network error modal dialog.
|
Chris@0
|
53 *
|
Chris@0
|
54 * @param {string} title
|
Chris@0
|
55 * The title to use in the modal dialog.
|
Chris@0
|
56 * @param {string} message
|
Chris@0
|
57 * The message to use in the modal dialog.
|
Chris@0
|
58 */
|
Chris@0
|
59 Drupal.quickedit.util.networkErrorModal = function (title, message) {
|
Chris@0
|
60 const $message = $(`<div>${message}</div>`);
|
Chris@14
|
61 const networkErrorModal = Drupal.dialog($message.get(0), {
|
Chris@0
|
62 title,
|
Chris@0
|
63 dialogClass: 'quickedit-network-error',
|
Chris@0
|
64 buttons: [
|
Chris@0
|
65 {
|
Chris@0
|
66 text: Drupal.t('OK'),
|
Chris@0
|
67 click() {
|
Chris@0
|
68 networkErrorModal.close();
|
Chris@0
|
69 },
|
Chris@0
|
70 primary: true,
|
Chris@0
|
71 },
|
Chris@0
|
72 ],
|
Chris@0
|
73 create() {
|
Chris@0
|
74 $(this).parent().find('.ui-dialog-titlebar-close').remove();
|
Chris@0
|
75 },
|
Chris@0
|
76 close(event) {
|
Chris@0
|
77 // Automatically destroy the DOM element that was used for the dialog.
|
Chris@0
|
78 $(event.target).remove();
|
Chris@0
|
79 },
|
Chris@0
|
80 });
|
Chris@0
|
81 networkErrorModal.showModal();
|
Chris@0
|
82 };
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * @namespace
|
Chris@0
|
86 */
|
Chris@0
|
87 Drupal.quickedit.util.form = {
|
Chris@0
|
88
|
Chris@0
|
89 /**
|
Chris@0
|
90 * Loads a form, calls a callback to insert.
|
Chris@0
|
91 *
|
Chris@0
|
92 * Leverages {@link Drupal.Ajax}' ability to have scoped (per-instance)
|
Chris@0
|
93 * command implementations to be able to call a callback.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @param {object} options
|
Chris@0
|
96 * An object with the following keys:
|
Chris@0
|
97 * @param {string} options.fieldID
|
Chris@0
|
98 * The field ID that uniquely identifies the field for which this form
|
Chris@0
|
99 * will be loaded.
|
Chris@0
|
100 * @param {bool} options.nocssjs
|
Chris@0
|
101 * Boolean indicating whether no CSS and JS should be returned (necessary
|
Chris@0
|
102 * when the form is invisible to the user).
|
Chris@0
|
103 * @param {bool} options.reset
|
Chris@0
|
104 * Boolean indicating whether the data stored for this field's entity in
|
Chris@0
|
105 * PrivateTempStore should be used or reset.
|
Chris@0
|
106 * @param {function} callback
|
Chris@0
|
107 * A callback function that will receive the form to be inserted, as well
|
Chris@0
|
108 * as the ajax object, necessary if the callback wants to perform other
|
Chris@0
|
109 * Ajax commands.
|
Chris@0
|
110 */
|
Chris@0
|
111 load(options, callback) {
|
Chris@0
|
112 const fieldID = options.fieldID;
|
Chris@0
|
113
|
Chris@0
|
114 // Create a Drupal.ajax instance to load the form.
|
Chris@0
|
115 const formLoaderAjax = Drupal.ajax({
|
Chris@0
|
116 url: Drupal.quickedit.util.buildUrl(fieldID, Drupal.url('quickedit/form/!entity_type/!id/!field_name/!langcode/!view_mode')),
|
Chris@0
|
117 submit: {
|
Chris@0
|
118 nocssjs: options.nocssjs,
|
Chris@0
|
119 reset: options.reset,
|
Chris@0
|
120 },
|
Chris@0
|
121 error(xhr, url) {
|
Chris@0
|
122 // Show a modal to inform the user of the network error.
|
Chris@0
|
123 const fieldLabel = Drupal.quickedit.metadata.get(fieldID, 'label');
|
Chris@0
|
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 });
|
Chris@0
|
125 Drupal.quickedit.util.networkErrorModal(Drupal.t('Network problem!'), message);
|
Chris@0
|
126
|
Chris@0
|
127 // Change the state back to "candidate", to allow the user to start
|
Chris@0
|
128 // in-place editing of the field again.
|
Chris@0
|
129 const fieldModel = Drupal.quickedit.app.model.get('activeField');
|
Chris@0
|
130 fieldModel.set('state', 'candidate');
|
Chris@0
|
131 },
|
Chris@0
|
132 });
|
Chris@0
|
133 // Implement a scoped quickeditFieldForm AJAX command: calls the callback.
|
Chris@0
|
134 formLoaderAjax.commands.quickeditFieldForm = function (ajax, response, status) {
|
Chris@0
|
135 callback(response.data, ajax);
|
Chris@0
|
136 Drupal.ajax.instances[this.instanceIndex] = null;
|
Chris@0
|
137 };
|
Chris@0
|
138 // This will ensure our scoped quickeditFieldForm AJAX command gets
|
Chris@0
|
139 // called.
|
Chris@0
|
140 formLoaderAjax.execute();
|
Chris@0
|
141 },
|
Chris@0
|
142
|
Chris@0
|
143 /**
|
Chris@0
|
144 * Creates a {@link Drupal.Ajax} instance that is used to save a form.
|
Chris@0
|
145 *
|
Chris@0
|
146 * @param {object} options
|
Chris@0
|
147 * Submit options to the form.
|
Chris@0
|
148 * @param {bool} options.nocssjs
|
Chris@0
|
149 * Boolean indicating whether no CSS and JS should be returned (necessary
|
Chris@0
|
150 * when the form is invisible to the user).
|
Chris@0
|
151 * @param {Array.<string>} options.other_view_modes
|
Chris@0
|
152 * Array containing view mode IDs (of other instances of this field on the
|
Chris@0
|
153 * page).
|
Chris@0
|
154 * @param {jQuery} $submit
|
Chris@0
|
155 * The submit element.
|
Chris@0
|
156 *
|
Chris@0
|
157 * @return {Drupal.Ajax}
|
Chris@0
|
158 * A {@link Drupal.Ajax} instance.
|
Chris@0
|
159 */
|
Chris@0
|
160 ajaxifySaving(options, $submit) {
|
Chris@0
|
161 // Re-wire the form to handle submit.
|
Chris@0
|
162 const settings = {
|
Chris@0
|
163 url: $submit.closest('form').attr('action'),
|
Chris@0
|
164 setClick: true,
|
Chris@0
|
165 event: 'click.quickedit',
|
Chris@0
|
166 progress: false,
|
Chris@0
|
167 submit: {
|
Chris@0
|
168 nocssjs: options.nocssjs,
|
Chris@0
|
169 other_view_modes: options.other_view_modes,
|
Chris@0
|
170 },
|
Chris@0
|
171
|
Chris@0
|
172 /**
|
Chris@0
|
173 * Reimplement the success handler.
|
Chris@0
|
174 *
|
Chris@0
|
175 * Ensure {@link Drupal.attachBehaviors} does not get called on the
|
Chris@0
|
176 * form.
|
Chris@0
|
177 *
|
Chris@0
|
178 * @param {Drupal.AjaxCommands~commandDefinition} response
|
Chris@0
|
179 * The Drupal AJAX response.
|
Chris@0
|
180 * @param {number} [status]
|
Chris@0
|
181 * The HTTP status code.
|
Chris@0
|
182 */
|
Chris@0
|
183 success(response, status) {
|
Chris@14
|
184 Object.keys(response || {}).forEach((i) => {
|
Chris@14
|
185 if (response[i].command && this.commands[response[i].command]) {
|
Chris@0
|
186 this.commands[response[i].command](this, response[i], status);
|
Chris@0
|
187 }
|
Chris@14
|
188 });
|
Chris@0
|
189 },
|
Chris@0
|
190 base: $submit.attr('id'),
|
Chris@0
|
191 element: $submit[0],
|
Chris@0
|
192 };
|
Chris@0
|
193
|
Chris@0
|
194 return Drupal.ajax(settings);
|
Chris@0
|
195 },
|
Chris@0
|
196
|
Chris@0
|
197 /**
|
Chris@0
|
198 * Cleans up the {@link Drupal.Ajax} instance that is used to save the form.
|
Chris@0
|
199 *
|
Chris@0
|
200 * @param {Drupal.Ajax} ajax
|
Chris@0
|
201 * A {@link Drupal.Ajax} instance that was returned by
|
Chris@0
|
202 * {@link Drupal.quickedit.form.ajaxifySaving}.
|
Chris@0
|
203 */
|
Chris@0
|
204 unajaxifySaving(ajax) {
|
Chris@0
|
205 $(ajax.element).off('click.quickedit');
|
Chris@0
|
206 },
|
Chris@0
|
207
|
Chris@0
|
208 };
|
Chris@0
|
209 }(jQuery, Drupal));
|