annotate core/modules/quickedit/js/views/AppView.es6.js @ 4:a9cd425dd02b

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