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@17: (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@17: Drupal.quickedit.AppView = Backbone.View.extend( Chris@17: /** @lends Drupal.quickedit.AppView# */ { Chris@17: /** Chris@17: * @constructs Chris@17: * Chris@17: * @augments Backbone.View Chris@17: * Chris@17: * @param {object} options Chris@17: * An object with the following keys: Chris@17: * @param {Drupal.quickedit.AppModel} options.model Chris@17: * The application state model. Chris@17: * @param {Drupal.quickedit.EntityCollection} options.entitiesCollection Chris@17: * All on-page entities. Chris@17: * @param {Drupal.quickedit.FieldCollection} options.fieldsCollection Chris@17: * All on-page fields Chris@17: */ Chris@17: initialize(options) { Chris@17: // AppView's configuration for handling states. Chris@17: // @see Drupal.quickedit.FieldModel.states Chris@17: this.activeFieldStates = ['activating', 'active']; Chris@17: this.singleFieldStates = ['highlighted', 'activating', 'active']; Chris@17: this.changedFieldStates = ['changed', 'saving', 'saved', 'invalid']; Chris@17: this.readyFieldStates = ['candidate', 'highlighted']; Chris@0: Chris@17: // Track app state. Chris@17: this.listenTo( Chris@17: options.entitiesCollection, Chris@17: 'change:state', Chris@17: this.appStateChange, Chris@17: ); Chris@17: this.listenTo( Chris@17: options.entitiesCollection, Chris@17: 'change:isActive', Chris@17: this.enforceSingleActiveEntity, Chris@17: ); Chris@0: Chris@17: // Track app state. Chris@17: this.listenTo( Chris@17: options.fieldsCollection, Chris@17: 'change:state', Chris@17: this.editorStateChange, Chris@17: ); Chris@17: // Respond to field model HTML representation change events. Chris@17: this.listenTo( Chris@17: options.fieldsCollection, Chris@17: 'change:html', Chris@17: this.renderUpdatedField, Chris@17: ); Chris@17: this.listenTo( Chris@17: options.fieldsCollection, Chris@17: 'change:html', Chris@17: this.propagateUpdatedField, Chris@17: ); Chris@17: // Respond to addition. Chris@17: this.listenTo( Chris@17: options.fieldsCollection, Chris@17: 'add', Chris@17: this.rerenderedFieldToCandidate, Chris@17: ); Chris@17: // Respond to destruction. Chris@17: this.listenTo(options.fieldsCollection, 'destroy', this.teardownEditor); Chris@17: }, Chris@0: Chris@17: /** Chris@17: * Handles setup/teardown and state changes when the active entity changes. Chris@17: * Chris@17: * @param {Drupal.quickedit.EntityModel} entityModel Chris@17: * An instance of the EntityModel class. Chris@17: * @param {string} state Chris@17: * The state of the associated field. One of Chris@17: * {@link Drupal.quickedit.EntityModel.states}. Chris@17: */ Chris@17: appStateChange(entityModel, state) { Chris@17: const app = this; Chris@17: let entityToolbarView; Chris@17: switch (state) { Chris@17: case 'launching': Chris@17: reload = false; Chris@17: // First, create an entity toolbar view. Chris@17: entityToolbarView = new Drupal.quickedit.EntityToolbarView({ Chris@17: model: entityModel, Chris@17: appModel: this.model, Chris@17: }); Chris@17: entityModel.toolbarView = entityToolbarView; Chris@17: // Second, set up in-place editors. Chris@17: // They must be notified of state changes, hence this must happen Chris@17: // while the associated fields are still in the 'inactive' state. Chris@17: entityModel.get('fields').each(fieldModel => { Chris@17: app.setupEditor(fieldModel); Chris@17: }); Chris@17: // Third, transition the entity to the 'opening' state, which will Chris@17: // transition all fields from 'inactive' to 'candidate'. Chris@17: _.defer(() => { Chris@17: entityModel.set('state', 'opening'); Chris@17: }); Chris@17: break; Chris@0: Chris@17: case 'closed': Chris@17: entityToolbarView = entityModel.toolbarView; Chris@17: // First, tear down the in-place editors. Chris@17: entityModel.get('fields').each(fieldModel => { Chris@17: app.teardownEditor(fieldModel); Chris@17: }); Chris@17: // Second, tear down the entity toolbar view. Chris@17: if (entityToolbarView) { Chris@17: entityToolbarView.remove(); Chris@17: delete entityModel.toolbarView; Chris@17: } Chris@17: // A page reload may be necessary to re-instate the original HTML of Chris@17: // the edited fields. Chris@17: if (reload) { Chris@17: reload = false; Chris@17: window.location.reload(); Chris@17: } Chris@17: break; Chris@17: } Chris@17: }, Chris@0: Chris@17: /** Chris@17: * Accepts or reject editor (Editor) state changes. Chris@17: * Chris@17: * This is what ensures that the app is in control of what happens. Chris@17: * Chris@17: * @param {string} from Chris@17: * The previous state. Chris@17: * @param {string} to Chris@17: * The new state. Chris@17: * @param {null|object} context Chris@17: * The context that is trying to trigger the state change. Chris@17: * @param {Drupal.quickedit.FieldModel} fieldModel Chris@17: * The fieldModel to which this change applies. Chris@17: * Chris@17: * @return {bool} Chris@17: * Whether the editor change was accepted or rejected. Chris@17: */ Chris@17: acceptEditorStateChange(from, to, context, fieldModel) { Chris@17: let accept = true; Chris@0: Chris@17: // If the app is in view mode, then reject all state changes except for Chris@17: // those to 'inactive'. Chris@17: if ( Chris@17: context && Chris@17: (context.reason === 'stop' || context.reason === 'rerender') Chris@17: ) { Chris@17: if (from === 'candidate' && to === 'inactive') { Chris@0: accept = true; Chris@0: } Chris@0: } Chris@17: // Handling of edit mode state changes is more granular. Chris@17: else { Chris@17: // In general, enforce the states sequence. Disallow going back from a Chris@17: // "later" state to an "earlier" state, except in explicitly allowed Chris@17: // cases. Chris@17: if (!Drupal.quickedit.FieldModel.followsStateSequence(from, to)) { Chris@17: accept = false; Chris@17: // Allow: activating/active -> candidate. Chris@17: // Necessary to stop editing a field. Chris@17: if ( Chris@17: _.indexOf(this.activeFieldStates, from) !== -1 && Chris@17: to === 'candidate' Chris@17: ) { Chris@17: accept = true; Chris@0: } Chris@17: // Allow: changed/invalid -> candidate. Chris@17: // Necessary to stop editing a field when it is changed or invalid. Chris@17: else if ( Chris@17: (from === 'changed' || from === 'invalid') && Chris@17: to === 'candidate' Chris@17: ) { Chris@17: accept = true; Chris@0: } Chris@17: // Allow: highlighted -> candidate. Chris@17: // Necessary to stop highlighting a field. Chris@17: else if (from === 'highlighted' && to === 'candidate') { Chris@17: accept = true; Chris@0: } Chris@17: // Allow: saved -> candidate. Chris@17: // Necessary when successfully saved a field. Chris@17: else if (from === 'saved' && to === 'candidate') { Chris@17: accept = true; Chris@17: } Chris@17: // Allow: invalid -> saving. Chris@17: // Necessary to be able to save a corrected, invalid field. Chris@17: else if (from === 'invalid' && to === 'saving') { Chris@17: accept = true; Chris@17: } Chris@17: // Allow: invalid -> activating. Chris@17: // Necessary to be able to correct a field that turned out to be Chris@17: // invalid after the user already had moved on to the next field Chris@17: // (which we explicitly allow to have a fluent UX). Chris@17: else if (from === 'invalid' && to === 'activating') { Chris@14: accept = true; Chris@0: } Chris@0: } Chris@0: Chris@17: // If it's not against the general principle, then here are more Chris@17: // disallowed cases to check. Chris@17: if (accept) { Chris@17: let activeField; Chris@17: let activeFieldState; Chris@17: // Ensure only one field (editor) at a time is active … but allow a Chris@17: // user to hop from one field to the next, even if we still have to Chris@17: // start saving the field that is currently active: assume it will be Chris@17: // valid, to allow for a fluent UX. (If it turns out to be invalid, Chris@17: // this block of code also handles that.) Chris@17: if ( Chris@17: (this.readyFieldStates.indexOf(from) !== -1 || Chris@17: from === 'invalid') && Chris@17: this.activeFieldStates.indexOf(to) !== -1 Chris@17: ) { Chris@17: activeField = this.model.get('activeField'); Chris@17: if (activeField && activeField !== fieldModel) { Chris@17: activeFieldState = activeField.get('state'); Chris@17: // Allow the state change. If the state of the active field is: Chris@17: // - 'activating' or 'active': change it to 'candidate' Chris@17: // - 'changed' or 'invalid': change it to 'saving' Chris@17: // - 'saving' or 'saved': don't do anything. Chris@17: if (this.activeFieldStates.indexOf(activeFieldState) !== -1) { Chris@17: activeField.set('state', 'candidate'); Chris@17: } else if ( Chris@17: activeFieldState === 'changed' || Chris@17: activeFieldState === 'invalid' Chris@17: ) { Chris@17: activeField.set('state', 'saving'); Chris@17: } Chris@0: Chris@17: // If the field that's being activated is in fact already in the Chris@17: // invalid state (which can only happen because above we allowed Chris@17: // the user to move on to another field to allow for a fluent UX; Chris@17: // we assumed it would be saved successfully), then we shouldn't Chris@17: // allow the field to enter the 'activating' state, instead, we Chris@17: // simply change the active editor. All guarantees and Chris@17: // assumptions for this field still hold! Chris@17: if (from === 'invalid') { Chris@17: this.model.set('activeField', fieldModel); Chris@17: accept = false; Chris@17: } Chris@17: // Do not reject: the field is either in the 'candidate' or Chris@17: // 'highlighted' state and we allow it to enter the 'activating' Chris@17: // state! Chris@17: } Chris@17: } Chris@17: // Reject going from activating/active to candidate because of a Chris@17: // mouseleave. Chris@17: else if ( Chris@17: _.indexOf(this.activeFieldStates, from) !== -1 && Chris@17: to === 'candidate' Chris@17: ) { Chris@17: if (context && context.reason === 'mouseleave') { Chris@17: accept = false; Chris@17: } Chris@17: } Chris@17: // When attempting to stop editing a changed/invalid property, ask for Chris@17: // confirmation. Chris@17: else if ( Chris@17: (from === 'changed' || from === 'invalid') && Chris@17: to === 'candidate' Chris@17: ) { Chris@17: if (context && context.reason === 'mouseleave') { Chris@17: accept = false; Chris@17: } Chris@17: // Check whether the transition has been confirmed? Chris@17: else if (context && context.confirmed) { Chris@17: accept = true; Chris@17: } Chris@17: } Chris@0: } Chris@0: } Chris@0: Chris@17: return accept; Chris@17: }, Chris@17: Chris@17: /** Chris@17: * Sets up the in-place editor for the given field. Chris@17: * Chris@17: * Must happen before the fieldModel's state is changed to 'candidate'. Chris@17: * Chris@17: * @param {Drupal.quickedit.FieldModel} fieldModel Chris@17: * The field for which an in-place editor must be set up. Chris@17: */ Chris@17: setupEditor(fieldModel) { Chris@17: // Get the corresponding entity toolbar. Chris@17: const entityModel = fieldModel.get('entity'); Chris@17: const entityToolbarView = entityModel.toolbarView; Chris@17: // Get the field toolbar DOM root from the entity toolbar. Chris@17: const fieldToolbarRoot = entityToolbarView.getToolbarRoot(); Chris@17: // Create in-place editor. Chris@17: const editorName = fieldModel.get('metadata').editor; Chris@17: const editorModel = new Drupal.quickedit.EditorModel(); Chris@17: const editorView = new Drupal.quickedit.editors[editorName]({ Chris@17: el: $(fieldModel.get('el')), Chris@17: model: editorModel, Chris@17: fieldModel, Chris@17: }); Chris@17: Chris@17: // Create in-place editor's toolbar for this field — stored inside the Chris@17: // entity toolbar, the entity toolbar will position itself appropriately Chris@17: // above (or below) the edited element. Chris@17: const toolbarView = new Drupal.quickedit.FieldToolbarView({ Chris@17: el: fieldToolbarRoot, Chris@17: model: fieldModel, Chris@17: $editedElement: $(editorView.getEditedElement()), Chris@17: editorView, Chris@17: entityModel, Chris@17: }); Chris@17: Chris@17: // Create decoration for edited element: padding if necessary, sets Chris@17: // classes on the element to style it according to the current state. Chris@17: const decorationView = new Drupal.quickedit.FieldDecorationView({ Chris@17: el: $(editorView.getEditedElement()), Chris@17: model: fieldModel, Chris@17: editorView, Chris@17: }); Chris@17: Chris@17: // Track these three views in FieldModel so that we can tear them down Chris@17: // correctly. Chris@17: fieldModel.editorView = editorView; Chris@17: fieldModel.toolbarView = toolbarView; Chris@17: fieldModel.decorationView = decorationView; Chris@17: }, Chris@17: Chris@17: /** Chris@17: * Tears down the in-place editor for the given field. Chris@17: * Chris@17: * Must happen after the fieldModel's state is changed to 'inactive'. Chris@17: * Chris@17: * @param {Drupal.quickedit.FieldModel} fieldModel Chris@17: * The field for which an in-place editor must be torn down. Chris@17: */ Chris@17: teardownEditor(fieldModel) { Chris@17: // Early-return if this field was not yet decorated. Chris@17: if (typeof fieldModel.editorView === 'undefined') { Chris@17: return; Chris@17: } Chris@17: Chris@17: // Unbind event handlers; remove toolbar element; delete toolbar view. Chris@17: fieldModel.toolbarView.remove(); Chris@17: delete fieldModel.toolbarView; Chris@17: Chris@17: // Unbind event handlers; delete decoration view. Don't remove the element Chris@17: // because that would remove the field itself. Chris@17: fieldModel.decorationView.remove(); Chris@17: delete fieldModel.decorationView; Chris@17: Chris@17: // Unbind event handlers; delete editor view. Don't remove the element Chris@17: // because that would remove the field itself. Chris@17: fieldModel.editorView.remove(); Chris@17: delete fieldModel.editorView; Chris@17: }, Chris@17: Chris@17: /** Chris@17: * Asks the user to confirm whether he wants to stop editing via a modal. Chris@17: * Chris@17: * @param {Drupal.quickedit.EntityModel} entityModel Chris@17: * An instance of the EntityModel class. Chris@17: * Chris@17: * @see Drupal.quickedit.AppView#acceptEditorStateChange Chris@17: */ Chris@17: confirmEntityDeactivation(entityModel) { Chris@17: const that = this; Chris@17: let discardDialog; Chris@17: Chris@17: function closeDiscardDialog(action) { Chris@17: discardDialog.close(action); Chris@17: // The active modal has been removed. Chris@17: that.model.set('activeModal', null); Chris@17: Chris@17: // If the targetState is saving, the field must be saved, then the Chris@17: // entity must be saved. Chris@17: if (action === 'save') { Chris@17: entityModel.set('state', 'committing', { confirmed: true }); Chris@17: } else { Chris@17: entityModel.set('state', 'deactivating', { confirmed: true }); Chris@17: // Editing has been canceled and the changes will not be saved. Mark Chris@17: // the page for reload if the entityModel declares that it requires Chris@17: // a reload. Chris@17: if (entityModel.get('reload')) { Chris@17: reload = true; Chris@17: entityModel.set('reload', false); Chris@17: } Chris@17: } Chris@17: } Chris@17: Chris@17: // Only instantiate if there isn't a modal instance visible yet. Chris@17: if (!this.model.get('activeModal')) { Chris@17: const $unsavedChanges = $( Chris@17: `