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 else {
|
Chris@0
|
235 // Check whether the transition has been confirmed?
|
Chris@0
|
236 if (context && context.confirmed) {
|
Chris@0
|
237 accept = true;
|
Chris@0
|
238 }
|
Chris@0
|
239 }
|
Chris@0
|
240 }
|
Chris@0
|
241 }
|
Chris@0
|
242 }
|
Chris@0
|
243
|
Chris@0
|
244 return accept;
|
Chris@0
|
245 },
|
Chris@0
|
246
|
Chris@0
|
247 /**
|
Chris@0
|
248 * Sets up the in-place editor for the given field.
|
Chris@0
|
249 *
|
Chris@0
|
250 * Must happen before the fieldModel's state is changed to 'candidate'.
|
Chris@0
|
251 *
|
Chris@0
|
252 * @param {Drupal.quickedit.FieldModel} fieldModel
|
Chris@0
|
253 * The field for which an in-place editor must be set up.
|
Chris@0
|
254 */
|
Chris@0
|
255 setupEditor(fieldModel) {
|
Chris@0
|
256 // Get the corresponding entity toolbar.
|
Chris@0
|
257 const entityModel = fieldModel.get('entity');
|
Chris@0
|
258 const entityToolbarView = entityModel.toolbarView;
|
Chris@0
|
259 // Get the field toolbar DOM root from the entity toolbar.
|
Chris@0
|
260 const fieldToolbarRoot = entityToolbarView.getToolbarRoot();
|
Chris@0
|
261 // Create in-place editor.
|
Chris@0
|
262 const editorName = fieldModel.get('metadata').editor;
|
Chris@0
|
263 const editorModel = new Drupal.quickedit.EditorModel();
|
Chris@0
|
264 const editorView = new Drupal.quickedit.editors[editorName]({
|
Chris@0
|
265 el: $(fieldModel.get('el')),
|
Chris@0
|
266 model: editorModel,
|
Chris@0
|
267 fieldModel,
|
Chris@0
|
268 });
|
Chris@0
|
269
|
Chris@0
|
270 // Create in-place editor's toolbar for this field — stored inside the
|
Chris@0
|
271 // entity toolbar, the entity toolbar will position itself appropriately
|
Chris@0
|
272 // above (or below) the edited element.
|
Chris@0
|
273 const toolbarView = new Drupal.quickedit.FieldToolbarView({
|
Chris@0
|
274 el: fieldToolbarRoot,
|
Chris@0
|
275 model: fieldModel,
|
Chris@0
|
276 $editedElement: $(editorView.getEditedElement()),
|
Chris@0
|
277 editorView,
|
Chris@0
|
278 entityModel,
|
Chris@0
|
279 });
|
Chris@0
|
280
|
Chris@0
|
281 // Create decoration for edited element: padding if necessary, sets
|
Chris@0
|
282 // classes on the element to style it according to the current state.
|
Chris@0
|
283 const decorationView = new Drupal.quickedit.FieldDecorationView({
|
Chris@0
|
284 el: $(editorView.getEditedElement()),
|
Chris@0
|
285 model: fieldModel,
|
Chris@0
|
286 editorView,
|
Chris@0
|
287 });
|
Chris@0
|
288
|
Chris@0
|
289 // Track these three views in FieldModel so that we can tear them down
|
Chris@0
|
290 // correctly.
|
Chris@0
|
291 fieldModel.editorView = editorView;
|
Chris@0
|
292 fieldModel.toolbarView = toolbarView;
|
Chris@0
|
293 fieldModel.decorationView = decorationView;
|
Chris@0
|
294 },
|
Chris@0
|
295
|
Chris@0
|
296 /**
|
Chris@0
|
297 * Tears down the in-place editor for the given field.
|
Chris@0
|
298 *
|
Chris@0
|
299 * Must happen after the fieldModel's state is changed to 'inactive'.
|
Chris@0
|
300 *
|
Chris@0
|
301 * @param {Drupal.quickedit.FieldModel} fieldModel
|
Chris@0
|
302 * The field for which an in-place editor must be torn down.
|
Chris@0
|
303 */
|
Chris@0
|
304 teardownEditor(fieldModel) {
|
Chris@0
|
305 // Early-return if this field was not yet decorated.
|
Chris@0
|
306 if (typeof fieldModel.editorView === 'undefined') {
|
Chris@0
|
307 return;
|
Chris@0
|
308 }
|
Chris@0
|
309
|
Chris@0
|
310 // Unbind event handlers; remove toolbar element; delete toolbar view.
|
Chris@0
|
311 fieldModel.toolbarView.remove();
|
Chris@0
|
312 delete fieldModel.toolbarView;
|
Chris@0
|
313
|
Chris@0
|
314 // Unbind event handlers; delete decoration view. Don't remove the element
|
Chris@0
|
315 // because that would remove the field itself.
|
Chris@0
|
316 fieldModel.decorationView.remove();
|
Chris@0
|
317 delete fieldModel.decorationView;
|
Chris@0
|
318
|
Chris@0
|
319 // Unbind event handlers; delete editor view. Don't remove the element
|
Chris@0
|
320 // because that would remove the field itself.
|
Chris@0
|
321 fieldModel.editorView.remove();
|
Chris@0
|
322 delete fieldModel.editorView;
|
Chris@0
|
323 },
|
Chris@0
|
324
|
Chris@0
|
325 /**
|
Chris@0
|
326 * Asks the user to confirm whether he wants to stop editing via a modal.
|
Chris@0
|
327 *
|
Chris@0
|
328 * @param {Drupal.quickedit.EntityModel} entityModel
|
Chris@0
|
329 * An instance of the EntityModel class.
|
Chris@0
|
330 *
|
Chris@0
|
331 * @see Drupal.quickedit.AppView#acceptEditorStateChange
|
Chris@0
|
332 */
|
Chris@0
|
333 confirmEntityDeactivation(entityModel) {
|
Chris@0
|
334 const that = this;
|
Chris@0
|
335 let discardDialog;
|
Chris@0
|
336
|
Chris@0
|
337 function closeDiscardDialog(action) {
|
Chris@0
|
338 discardDialog.close(action);
|
Chris@0
|
339 // The active modal has been removed.
|
Chris@0
|
340 that.model.set('activeModal', null);
|
Chris@0
|
341
|
Chris@0
|
342 // If the targetState is saving, the field must be saved, then the
|
Chris@0
|
343 // entity must be saved.
|
Chris@0
|
344 if (action === 'save') {
|
Chris@0
|
345 entityModel.set('state', 'committing', { confirmed: true });
|
Chris@0
|
346 }
|
Chris@0
|
347 else {
|
Chris@0
|
348 entityModel.set('state', 'deactivating', { confirmed: true });
|
Chris@0
|
349 // Editing has been canceled and the changes will not be saved. Mark
|
Chris@0
|
350 // the page for reload if the entityModel declares that it requires
|
Chris@0
|
351 // a reload.
|
Chris@0
|
352 if (entityModel.get('reload')) {
|
Chris@0
|
353 reload = true;
|
Chris@0
|
354 entityModel.set('reload', false);
|
Chris@0
|
355 }
|
Chris@0
|
356 }
|
Chris@0
|
357 }
|
Chris@0
|
358
|
Chris@0
|
359 // Only instantiate if there isn't a modal instance visible yet.
|
Chris@0
|
360 if (!this.model.get('activeModal')) {
|
Chris@0
|
361 const $unsavedChanges = $(`<div>${Drupal.t('You have unsaved changes')}</div>`);
|
Chris@0
|
362 discardDialog = Drupal.dialog($unsavedChanges.get(0), {
|
Chris@0
|
363 title: Drupal.t('Discard changes?'),
|
Chris@0
|
364 dialogClass: 'quickedit-discard-modal',
|
Chris@0
|
365 resizable: false,
|
Chris@0
|
366 buttons: [
|
Chris@0
|
367 {
|
Chris@0
|
368 text: Drupal.t('Save'),
|
Chris@0
|
369 click() {
|
Chris@0
|
370 closeDiscardDialog('save');
|
Chris@0
|
371 },
|
Chris@0
|
372 primary: true,
|
Chris@0
|
373 },
|
Chris@0
|
374 {
|
Chris@0
|
375 text: Drupal.t('Discard changes'),
|
Chris@0
|
376 click() {
|
Chris@0
|
377 closeDiscardDialog('discard');
|
Chris@0
|
378 },
|
Chris@0
|
379 },
|
Chris@0
|
380 ],
|
Chris@0
|
381 // Prevent this modal from being closed without the user making a
|
Chris@0
|
382 // choice as per http://stackoverflow.com/a/5438771.
|
Chris@0
|
383 closeOnEscape: false,
|
Chris@0
|
384 create() {
|
Chris@0
|
385 $(this).parent().find('.ui-dialog-titlebar-close').remove();
|
Chris@0
|
386 },
|
Chris@0
|
387 beforeClose: false,
|
Chris@0
|
388 close(event) {
|
Chris@0
|
389 // Automatically destroy the DOM element that was used for the
|
Chris@0
|
390 // dialog.
|
Chris@0
|
391 $(event.target).remove();
|
Chris@0
|
392 },
|
Chris@0
|
393 });
|
Chris@0
|
394 this.model.set('activeModal', discardDialog);
|
Chris@0
|
395
|
Chris@0
|
396 discardDialog.showModal();
|
Chris@0
|
397 }
|
Chris@0
|
398 },
|
Chris@0
|
399
|
Chris@0
|
400 /**
|
Chris@0
|
401 * Reacts to field state changes; tracks global state.
|
Chris@0
|
402 *
|
Chris@0
|
403 * @param {Drupal.quickedit.FieldModel} fieldModel
|
Chris@0
|
404 * The `fieldModel` holding the state.
|
Chris@0
|
405 * @param {string} state
|
Chris@0
|
406 * The state of the associated field. One of
|
Chris@0
|
407 * {@link Drupal.quickedit.FieldModel.states}.
|
Chris@0
|
408 */
|
Chris@0
|
409 editorStateChange(fieldModel, state) {
|
Chris@0
|
410 const from = fieldModel.previous('state');
|
Chris@0
|
411 const to = state;
|
Chris@0
|
412
|
Chris@0
|
413 // Keep track of the highlighted field in the global state.
|
Chris@0
|
414 if (_.indexOf(this.singleFieldStates, to) !== -1 && this.model.get('highlightedField') !== fieldModel) {
|
Chris@0
|
415 this.model.set('highlightedField', fieldModel);
|
Chris@0
|
416 }
|
Chris@0
|
417 else if (this.model.get('highlightedField') === fieldModel && to === 'candidate') {
|
Chris@0
|
418 this.model.set('highlightedField', null);
|
Chris@0
|
419 }
|
Chris@0
|
420
|
Chris@0
|
421 // Keep track of the active field in the global state.
|
Chris@0
|
422 if (_.indexOf(this.activeFieldStates, to) !== -1 && this.model.get('activeField') !== fieldModel) {
|
Chris@0
|
423 this.model.set('activeField', fieldModel);
|
Chris@0
|
424 }
|
Chris@0
|
425 else if (this.model.get('activeField') === fieldModel && to === 'candidate') {
|
Chris@0
|
426 // Discarded if it transitions from a changed state to 'candidate'.
|
Chris@0
|
427 if (from === 'changed' || from === 'invalid') {
|
Chris@0
|
428 fieldModel.editorView.revert();
|
Chris@0
|
429 }
|
Chris@0
|
430 this.model.set('activeField', null);
|
Chris@0
|
431 }
|
Chris@0
|
432 },
|
Chris@0
|
433
|
Chris@0
|
434 /**
|
Chris@0
|
435 * Render an updated field (a field whose 'html' attribute changed).
|
Chris@0
|
436 *
|
Chris@0
|
437 * @param {Drupal.quickedit.FieldModel} fieldModel
|
Chris@0
|
438 * The FieldModel whose 'html' attribute changed.
|
Chris@0
|
439 * @param {string} html
|
Chris@0
|
440 * The updated 'html' attribute.
|
Chris@0
|
441 * @param {object} options
|
Chris@0
|
442 * An object with the following keys:
|
Chris@0
|
443 * @param {bool} options.propagation
|
Chris@0
|
444 * Whether this change to the 'html' attribute occurred because of the
|
Chris@0
|
445 * propagation of changes to another instance of this field.
|
Chris@0
|
446 */
|
Chris@0
|
447 renderUpdatedField(fieldModel, html, options) {
|
Chris@0
|
448 // Get data necessary to rerender property before it is unavailable.
|
Chris@0
|
449 const $fieldWrapper = $(fieldModel.get('el'));
|
Chris@0
|
450 const $context = $fieldWrapper.parent();
|
Chris@0
|
451
|
Chris@0
|
452 const renderField = function () {
|
Chris@0
|
453 // Destroy the field model; this will cause all attached views to be
|
Chris@0
|
454 // destroyed too, and removal from all collections in which it exists.
|
Chris@0
|
455 fieldModel.destroy();
|
Chris@0
|
456
|
Chris@0
|
457 // Replace the old content with the new content.
|
Chris@0
|
458 $fieldWrapper.replaceWith(html);
|
Chris@0
|
459
|
Chris@0
|
460 // Attach behaviors again to the modified piece of HTML; this will
|
Chris@0
|
461 // create a new field model and call rerenderedFieldToCandidate() with
|
Chris@0
|
462 // it.
|
Chris@0
|
463 Drupal.attachBehaviors($context.get(0));
|
Chris@0
|
464 };
|
Chris@0
|
465
|
Chris@0
|
466 // When propagating the changes of another instance of this field, this
|
Chris@0
|
467 // field is not being actively edited and hence no state changes are
|
Chris@0
|
468 // necessary. So: only update the state of this field when the rerendering
|
Chris@0
|
469 // of this field happens not because of propagation, but because it is
|
Chris@0
|
470 // being edited itself.
|
Chris@0
|
471 if (!options.propagation) {
|
Chris@0
|
472 // Deferred because renderUpdatedField is reacting to a field model
|
Chris@0
|
473 // change event, and we want to make sure that event fully propagates
|
Chris@0
|
474 // before making another change to the same model.
|
Chris@0
|
475 _.defer(() => {
|
Chris@0
|
476 // First set the state to 'candidate', to allow all attached views to
|
Chris@0
|
477 // clean up all their "active state"-related changes.
|
Chris@0
|
478 fieldModel.set('state', 'candidate');
|
Chris@0
|
479
|
Chris@0
|
480 // Similarly, the above .set() call's change event must fully
|
Chris@0
|
481 // propagate before calling it again.
|
Chris@0
|
482 _.defer(() => {
|
Chris@0
|
483 // Set the field's state to 'inactive', to enable the updating of
|
Chris@0
|
484 // its DOM value.
|
Chris@0
|
485 fieldModel.set('state', 'inactive', { reason: 'rerender' });
|
Chris@0
|
486
|
Chris@0
|
487 renderField();
|
Chris@0
|
488 });
|
Chris@0
|
489 });
|
Chris@0
|
490 }
|
Chris@0
|
491 else {
|
Chris@0
|
492 renderField();
|
Chris@0
|
493 }
|
Chris@0
|
494 },
|
Chris@0
|
495
|
Chris@0
|
496 /**
|
Chris@0
|
497 * Propagates changes to an updated field to all instances of that field.
|
Chris@0
|
498 *
|
Chris@0
|
499 * @param {Drupal.quickedit.FieldModel} updatedField
|
Chris@0
|
500 * The FieldModel whose 'html' attribute changed.
|
Chris@0
|
501 * @param {string} html
|
Chris@0
|
502 * The updated 'html' attribute.
|
Chris@0
|
503 * @param {object} options
|
Chris@0
|
504 * An object with the following keys:
|
Chris@0
|
505 * @param {bool} options.propagation
|
Chris@0
|
506 * Whether this change to the 'html' attribute occurred because of the
|
Chris@0
|
507 * propagation of changes to another instance of this field.
|
Chris@0
|
508 *
|
Chris@0
|
509 * @see Drupal.quickedit.AppView#renderUpdatedField
|
Chris@0
|
510 */
|
Chris@0
|
511 propagateUpdatedField(updatedField, html, options) {
|
Chris@0
|
512 // Don't propagate field updates that themselves were caused by
|
Chris@0
|
513 // propagation.
|
Chris@0
|
514 if (options.propagation) {
|
Chris@0
|
515 return;
|
Chris@0
|
516 }
|
Chris@0
|
517
|
Chris@0
|
518 const htmlForOtherViewModes = updatedField.get('htmlForOtherViewModes');
|
Chris@0
|
519 Drupal.quickedit.collections.fields
|
Chris@0
|
520 // Find all instances of fields that display the same logical field
|
Chris@0
|
521 // (same entity, same field, just a different instance and maybe a
|
Chris@0
|
522 // different view mode).
|
Chris@0
|
523 .where({ logicalFieldID: updatedField.get('logicalFieldID') })
|
Chris@0
|
524 .forEach((field) => {
|
Chris@0
|
525 // Ignore the field that was already updated.
|
Chris@0
|
526 if (field === updatedField) {
|
Chris@0
|
527
|
Chris@0
|
528 }
|
Chris@0
|
529 // If this other instance of the field has the same view mode, we can
|
Chris@0
|
530 // update it easily.
|
Chris@0
|
531 else if (field.getViewMode() === updatedField.getViewMode()) {
|
Chris@0
|
532 field.set('html', updatedField.get('html'));
|
Chris@0
|
533 }
|
Chris@0
|
534 // If this other instance of the field has a different view mode, and
|
Chris@0
|
535 // that is one of the view modes for which a re-rendered version is
|
Chris@0
|
536 // available (and that should be the case unless this field was only
|
Chris@0
|
537 // added to the page after editing of the updated field began), then
|
Chris@0
|
538 // use that view mode's re-rendered version.
|
Chris@0
|
539 else if (field.getViewMode() in htmlForOtherViewModes) {
|
Chris@0
|
540 field.set('html', htmlForOtherViewModes[field.getViewMode()], { propagation: true });
|
Chris@0
|
541 }
|
Chris@0
|
542 });
|
Chris@0
|
543 },
|
Chris@0
|
544
|
Chris@0
|
545 /**
|
Chris@0
|
546 * If the new in-place editable field is for the entity that's currently
|
Chris@0
|
547 * being edited, then transition it to the 'candidate' state.
|
Chris@0
|
548 *
|
Chris@0
|
549 * This happens when a field was modified, saved and hence rerendered.
|
Chris@0
|
550 *
|
Chris@0
|
551 * @param {Drupal.quickedit.FieldModel} fieldModel
|
Chris@0
|
552 * A field that was just added to the collection of fields.
|
Chris@0
|
553 */
|
Chris@0
|
554 rerenderedFieldToCandidate(fieldModel) {
|
Chris@0
|
555 const activeEntity = Drupal.quickedit.collections.entities.findWhere({ isActive: true });
|
Chris@0
|
556
|
Chris@0
|
557 // Early-return if there is no active entity.
|
Chris@0
|
558 if (!activeEntity) {
|
Chris@0
|
559 return;
|
Chris@0
|
560 }
|
Chris@0
|
561
|
Chris@0
|
562 // If the field's entity is the active entity, make it a candidate.
|
Chris@0
|
563 if (fieldModel.get('entity') === activeEntity) {
|
Chris@0
|
564 this.setupEditor(fieldModel);
|
Chris@0
|
565 fieldModel.set('state', 'candidate');
|
Chris@0
|
566 }
|
Chris@0
|
567 },
|
Chris@0
|
568
|
Chris@0
|
569 /**
|
Chris@0
|
570 * EntityModel Collection change handler.
|
Chris@0
|
571 *
|
Chris@0
|
572 * Handler is called `change:isActive` and enforces a single active entity.
|
Chris@0
|
573 *
|
Chris@0
|
574 * @param {Drupal.quickedit.EntityModel} changedEntityModel
|
Chris@0
|
575 * The entityModel instance whose active state has changed.
|
Chris@0
|
576 */
|
Chris@0
|
577 enforceSingleActiveEntity(changedEntityModel) {
|
Chris@0
|
578 // When an entity is deactivated, we don't need to enforce anything.
|
Chris@0
|
579 if (changedEntityModel.get('isActive') === false) {
|
Chris@0
|
580 return;
|
Chris@0
|
581 }
|
Chris@0
|
582
|
Chris@0
|
583 // This entity was activated; deactivate all other entities.
|
Chris@0
|
584 changedEntityModel.collection.chain()
|
Chris@0
|
585 .filter(entityModel => entityModel.get('isActive') === true && entityModel !== changedEntityModel)
|
Chris@0
|
586 .each((entityModel) => {
|
Chris@0
|
587 entityModel.set('state', 'deactivating');
|
Chris@0
|
588 });
|
Chris@0
|
589 },
|
Chris@0
|
590
|
Chris@0
|
591 });
|
Chris@0
|
592 }(jQuery, _, Backbone, Drupal));
|