Chris@0: /** Chris@0: * @file Chris@0: * A Backbone View that controls the overall "in-place editing application". Chris@0: * Chris@0: * @see Drupal.quickedit.AppModel Chris@0: */ Chris@0: Chris@0: (function ($, _, Backbone, Drupal) { Chris@0: // Indicates whether the page should be reloaded after in-place editing has Chris@0: // shut down. A page reload is necessary to re-instate the original HTML of Chris@0: // the edited fields if in-place editing has been canceled and one or more of Chris@0: // the entity's fields were saved to PrivateTempStore: one of them may have Chris@0: // been changed to the empty value and hence may have been rerendered as the Chris@0: // empty string, which makes it impossible for Quick Edit to know where to Chris@0: // restore the original HTML. Chris@0: let reload = false; Chris@0: Chris@0: Drupal.quickedit.AppView = Backbone.View.extend(/** @lends Drupal.quickedit.AppView# */{ Chris@0: Chris@0: /** Chris@0: * @constructs Chris@0: * Chris@0: * @augments Backbone.View Chris@0: * Chris@0: * @param {object} options Chris@0: * An object with the following keys: Chris@0: * @param {Drupal.quickedit.AppModel} options.model Chris@0: * The application state model. Chris@0: * @param {Drupal.quickedit.EntityCollection} options.entitiesCollection Chris@0: * All on-page entities. Chris@0: * @param {Drupal.quickedit.FieldCollection} options.fieldsCollection Chris@0: * All on-page fields Chris@0: */ Chris@0: initialize(options) { Chris@0: // AppView's configuration for handling states. Chris@0: // @see Drupal.quickedit.FieldModel.states Chris@0: this.activeFieldStates = ['activating', 'active']; Chris@0: this.singleFieldStates = ['highlighted', 'activating', 'active']; Chris@0: this.changedFieldStates = ['changed', 'saving', 'saved', 'invalid']; Chris@0: this.readyFieldStates = ['candidate', 'highlighted']; Chris@0: Chris@0: // Track app state. Chris@0: this.listenTo(options.entitiesCollection, 'change:state', this.appStateChange); Chris@0: this.listenTo(options.entitiesCollection, 'change:isActive', this.enforceSingleActiveEntity); Chris@0: Chris@0: // Track app state. Chris@0: this.listenTo(options.fieldsCollection, 'change:state', this.editorStateChange); Chris@0: // Respond to field model HTML representation change events. Chris@0: this.listenTo(options.fieldsCollection, 'change:html', this.renderUpdatedField); Chris@0: this.listenTo(options.fieldsCollection, 'change:html', this.propagateUpdatedField); Chris@0: // Respond to addition. Chris@0: this.listenTo(options.fieldsCollection, 'add', this.rerenderedFieldToCandidate); Chris@0: // Respond to destruction. Chris@0: this.listenTo(options.fieldsCollection, 'destroy', this.teardownEditor); Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Handles setup/teardown and state changes when the active entity changes. Chris@0: * Chris@0: * @param {Drupal.quickedit.EntityModel} entityModel Chris@0: * An instance of the EntityModel class. Chris@0: * @param {string} state Chris@0: * The state of the associated field. One of Chris@0: * {@link Drupal.quickedit.EntityModel.states}. Chris@0: */ Chris@0: appStateChange(entityModel, state) { Chris@0: const app = this; Chris@0: let entityToolbarView; Chris@0: switch (state) { Chris@0: case 'launching': Chris@0: reload = false; Chris@0: // First, create an entity toolbar view. Chris@0: entityToolbarView = new Drupal.quickedit.EntityToolbarView({ Chris@0: model: entityModel, Chris@0: appModel: this.model, Chris@0: }); Chris@0: entityModel.toolbarView = entityToolbarView; Chris@0: // Second, set up in-place editors. Chris@0: // They must be notified of state changes, hence this must happen Chris@0: // while the associated fields are still in the 'inactive' state. Chris@0: entityModel.get('fields').each((fieldModel) => { Chris@0: app.setupEditor(fieldModel); Chris@0: }); Chris@0: // Third, transition the entity to the 'opening' state, which will Chris@0: // transition all fields from 'inactive' to 'candidate'. Chris@0: _.defer(() => { Chris@0: entityModel.set('state', 'opening'); Chris@0: }); Chris@0: break; Chris@0: Chris@0: case 'closed': Chris@0: entityToolbarView = entityModel.toolbarView; Chris@0: // First, tear down the in-place editors. Chris@0: entityModel.get('fields').each((fieldModel) => { Chris@0: app.teardownEditor(fieldModel); Chris@0: }); Chris@0: // Second, tear down the entity toolbar view. Chris@0: if (entityToolbarView) { Chris@0: entityToolbarView.remove(); Chris@0: delete entityModel.toolbarView; Chris@0: } Chris@0: // A page reload may be necessary to re-instate the original HTML of Chris@0: // the edited fields. Chris@0: if (reload) { Chris@0: reload = false; Chris@0: location.reload(); Chris@0: } Chris@0: break; Chris@0: } Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Accepts or reject editor (Editor) state changes. Chris@0: * Chris@0: * This is what ensures that the app is in control of what happens. Chris@0: * Chris@0: * @param {string} from Chris@0: * The previous state. Chris@0: * @param {string} to Chris@0: * The new state. Chris@0: * @param {null|object} context Chris@0: * The context that is trying to trigger the state change. Chris@0: * @param {Drupal.quickedit.FieldModel} fieldModel Chris@0: * The fieldModel to which this change applies. Chris@0: * Chris@0: * @return {bool} Chris@0: * Whether the editor change was accepted or rejected. Chris@0: */ Chris@0: acceptEditorStateChange(from, to, context, fieldModel) { Chris@0: let accept = true; Chris@0: Chris@0: // If the app is in view mode, then reject all state changes except for Chris@0: // those to 'inactive'. Chris@0: if (context && (context.reason === 'stop' || context.reason === 'rerender')) { Chris@0: if (from === 'candidate' && to === 'inactive') { Chris@0: accept = true; Chris@0: } Chris@0: } Chris@0: // Handling of edit mode state changes is more granular. Chris@0: else { Chris@0: // In general, enforce the states sequence. Disallow going back from a Chris@0: // "later" state to an "earlier" state, except in explicitly allowed Chris@0: // cases. Chris@0: if (!Drupal.quickedit.FieldModel.followsStateSequence(from, to)) { Chris@0: accept = false; Chris@0: // Allow: activating/active -> candidate. Chris@0: // Necessary to stop editing a field. Chris@0: if (_.indexOf(this.activeFieldStates, from) !== -1 && to === 'candidate') { Chris@0: accept = true; Chris@0: } Chris@0: // Allow: changed/invalid -> candidate. Chris@0: // Necessary to stop editing a field when it is changed or invalid. Chris@0: else if ((from === 'changed' || from === 'invalid') && to === 'candidate') { Chris@0: accept = true; Chris@0: } Chris@0: // Allow: highlighted -> candidate. Chris@0: // Necessary to stop highlighting a field. Chris@0: else if (from === 'highlighted' && to === 'candidate') { Chris@0: accept = true; Chris@0: } Chris@0: // Allow: saved -> candidate. Chris@0: // Necessary when successfully saved a field. Chris@0: else if (from === 'saved' && to === 'candidate') { Chris@0: accept = true; Chris@0: } Chris@0: // Allow: invalid -> saving. Chris@0: // Necessary to be able to save a corrected, invalid field. Chris@0: else if (from === 'invalid' && to === 'saving') { Chris@0: accept = true; Chris@0: } Chris@0: // Allow: invalid -> activating. Chris@0: // Necessary to be able to correct a field that turned out to be Chris@0: // invalid after the user already had moved on to the next field Chris@0: // (which we explicitly allow to have a fluent UX). Chris@0: else if (from === 'invalid' && to === 'activating') { Chris@0: accept = true; Chris@0: } Chris@0: } Chris@0: Chris@0: // If it's not against the general principle, then here are more Chris@0: // disallowed cases to check. Chris@0: if (accept) { Chris@0: let activeField; Chris@0: let activeFieldState; Chris@0: // Ensure only one field (editor) at a time is active … but allow a Chris@0: // user to hop from one field to the next, even if we still have to Chris@0: // start saving the field that is currently active: assume it will be Chris@0: // valid, to allow for a fluent UX. (If it turns out to be invalid, Chris@0: // this block of code also handles that.) Chris@0: if ((this.readyFieldStates.indexOf(from) !== -1 || from === 'invalid') && this.activeFieldStates.indexOf(to) !== -1) { Chris@0: activeField = this.model.get('activeField'); Chris@0: if (activeField && activeField !== fieldModel) { Chris@0: activeFieldState = activeField.get('state'); Chris@0: // Allow the state change. If the state of the active field is: Chris@0: // - 'activating' or 'active': change it to 'candidate' Chris@0: // - 'changed' or 'invalid': change it to 'saving' Chris@0: // - 'saving' or 'saved': don't do anything. Chris@0: if (this.activeFieldStates.indexOf(activeFieldState) !== -1) { Chris@0: activeField.set('state', 'candidate'); Chris@0: } Chris@0: else if (activeFieldState === 'changed' || activeFieldState === 'invalid') { Chris@0: activeField.set('state', 'saving'); Chris@0: } Chris@0: Chris@0: // If the field that's being activated is in fact already in the Chris@0: // invalid state (which can only happen because above we allowed Chris@0: // the user to move on to another field to allow for a fluent UX; Chris@0: // we assumed it would be saved successfully), then we shouldn't Chris@0: // allow the field to enter the 'activating' state, instead, we Chris@0: // simply change the active editor. All guarantees and Chris@0: // assumptions for this field still hold! Chris@0: if (from === 'invalid') { Chris@0: this.model.set('activeField', fieldModel); Chris@0: accept = false; Chris@0: } Chris@0: // Do not reject: the field is either in the 'candidate' or Chris@0: // 'highlighted' state and we allow it to enter the 'activating' Chris@0: // state! Chris@0: } Chris@0: } Chris@0: // Reject going from activating/active to candidate because of a Chris@0: // mouseleave. Chris@0: else if (_.indexOf(this.activeFieldStates, from) !== -1 && to === 'candidate') { Chris@0: if (context && context.reason === 'mouseleave') { Chris@0: accept = false; Chris@0: } Chris@0: } Chris@0: // When attempting to stop editing a changed/invalid property, ask for Chris@0: // confirmation. Chris@0: else if ((from === 'changed' || from === 'invalid') && to === 'candidate') { Chris@0: if (context && context.reason === 'mouseleave') { Chris@0: accept = false; Chris@0: } Chris@0: // Check whether the transition has been confirmed? Chris@0: else if (context && context.confirmed) { Chris@0: accept = true; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return accept; Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Sets up the in-place editor for the given field. Chris@0: * Chris@0: * Must happen before the fieldModel's state is changed to 'candidate'. Chris@0: * Chris@0: * @param {Drupal.quickedit.FieldModel} fieldModel Chris@0: * The field for which an in-place editor must be set up. Chris@0: */ Chris@0: setupEditor(fieldModel) { Chris@0: // Get the corresponding entity toolbar. Chris@0: const entityModel = fieldModel.get('entity'); Chris@0: const entityToolbarView = entityModel.toolbarView; Chris@0: // Get the field toolbar DOM root from the entity toolbar. Chris@0: const fieldToolbarRoot = entityToolbarView.getToolbarRoot(); Chris@0: // Create in-place editor. Chris@0: const editorName = fieldModel.get('metadata').editor; Chris@0: const editorModel = new Drupal.quickedit.EditorModel(); Chris@0: const editorView = new Drupal.quickedit.editors[editorName]({ Chris@0: el: $(fieldModel.get('el')), Chris@0: model: editorModel, Chris@0: fieldModel, Chris@0: }); Chris@0: Chris@0: // Create in-place editor's toolbar for this field — stored inside the Chris@0: // entity toolbar, the entity toolbar will position itself appropriately Chris@0: // above (or below) the edited element. Chris@0: const toolbarView = new Drupal.quickedit.FieldToolbarView({ Chris@0: el: fieldToolbarRoot, Chris@0: model: fieldModel, Chris@0: $editedElement: $(editorView.getEditedElement()), Chris@0: editorView, Chris@0: entityModel, Chris@0: }); Chris@0: Chris@0: // Create decoration for edited element: padding if necessary, sets Chris@0: // classes on the element to style it according to the current state. Chris@0: const decorationView = new Drupal.quickedit.FieldDecorationView({ Chris@0: el: $(editorView.getEditedElement()), Chris@0: model: fieldModel, Chris@0: editorView, Chris@0: }); Chris@0: Chris@0: // Track these three views in FieldModel so that we can tear them down Chris@0: // correctly. Chris@0: fieldModel.editorView = editorView; Chris@0: fieldModel.toolbarView = toolbarView; Chris@0: fieldModel.decorationView = decorationView; Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Tears down the in-place editor for the given field. Chris@0: * Chris@0: * Must happen after the fieldModel's state is changed to 'inactive'. Chris@0: * Chris@0: * @param {Drupal.quickedit.FieldModel} fieldModel Chris@0: * The field for which an in-place editor must be torn down. Chris@0: */ Chris@0: teardownEditor(fieldModel) { Chris@0: // Early-return if this field was not yet decorated. Chris@0: if (typeof fieldModel.editorView === 'undefined') { Chris@0: return; Chris@0: } Chris@0: Chris@0: // Unbind event handlers; remove toolbar element; delete toolbar view. Chris@0: fieldModel.toolbarView.remove(); Chris@0: delete fieldModel.toolbarView; Chris@0: Chris@0: // Unbind event handlers; delete decoration view. Don't remove the element Chris@0: // because that would remove the field itself. Chris@0: fieldModel.decorationView.remove(); Chris@0: delete fieldModel.decorationView; Chris@0: Chris@0: // Unbind event handlers; delete editor view. Don't remove the element Chris@0: // because that would remove the field itself. Chris@0: fieldModel.editorView.remove(); Chris@0: delete fieldModel.editorView; Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Asks the user to confirm whether he wants to stop editing via a modal. Chris@0: * Chris@0: * @param {Drupal.quickedit.EntityModel} entityModel Chris@0: * An instance of the EntityModel class. Chris@0: * Chris@0: * @see Drupal.quickedit.AppView#acceptEditorStateChange Chris@0: */ Chris@0: confirmEntityDeactivation(entityModel) { Chris@0: const that = this; Chris@0: let discardDialog; Chris@0: Chris@0: function closeDiscardDialog(action) { Chris@0: discardDialog.close(action); Chris@0: // The active modal has been removed. Chris@0: that.model.set('activeModal', null); Chris@0: Chris@0: // If the targetState is saving, the field must be saved, then the Chris@0: // entity must be saved. Chris@0: if (action === 'save') { Chris@0: entityModel.set('state', 'committing', { confirmed: true }); Chris@0: } Chris@0: else { Chris@0: entityModel.set('state', 'deactivating', { confirmed: true }); Chris@0: // Editing has been canceled and the changes will not be saved. Mark Chris@0: // the page for reload if the entityModel declares that it requires Chris@0: // a reload. Chris@0: if (entityModel.get('reload')) { Chris@0: reload = true; Chris@0: entityModel.set('reload', false); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // Only instantiate if there isn't a modal instance visible yet. Chris@0: if (!this.model.get('activeModal')) { Chris@0: const $unsavedChanges = $(`
${Drupal.t('You have unsaved changes')}
`); Chris@0: discardDialog = Drupal.dialog($unsavedChanges.get(0), { Chris@0: title: Drupal.t('Discard changes?'), Chris@0: dialogClass: 'quickedit-discard-modal', Chris@0: resizable: false, Chris@0: buttons: [ Chris@0: { Chris@0: text: Drupal.t('Save'), Chris@0: click() { Chris@0: closeDiscardDialog('save'); Chris@0: }, Chris@0: primary: true, Chris@0: }, Chris@0: { Chris@0: text: Drupal.t('Discard changes'), Chris@0: click() { Chris@0: closeDiscardDialog('discard'); Chris@0: }, Chris@0: }, Chris@0: ], Chris@0: // Prevent this modal from being closed without the user making a Chris@0: // choice as per http://stackoverflow.com/a/5438771. Chris@0: closeOnEscape: false, Chris@0: create() { Chris@0: $(this).parent().find('.ui-dialog-titlebar-close').remove(); Chris@0: }, Chris@0: beforeClose: false, Chris@0: close(event) { Chris@0: // Automatically destroy the DOM element that was used for the Chris@0: // dialog. Chris@0: $(event.target).remove(); Chris@0: }, Chris@0: }); Chris@0: this.model.set('activeModal', discardDialog); Chris@0: Chris@0: discardDialog.showModal(); Chris@0: } Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Reacts to field state changes; tracks global state. Chris@0: * Chris@0: * @param {Drupal.quickedit.FieldModel} fieldModel Chris@0: * The `fieldModel` holding the state. Chris@0: * @param {string} state Chris@0: * The state of the associated field. One of Chris@0: * {@link Drupal.quickedit.FieldModel.states}. Chris@0: */ Chris@0: editorStateChange(fieldModel, state) { Chris@0: const from = fieldModel.previous('state'); Chris@0: const to = state; Chris@0: Chris@0: // Keep track of the highlighted field in the global state. Chris@0: if (_.indexOf(this.singleFieldStates, to) !== -1 && this.model.get('highlightedField') !== fieldModel) { Chris@0: this.model.set('highlightedField', fieldModel); Chris@0: } Chris@0: else if (this.model.get('highlightedField') === fieldModel && to === 'candidate') { Chris@0: this.model.set('highlightedField', null); Chris@0: } Chris@0: Chris@0: // Keep track of the active field in the global state. Chris@0: if (_.indexOf(this.activeFieldStates, to) !== -1 && this.model.get('activeField') !== fieldModel) { Chris@0: this.model.set('activeField', fieldModel); Chris@0: } Chris@0: else if (this.model.get('activeField') === fieldModel && to === 'candidate') { Chris@0: // Discarded if it transitions from a changed state to 'candidate'. Chris@0: if (from === 'changed' || from === 'invalid') { Chris@0: fieldModel.editorView.revert(); Chris@0: } Chris@0: this.model.set('activeField', null); Chris@0: } Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Render an updated field (a field whose 'html' attribute changed). Chris@0: * Chris@0: * @param {Drupal.quickedit.FieldModel} fieldModel Chris@0: * The FieldModel whose 'html' attribute changed. Chris@0: * @param {string} html Chris@0: * The updated 'html' attribute. Chris@0: * @param {object} options Chris@0: * An object with the following keys: Chris@0: * @param {bool} options.propagation Chris@0: * Whether this change to the 'html' attribute occurred because of the Chris@0: * propagation of changes to another instance of this field. Chris@0: */ Chris@0: renderUpdatedField(fieldModel, html, options) { Chris@0: // Get data necessary to rerender property before it is unavailable. Chris@0: const $fieldWrapper = $(fieldModel.get('el')); Chris@0: const $context = $fieldWrapper.parent(); Chris@0: Chris@0: const renderField = function () { Chris@0: // Destroy the field model; this will cause all attached views to be Chris@0: // destroyed too, and removal from all collections in which it exists. Chris@0: fieldModel.destroy(); Chris@0: Chris@0: // Replace the old content with the new content. Chris@0: $fieldWrapper.replaceWith(html); Chris@0: Chris@0: // Attach behaviors again to the modified piece of HTML; this will Chris@0: // create a new field model and call rerenderedFieldToCandidate() with Chris@0: // it. Chris@0: Drupal.attachBehaviors($context.get(0)); Chris@0: }; Chris@0: Chris@0: // When propagating the changes of another instance of this field, this Chris@0: // field is not being actively edited and hence no state changes are Chris@0: // necessary. So: only update the state of this field when the rerendering Chris@0: // of this field happens not because of propagation, but because it is Chris@0: // being edited itself. Chris@0: if (!options.propagation) { Chris@0: // Deferred because renderUpdatedField is reacting to a field model Chris@0: // change event, and we want to make sure that event fully propagates Chris@0: // before making another change to the same model. Chris@0: _.defer(() => { Chris@0: // First set the state to 'candidate', to allow all attached views to Chris@0: // clean up all their "active state"-related changes. Chris@0: fieldModel.set('state', 'candidate'); Chris@0: Chris@0: // Similarly, the above .set() call's change event must fully Chris@0: // propagate before calling it again. Chris@0: _.defer(() => { Chris@0: // Set the field's state to 'inactive', to enable the updating of Chris@0: // its DOM value. Chris@0: fieldModel.set('state', 'inactive', { reason: 'rerender' }); Chris@0: Chris@0: renderField(); Chris@0: }); Chris@0: }); Chris@0: } Chris@0: else { Chris@0: renderField(); Chris@0: } Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Propagates changes to an updated field to all instances of that field. Chris@0: * Chris@0: * @param {Drupal.quickedit.FieldModel} updatedField Chris@0: * The FieldModel whose 'html' attribute changed. Chris@0: * @param {string} html Chris@0: * The updated 'html' attribute. Chris@0: * @param {object} options Chris@0: * An object with the following keys: Chris@0: * @param {bool} options.propagation Chris@0: * Whether this change to the 'html' attribute occurred because of the Chris@0: * propagation of changes to another instance of this field. Chris@0: * Chris@0: * @see Drupal.quickedit.AppView#renderUpdatedField Chris@0: */ Chris@0: propagateUpdatedField(updatedField, html, options) { Chris@0: // Don't propagate field updates that themselves were caused by Chris@0: // propagation. Chris@0: if (options.propagation) { Chris@0: return; Chris@0: } Chris@0: Chris@0: const htmlForOtherViewModes = updatedField.get('htmlForOtherViewModes'); Chris@0: Drupal.quickedit.collections.fields Chris@0: // Find all instances of fields that display the same logical field Chris@0: // (same entity, same field, just a different instance and maybe a Chris@0: // different view mode). Chris@0: .where({ logicalFieldID: updatedField.get('logicalFieldID') }) Chris@0: .forEach((field) => { Chris@0: // Ignore the field that was already updated. Chris@0: if (field === updatedField) { Chris@0: Chris@0: } Chris@0: // If this other instance of the field has the same view mode, we can Chris@0: // update it easily. Chris@0: else if (field.getViewMode() === updatedField.getViewMode()) { Chris@0: field.set('html', updatedField.get('html')); Chris@0: } Chris@0: // If this other instance of the field has a different view mode, and Chris@0: // that is one of the view modes for which a re-rendered version is Chris@0: // available (and that should be the case unless this field was only Chris@0: // added to the page after editing of the updated field began), then Chris@0: // use that view mode's re-rendered version. Chris@0: else if (field.getViewMode() in htmlForOtherViewModes) { Chris@0: field.set('html', htmlForOtherViewModes[field.getViewMode()], { propagation: true }); Chris@0: } Chris@0: }); Chris@0: }, Chris@0: Chris@0: /** Chris@0: * If the new in-place editable field is for the entity that's currently Chris@0: * being edited, then transition it to the 'candidate' state. Chris@0: * Chris@0: * This happens when a field was modified, saved and hence rerendered. Chris@0: * Chris@0: * @param {Drupal.quickedit.FieldModel} fieldModel Chris@0: * A field that was just added to the collection of fields. Chris@0: */ Chris@0: rerenderedFieldToCandidate(fieldModel) { Chris@0: const activeEntity = Drupal.quickedit.collections.entities.findWhere({ isActive: true }); Chris@0: Chris@0: // Early-return if there is no active entity. Chris@0: if (!activeEntity) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // If the field's entity is the active entity, make it a candidate. Chris@0: if (fieldModel.get('entity') === activeEntity) { Chris@0: this.setupEditor(fieldModel); Chris@0: fieldModel.set('state', 'candidate'); Chris@0: } Chris@0: }, Chris@0: Chris@0: /** Chris@0: * EntityModel Collection change handler. Chris@0: * Chris@0: * Handler is called `change:isActive` and enforces a single active entity. Chris@0: * Chris@0: * @param {Drupal.quickedit.EntityModel} changedEntityModel Chris@0: * The entityModel instance whose active state has changed. Chris@0: */ Chris@0: enforceSingleActiveEntity(changedEntityModel) { Chris@0: // When an entity is deactivated, we don't need to enforce anything. Chris@0: if (changedEntityModel.get('isActive') === false) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // This entity was activated; deactivate all other entities. Chris@0: changedEntityModel.collection.chain() Chris@0: .filter(entityModel => entityModel.get('isActive') === true && entityModel !== changedEntityModel) Chris@0: .each((entityModel) => { Chris@0: entityModel.set('state', 'deactivating'); Chris@0: }); Chris@0: }, Chris@0: Chris@0: }); Chris@0: }(jQuery, _, Backbone, Drupal));