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 = $(`