Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/quickedit/js/views/AppView.es6.js @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 /** | |
2 * @file | |
3 * A Backbone View that controls the overall "in-place editing application". | |
4 * | |
5 * @see Drupal.quickedit.AppModel | |
6 */ | |
7 | |
8 (function ($, _, Backbone, Drupal) { | |
9 // Indicates whether the page should be reloaded after in-place editing has | |
10 // shut down. A page reload is necessary to re-instate the original HTML of | |
11 // the edited fields if in-place editing has been canceled and one or more of | |
12 // the entity's fields were saved to PrivateTempStore: one of them may have | |
13 // been changed to the empty value and hence may have been rerendered as the | |
14 // empty string, which makes it impossible for Quick Edit to know where to | |
15 // restore the original HTML. | |
16 let reload = false; | |
17 | |
18 Drupal.quickedit.AppView = Backbone.View.extend(/** @lends Drupal.quickedit.AppView# */{ | |
19 | |
20 /** | |
21 * @constructs | |
22 * | |
23 * @augments Backbone.View | |
24 * | |
25 * @param {object} options | |
26 * An object with the following keys: | |
27 * @param {Drupal.quickedit.AppModel} options.model | |
28 * The application state model. | |
29 * @param {Drupal.quickedit.EntityCollection} options.entitiesCollection | |
30 * All on-page entities. | |
31 * @param {Drupal.quickedit.FieldCollection} options.fieldsCollection | |
32 * All on-page fields | |
33 */ | |
34 initialize(options) { | |
35 // AppView's configuration for handling states. | |
36 // @see Drupal.quickedit.FieldModel.states | |
37 this.activeFieldStates = ['activating', 'active']; | |
38 this.singleFieldStates = ['highlighted', 'activating', 'active']; | |
39 this.changedFieldStates = ['changed', 'saving', 'saved', 'invalid']; | |
40 this.readyFieldStates = ['candidate', 'highlighted']; | |
41 | |
42 // Track app state. | |
43 this.listenTo(options.entitiesCollection, 'change:state', this.appStateChange); | |
44 this.listenTo(options.entitiesCollection, 'change:isActive', this.enforceSingleActiveEntity); | |
45 | |
46 // Track app state. | |
47 this.listenTo(options.fieldsCollection, 'change:state', this.editorStateChange); | |
48 // Respond to field model HTML representation change events. | |
49 this.listenTo(options.fieldsCollection, 'change:html', this.renderUpdatedField); | |
50 this.listenTo(options.fieldsCollection, 'change:html', this.propagateUpdatedField); | |
51 // Respond to addition. | |
52 this.listenTo(options.fieldsCollection, 'add', this.rerenderedFieldToCandidate); | |
53 // Respond to destruction. | |
54 this.listenTo(options.fieldsCollection, 'destroy', this.teardownEditor); | |
55 }, | |
56 | |
57 /** | |
58 * Handles setup/teardown and state changes when the active entity changes. | |
59 * | |
60 * @param {Drupal.quickedit.EntityModel} entityModel | |
61 * An instance of the EntityModel class. | |
62 * @param {string} state | |
63 * The state of the associated field. One of | |
64 * {@link Drupal.quickedit.EntityModel.states}. | |
65 */ | |
66 appStateChange(entityModel, state) { | |
67 const app = this; | |
68 let entityToolbarView; | |
69 switch (state) { | |
70 case 'launching': | |
71 reload = false; | |
72 // First, create an entity toolbar view. | |
73 entityToolbarView = new Drupal.quickedit.EntityToolbarView({ | |
74 model: entityModel, | |
75 appModel: this.model, | |
76 }); | |
77 entityModel.toolbarView = entityToolbarView; | |
78 // Second, set up in-place editors. | |
79 // They must be notified of state changes, hence this must happen | |
80 // while the associated fields are still in the 'inactive' state. | |
81 entityModel.get('fields').each((fieldModel) => { | |
82 app.setupEditor(fieldModel); | |
83 }); | |
84 // Third, transition the entity to the 'opening' state, which will | |
85 // transition all fields from 'inactive' to 'candidate'. | |
86 _.defer(() => { | |
87 entityModel.set('state', 'opening'); | |
88 }); | |
89 break; | |
90 | |
91 case 'closed': | |
92 entityToolbarView = entityModel.toolbarView; | |
93 // First, tear down the in-place editors. | |
94 entityModel.get('fields').each((fieldModel) => { | |
95 app.teardownEditor(fieldModel); | |
96 }); | |
97 // Second, tear down the entity toolbar view. | |
98 if (entityToolbarView) { | |
99 entityToolbarView.remove(); | |
100 delete entityModel.toolbarView; | |
101 } | |
102 // A page reload may be necessary to re-instate the original HTML of | |
103 // the edited fields. | |
104 if (reload) { | |
105 reload = false; | |
106 location.reload(); | |
107 } | |
108 break; | |
109 } | |
110 }, | |
111 | |
112 /** | |
113 * Accepts or reject editor (Editor) state changes. | |
114 * | |
115 * This is what ensures that the app is in control of what happens. | |
116 * | |
117 * @param {string} from | |
118 * The previous state. | |
119 * @param {string} to | |
120 * The new state. | |
121 * @param {null|object} context | |
122 * The context that is trying to trigger the state change. | |
123 * @param {Drupal.quickedit.FieldModel} fieldModel | |
124 * The fieldModel to which this change applies. | |
125 * | |
126 * @return {bool} | |
127 * Whether the editor change was accepted or rejected. | |
128 */ | |
129 acceptEditorStateChange(from, to, context, fieldModel) { | |
130 let accept = true; | |
131 | |
132 // If the app is in view mode, then reject all state changes except for | |
133 // those to 'inactive'. | |
134 if (context && (context.reason === 'stop' || context.reason === 'rerender')) { | |
135 if (from === 'candidate' && to === 'inactive') { | |
136 accept = true; | |
137 } | |
138 } | |
139 // Handling of edit mode state changes is more granular. | |
140 else { | |
141 // In general, enforce the states sequence. Disallow going back from a | |
142 // "later" state to an "earlier" state, except in explicitly allowed | |
143 // cases. | |
144 if (!Drupal.quickedit.FieldModel.followsStateSequence(from, to)) { | |
145 accept = false; | |
146 // Allow: activating/active -> candidate. | |
147 // Necessary to stop editing a field. | |
148 if (_.indexOf(this.activeFieldStates, from) !== -1 && to === 'candidate') { | |
149 accept = true; | |
150 } | |
151 // Allow: changed/invalid -> candidate. | |
152 // Necessary to stop editing a field when it is changed or invalid. | |
153 else if ((from === 'changed' || from === 'invalid') && to === 'candidate') { | |
154 accept = true; | |
155 } | |
156 // Allow: highlighted -> candidate. | |
157 // Necessary to stop highlighting a field. | |
158 else if (from === 'highlighted' && to === 'candidate') { | |
159 accept = true; | |
160 } | |
161 // Allow: saved -> candidate. | |
162 // Necessary when successfully saved a field. | |
163 else if (from === 'saved' && to === 'candidate') { | |
164 accept = true; | |
165 } | |
166 // Allow: invalid -> saving. | |
167 // Necessary to be able to save a corrected, invalid field. | |
168 else if (from === 'invalid' && to === 'saving') { | |
169 accept = true; | |
170 } | |
171 // Allow: invalid -> activating. | |
172 // Necessary to be able to correct a field that turned out to be | |
173 // invalid after the user already had moved on to the next field | |
174 // (which we explicitly allow to have a fluent UX). | |
175 else if (from === 'invalid' && to === 'activating') { | |
176 accept = true; | |
177 } | |
178 } | |
179 | |
180 // If it's not against the general principle, then here are more | |
181 // disallowed cases to check. | |
182 if (accept) { | |
183 let activeField; | |
184 let activeFieldState; | |
185 // Ensure only one field (editor) at a time is active … but allow a | |
186 // user to hop from one field to the next, even if we still have to | |
187 // start saving the field that is currently active: assume it will be | |
188 // valid, to allow for a fluent UX. (If it turns out to be invalid, | |
189 // this block of code also handles that.) | |
190 if ((this.readyFieldStates.indexOf(from) !== -1 || from === 'invalid') && this.activeFieldStates.indexOf(to) !== -1) { | |
191 activeField = this.model.get('activeField'); | |
192 if (activeField && activeField !== fieldModel) { | |
193 activeFieldState = activeField.get('state'); | |
194 // Allow the state change. If the state of the active field is: | |
195 // - 'activating' or 'active': change it to 'candidate' | |
196 // - 'changed' or 'invalid': change it to 'saving' | |
197 // - 'saving' or 'saved': don't do anything. | |
198 if (this.activeFieldStates.indexOf(activeFieldState) !== -1) { | |
199 activeField.set('state', 'candidate'); | |
200 } | |
201 else if (activeFieldState === 'changed' || activeFieldState === 'invalid') { | |
202 activeField.set('state', 'saving'); | |
203 } | |
204 | |
205 // If the field that's being activated is in fact already in the | |
206 // invalid state (which can only happen because above we allowed | |
207 // the user to move on to another field to allow for a fluent UX; | |
208 // we assumed it would be saved successfully), then we shouldn't | |
209 // allow the field to enter the 'activating' state, instead, we | |
210 // simply change the active editor. All guarantees and | |
211 // assumptions for this field still hold! | |
212 if (from === 'invalid') { | |
213 this.model.set('activeField', fieldModel); | |
214 accept = false; | |
215 } | |
216 // Do not reject: the field is either in the 'candidate' or | |
217 // 'highlighted' state and we allow it to enter the 'activating' | |
218 // state! | |
219 } | |
220 } | |
221 // Reject going from activating/active to candidate because of a | |
222 // mouseleave. | |
223 else if (_.indexOf(this.activeFieldStates, from) !== -1 && to === 'candidate') { | |
224 if (context && context.reason === 'mouseleave') { | |
225 accept = false; | |
226 } | |
227 } | |
228 // When attempting to stop editing a changed/invalid property, ask for | |
229 // confirmation. | |
230 else if ((from === 'changed' || from === 'invalid') && to === 'candidate') { | |
231 if (context && context.reason === 'mouseleave') { | |
232 accept = false; | |
233 } | |
234 // Check whether the transition has been confirmed? | |
235 else if (context && context.confirmed) { | |
236 accept = true; | |
237 } | |
238 } | |
239 } | |
240 } | |
241 | |
242 return accept; | |
243 }, | |
244 | |
245 /** | |
246 * Sets up the in-place editor for the given field. | |
247 * | |
248 * Must happen before the fieldModel's state is changed to 'candidate'. | |
249 * | |
250 * @param {Drupal.quickedit.FieldModel} fieldModel | |
251 * The field for which an in-place editor must be set up. | |
252 */ | |
253 setupEditor(fieldModel) { | |
254 // Get the corresponding entity toolbar. | |
255 const entityModel = fieldModel.get('entity'); | |
256 const entityToolbarView = entityModel.toolbarView; | |
257 // Get the field toolbar DOM root from the entity toolbar. | |
258 const fieldToolbarRoot = entityToolbarView.getToolbarRoot(); | |
259 // Create in-place editor. | |
260 const editorName = fieldModel.get('metadata').editor; | |
261 const editorModel = new Drupal.quickedit.EditorModel(); | |
262 const editorView = new Drupal.quickedit.editors[editorName]({ | |
263 el: $(fieldModel.get('el')), | |
264 model: editorModel, | |
265 fieldModel, | |
266 }); | |
267 | |
268 // Create in-place editor's toolbar for this field — stored inside the | |
269 // entity toolbar, the entity toolbar will position itself appropriately | |
270 // above (or below) the edited element. | |
271 const toolbarView = new Drupal.quickedit.FieldToolbarView({ | |
272 el: fieldToolbarRoot, | |
273 model: fieldModel, | |
274 $editedElement: $(editorView.getEditedElement()), | |
275 editorView, | |
276 entityModel, | |
277 }); | |
278 | |
279 // Create decoration for edited element: padding if necessary, sets | |
280 // classes on the element to style it according to the current state. | |
281 const decorationView = new Drupal.quickedit.FieldDecorationView({ | |
282 el: $(editorView.getEditedElement()), | |
283 model: fieldModel, | |
284 editorView, | |
285 }); | |
286 | |
287 // Track these three views in FieldModel so that we can tear them down | |
288 // correctly. | |
289 fieldModel.editorView = editorView; | |
290 fieldModel.toolbarView = toolbarView; | |
291 fieldModel.decorationView = decorationView; | |
292 }, | |
293 | |
294 /** | |
295 * Tears down the in-place editor for the given field. | |
296 * | |
297 * Must happen after the fieldModel's state is changed to 'inactive'. | |
298 * | |
299 * @param {Drupal.quickedit.FieldModel} fieldModel | |
300 * The field for which an in-place editor must be torn down. | |
301 */ | |
302 teardownEditor(fieldModel) { | |
303 // Early-return if this field was not yet decorated. | |
304 if (typeof fieldModel.editorView === 'undefined') { | |
305 return; | |
306 } | |
307 | |
308 // Unbind event handlers; remove toolbar element; delete toolbar view. | |
309 fieldModel.toolbarView.remove(); | |
310 delete fieldModel.toolbarView; | |
311 | |
312 // Unbind event handlers; delete decoration view. Don't remove the element | |
313 // because that would remove the field itself. | |
314 fieldModel.decorationView.remove(); | |
315 delete fieldModel.decorationView; | |
316 | |
317 // Unbind event handlers; delete editor view. Don't remove the element | |
318 // because that would remove the field itself. | |
319 fieldModel.editorView.remove(); | |
320 delete fieldModel.editorView; | |
321 }, | |
322 | |
323 /** | |
324 * Asks the user to confirm whether he wants to stop editing via a modal. | |
325 * | |
326 * @param {Drupal.quickedit.EntityModel} entityModel | |
327 * An instance of the EntityModel class. | |
328 * | |
329 * @see Drupal.quickedit.AppView#acceptEditorStateChange | |
330 */ | |
331 confirmEntityDeactivation(entityModel) { | |
332 const that = this; | |
333 let discardDialog; | |
334 | |
335 function closeDiscardDialog(action) { | |
336 discardDialog.close(action); | |
337 // The active modal has been removed. | |
338 that.model.set('activeModal', null); | |
339 | |
340 // If the targetState is saving, the field must be saved, then the | |
341 // entity must be saved. | |
342 if (action === 'save') { | |
343 entityModel.set('state', 'committing', { confirmed: true }); | |
344 } | |
345 else { | |
346 entityModel.set('state', 'deactivating', { confirmed: true }); | |
347 // Editing has been canceled and the changes will not be saved. Mark | |
348 // the page for reload if the entityModel declares that it requires | |
349 // a reload. | |
350 if (entityModel.get('reload')) { | |
351 reload = true; | |
352 entityModel.set('reload', false); | |
353 } | |
354 } | |
355 } | |
356 | |
357 // Only instantiate if there isn't a modal instance visible yet. | |
358 if (!this.model.get('activeModal')) { | |
359 const $unsavedChanges = $(`<div>${Drupal.t('You have unsaved changes')}</div>`); | |
360 discardDialog = Drupal.dialog($unsavedChanges.get(0), { | |
361 title: Drupal.t('Discard changes?'), | |
362 dialogClass: 'quickedit-discard-modal', | |
363 resizable: false, | |
364 buttons: [ | |
365 { | |
366 text: Drupal.t('Save'), | |
367 click() { | |
368 closeDiscardDialog('save'); | |
369 }, | |
370 primary: true, | |
371 }, | |
372 { | |
373 text: Drupal.t('Discard changes'), | |
374 click() { | |
375 closeDiscardDialog('discard'); | |
376 }, | |
377 }, | |
378 ], | |
379 // Prevent this modal from being closed without the user making a | |
380 // choice as per http://stackoverflow.com/a/5438771. | |
381 closeOnEscape: false, | |
382 create() { | |
383 $(this).parent().find('.ui-dialog-titlebar-close').remove(); | |
384 }, | |
385 beforeClose: false, | |
386 close(event) { | |
387 // Automatically destroy the DOM element that was used for the | |
388 // dialog. | |
389 $(event.target).remove(); | |
390 }, | |
391 }); | |
392 this.model.set('activeModal', discardDialog); | |
393 | |
394 discardDialog.showModal(); | |
395 } | |
396 }, | |
397 | |
398 /** | |
399 * Reacts to field state changes; tracks global state. | |
400 * | |
401 * @param {Drupal.quickedit.FieldModel} fieldModel | |
402 * The `fieldModel` holding the state. | |
403 * @param {string} state | |
404 * The state of the associated field. One of | |
405 * {@link Drupal.quickedit.FieldModel.states}. | |
406 */ | |
407 editorStateChange(fieldModel, state) { | |
408 const from = fieldModel.previous('state'); | |
409 const to = state; | |
410 | |
411 // Keep track of the highlighted field in the global state. | |
412 if (_.indexOf(this.singleFieldStates, to) !== -1 && this.model.get('highlightedField') !== fieldModel) { | |
413 this.model.set('highlightedField', fieldModel); | |
414 } | |
415 else if (this.model.get('highlightedField') === fieldModel && to === 'candidate') { | |
416 this.model.set('highlightedField', null); | |
417 } | |
418 | |
419 // Keep track of the active field in the global state. | |
420 if (_.indexOf(this.activeFieldStates, to) !== -1 && this.model.get('activeField') !== fieldModel) { | |
421 this.model.set('activeField', fieldModel); | |
422 } | |
423 else if (this.model.get('activeField') === fieldModel && to === 'candidate') { | |
424 // Discarded if it transitions from a changed state to 'candidate'. | |
425 if (from === 'changed' || from === 'invalid') { | |
426 fieldModel.editorView.revert(); | |
427 } | |
428 this.model.set('activeField', null); | |
429 } | |
430 }, | |
431 | |
432 /** | |
433 * Render an updated field (a field whose 'html' attribute changed). | |
434 * | |
435 * @param {Drupal.quickedit.FieldModel} fieldModel | |
436 * The FieldModel whose 'html' attribute changed. | |
437 * @param {string} html | |
438 * The updated 'html' attribute. | |
439 * @param {object} options | |
440 * An object with the following keys: | |
441 * @param {bool} options.propagation | |
442 * Whether this change to the 'html' attribute occurred because of the | |
443 * propagation of changes to another instance of this field. | |
444 */ | |
445 renderUpdatedField(fieldModel, html, options) { | |
446 // Get data necessary to rerender property before it is unavailable. | |
447 const $fieldWrapper = $(fieldModel.get('el')); | |
448 const $context = $fieldWrapper.parent(); | |
449 | |
450 const renderField = function () { | |
451 // Destroy the field model; this will cause all attached views to be | |
452 // destroyed too, and removal from all collections in which it exists. | |
453 fieldModel.destroy(); | |
454 | |
455 // Replace the old content with the new content. | |
456 $fieldWrapper.replaceWith(html); | |
457 | |
458 // Attach behaviors again to the modified piece of HTML; this will | |
459 // create a new field model and call rerenderedFieldToCandidate() with | |
460 // it. | |
461 Drupal.attachBehaviors($context.get(0)); | |
462 }; | |
463 | |
464 // When propagating the changes of another instance of this field, this | |
465 // field is not being actively edited and hence no state changes are | |
466 // necessary. So: only update the state of this field when the rerendering | |
467 // of this field happens not because of propagation, but because it is | |
468 // being edited itself. | |
469 if (!options.propagation) { | |
470 // Deferred because renderUpdatedField is reacting to a field model | |
471 // change event, and we want to make sure that event fully propagates | |
472 // before making another change to the same model. | |
473 _.defer(() => { | |
474 // First set the state to 'candidate', to allow all attached views to | |
475 // clean up all their "active state"-related changes. | |
476 fieldModel.set('state', 'candidate'); | |
477 | |
478 // Similarly, the above .set() call's change event must fully | |
479 // propagate before calling it again. | |
480 _.defer(() => { | |
481 // Set the field's state to 'inactive', to enable the updating of | |
482 // its DOM value. | |
483 fieldModel.set('state', 'inactive', { reason: 'rerender' }); | |
484 | |
485 renderField(); | |
486 }); | |
487 }); | |
488 } | |
489 else { | |
490 renderField(); | |
491 } | |
492 }, | |
493 | |
494 /** | |
495 * Propagates changes to an updated field to all instances of that field. | |
496 * | |
497 * @param {Drupal.quickedit.FieldModel} updatedField | |
498 * The FieldModel whose 'html' attribute changed. | |
499 * @param {string} html | |
500 * The updated 'html' attribute. | |
501 * @param {object} options | |
502 * An object with the following keys: | |
503 * @param {bool} options.propagation | |
504 * Whether this change to the 'html' attribute occurred because of the | |
505 * propagation of changes to another instance of this field. | |
506 * | |
507 * @see Drupal.quickedit.AppView#renderUpdatedField | |
508 */ | |
509 propagateUpdatedField(updatedField, html, options) { | |
510 // Don't propagate field updates that themselves were caused by | |
511 // propagation. | |
512 if (options.propagation) { | |
513 return; | |
514 } | |
515 | |
516 const htmlForOtherViewModes = updatedField.get('htmlForOtherViewModes'); | |
517 Drupal.quickedit.collections.fields | |
518 // Find all instances of fields that display the same logical field | |
519 // (same entity, same field, just a different instance and maybe a | |
520 // different view mode). | |
521 .where({ logicalFieldID: updatedField.get('logicalFieldID') }) | |
522 .forEach((field) => { | |
523 // Ignore the field that was already updated. | |
524 if (field === updatedField) { | |
525 | |
526 } | |
527 // If this other instance of the field has the same view mode, we can | |
528 // update it easily. | |
529 else if (field.getViewMode() === updatedField.getViewMode()) { | |
530 field.set('html', updatedField.get('html')); | |
531 } | |
532 // If this other instance of the field has a different view mode, and | |
533 // that is one of the view modes for which a re-rendered version is | |
534 // available (and that should be the case unless this field was only | |
535 // added to the page after editing of the updated field began), then | |
536 // use that view mode's re-rendered version. | |
537 else if (field.getViewMode() in htmlForOtherViewModes) { | |
538 field.set('html', htmlForOtherViewModes[field.getViewMode()], { propagation: true }); | |
539 } | |
540 }); | |
541 }, | |
542 | |
543 /** | |
544 * If the new in-place editable field is for the entity that's currently | |
545 * being edited, then transition it to the 'candidate' state. | |
546 * | |
547 * This happens when a field was modified, saved and hence rerendered. | |
548 * | |
549 * @param {Drupal.quickedit.FieldModel} fieldModel | |
550 * A field that was just added to the collection of fields. | |
551 */ | |
552 rerenderedFieldToCandidate(fieldModel) { | |
553 const activeEntity = Drupal.quickedit.collections.entities.findWhere({ isActive: true }); | |
554 | |
555 // Early-return if there is no active entity. | |
556 if (!activeEntity) { | |
557 return; | |
558 } | |
559 | |
560 // If the field's entity is the active entity, make it a candidate. | |
561 if (fieldModel.get('entity') === activeEntity) { | |
562 this.setupEditor(fieldModel); | |
563 fieldModel.set('state', 'candidate'); | |
564 } | |
565 }, | |
566 | |
567 /** | |
568 * EntityModel Collection change handler. | |
569 * | |
570 * Handler is called `change:isActive` and enforces a single active entity. | |
571 * | |
572 * @param {Drupal.quickedit.EntityModel} changedEntityModel | |
573 * The entityModel instance whose active state has changed. | |
574 */ | |
575 enforceSingleActiveEntity(changedEntityModel) { | |
576 // When an entity is deactivated, we don't need to enforce anything. | |
577 if (changedEntityModel.get('isActive') === false) { | |
578 return; | |
579 } | |
580 | |
581 // This entity was activated; deactivate all other entities. | |
582 changedEntityModel.collection.chain() | |
583 .filter(entityModel => entityModel.get('isActive') === true && entityModel !== changedEntityModel) | |
584 .each((entityModel) => { | |
585 entityModel.set('state', 'deactivating'); | |
586 }); | |
587 }, | |
588 | |
589 }); | |
590 }(jQuery, _, Backbone, Drupal)); |