Chris@0: /** Chris@0: * @file Chris@0: * A Backbone Model for the state of an in-place editable entity in the DOM. Chris@0: */ Chris@0: Chris@0: (function (_, $, Backbone, Drupal) { Chris@0: Drupal.quickedit.EntityModel = Drupal.quickedit.BaseModel.extend(/** @lends Drupal.quickedit.EntityModel# */{ Chris@0: Chris@0: /** Chris@0: * @type {object} Chris@0: */ Chris@0: defaults: /** @lends Drupal.quickedit.EntityModel# */{ Chris@0: Chris@0: /** Chris@0: * The DOM element that represents this entity. Chris@0: * Chris@0: * It may seem bizarre to have a DOM element in a Backbone Model, but we Chris@0: * need to be able to map entities in the DOM to EntityModels in memory. Chris@0: * Chris@0: * @type {HTMLElement} Chris@0: */ Chris@0: el: null, Chris@0: Chris@0: /** Chris@0: * An entity ID, of the form `/` Chris@0: * Chris@0: * @example Chris@0: * "node/1" Chris@0: * Chris@0: * @type {string} Chris@0: */ Chris@0: entityID: null, Chris@0: Chris@0: /** Chris@0: * An entity instance ID. Chris@0: * Chris@0: * The first instance of a specific entity (i.e. with a given entity ID) Chris@0: * is assigned 0, the second 1, and so on. Chris@0: * Chris@0: * @type {number} Chris@0: */ Chris@0: entityInstanceID: null, Chris@0: Chris@0: /** Chris@0: * The unique ID of this entity instance on the page, of the form Chris@0: * `/[entity instance ID]` Chris@0: * Chris@0: * @example Chris@0: * "node/1[0]" Chris@0: * Chris@0: * @type {string} Chris@0: */ Chris@0: id: null, Chris@0: Chris@0: /** Chris@0: * The label of the entity. Chris@0: * Chris@0: * @type {string} Chris@0: */ Chris@0: label: null, Chris@0: Chris@0: /** Chris@0: * A FieldCollection for all fields of the entity. Chris@0: * Chris@0: * @type {Drupal.quickedit.FieldCollection} Chris@0: * Chris@0: * @see Drupal.quickedit.FieldCollection Chris@0: */ Chris@0: fields: null, Chris@0: Chris@0: // The attributes below are stateful. The ones above will never change Chris@0: // during the life of a EntityModel instance. Chris@0: Chris@0: /** Chris@0: * Indicates whether this entity is currently being edited in-place. Chris@0: * Chris@0: * @type {bool} Chris@0: */ Chris@0: isActive: false, Chris@0: Chris@0: /** Chris@0: * Whether one or more fields are already been stored in PrivateTempStore. Chris@0: * Chris@0: * @type {bool} Chris@0: */ Chris@0: inTempStore: false, Chris@0: Chris@0: /** Chris@0: * Indicates whether a "Save" button is necessary or not. Chris@0: * Chris@0: * Whether one or more fields have already been stored in PrivateTempStore Chris@0: * *or* the field that's currently being edited is in the 'changed' or a Chris@0: * later state. Chris@0: * Chris@0: * @type {bool} Chris@0: */ Chris@0: isDirty: false, Chris@0: Chris@0: /** Chris@0: * Whether the request to the server has been made to commit this entity. Chris@0: * Chris@0: * Used to prevent multiple such requests. Chris@0: * Chris@0: * @type {bool} Chris@0: */ Chris@0: isCommitting: false, Chris@0: Chris@0: /** Chris@0: * The current processing state of an entity. Chris@0: * Chris@0: * @type {string} Chris@0: */ Chris@0: state: 'closed', Chris@0: Chris@0: /** Chris@0: * IDs of fields whose new values have been stored in PrivateTempStore. Chris@0: * Chris@0: * We must store this on the EntityModel as well (even though it already Chris@0: * is on the FieldModel) because when a field is rerendered, its Chris@0: * FieldModel is destroyed and this allows us to transition it back to Chris@0: * the proper state. Chris@0: * Chris@0: * @type {Array.} Chris@0: */ Chris@0: fieldsInTempStore: [], Chris@0: Chris@0: /** Chris@0: * A flag the tells the application that this EntityModel must be reloaded Chris@0: * in order to restore the original values to its fields in the client. Chris@0: * Chris@0: * @type {bool} Chris@0: */ Chris@0: reload: false, Chris@0: }, Chris@0: Chris@0: /** Chris@0: * @constructs Chris@0: * Chris@0: * @augments Drupal.quickedit.BaseModel Chris@0: */ Chris@0: initialize() { Chris@0: this.set('fields', new Drupal.quickedit.FieldCollection()); Chris@0: Chris@0: // Respond to entity state changes. Chris@0: this.listenTo(this, 'change:state', this.stateChange); Chris@0: Chris@0: // The state of the entity is largely dependent on the state of its Chris@0: // fields. Chris@0: this.listenTo(this.get('fields'), 'change:state', this.fieldStateChange); Chris@0: Chris@0: // Call Drupal.quickedit.BaseModel's initialize() method. Chris@0: Drupal.quickedit.BaseModel.prototype.initialize.call(this); Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Updates FieldModels' states when an EntityModel change occurs. Chris@0: * Chris@0: * @param {Drupal.quickedit.EntityModel} entityModel Chris@0: * The entity model Chris@0: * @param {string} state Chris@0: * The state of the associated entity. One of Chris@0: * {@link Drupal.quickedit.EntityModel.states}. Chris@0: * @param {object} options Chris@0: * Options for the entity model. Chris@0: */ Chris@0: stateChange(entityModel, state, options) { Chris@0: const to = state; Chris@0: switch (to) { Chris@0: case 'closed': Chris@0: this.set({ Chris@0: isActive: false, Chris@0: inTempStore: false, Chris@0: isDirty: false, Chris@0: }); Chris@0: break; Chris@0: Chris@0: case 'launching': Chris@0: break; Chris@0: Chris@0: case 'opening': Chris@0: // Set the fields to candidate state. Chris@0: entityModel.get('fields').each((fieldModel) => { Chris@0: fieldModel.set('state', 'candidate', options); Chris@0: }); Chris@0: break; Chris@0: Chris@0: case 'opened': Chris@0: // The entity is now ready for editing! Chris@0: this.set('isActive', true); Chris@0: break; Chris@0: Chris@0: case 'committing': Chris@0: // The user indicated they want to save the entity. Chris@0: var fields = this.get('fields'); Chris@0: // For fields that are in an active state, transition them to Chris@0: // candidate. Chris@0: fields.chain() Chris@0: .filter(fieldModel => _.intersection([fieldModel.get('state')], ['active']).length) Chris@0: .each((fieldModel) => { Chris@0: fieldModel.set('state', 'candidate'); Chris@0: }); Chris@0: // For fields that are in a changed state, field values must first be Chris@0: // stored in PrivateTempStore. Chris@0: fields.chain() Chris@0: .filter(fieldModel => _.intersection([fieldModel.get('state')], Drupal.quickedit.app.changedFieldStates).length) Chris@0: .each((fieldModel) => { Chris@0: fieldModel.set('state', 'saving'); Chris@0: }); Chris@0: break; Chris@0: Chris@0: case 'deactivating': Chris@0: var changedFields = this.get('fields') Chris@0: .filter(fieldModel => _.intersection([fieldModel.get('state')], ['changed', 'invalid']).length); Chris@0: // If the entity contains unconfirmed or unsaved changes, return the Chris@0: // entity to an opened state and ask the user if they would like to Chris@0: // save the changes or discard the changes. Chris@0: // 1. One of the fields is in a changed state. The changed field Chris@0: // might just be a change in the client or it might have been saved Chris@0: // to tempstore. Chris@0: // 2. The saved flag is empty and the confirmed flag is empty. If Chris@0: // the entity has been saved to the server, the fields changed in Chris@0: // the client are irrelevant. If the changes are confirmed, then Chris@0: // proceed to set the fields to candidate state. Chris@0: if ((changedFields.length || this.get('fieldsInTempStore').length) && (!options.saved && !options.confirmed)) { Chris@0: // Cancel deactivation until the user confirms save or discard. Chris@0: this.set('state', 'opened', { confirming: true }); Chris@0: // An action in reaction to state change must be deferred. Chris@0: _.defer(() => { Chris@0: Drupal.quickedit.app.confirmEntityDeactivation(entityModel); Chris@0: }); Chris@0: } Chris@0: else { Chris@0: const invalidFields = this.get('fields') Chris@0: .filter(fieldModel => _.intersection([fieldModel.get('state')], ['invalid']).length); Chris@0: // Indicate if this EntityModel needs to be reloaded in order to Chris@0: // restore the original values of its fields. Chris@0: entityModel.set('reload', (this.get('fieldsInTempStore').length || invalidFields.length)); Chris@0: // Set all fields to the 'candidate' state. A changed field may have Chris@0: // to go through confirmation first. Chris@0: entityModel.get('fields').each((fieldModel) => { Chris@0: // If the field is already in the candidate state, trigger a Chris@0: // change event so that the entityModel can move to the next state Chris@0: // in deactivation. Chris@0: if (_.intersection([fieldModel.get('state')], ['candidate', 'highlighted']).length) { Chris@0: fieldModel.trigger('change:state', fieldModel, fieldModel.get('state'), options); Chris@0: } Chris@0: else { Chris@0: fieldModel.set('state', 'candidate', options); Chris@0: } Chris@0: }); Chris@0: } Chris@0: break; Chris@0: Chris@0: case 'closing': Chris@0: // Set all fields to the 'inactive' state. Chris@0: options.reason = 'stop'; Chris@0: this.get('fields').each((fieldModel) => { Chris@0: fieldModel.set({ Chris@0: inTempStore: false, Chris@0: state: 'inactive', Chris@0: }, options); Chris@0: }); Chris@0: break; Chris@0: } Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Updates a Field and Entity model's "inTempStore" when appropriate. Chris@0: * Chris@0: * Helper function. Chris@0: * Chris@0: * @param {Drupal.quickedit.EntityModel} entityModel Chris@0: * The model of the entity for which a field's state attribute has Chris@0: * changed. Chris@0: * @param {Drupal.quickedit.FieldModel} fieldModel Chris@0: * The model of the field whose state attribute has changed. Chris@0: * Chris@0: * @see Drupal.quickedit.EntityModel#fieldStateChange Chris@0: */ Chris@0: _updateInTempStoreAttributes(entityModel, fieldModel) { Chris@0: const current = fieldModel.get('state'); Chris@0: const previous = fieldModel.previous('state'); Chris@0: let fieldsInTempStore = entityModel.get('fieldsInTempStore'); Chris@0: // If the fieldModel changed to the 'saved' state: remember that this Chris@0: // field was saved to PrivateTempStore. Chris@0: if (current === 'saved') { Chris@0: // Mark the entity as saved in PrivateTempStore, so that we can pass the Chris@0: // proper "reset PrivateTempStore" boolean value when communicating with Chris@0: // the server. Chris@0: entityModel.set('inTempStore', true); Chris@0: // Mark the field as saved in PrivateTempStore, so that visual Chris@0: // indicators signifying just that may be rendered. Chris@0: fieldModel.set('inTempStore', true); Chris@0: // Remember that this field is in PrivateTempStore, restore when Chris@0: // rerendered. Chris@0: fieldsInTempStore.push(fieldModel.get('fieldID')); Chris@0: fieldsInTempStore = _.uniq(fieldsInTempStore); Chris@0: entityModel.set('fieldsInTempStore', fieldsInTempStore); Chris@0: } Chris@0: // If the fieldModel changed to the 'candidate' state from the Chris@0: // 'inactive' state, then this is a field for this entity that got Chris@0: // rerendered. Restore its previous 'inTempStore' attribute value. Chris@0: else if (current === 'candidate' && previous === 'inactive') { Chris@0: fieldModel.set('inTempStore', _.intersection([fieldModel.get('fieldID')], fieldsInTempStore).length > 0); Chris@0: } Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Reacts to state changes in this entity's fields. Chris@0: * Chris@0: * @param {Drupal.quickedit.FieldModel} fieldModel Chris@0: * The model of the field whose state attribute changed. 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: fieldStateChange(fieldModel, state) { Chris@0: const entityModel = this; Chris@0: const fieldState = state; Chris@0: // Switch on the entityModel state. Chris@0: // The EntityModel responds to FieldModel state changes as a function of Chris@0: // its state. For example, a field switching back to 'candidate' state Chris@0: // when its entity is in the 'opened' state has no effect on the entity. Chris@0: // But that same switch back to 'candidate' state of a field when the Chris@0: // entity is in the 'committing' state might allow the entity to proceed Chris@0: // with the commit flow. Chris@0: switch (this.get('state')) { Chris@0: case 'closed': Chris@0: case 'launching': Chris@0: // It should be impossible to reach these: fields can't change state Chris@0: // while the entity is closed or still launching. Chris@0: break; Chris@0: Chris@0: case 'opening': Chris@0: // We must change the entity to the 'opened' state, but it must first Chris@0: // be confirmed that all of its fieldModels have transitioned to the Chris@0: // 'candidate' state. Chris@0: // We do this here, because this is called every time a fieldModel Chris@0: // changes state, hence each time this is called, we get closer to the Chris@0: // goal of having all fieldModels in the 'candidate' state. Chris@0: // A state change in reaction to another state change must be Chris@0: // deferred. Chris@0: _.defer(() => { Chris@0: entityModel.set('state', 'opened', { Chris@0: 'accept-field-states': Drupal.quickedit.app.readyFieldStates, Chris@0: }); Chris@0: }); Chris@0: break; Chris@0: Chris@0: case 'opened': Chris@0: // Set the isDirty attribute when appropriate so that it is known when Chris@0: // to display the "Save" button in the entity toolbar. Chris@0: // Note that once a field has been changed, there's no way to discard Chris@0: // that change, hence it will have to be saved into PrivateTempStore, Chris@0: // or the in-place editing of this field will have to be stopped Chris@0: // completely. In other words: once any field enters the 'changed' Chris@0: // field, then for the remainder of the in-place editing session, the Chris@0: // entity is by definition dirty. Chris@0: if (fieldState === 'changed') { Chris@0: entityModel.set('isDirty', true); Chris@0: } Chris@0: else { Chris@0: this._updateInTempStoreAttributes(entityModel, fieldModel); Chris@0: } Chris@0: break; Chris@0: Chris@0: case 'committing': Chris@0: // If the field save returned a validation error, set the state of the Chris@0: // entity back to 'opened'. Chris@0: if (fieldState === 'invalid') { Chris@0: // A state change in reaction to another state change must be Chris@0: // deferred. Chris@0: _.defer(() => { Chris@0: entityModel.set('state', 'opened', { reason: 'invalid' }); Chris@0: }); Chris@0: } Chris@0: else { Chris@0: this._updateInTempStoreAttributes(entityModel, fieldModel); Chris@0: } Chris@0: Chris@0: // Attempt to save the entity. If the entity's fields are not yet all Chris@0: // in a ready state, the save will not be processed. Chris@0: var options = { Chris@0: 'accept-field-states': Drupal.quickedit.app.readyFieldStates, Chris@0: }; Chris@0: if (entityModel.set('isCommitting', true, options)) { Chris@0: entityModel.save({ Chris@0: success() { Chris@0: entityModel.set({ Chris@0: state: 'deactivating', Chris@0: isCommitting: false, Chris@0: }, { saved: true }); Chris@0: }, Chris@0: error() { Chris@0: // Reset the "isCommitting" mutex. Chris@0: entityModel.set('isCommitting', false); Chris@0: // Change the state back to "opened", to allow the user to hit Chris@0: // the "Save" button again. Chris@0: entityModel.set('state', 'opened', { reason: 'networkerror' }); Chris@0: // Show a modal to inform the user of the network error. Chris@0: const message = Drupal.t('Your changes to @entity-title could not be saved, either due to a website problem or a network connection problem.
Please try again.', { '@entity-title': entityModel.get('label') }); Chris@0: Drupal.quickedit.util.networkErrorModal(Drupal.t('Network problem!'), message); Chris@0: }, Chris@0: }); Chris@0: } Chris@0: break; Chris@0: Chris@0: case 'deactivating': Chris@0: // When setting the entity to 'closing', require that all fieldModels Chris@0: // are in either the 'candidate' or 'highlighted' state. Chris@0: // A state change in reaction to another state change must be Chris@0: // deferred. Chris@0: _.defer(() => { Chris@0: entityModel.set('state', 'closing', { Chris@0: 'accept-field-states': Drupal.quickedit.app.readyFieldStates, Chris@0: }); Chris@0: }); Chris@0: break; Chris@0: Chris@0: case 'closing': Chris@0: // When setting the entity to 'closed', require that all fieldModels Chris@0: // are in the 'inactive' state. Chris@0: // A state change in reaction to another state change must be Chris@0: // deferred. Chris@0: _.defer(() => { Chris@0: entityModel.set('state', 'closed', { Chris@0: 'accept-field-states': ['inactive'], Chris@0: }); Chris@0: }); Chris@0: break; Chris@0: } Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Fires an AJAX request to the REST save URL for an entity. Chris@0: * Chris@0: * @param {object} options Chris@0: * An object of options that contains: Chris@0: * @param {function} [options.success] Chris@0: * A function to invoke if the entity is successfully saved. Chris@0: */ Chris@0: save(options) { Chris@0: const entityModel = this; Chris@0: Chris@0: // Create a Drupal.ajax instance to save the entity. Chris@0: const entitySaverAjax = Drupal.ajax({ Chris@0: url: Drupal.url(`quickedit/entity/${entityModel.get('entityID')}`), Chris@0: error() { Chris@0: // Let the Drupal.quickedit.EntityModel Backbone model's error() Chris@0: // method handle errors. Chris@0: options.error.call(entityModel); Chris@0: }, Chris@0: }); Chris@0: // Entity saved successfully. Chris@0: entitySaverAjax.commands.quickeditEntitySaved = function (ajax, response, status) { Chris@0: // All fields have been moved from PrivateTempStore to permanent Chris@0: // storage, update the "inTempStore" attribute on FieldModels, on the Chris@0: // EntityModel and clear EntityModel's "fieldInTempStore" attribute. Chris@0: entityModel.get('fields').each((fieldModel) => { Chris@0: fieldModel.set('inTempStore', false); Chris@0: }); Chris@0: entityModel.set('inTempStore', false); Chris@0: entityModel.set('fieldsInTempStore', []); Chris@0: Chris@0: // Invoke the optional success callback. Chris@0: if (options.success) { Chris@0: options.success.call(entityModel); Chris@0: } Chris@0: }; Chris@0: // Trigger the AJAX request, which will will return the Chris@0: // quickeditEntitySaved AJAX command to which we then react. Chris@0: entitySaverAjax.execute(); Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Validate the entity model. Chris@0: * Chris@0: * @param {object} attrs Chris@0: * The attributes changes in the save or set call. Chris@0: * @param {object} options Chris@0: * An object with the following option: Chris@0: * @param {string} [options.reason] Chris@0: * A string that conveys a particular reason to allow for an exceptional Chris@0: * state change. Chris@0: * @param {Array} options.accept-field-states Chris@0: * An array of strings that represent field states that the entities must Chris@0: * be in to validate. For example, if `accept-field-states` is Chris@0: * `['candidate', 'highlighted']`, then all the fields of the entity must Chris@0: * be in either of these two states for the save or set call to Chris@0: * validate and proceed. Chris@0: * Chris@0: * @return {string} Chris@0: * A string to say something about the state of the entity model. Chris@0: */ Chris@0: validate(attrs, options) { Chris@0: const acceptedFieldStates = options['accept-field-states'] || []; Chris@0: Chris@0: // Validate state change. Chris@0: const currentState = this.get('state'); Chris@0: const nextState = attrs.state; Chris@0: if (currentState !== nextState) { Chris@0: // Ensure it's a valid state. Chris@0: if (_.indexOf(this.constructor.states, nextState) === -1) { Chris@0: return `"${nextState}" is an invalid state`; Chris@0: } Chris@0: Chris@0: // Ensure it's a state change that is allowed. Chris@0: // Check if the acceptStateChange function accepts it. Chris@0: if (!this._acceptStateChange(currentState, nextState, options)) { Chris@0: return 'state change not accepted'; Chris@0: } Chris@0: // If that function accepts it, then ensure all fields are also in an Chris@0: // acceptable state. Chris@0: else if (!this._fieldsHaveAcceptableStates(acceptedFieldStates)) { Chris@0: return 'state change not accepted because fields are not in acceptable state'; Chris@0: } Chris@0: } Chris@0: Chris@0: // Validate setting isCommitting = true. Chris@0: const currentIsCommitting = this.get('isCommitting'); Chris@0: const nextIsCommitting = attrs.isCommitting; Chris@0: if (currentIsCommitting === false && nextIsCommitting === true) { Chris@0: if (!this._fieldsHaveAcceptableStates(acceptedFieldStates)) { Chris@0: return 'isCommitting change not accepted because fields are not in acceptable state'; Chris@0: } Chris@0: } Chris@0: else if (currentIsCommitting === true && nextIsCommitting === true) { Chris@0: return 'isCommitting is a mutex, hence only changes are allowed'; Chris@0: } Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Checks if a state change can be accepted. Chris@0: * Chris@0: * @param {string} from Chris@0: * From state. Chris@0: * @param {string} to Chris@0: * To state. Chris@0: * @param {object} context Chris@0: * Context for the check. Chris@0: * @param {string} context.reason Chris@0: * The reason for the state change. Chris@0: * @param {bool} context.confirming Chris@0: * Whether context is confirming or not. Chris@0: * Chris@0: * @return {bool} Chris@0: * Whether the state change is accepted or not. Chris@0: * Chris@0: * @see Drupal.quickedit.AppView#acceptEditorStateChange Chris@0: */ Chris@0: _acceptStateChange(from, to, context) { Chris@0: let accept = true; Chris@0: 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 (!this.constructor.followsStateSequence(from, to)) { Chris@0: accept = false; Chris@0: Chris@0: // Allow: closing -> closed. Chris@0: // Necessary to stop editing an entity. Chris@0: if (from === 'closing' && to === 'closed') { Chris@0: accept = true; Chris@0: } Chris@0: // Allow: committing -> opened. Chris@0: // Necessary to be able to correct an invalid field, or to hit the Chris@0: // "Save" button again after a server/network error. Chris@0: else if (from === 'committing' && to === 'opened' && context.reason && (context.reason === 'invalid' || context.reason === 'networkerror')) { Chris@0: accept = true; Chris@0: } Chris@0: // Allow: deactivating -> opened. Chris@0: // Necessary to be able to confirm changes with the user. Chris@0: else if (from === 'deactivating' && to === 'opened' && context.confirming) { Chris@0: accept = true; Chris@0: } Chris@0: // Allow: opened -> deactivating. Chris@0: // Necessary to be able to stop editing. Chris@0: else if (from === 'opened' && to === 'deactivating' && context.confirmed) { Chris@0: accept = true; Chris@0: } Chris@0: } Chris@0: Chris@0: return accept; Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Checks if fields have acceptable states. Chris@0: * Chris@0: * @param {Array} acceptedFieldStates Chris@0: * An array of acceptable field states to check for. Chris@0: * Chris@0: * @return {bool} Chris@0: * Whether the fields have an acceptable state. Chris@0: * Chris@0: * @see Drupal.quickedit.EntityModel#validate Chris@0: */ Chris@0: _fieldsHaveAcceptableStates(acceptedFieldStates) { Chris@0: let accept = true; Chris@0: Chris@0: // If no acceptable field states are provided, assume all field states are Chris@0: // acceptable. We want to let validation pass as a default and only Chris@0: // check validity on calls to set that explicitly request it. Chris@0: if (acceptedFieldStates.length > 0) { Chris@0: const fieldStates = this.get('fields').pluck('state') || []; Chris@0: // If not all fields are in one of the accepted field states, then we Chris@0: // still can't allow this state change. Chris@0: if (_.difference(fieldStates, acceptedFieldStates).length) { Chris@0: accept = false; Chris@0: } Chris@0: } Chris@0: Chris@0: return accept; Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Destroys the entity model. Chris@0: * Chris@0: * @param {object} options Chris@0: * Options for the entity model. Chris@0: */ Chris@0: destroy(options) { Chris@0: Drupal.quickedit.BaseModel.prototype.destroy.call(this, options); Chris@0: Chris@0: this.stopListening(); Chris@0: Chris@0: // Destroy all fields of this entity. Chris@0: this.get('fields').reset(); Chris@0: }, Chris@0: Chris@0: /** Chris@0: * @inheritdoc Chris@0: */ Chris@0: sync() { Chris@0: // We don't use REST updates to sync. Chris@0: Chris@0: }, Chris@0: Chris@0: }, /** @lends Drupal.quickedit.EntityModel */{ Chris@0: Chris@0: /** Chris@0: * Sequence of all possible states an entity can be in during quickediting. Chris@0: * Chris@0: * @type {Array.} Chris@0: */ Chris@0: states: [ Chris@0: // Initial state, like field's 'inactive' OR the user has just finished Chris@0: // in-place editing this entity. Chris@0: // - Trigger: none (initial) or EntityModel (finished). Chris@0: // - Expected behavior: (when not initial state): tear down Chris@0: // EntityToolbarView, in-place editors and related views. Chris@0: 'closed', Chris@0: // User has activated in-place editing of this entity. Chris@0: // - Trigger: user. Chris@0: // - Expected behavior: the EntityToolbarView is gets set up, in-place Chris@0: // editors (EditorViews) and related views for this entity's fields are Chris@0: // set up. Upon completion of those, the state is changed to 'opening'. Chris@0: 'launching', Chris@0: // Launching has finished. Chris@0: // - Trigger: application. Chris@0: // - Guarantees: in-place editors ready for use, all entity and field Chris@0: // views have been set up, all fields are in the 'inactive' state. Chris@0: // - Expected behavior: all fields are changed to the 'candidate' state Chris@0: // and once this is completed, the entity state will be changed to Chris@0: // 'opened'. Chris@0: 'opening', Chris@0: // Opening has finished. Chris@0: // - Trigger: EntityModel. Chris@0: // - Guarantees: see 'opening', all fields are in the 'candidate' state. Chris@0: // - Expected behavior: the user is able to actually use in-place editing. Chris@0: 'opened', Chris@0: // User has clicked the 'Save' button (and has thus changed at least one Chris@0: // field). Chris@0: // - Trigger: user. Chris@0: // - Guarantees: see 'opened', plus: either a changed field is in Chris@0: // PrivateTempStore, or the user has just modified a field without Chris@0: // activating (switching to) another field. Chris@0: // - Expected behavior: 1) if any of the fields are not yet in Chris@0: // PrivateTempStore, save them to PrivateTempStore, 2) if then any of Chris@0: // the fields has the 'invalid' state, then change the entity state back Chris@0: // to 'opened', otherwise: save the entity by committing it from Chris@0: // PrivateTempStore into permanent storage. Chris@0: 'committing', Chris@0: // User has clicked the 'Close' button, or has clicked the 'Save' button Chris@0: // and that was successfully completed. Chris@0: // - Trigger: user or EntityModel. Chris@0: // - Guarantees: when having clicked 'Close' hardly any: fields may be in Chris@0: // a variety of states; when having clicked 'Save': all fields are in Chris@0: // the 'candidate' state. Chris@0: // - Expected behavior: transition all fields to the 'candidate' state, Chris@0: // possibly requiring confirmation in the case of having clicked Chris@0: // 'Close'. Chris@0: 'deactivating', Chris@0: // Deactivation has been completed. Chris@0: // - Trigger: EntityModel. Chris@0: // - Guarantees: all fields are in the 'candidate' state. Chris@0: // - Expected behavior: change all fields to the 'inactive' state. Chris@0: 'closing', Chris@0: ], Chris@0: Chris@0: /** Chris@0: * Indicates whether the 'from' state comes before the 'to' state. Chris@0: * Chris@0: * @param {string} from Chris@0: * One of {@link Drupal.quickedit.EntityModel.states}. Chris@0: * @param {string} to Chris@0: * One of {@link Drupal.quickedit.EntityModel.states}. Chris@0: * Chris@0: * @return {bool} Chris@0: * Whether the 'from' state comes before the 'to' state. Chris@0: */ Chris@0: followsStateSequence(from, to) { Chris@0: return _.indexOf(this.states, from) < _.indexOf(this.states, to); Chris@0: }, Chris@0: Chris@0: }); Chris@0: Chris@0: /** Chris@0: * @constructor Chris@0: * Chris@0: * @augments Backbone.Collection Chris@0: */ Chris@0: Drupal.quickedit.EntityCollection = Backbone.Collection.extend(/** @lends Drupal.quickedit.EntityCollection# */{ Chris@0: Chris@0: /** Chris@0: * @type {Drupal.quickedit.EntityModel} Chris@0: */ Chris@0: model: Drupal.quickedit.EntityModel, Chris@0: }); Chris@0: }(_, jQuery, Backbone, Drupal));