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