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