X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fquickedit%2Fjs%2Fviews%2FAppView.js;fp=web%2Fcore%2Fmodules%2Fquickedit%2Fjs%2Fviews%2FAppView.js;h=1903846efdb0b75461581b78693f4dd39bbbb23d;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hp=b09a110b8bf7e6e76d59d113654dcc5a329557fd;hpb=aea91e65e895364e460983b890e295aa5d5540a5;p=yaffs-website diff --git a/web/core/modules/quickedit/js/views/AppView.js b/web/core/modules/quickedit/js/views/AppView.js index b09a110b8..1903846ef 100644 --- a/web/core/modules/quickedit/js/views/AppView.js +++ b/web/core/modules/quickedit/js/views/AppView.js @@ -1,91 +1,49 @@ /** - * @file - * A Backbone View that controls the overall "in-place editing application". - * - * @see Drupal.quickedit.AppModel - */ +* DO NOT EDIT THIS FILE. +* See the following change record for more information, +* https://www.drupal.org/node/2815083 +* @preserve +**/ (function ($, _, Backbone, Drupal) { - - 'use strict'; - - // Indicates whether the page should be reloaded after in-place editing has - // shut down. A page reload is necessary to re-instate the original HTML of - // the edited fields if in-place editing has been canceled and one or more of - // the entity's fields were saved to PrivateTempStore: one of them may have - // been changed to the empty value and hence may have been rerendered as the - // empty string, which makes it impossible for Quick Edit to know where to - // restore the original HTML. var reload = false; - Drupal.quickedit.AppView = Backbone.View.extend(/** @lends Drupal.quickedit.AppView# */{ - - /** - * @constructs - * - * @augments Backbone.View - * - * @param {object} options - * An object with the following keys: - * @param {Drupal.quickedit.AppModel} options.model - * The application state model. - * @param {Drupal.quickedit.EntityCollection} options.entitiesCollection - * All on-page entities. - * @param {Drupal.quickedit.FieldCollection} options.fieldsCollection - * All on-page fields - */ - initialize: function (options) { - // AppView's configuration for handling states. - // @see Drupal.quickedit.FieldModel.states + Drupal.quickedit.AppView = Backbone.View.extend({ + initialize: function initialize(options) { this.activeFieldStates = ['activating', 'active']; this.singleFieldStates = ['highlighted', 'activating', 'active']; this.changedFieldStates = ['changed', 'saving', 'saved', 'invalid']; this.readyFieldStates = ['candidate', 'highlighted']; - // Track app state. this.listenTo(options.entitiesCollection, 'change:state', this.appStateChange); this.listenTo(options.entitiesCollection, 'change:isActive', this.enforceSingleActiveEntity); - // Track app state. this.listenTo(options.fieldsCollection, 'change:state', this.editorStateChange); - // Respond to field model HTML representation change events. + this.listenTo(options.fieldsCollection, 'change:html', this.renderUpdatedField); this.listenTo(options.fieldsCollection, 'change:html', this.propagateUpdatedField); - // Respond to addition. + this.listenTo(options.fieldsCollection, 'add', this.rerenderedFieldToCandidate); - // Respond to destruction. + this.listenTo(options.fieldsCollection, 'destroy', this.teardownEditor); }, - - /** - * Handles setup/teardown and state changes when the active entity changes. - * - * @param {Drupal.quickedit.EntityModel} entityModel - * An instance of the EntityModel class. - * @param {string} state - * The state of the associated field. One of - * {@link Drupal.quickedit.EntityModel.states}. - */ - appStateChange: function (entityModel, state) { + appStateChange: function appStateChange(entityModel, state) { var app = this; - var entityToolbarView; + var entityToolbarView = void 0; switch (state) { case 'launching': reload = false; - // First, create an entity toolbar view. + entityToolbarView = new Drupal.quickedit.EntityToolbarView({ model: entityModel, appModel: this.model }); entityModel.toolbarView = entityToolbarView; - // Second, set up in-place editors. - // They must be notified of state changes, hence this must happen - // while the associated fields are still in the 'inactive' state. + entityModel.get('fields').each(function (fieldModel) { app.setupEditor(fieldModel); }); - // Third, transition the entity to the 'opening' state, which will - // transition all fields from 'inactive' to 'candidate'. + _.defer(function () { entityModel.set('state', 'opening'); }); @@ -93,17 +51,16 @@ case 'closed': entityToolbarView = entityModel.toolbarView; - // First, tear down the in-place editors. + entityModel.get('fields').each(function (fieldModel) { app.teardownEditor(fieldModel); }); - // Second, tear down the entity toolbar view. + if (entityToolbarView) { entityToolbarView.remove(); delete entityModel.toolbarView; } - // A page reload may be necessary to re-instate the original HTML of - // the edited fields. + if (reload) { reload = false; location.reload(); @@ -111,157 +68,76 @@ break; } }, - - /** - * Accepts or reject editor (Editor) state changes. - * - * This is what ensures that the app is in control of what happens. - * - * @param {string} from - * The previous state. - * @param {string} to - * The new state. - * @param {null|object} context - * The context that is trying to trigger the state change. - * @param {Drupal.quickedit.FieldModel} fieldModel - * The fieldModel to which this change applies. - * - * @return {bool} - * Whether the editor change was accepted or rejected. - */ - acceptEditorStateChange: function (from, to, context, fieldModel) { + acceptEditorStateChange: function acceptEditorStateChange(from, to, context, fieldModel) { var accept = true; - // If the app is in view mode, then reject all state changes except for - // those to 'inactive'. if (context && (context.reason === 'stop' || context.reason === 'rerender')) { if (from === 'candidate' && to === 'inactive') { accept = true; } - } - // Handling of edit mode state changes is more granular. - else { - // In general, enforce the states sequence. Disallow going back from a - // "later" state to an "earlier" state, except in explicitly allowed - // cases. - if (!Drupal.quickedit.FieldModel.followsStateSequence(from, to)) { - accept = false; - // Allow: activating/active -> candidate. - // Necessary to stop editing a field. - if (_.indexOf(this.activeFieldStates, from) !== -1 && to === 'candidate') { - accept = true; - } - // Allow: changed/invalid -> candidate. - // Necessary to stop editing a field when it is changed or invalid. - else if ((from === 'changed' || from === 'invalid') && to === 'candidate') { - accept = true; - } - // Allow: highlighted -> candidate. - // Necessary to stop highlighting a field. - else if (from === 'highlighted' && to === 'candidate') { - accept = true; - } - // Allow: saved -> candidate. - // Necessary when successfully saved a field. - else if (from === 'saved' && to === 'candidate') { - accept = true; - } - // Allow: invalid -> saving. - // Necessary to be able to save a corrected, invalid field. - else if (from === 'invalid' && to === 'saving') { - accept = true; - } - // Allow: invalid -> activating. - // Necessary to be able to correct a field that turned out to be - // invalid after the user already had moved on to the next field - // (which we explicitly allow to have a fluent UX). - else if (from === 'invalid' && to === 'activating') { - accept = true; - } - } - - // If it's not against the general principle, then here are more - // disallowed cases to check. - if (accept) { - var activeField; - var activeFieldState; - // Ensure only one field (editor) at a time is active ⦠but allow a - // user to hop from one field to the next, even if we still have to - // start saving the field that is currently active: assume it will be - // valid, to allow for a fluent UX. (If it turns out to be invalid, - // this block of code also handles that.) - if ((this.readyFieldStates.indexOf(from) !== -1 || from === 'invalid') && this.activeFieldStates.indexOf(to) !== -1) { - activeField = this.model.get('activeField'); - if (activeField && activeField !== fieldModel) { - activeFieldState = activeField.get('state'); - // Allow the state change. If the state of the active field is: - // - 'activating' or 'active': change it to 'candidate' - // - 'changed' or 'invalid': change it to 'saving' - // - 'saving' or 'saved': don't do anything. - if (this.activeFieldStates.indexOf(activeFieldState) !== -1) { - activeField.set('state', 'candidate'); - } - else if (activeFieldState === 'changed' || activeFieldState === 'invalid') { - activeField.set('state', 'saving'); - } + } else { + if (!Drupal.quickedit.FieldModel.followsStateSequence(from, to)) { + accept = false; - // If the field that's being activated is in fact already in the - // invalid state (which can only happen because above we allowed - // the user to move on to another field to allow for a fluent UX; - // we assumed it would be saved successfully), then we shouldn't - // allow the field to enter the 'activating' state, instead, we - // simply change the active editor. All guarantees and - // assumptions for this field still hold! - if (from === 'invalid') { - this.model.set('activeField', fieldModel); - accept = false; - } - // Do not reject: the field is either in the 'candidate' or - // 'highlighted' state and we allow it to enter the 'activating' - // state! - } - } - // Reject going from activating/active to candidate because of a - // mouseleave. - else if (_.indexOf(this.activeFieldStates, from) !== -1 && to === 'candidate') { - if (context && context.reason === 'mouseleave') { - accept = false; - } - } - // When attempting to stop editing a changed/invalid property, ask for - // confirmation. - else if ((from === 'changed' || from === 'invalid') && to === 'candidate') { - if (context && context.reason === 'mouseleave') { - accept = false; - } - else { - // Check whether the transition has been confirmed? - if (context && context.confirmed) { + if (_.indexOf(this.activeFieldStates, from) !== -1 && to === 'candidate') { + accept = true; + } else if ((from === 'changed' || from === 'invalid') && to === 'candidate') { accept = true; + } else if (from === 'highlighted' && to === 'candidate') { + accept = true; + } else if (from === 'saved' && to === 'candidate') { + accept = true; + } else if (from === 'invalid' && to === 'saving') { + accept = true; + } else if (from === 'invalid' && to === 'activating') { + accept = true; + } + } + + if (accept) { + var activeField = void 0; + var activeFieldState = void 0; + + if ((this.readyFieldStates.indexOf(from) !== -1 || from === 'invalid') && this.activeFieldStates.indexOf(to) !== -1) { + activeField = this.model.get('activeField'); + if (activeField && activeField !== fieldModel) { + activeFieldState = activeField.get('state'); + + if (this.activeFieldStates.indexOf(activeFieldState) !== -1) { + activeField.set('state', 'candidate'); + } else if (activeFieldState === 'changed' || activeFieldState === 'invalid') { + activeField.set('state', 'saving'); + } + + if (from === 'invalid') { + this.model.set('activeField', fieldModel); + accept = false; + } } - } + } else if (_.indexOf(this.activeFieldStates, from) !== -1 && to === 'candidate') { + if (context && context.reason === 'mouseleave') { + accept = false; + } + } else if ((from === 'changed' || from === 'invalid') && to === 'candidate') { + if (context && context.reason === 'mouseleave') { + accept = false; + } else { + if (context && context.confirmed) { + accept = true; + } + } + } } } - } return accept; }, - - /** - * Sets up the in-place editor for the given field. - * - * Must happen before the fieldModel's state is changed to 'candidate'. - * - * @param {Drupal.quickedit.FieldModel} fieldModel - * The field for which an in-place editor must be set up. - */ - setupEditor: function (fieldModel) { - // Get the corresponding entity toolbar. + setupEditor: function setupEditor(fieldModel) { var entityModel = fieldModel.get('entity'); var entityToolbarView = entityModel.toolbarView; - // Get the field toolbar DOM root from the entity toolbar. + var fieldToolbarRoot = entityToolbarView.getToolbarRoot(); - // Create in-place editor. + var editorName = fieldModel.get('metadata').editor; var editorModel = new Drupal.quickedit.EditorModel(); var editorView = new Drupal.quickedit.editors[editorName]({ @@ -270,9 +146,6 @@ fieldModel: fieldModel }); - // Create in-place editor's toolbar for this field â stored inside the - // entity toolbar, the entity toolbar will position itself appropriately - // above (or below) the edited element. var toolbarView = new Drupal.quickedit.FieldToolbarView({ el: fieldToolbarRoot, model: fieldModel, @@ -281,77 +154,44 @@ entityModel: entityModel }); - // Create decoration for edited element: padding if necessary, sets - // classes on the element to style it according to the current state. var decorationView = new Drupal.quickedit.FieldDecorationView({ el: $(editorView.getEditedElement()), model: fieldModel, editorView: editorView }); - // Track these three views in FieldModel so that we can tear them down - // correctly. fieldModel.editorView = editorView; fieldModel.toolbarView = toolbarView; fieldModel.decorationView = decorationView; }, - - /** - * Tears down the in-place editor for the given field. - * - * Must happen after the fieldModel's state is changed to 'inactive'. - * - * @param {Drupal.quickedit.FieldModel} fieldModel - * The field for which an in-place editor must be torn down. - */ - teardownEditor: function (fieldModel) { - // Early-return if this field was not yet decorated. + teardownEditor: function teardownEditor(fieldModel) { if (typeof fieldModel.editorView === 'undefined') { return; } - // Unbind event handlers; remove toolbar element; delete toolbar view. fieldModel.toolbarView.remove(); delete fieldModel.toolbarView; - // Unbind event handlers; delete decoration view. Don't remove the element - // because that would remove the field itself. fieldModel.decorationView.remove(); delete fieldModel.decorationView; - // Unbind event handlers; delete editor view. Don't remove the element - // because that would remove the field itself. fieldModel.editorView.remove(); delete fieldModel.editorView; }, - - /** - * Asks the user to confirm whether he wants to stop editing via a modal. - * - * @param {Drupal.quickedit.EntityModel} entityModel - * An instance of the EntityModel class. - * - * @see Drupal.quickedit.AppView#acceptEditorStateChange - */ - confirmEntityDeactivation: function (entityModel) { + confirmEntityDeactivation: function confirmEntityDeactivation(entityModel) { var that = this; - var discardDialog; + var discardDialog = void 0; function closeDiscardDialog(action) { discardDialog.close(action); - // The active modal has been removed. + that.model.set('activeModal', null); - // If the targetState is saving, the field must be saved, then the - // entity must be saved. if (action === 'save') { - entityModel.set('state', 'committing', {confirmed: true}); - } - else { - entityModel.set('state', 'deactivating', {confirmed: true}); - // Editing has been canceled and the changes will not be saved. Mark - // the page for reload if the entityModel declares that it requires - // a reload. + entityModel.set('state', 'committing', { confirmed: true }); + } else { + entityModel.set('state', 'deactivating', { confirmed: true }); + if (entityModel.get('reload')) { reload = true; entityModel.set('reload', false); @@ -359,38 +199,33 @@ } } - // Only instantiate if there isn't a modal instance visible yet. if (!this.model.get('activeModal')) { var $unsavedChanges = $('