Chris@0
|
1 /**
|
Chris@0
|
2 * @file
|
Chris@0
|
3 * A Backbone Model for the state of an in-place editable entity in the DOM.
|
Chris@0
|
4 */
|
Chris@0
|
5
|
Chris@0
|
6 (function (_, $, Backbone, Drupal) {
|
Chris@0
|
7 Drupal.quickedit.EntityModel = Drupal.quickedit.BaseModel.extend(/** @lends Drupal.quickedit.EntityModel# */{
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * @type {object}
|
Chris@0
|
11 */
|
Chris@0
|
12 defaults: /** @lends Drupal.quickedit.EntityModel# */{
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * The DOM element that represents this entity.
|
Chris@0
|
16 *
|
Chris@0
|
17 * It may seem bizarre to have a DOM element in a Backbone Model, but we
|
Chris@0
|
18 * need to be able to map entities in the DOM to EntityModels in memory.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @type {HTMLElement}
|
Chris@0
|
21 */
|
Chris@0
|
22 el: null,
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * An entity ID, of the form `<entity type>/<entity ID>`
|
Chris@0
|
26 *
|
Chris@0
|
27 * @example
|
Chris@0
|
28 * "node/1"
|
Chris@0
|
29 *
|
Chris@0
|
30 * @type {string}
|
Chris@0
|
31 */
|
Chris@0
|
32 entityID: null,
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * An entity instance ID.
|
Chris@0
|
36 *
|
Chris@0
|
37 * The first instance of a specific entity (i.e. with a given entity ID)
|
Chris@0
|
38 * is assigned 0, the second 1, and so on.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @type {number}
|
Chris@0
|
41 */
|
Chris@0
|
42 entityInstanceID: null,
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * The unique ID of this entity instance on the page, of the form
|
Chris@0
|
46 * `<entity type>/<entity ID>[entity instance ID]`
|
Chris@0
|
47 *
|
Chris@0
|
48 * @example
|
Chris@0
|
49 * "node/1[0]"
|
Chris@0
|
50 *
|
Chris@0
|
51 * @type {string}
|
Chris@0
|
52 */
|
Chris@0
|
53 id: null,
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * The label of the entity.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @type {string}
|
Chris@0
|
59 */
|
Chris@0
|
60 label: null,
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * A FieldCollection for all fields of the entity.
|
Chris@0
|
64 *
|
Chris@0
|
65 * @type {Drupal.quickedit.FieldCollection}
|
Chris@0
|
66 *
|
Chris@0
|
67 * @see Drupal.quickedit.FieldCollection
|
Chris@0
|
68 */
|
Chris@0
|
69 fields: null,
|
Chris@0
|
70
|
Chris@0
|
71 // The attributes below are stateful. The ones above will never change
|
Chris@0
|
72 // during the life of a EntityModel instance.
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * Indicates whether this entity is currently being edited in-place.
|
Chris@0
|
76 *
|
Chris@0
|
77 * @type {bool}
|
Chris@0
|
78 */
|
Chris@0
|
79 isActive: false,
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * Whether one or more fields are already been stored in PrivateTempStore.
|
Chris@0
|
83 *
|
Chris@0
|
84 * @type {bool}
|
Chris@0
|
85 */
|
Chris@0
|
86 inTempStore: false,
|
Chris@0
|
87
|
Chris@0
|
88 /**
|
Chris@0
|
89 * Indicates whether a "Save" button is necessary or not.
|
Chris@0
|
90 *
|
Chris@0
|
91 * Whether one or more fields have already been stored in PrivateTempStore
|
Chris@0
|
92 * *or* the field that's currently being edited is in the 'changed' or a
|
Chris@0
|
93 * later state.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @type {bool}
|
Chris@0
|
96 */
|
Chris@0
|
97 isDirty: false,
|
Chris@0
|
98
|
Chris@0
|
99 /**
|
Chris@0
|
100 * Whether the request to the server has been made to commit this entity.
|
Chris@0
|
101 *
|
Chris@0
|
102 * Used to prevent multiple such requests.
|
Chris@0
|
103 *
|
Chris@0
|
104 * @type {bool}
|
Chris@0
|
105 */
|
Chris@0
|
106 isCommitting: false,
|
Chris@0
|
107
|
Chris@0
|
108 /**
|
Chris@0
|
109 * The current processing state of an entity.
|
Chris@0
|
110 *
|
Chris@0
|
111 * @type {string}
|
Chris@0
|
112 */
|
Chris@0
|
113 state: 'closed',
|
Chris@0
|
114
|
Chris@0
|
115 /**
|
Chris@0
|
116 * IDs of fields whose new values have been stored in PrivateTempStore.
|
Chris@0
|
117 *
|
Chris@0
|
118 * We must store this on the EntityModel as well (even though it already
|
Chris@0
|
119 * is on the FieldModel) because when a field is rerendered, its
|
Chris@0
|
120 * FieldModel is destroyed and this allows us to transition it back to
|
Chris@0
|
121 * the proper state.
|
Chris@0
|
122 *
|
Chris@0
|
123 * @type {Array.<string>}
|
Chris@0
|
124 */
|
Chris@0
|
125 fieldsInTempStore: [],
|
Chris@0
|
126
|
Chris@0
|
127 /**
|
Chris@0
|
128 * A flag the tells the application that this EntityModel must be reloaded
|
Chris@0
|
129 * in order to restore the original values to its fields in the client.
|
Chris@0
|
130 *
|
Chris@0
|
131 * @type {bool}
|
Chris@0
|
132 */
|
Chris@0
|
133 reload: false,
|
Chris@0
|
134 },
|
Chris@0
|
135
|
Chris@0
|
136 /**
|
Chris@0
|
137 * @constructs
|
Chris@0
|
138 *
|
Chris@0
|
139 * @augments Drupal.quickedit.BaseModel
|
Chris@0
|
140 */
|
Chris@0
|
141 initialize() {
|
Chris@0
|
142 this.set('fields', new Drupal.quickedit.FieldCollection());
|
Chris@0
|
143
|
Chris@0
|
144 // Respond to entity state changes.
|
Chris@0
|
145 this.listenTo(this, 'change:state', this.stateChange);
|
Chris@0
|
146
|
Chris@0
|
147 // The state of the entity is largely dependent on the state of its
|
Chris@0
|
148 // fields.
|
Chris@0
|
149 this.listenTo(this.get('fields'), 'change:state', this.fieldStateChange);
|
Chris@0
|
150
|
Chris@0
|
151 // Call Drupal.quickedit.BaseModel's initialize() method.
|
Chris@0
|
152 Drupal.quickedit.BaseModel.prototype.initialize.call(this);
|
Chris@0
|
153 },
|
Chris@0
|
154
|
Chris@0
|
155 /**
|
Chris@0
|
156 * Updates FieldModels' states when an EntityModel change occurs.
|
Chris@0
|
157 *
|
Chris@0
|
158 * @param {Drupal.quickedit.EntityModel} entityModel
|
Chris@0
|
159 * The entity model
|
Chris@0
|
160 * @param {string} state
|
Chris@0
|
161 * The state of the associated entity. One of
|
Chris@0
|
162 * {@link Drupal.quickedit.EntityModel.states}.
|
Chris@0
|
163 * @param {object} options
|
Chris@0
|
164 * Options for the entity model.
|
Chris@0
|
165 */
|
Chris@0
|
166 stateChange(entityModel, state, options) {
|
Chris@0
|
167 const to = state;
|
Chris@0
|
168 switch (to) {
|
Chris@0
|
169 case 'closed':
|
Chris@0
|
170 this.set({
|
Chris@0
|
171 isActive: false,
|
Chris@0
|
172 inTempStore: false,
|
Chris@0
|
173 isDirty: false,
|
Chris@0
|
174 });
|
Chris@0
|
175 break;
|
Chris@0
|
176
|
Chris@0
|
177 case 'launching':
|
Chris@0
|
178 break;
|
Chris@0
|
179
|
Chris@0
|
180 case 'opening':
|
Chris@0
|
181 // Set the fields to candidate state.
|
Chris@0
|
182 entityModel.get('fields').each((fieldModel) => {
|
Chris@0
|
183 fieldModel.set('state', 'candidate', options);
|
Chris@0
|
184 });
|
Chris@0
|
185 break;
|
Chris@0
|
186
|
Chris@0
|
187 case 'opened':
|
Chris@0
|
188 // The entity is now ready for editing!
|
Chris@0
|
189 this.set('isActive', true);
|
Chris@0
|
190 break;
|
Chris@0
|
191
|
Chris@0
|
192 case 'committing':
|
Chris@0
|
193 // The user indicated they want to save the entity.
|
Chris@0
|
194 var fields = this.get('fields');
|
Chris@0
|
195 // For fields that are in an active state, transition them to
|
Chris@0
|
196 // candidate.
|
Chris@0
|
197 fields.chain()
|
Chris@0
|
198 .filter(fieldModel => _.intersection([fieldModel.get('state')], ['active']).length)
|
Chris@0
|
199 .each((fieldModel) => {
|
Chris@0
|
200 fieldModel.set('state', 'candidate');
|
Chris@0
|
201 });
|
Chris@0
|
202 // For fields that are in a changed state, field values must first be
|
Chris@0
|
203 // stored in PrivateTempStore.
|
Chris@0
|
204 fields.chain()
|
Chris@0
|
205 .filter(fieldModel => _.intersection([fieldModel.get('state')], Drupal.quickedit.app.changedFieldStates).length)
|
Chris@0
|
206 .each((fieldModel) => {
|
Chris@0
|
207 fieldModel.set('state', 'saving');
|
Chris@0
|
208 });
|
Chris@0
|
209 break;
|
Chris@0
|
210
|
Chris@0
|
211 case 'deactivating':
|
Chris@0
|
212 var changedFields = this.get('fields')
|
Chris@0
|
213 .filter(fieldModel => _.intersection([fieldModel.get('state')], ['changed', 'invalid']).length);
|
Chris@0
|
214 // If the entity contains unconfirmed or unsaved changes, return the
|
Chris@0
|
215 // entity to an opened state and ask the user if they would like to
|
Chris@0
|
216 // save the changes or discard the changes.
|
Chris@0
|
217 // 1. One of the fields is in a changed state. The changed field
|
Chris@0
|
218 // might just be a change in the client or it might have been saved
|
Chris@0
|
219 // to tempstore.
|
Chris@0
|
220 // 2. The saved flag is empty and the confirmed flag is empty. If
|
Chris@0
|
221 // the entity has been saved to the server, the fields changed in
|
Chris@0
|
222 // the client are irrelevant. If the changes are confirmed, then
|
Chris@0
|
223 // proceed to set the fields to candidate state.
|
Chris@0
|
224 if ((changedFields.length || this.get('fieldsInTempStore').length) && (!options.saved && !options.confirmed)) {
|
Chris@0
|
225 // Cancel deactivation until the user confirms save or discard.
|
Chris@0
|
226 this.set('state', 'opened', { confirming: true });
|
Chris@0
|
227 // An action in reaction to state change must be deferred.
|
Chris@0
|
228 _.defer(() => {
|
Chris@0
|
229 Drupal.quickedit.app.confirmEntityDeactivation(entityModel);
|
Chris@0
|
230 });
|
Chris@0
|
231 }
|
Chris@0
|
232 else {
|
Chris@0
|
233 const invalidFields = this.get('fields')
|
Chris@0
|
234 .filter(fieldModel => _.intersection([fieldModel.get('state')], ['invalid']).length);
|
Chris@0
|
235 // Indicate if this EntityModel needs to be reloaded in order to
|
Chris@0
|
236 // restore the original values of its fields.
|
Chris@0
|
237 entityModel.set('reload', (this.get('fieldsInTempStore').length || invalidFields.length));
|
Chris@0
|
238 // Set all fields to the 'candidate' state. A changed field may have
|
Chris@0
|
239 // to go through confirmation first.
|
Chris@0
|
240 entityModel.get('fields').each((fieldModel) => {
|
Chris@0
|
241 // If the field is already in the candidate state, trigger a
|
Chris@0
|
242 // change event so that the entityModel can move to the next state
|
Chris@0
|
243 // in deactivation.
|
Chris@0
|
244 if (_.intersection([fieldModel.get('state')], ['candidate', 'highlighted']).length) {
|
Chris@0
|
245 fieldModel.trigger('change:state', fieldModel, fieldModel.get('state'), options);
|
Chris@0
|
246 }
|
Chris@0
|
247 else {
|
Chris@0
|
248 fieldModel.set('state', 'candidate', options);
|
Chris@0
|
249 }
|
Chris@0
|
250 });
|
Chris@0
|
251 }
|
Chris@0
|
252 break;
|
Chris@0
|
253
|
Chris@0
|
254 case 'closing':
|
Chris@0
|
255 // Set all fields to the 'inactive' state.
|
Chris@0
|
256 options.reason = 'stop';
|
Chris@0
|
257 this.get('fields').each((fieldModel) => {
|
Chris@0
|
258 fieldModel.set({
|
Chris@0
|
259 inTempStore: false,
|
Chris@0
|
260 state: 'inactive',
|
Chris@0
|
261 }, options);
|
Chris@0
|
262 });
|
Chris@0
|
263 break;
|
Chris@0
|
264 }
|
Chris@0
|
265 },
|
Chris@0
|
266
|
Chris@0
|
267 /**
|
Chris@0
|
268 * Updates a Field and Entity model's "inTempStore" when appropriate.
|
Chris@0
|
269 *
|
Chris@0
|
270 * Helper function.
|
Chris@0
|
271 *
|
Chris@0
|
272 * @param {Drupal.quickedit.EntityModel} entityModel
|
Chris@0
|
273 * The model of the entity for which a field's state attribute has
|
Chris@0
|
274 * changed.
|
Chris@0
|
275 * @param {Drupal.quickedit.FieldModel} fieldModel
|
Chris@0
|
276 * The model of the field whose state attribute has changed.
|
Chris@0
|
277 *
|
Chris@0
|
278 * @see Drupal.quickedit.EntityModel#fieldStateChange
|
Chris@0
|
279 */
|
Chris@0
|
280 _updateInTempStoreAttributes(entityModel, fieldModel) {
|
Chris@0
|
281 const current = fieldModel.get('state');
|
Chris@0
|
282 const previous = fieldModel.previous('state');
|
Chris@0
|
283 let fieldsInTempStore = entityModel.get('fieldsInTempStore');
|
Chris@0
|
284 // If the fieldModel changed to the 'saved' state: remember that this
|
Chris@0
|
285 // field was saved to PrivateTempStore.
|
Chris@0
|
286 if (current === 'saved') {
|
Chris@0
|
287 // Mark the entity as saved in PrivateTempStore, so that we can pass the
|
Chris@0
|
288 // proper "reset PrivateTempStore" boolean value when communicating with
|
Chris@0
|
289 // the server.
|
Chris@0
|
290 entityModel.set('inTempStore', true);
|
Chris@0
|
291 // Mark the field as saved in PrivateTempStore, so that visual
|
Chris@0
|
292 // indicators signifying just that may be rendered.
|
Chris@0
|
293 fieldModel.set('inTempStore', true);
|
Chris@0
|
294 // Remember that this field is in PrivateTempStore, restore when
|
Chris@0
|
295 // rerendered.
|
Chris@0
|
296 fieldsInTempStore.push(fieldModel.get('fieldID'));
|
Chris@0
|
297 fieldsInTempStore = _.uniq(fieldsInTempStore);
|
Chris@0
|
298 entityModel.set('fieldsInTempStore', fieldsInTempStore);
|
Chris@0
|
299 }
|
Chris@0
|
300 // If the fieldModel changed to the 'candidate' state from the
|
Chris@0
|
301 // 'inactive' state, then this is a field for this entity that got
|
Chris@0
|
302 // rerendered. Restore its previous 'inTempStore' attribute value.
|
Chris@0
|
303 else if (current === 'candidate' && previous === 'inactive') {
|
Chris@0
|
304 fieldModel.set('inTempStore', _.intersection([fieldModel.get('fieldID')], fieldsInTempStore).length > 0);
|
Chris@0
|
305 }
|
Chris@0
|
306 },
|
Chris@0
|
307
|
Chris@0
|
308 /**
|
Chris@0
|
309 * Reacts to state changes in this entity's fields.
|
Chris@0
|
310 *
|
Chris@0
|
311 * @param {Drupal.quickedit.FieldModel} fieldModel
|
Chris@0
|
312 * The model of the field whose state attribute changed.
|
Chris@0
|
313 * @param {string} state
|
Chris@0
|
314 * The state of the associated field. One of
|
Chris@0
|
315 * {@link Drupal.quickedit.FieldModel.states}.
|
Chris@0
|
316 */
|
Chris@0
|
317 fieldStateChange(fieldModel, state) {
|
Chris@0
|
318 const entityModel = this;
|
Chris@0
|
319 const fieldState = state;
|
Chris@0
|
320 // Switch on the entityModel state.
|
Chris@0
|
321 // The EntityModel responds to FieldModel state changes as a function of
|
Chris@0
|
322 // its state. For example, a field switching back to 'candidate' state
|
Chris@0
|
323 // when its entity is in the 'opened' state has no effect on the entity.
|
Chris@0
|
324 // But that same switch back to 'candidate' state of a field when the
|
Chris@0
|
325 // entity is in the 'committing' state might allow the entity to proceed
|
Chris@0
|
326 // with the commit flow.
|
Chris@0
|
327 switch (this.get('state')) {
|
Chris@0
|
328 case 'closed':
|
Chris@0
|
329 case 'launching':
|
Chris@0
|
330 // It should be impossible to reach these: fields can't change state
|
Chris@0
|
331 // while the entity is closed or still launching.
|
Chris@0
|
332 break;
|
Chris@0
|
333
|
Chris@0
|
334 case 'opening':
|
Chris@0
|
335 // We must change the entity to the 'opened' state, but it must first
|
Chris@0
|
336 // be confirmed that all of its fieldModels have transitioned to the
|
Chris@0
|
337 // 'candidate' state.
|
Chris@0
|
338 // We do this here, because this is called every time a fieldModel
|
Chris@0
|
339 // changes state, hence each time this is called, we get closer to the
|
Chris@0
|
340 // goal of having all fieldModels in the 'candidate' state.
|
Chris@0
|
341 // A state change in reaction to another state change must be
|
Chris@0
|
342 // deferred.
|
Chris@0
|
343 _.defer(() => {
|
Chris@0
|
344 entityModel.set('state', 'opened', {
|
Chris@0
|
345 'accept-field-states': Drupal.quickedit.app.readyFieldStates,
|
Chris@0
|
346 });
|
Chris@0
|
347 });
|
Chris@0
|
348 break;
|
Chris@0
|
349
|
Chris@0
|
350 case 'opened':
|
Chris@0
|
351 // Set the isDirty attribute when appropriate so that it is known when
|
Chris@0
|
352 // to display the "Save" button in the entity toolbar.
|
Chris@0
|
353 // Note that once a field has been changed, there's no way to discard
|
Chris@0
|
354 // that change, hence it will have to be saved into PrivateTempStore,
|
Chris@0
|
355 // or the in-place editing of this field will have to be stopped
|
Chris@0
|
356 // completely. In other words: once any field enters the 'changed'
|
Chris@0
|
357 // field, then for the remainder of the in-place editing session, the
|
Chris@0
|
358 // entity is by definition dirty.
|
Chris@0
|
359 if (fieldState === 'changed') {
|
Chris@0
|
360 entityModel.set('isDirty', true);
|
Chris@0
|
361 }
|
Chris@0
|
362 else {
|
Chris@0
|
363 this._updateInTempStoreAttributes(entityModel, fieldModel);
|
Chris@0
|
364 }
|
Chris@0
|
365 break;
|
Chris@0
|
366
|
Chris@0
|
367 case 'committing':
|
Chris@0
|
368 // If the field save returned a validation error, set the state of the
|
Chris@0
|
369 // entity back to 'opened'.
|
Chris@0
|
370 if (fieldState === 'invalid') {
|
Chris@0
|
371 // A state change in reaction to another state change must be
|
Chris@0
|
372 // deferred.
|
Chris@0
|
373 _.defer(() => {
|
Chris@0
|
374 entityModel.set('state', 'opened', { reason: 'invalid' });
|
Chris@0
|
375 });
|
Chris@0
|
376 }
|
Chris@0
|
377 else {
|
Chris@0
|
378 this._updateInTempStoreAttributes(entityModel, fieldModel);
|
Chris@0
|
379 }
|
Chris@0
|
380
|
Chris@0
|
381 // Attempt to save the entity. If the entity's fields are not yet all
|
Chris@0
|
382 // in a ready state, the save will not be processed.
|
Chris@0
|
383 var options = {
|
Chris@0
|
384 'accept-field-states': Drupal.quickedit.app.readyFieldStates,
|
Chris@0
|
385 };
|
Chris@0
|
386 if (entityModel.set('isCommitting', true, options)) {
|
Chris@0
|
387 entityModel.save({
|
Chris@0
|
388 success() {
|
Chris@0
|
389 entityModel.set({
|
Chris@0
|
390 state: 'deactivating',
|
Chris@0
|
391 isCommitting: false,
|
Chris@0
|
392 }, { saved: true });
|
Chris@0
|
393 },
|
Chris@0
|
394 error() {
|
Chris@0
|
395 // Reset the "isCommitting" mutex.
|
Chris@0
|
396 entityModel.set('isCommitting', false);
|
Chris@0
|
397 // Change the state back to "opened", to allow the user to hit
|
Chris@0
|
398 // the "Save" button again.
|
Chris@0
|
399 entityModel.set('state', 'opened', { reason: 'networkerror' });
|
Chris@0
|
400 // Show a modal to inform the user of the network error.
|
Chris@0
|
401 const message = Drupal.t('Your changes to <q>@entity-title</q> could not be saved, either due to a website problem or a network connection problem.<br>Please try again.', { '@entity-title': entityModel.get('label') });
|
Chris@0
|
402 Drupal.quickedit.util.networkErrorModal(Drupal.t('Network problem!'), message);
|
Chris@0
|
403 },
|
Chris@0
|
404 });
|
Chris@0
|
405 }
|
Chris@0
|
406 break;
|
Chris@0
|
407
|
Chris@0
|
408 case 'deactivating':
|
Chris@0
|
409 // When setting the entity to 'closing', require that all fieldModels
|
Chris@0
|
410 // are in either the 'candidate' or 'highlighted' state.
|
Chris@0
|
411 // A state change in reaction to another state change must be
|
Chris@0
|
412 // deferred.
|
Chris@0
|
413 _.defer(() => {
|
Chris@0
|
414 entityModel.set('state', 'closing', {
|
Chris@0
|
415 'accept-field-states': Drupal.quickedit.app.readyFieldStates,
|
Chris@0
|
416 });
|
Chris@0
|
417 });
|
Chris@0
|
418 break;
|
Chris@0
|
419
|
Chris@0
|
420 case 'closing':
|
Chris@0
|
421 // When setting the entity to 'closed', require that all fieldModels
|
Chris@0
|
422 // are in the 'inactive' state.
|
Chris@0
|
423 // A state change in reaction to another state change must be
|
Chris@0
|
424 // deferred.
|
Chris@0
|
425 _.defer(() => {
|
Chris@0
|
426 entityModel.set('state', 'closed', {
|
Chris@0
|
427 'accept-field-states': ['inactive'],
|
Chris@0
|
428 });
|
Chris@0
|
429 });
|
Chris@0
|
430 break;
|
Chris@0
|
431 }
|
Chris@0
|
432 },
|
Chris@0
|
433
|
Chris@0
|
434 /**
|
Chris@0
|
435 * Fires an AJAX request to the REST save URL for an entity.
|
Chris@0
|
436 *
|
Chris@0
|
437 * @param {object} options
|
Chris@0
|
438 * An object of options that contains:
|
Chris@0
|
439 * @param {function} [options.success]
|
Chris@0
|
440 * A function to invoke if the entity is successfully saved.
|
Chris@0
|
441 */
|
Chris@0
|
442 save(options) {
|
Chris@0
|
443 const entityModel = this;
|
Chris@0
|
444
|
Chris@0
|
445 // Create a Drupal.ajax instance to save the entity.
|
Chris@0
|
446 const entitySaverAjax = Drupal.ajax({
|
Chris@0
|
447 url: Drupal.url(`quickedit/entity/${entityModel.get('entityID')}`),
|
Chris@0
|
448 error() {
|
Chris@0
|
449 // Let the Drupal.quickedit.EntityModel Backbone model's error()
|
Chris@0
|
450 // method handle errors.
|
Chris@0
|
451 options.error.call(entityModel);
|
Chris@0
|
452 },
|
Chris@0
|
453 });
|
Chris@0
|
454 // Entity saved successfully.
|
Chris@0
|
455 entitySaverAjax.commands.quickeditEntitySaved = function (ajax, response, status) {
|
Chris@0
|
456 // All fields have been moved from PrivateTempStore to permanent
|
Chris@0
|
457 // storage, update the "inTempStore" attribute on FieldModels, on the
|
Chris@0
|
458 // EntityModel and clear EntityModel's "fieldInTempStore" attribute.
|
Chris@0
|
459 entityModel.get('fields').each((fieldModel) => {
|
Chris@0
|
460 fieldModel.set('inTempStore', false);
|
Chris@0
|
461 });
|
Chris@0
|
462 entityModel.set('inTempStore', false);
|
Chris@0
|
463 entityModel.set('fieldsInTempStore', []);
|
Chris@0
|
464
|
Chris@0
|
465 // Invoke the optional success callback.
|
Chris@0
|
466 if (options.success) {
|
Chris@0
|
467 options.success.call(entityModel);
|
Chris@0
|
468 }
|
Chris@0
|
469 };
|
Chris@0
|
470 // Trigger the AJAX request, which will will return the
|
Chris@0
|
471 // quickeditEntitySaved AJAX command to which we then react.
|
Chris@0
|
472 entitySaverAjax.execute();
|
Chris@0
|
473 },
|
Chris@0
|
474
|
Chris@0
|
475 /**
|
Chris@0
|
476 * Validate the entity model.
|
Chris@0
|
477 *
|
Chris@0
|
478 * @param {object} attrs
|
Chris@0
|
479 * The attributes changes in the save or set call.
|
Chris@0
|
480 * @param {object} options
|
Chris@0
|
481 * An object with the following option:
|
Chris@0
|
482 * @param {string} [options.reason]
|
Chris@0
|
483 * A string that conveys a particular reason to allow for an exceptional
|
Chris@0
|
484 * state change.
|
Chris@0
|
485 * @param {Array} options.accept-field-states
|
Chris@0
|
486 * An array of strings that represent field states that the entities must
|
Chris@0
|
487 * be in to validate. For example, if `accept-field-states` is
|
Chris@0
|
488 * `['candidate', 'highlighted']`, then all the fields of the entity must
|
Chris@0
|
489 * be in either of these two states for the save or set call to
|
Chris@0
|
490 * validate and proceed.
|
Chris@0
|
491 *
|
Chris@0
|
492 * @return {string}
|
Chris@0
|
493 * A string to say something about the state of the entity model.
|
Chris@0
|
494 */
|
Chris@0
|
495 validate(attrs, options) {
|
Chris@0
|
496 const acceptedFieldStates = options['accept-field-states'] || [];
|
Chris@0
|
497
|
Chris@0
|
498 // Validate state change.
|
Chris@0
|
499 const currentState = this.get('state');
|
Chris@0
|
500 const nextState = attrs.state;
|
Chris@0
|
501 if (currentState !== nextState) {
|
Chris@0
|
502 // Ensure it's a valid state.
|
Chris@0
|
503 if (_.indexOf(this.constructor.states, nextState) === -1) {
|
Chris@0
|
504 return `"${nextState}" is an invalid state`;
|
Chris@0
|
505 }
|
Chris@0
|
506
|
Chris@0
|
507 // Ensure it's a state change that is allowed.
|
Chris@0
|
508 // Check if the acceptStateChange function accepts it.
|
Chris@0
|
509 if (!this._acceptStateChange(currentState, nextState, options)) {
|
Chris@0
|
510 return 'state change not accepted';
|
Chris@0
|
511 }
|
Chris@0
|
512 // If that function accepts it, then ensure all fields are also in an
|
Chris@0
|
513 // acceptable state.
|
Chris@0
|
514 else if (!this._fieldsHaveAcceptableStates(acceptedFieldStates)) {
|
Chris@0
|
515 return 'state change not accepted because fields are not in acceptable state';
|
Chris@0
|
516 }
|
Chris@0
|
517 }
|
Chris@0
|
518
|
Chris@0
|
519 // Validate setting isCommitting = true.
|
Chris@0
|
520 const currentIsCommitting = this.get('isCommitting');
|
Chris@0
|
521 const nextIsCommitting = attrs.isCommitting;
|
Chris@0
|
522 if (currentIsCommitting === false && nextIsCommitting === true) {
|
Chris@0
|
523 if (!this._fieldsHaveAcceptableStates(acceptedFieldStates)) {
|
Chris@0
|
524 return 'isCommitting change not accepted because fields are not in acceptable state';
|
Chris@0
|
525 }
|
Chris@0
|
526 }
|
Chris@0
|
527 else if (currentIsCommitting === true && nextIsCommitting === true) {
|
Chris@0
|
528 return 'isCommitting is a mutex, hence only changes are allowed';
|
Chris@0
|
529 }
|
Chris@0
|
530 },
|
Chris@0
|
531
|
Chris@0
|
532 /**
|
Chris@0
|
533 * Checks if a state change can be accepted.
|
Chris@0
|
534 *
|
Chris@0
|
535 * @param {string} from
|
Chris@0
|
536 * From state.
|
Chris@0
|
537 * @param {string} to
|
Chris@0
|
538 * To state.
|
Chris@0
|
539 * @param {object} context
|
Chris@0
|
540 * Context for the check.
|
Chris@0
|
541 * @param {string} context.reason
|
Chris@0
|
542 * The reason for the state change.
|
Chris@0
|
543 * @param {bool} context.confirming
|
Chris@0
|
544 * Whether context is confirming or not.
|
Chris@0
|
545 *
|
Chris@0
|
546 * @return {bool}
|
Chris@0
|
547 * Whether the state change is accepted or not.
|
Chris@0
|
548 *
|
Chris@0
|
549 * @see Drupal.quickedit.AppView#acceptEditorStateChange
|
Chris@0
|
550 */
|
Chris@0
|
551 _acceptStateChange(from, to, context) {
|
Chris@0
|
552 let accept = true;
|
Chris@0
|
553
|
Chris@0
|
554 // In general, enforce the states sequence. Disallow going back from a
|
Chris@0
|
555 // "later" state to an "earlier" state, except in explicitly allowed
|
Chris@0
|
556 // cases.
|
Chris@0
|
557 if (!this.constructor.followsStateSequence(from, to)) {
|
Chris@0
|
558 accept = false;
|
Chris@0
|
559
|
Chris@0
|
560 // Allow: closing -> closed.
|
Chris@0
|
561 // Necessary to stop editing an entity.
|
Chris@0
|
562 if (from === 'closing' && to === 'closed') {
|
Chris@0
|
563 accept = true;
|
Chris@0
|
564 }
|
Chris@0
|
565 // Allow: committing -> opened.
|
Chris@0
|
566 // Necessary to be able to correct an invalid field, or to hit the
|
Chris@0
|
567 // "Save" button again after a server/network error.
|
Chris@0
|
568 else if (from === 'committing' && to === 'opened' && context.reason && (context.reason === 'invalid' || context.reason === 'networkerror')) {
|
Chris@0
|
569 accept = true;
|
Chris@0
|
570 }
|
Chris@0
|
571 // Allow: deactivating -> opened.
|
Chris@0
|
572 // Necessary to be able to confirm changes with the user.
|
Chris@0
|
573 else if (from === 'deactivating' && to === 'opened' && context.confirming) {
|
Chris@0
|
574 accept = true;
|
Chris@0
|
575 }
|
Chris@0
|
576 // Allow: opened -> deactivating.
|
Chris@0
|
577 // Necessary to be able to stop editing.
|
Chris@0
|
578 else if (from === 'opened' && to === 'deactivating' && context.confirmed) {
|
Chris@0
|
579 accept = true;
|
Chris@0
|
580 }
|
Chris@0
|
581 }
|
Chris@0
|
582
|
Chris@0
|
583 return accept;
|
Chris@0
|
584 },
|
Chris@0
|
585
|
Chris@0
|
586 /**
|
Chris@0
|
587 * Checks if fields have acceptable states.
|
Chris@0
|
588 *
|
Chris@0
|
589 * @param {Array} acceptedFieldStates
|
Chris@0
|
590 * An array of acceptable field states to check for.
|
Chris@0
|
591 *
|
Chris@0
|
592 * @return {bool}
|
Chris@0
|
593 * Whether the fields have an acceptable state.
|
Chris@0
|
594 *
|
Chris@0
|
595 * @see Drupal.quickedit.EntityModel#validate
|
Chris@0
|
596 */
|
Chris@0
|
597 _fieldsHaveAcceptableStates(acceptedFieldStates) {
|
Chris@0
|
598 let accept = true;
|
Chris@0
|
599
|
Chris@0
|
600 // If no acceptable field states are provided, assume all field states are
|
Chris@0
|
601 // acceptable. We want to let validation pass as a default and only
|
Chris@0
|
602 // check validity on calls to set that explicitly request it.
|
Chris@0
|
603 if (acceptedFieldStates.length > 0) {
|
Chris@0
|
604 const fieldStates = this.get('fields').pluck('state') || [];
|
Chris@0
|
605 // If not all fields are in one of the accepted field states, then we
|
Chris@0
|
606 // still can't allow this state change.
|
Chris@0
|
607 if (_.difference(fieldStates, acceptedFieldStates).length) {
|
Chris@0
|
608 accept = false;
|
Chris@0
|
609 }
|
Chris@0
|
610 }
|
Chris@0
|
611
|
Chris@0
|
612 return accept;
|
Chris@0
|
613 },
|
Chris@0
|
614
|
Chris@0
|
615 /**
|
Chris@0
|
616 * Destroys the entity model.
|
Chris@0
|
617 *
|
Chris@0
|
618 * @param {object} options
|
Chris@0
|
619 * Options for the entity model.
|
Chris@0
|
620 */
|
Chris@0
|
621 destroy(options) {
|
Chris@0
|
622 Drupal.quickedit.BaseModel.prototype.destroy.call(this, options);
|
Chris@0
|
623
|
Chris@0
|
624 this.stopListening();
|
Chris@0
|
625
|
Chris@0
|
626 // Destroy all fields of this entity.
|
Chris@0
|
627 this.get('fields').reset();
|
Chris@0
|
628 },
|
Chris@0
|
629
|
Chris@0
|
630 /**
|
Chris@0
|
631 * @inheritdoc
|
Chris@0
|
632 */
|
Chris@0
|
633 sync() {
|
Chris@0
|
634 // We don't use REST updates to sync.
|
Chris@0
|
635
|
Chris@0
|
636 },
|
Chris@0
|
637
|
Chris@0
|
638 }, /** @lends Drupal.quickedit.EntityModel */{
|
Chris@0
|
639
|
Chris@0
|
640 /**
|
Chris@0
|
641 * Sequence of all possible states an entity can be in during quickediting.
|
Chris@0
|
642 *
|
Chris@0
|
643 * @type {Array.<string>}
|
Chris@0
|
644 */
|
Chris@0
|
645 states: [
|
Chris@0
|
646 // Initial state, like field's 'inactive' OR the user has just finished
|
Chris@0
|
647 // in-place editing this entity.
|
Chris@0
|
648 // - Trigger: none (initial) or EntityModel (finished).
|
Chris@0
|
649 // - Expected behavior: (when not initial state): tear down
|
Chris@0
|
650 // EntityToolbarView, in-place editors and related views.
|
Chris@0
|
651 'closed',
|
Chris@0
|
652 // User has activated in-place editing of this entity.
|
Chris@0
|
653 // - Trigger: user.
|
Chris@0
|
654 // - Expected behavior: the EntityToolbarView is gets set up, in-place
|
Chris@0
|
655 // editors (EditorViews) and related views for this entity's fields are
|
Chris@0
|
656 // set up. Upon completion of those, the state is changed to 'opening'.
|
Chris@0
|
657 'launching',
|
Chris@0
|
658 // Launching has finished.
|
Chris@0
|
659 // - Trigger: application.
|
Chris@0
|
660 // - Guarantees: in-place editors ready for use, all entity and field
|
Chris@0
|
661 // views have been set up, all fields are in the 'inactive' state.
|
Chris@0
|
662 // - Expected behavior: all fields are changed to the 'candidate' state
|
Chris@0
|
663 // and once this is completed, the entity state will be changed to
|
Chris@0
|
664 // 'opened'.
|
Chris@0
|
665 'opening',
|
Chris@0
|
666 // Opening has finished.
|
Chris@0
|
667 // - Trigger: EntityModel.
|
Chris@0
|
668 // - Guarantees: see 'opening', all fields are in the 'candidate' state.
|
Chris@0
|
669 // - Expected behavior: the user is able to actually use in-place editing.
|
Chris@0
|
670 'opened',
|
Chris@0
|
671 // User has clicked the 'Save' button (and has thus changed at least one
|
Chris@0
|
672 // field).
|
Chris@0
|
673 // - Trigger: user.
|
Chris@0
|
674 // - Guarantees: see 'opened', plus: either a changed field is in
|
Chris@0
|
675 // PrivateTempStore, or the user has just modified a field without
|
Chris@0
|
676 // activating (switching to) another field.
|
Chris@0
|
677 // - Expected behavior: 1) if any of the fields are not yet in
|
Chris@0
|
678 // PrivateTempStore, save them to PrivateTempStore, 2) if then any of
|
Chris@0
|
679 // the fields has the 'invalid' state, then change the entity state back
|
Chris@0
|
680 // to 'opened', otherwise: save the entity by committing it from
|
Chris@0
|
681 // PrivateTempStore into permanent storage.
|
Chris@0
|
682 'committing',
|
Chris@0
|
683 // User has clicked the 'Close' button, or has clicked the 'Save' button
|
Chris@0
|
684 // and that was successfully completed.
|
Chris@0
|
685 // - Trigger: user or EntityModel.
|
Chris@0
|
686 // - Guarantees: when having clicked 'Close' hardly any: fields may be in
|
Chris@0
|
687 // a variety of states; when having clicked 'Save': all fields are in
|
Chris@0
|
688 // the 'candidate' state.
|
Chris@0
|
689 // - Expected behavior: transition all fields to the 'candidate' state,
|
Chris@0
|
690 // possibly requiring confirmation in the case of having clicked
|
Chris@0
|
691 // 'Close'.
|
Chris@0
|
692 'deactivating',
|
Chris@0
|
693 // Deactivation has been completed.
|
Chris@0
|
694 // - Trigger: EntityModel.
|
Chris@0
|
695 // - Guarantees: all fields are in the 'candidate' state.
|
Chris@0
|
696 // - Expected behavior: change all fields to the 'inactive' state.
|
Chris@0
|
697 'closing',
|
Chris@0
|
698 ],
|
Chris@0
|
699
|
Chris@0
|
700 /**
|
Chris@0
|
701 * Indicates whether the 'from' state comes before the 'to' state.
|
Chris@0
|
702 *
|
Chris@0
|
703 * @param {string} from
|
Chris@0
|
704 * One of {@link Drupal.quickedit.EntityModel.states}.
|
Chris@0
|
705 * @param {string} to
|
Chris@0
|
706 * One of {@link Drupal.quickedit.EntityModel.states}.
|
Chris@0
|
707 *
|
Chris@0
|
708 * @return {bool}
|
Chris@0
|
709 * Whether the 'from' state comes before the 'to' state.
|
Chris@0
|
710 */
|
Chris@0
|
711 followsStateSequence(from, to) {
|
Chris@0
|
712 return _.indexOf(this.states, from) < _.indexOf(this.states, to);
|
Chris@0
|
713 },
|
Chris@0
|
714
|
Chris@0
|
715 });
|
Chris@0
|
716
|
Chris@0
|
717 /**
|
Chris@0
|
718 * @constructor
|
Chris@0
|
719 *
|
Chris@0
|
720 * @augments Backbone.Collection
|
Chris@0
|
721 */
|
Chris@0
|
722 Drupal.quickedit.EntityCollection = Backbone.Collection.extend(/** @lends Drupal.quickedit.EntityCollection# */{
|
Chris@0
|
723
|
Chris@0
|
724 /**
|
Chris@0
|
725 * @type {Drupal.quickedit.EntityModel}
|
Chris@0
|
726 */
|
Chris@0
|
727 model: Drupal.quickedit.EntityModel,
|
Chris@0
|
728 });
|
Chris@0
|
729 }(_, jQuery, Backbone, Drupal));
|