Chris@0: /** Chris@0: * @file Chris@0: * An abstract Backbone View that controls an in-place editor. Chris@0: */ Chris@0: Chris@0: (function ($, Backbone, Drupal) { Chris@0: Drupal.quickedit.EditorView = Backbone.View.extend(/** @lends Drupal.quickedit.EditorView# */{ Chris@0: Chris@0: /** Chris@0: * A base implementation that outlines the structure for in-place editors. Chris@0: * Chris@0: * Specific in-place editor implementations should subclass (extend) this Chris@0: * View and override whichever method they deem necessary to override. Chris@0: * Chris@0: * Typically you would want to override this method to set the Chris@0: * originalValue attribute in the FieldModel to such a value that your Chris@0: * in-place editor can revert to the original value when necessary. Chris@0: * Chris@0: * @example Chris@0: * If you override this method, you should call this Chris@0: * method (the parent class' initialize()) first. Chris@0: * Drupal.quickedit.EditorView.prototype.initialize.call(this, options); 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.EditorModel} options.model Chris@0: * The in-place editor state model. Chris@0: * @param {Drupal.quickedit.FieldModel} options.fieldModel Chris@0: * The field model. Chris@0: * Chris@0: * @see Drupal.quickedit.EditorModel Chris@0: * @see Drupal.quickedit.editors.plain_text Chris@0: */ Chris@0: initialize(options) { Chris@0: this.fieldModel = options.fieldModel; Chris@0: this.listenTo(this.fieldModel, 'change:state', this.stateChange); Chris@0: }, Chris@0: Chris@0: /** Chris@0: * @inheritdoc Chris@0: */ Chris@0: remove() { Chris@0: // The el property is the field, which should not be removed. Remove the Chris@0: // pointer to it, then call Backbone.View.prototype.remove(). Chris@0: this.setElement(); Chris@0: Backbone.View.prototype.remove.call(this); Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Returns the edited element. Chris@0: * Chris@0: * For some single cardinality fields, it may be necessary or useful to Chris@0: * not in-place edit (and hence decorate) the DOM element with the Chris@0: * data-quickedit-field-id attribute (which is the field's wrapper), but a Chris@0: * specific element within the field's wrapper. Chris@0: * e.g. using a WYSIWYG editor on a body field should happen on the DOM Chris@0: * element containing the text itself, not on the field wrapper. Chris@0: * Chris@0: * @return {jQuery} Chris@0: * A jQuery-wrapped DOM element. Chris@0: * Chris@0: * @see Drupal.quickedit.editors.plain_text Chris@0: */ Chris@0: getEditedElement() { Chris@0: return this.$el; Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Chris@0: * @return {object} Chris@0: * Returns 3 Quick Edit UI settings that depend on the in-place editor: Chris@0: * - Boolean padding: indicates whether padding should be applied to the Chris@0: * edited element, to guarantee legibility of text. Chris@0: * - Boolean unifiedToolbar: provides the in-place editor with the ability Chris@0: * to insert its own toolbar UI into Quick Edit's tightly integrated Chris@0: * toolbar. Chris@0: * - Boolean fullWidthToolbar: indicates whether Quick Edit's tightly Chris@0: * integrated toolbar should consume the full width of the element, Chris@0: * rather than being just long enough to accommodate a label. Chris@0: */ Chris@0: getQuickEditUISettings() { Chris@0: return { padding: false, unifiedToolbar: false, fullWidthToolbar: false, popup: false }; Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Determines the actions to take given a change of state. Chris@0: * Chris@0: * @param {Drupal.quickedit.FieldModel} fieldModel Chris@0: * The quickedit `FieldModel` that holds 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: stateChange(fieldModel, state) { Chris@0: const from = fieldModel.previous('state'); Chris@0: const to = state; Chris@0: switch (to) { Chris@0: case 'inactive': Chris@0: // An in-place editor view will not yet exist in this state, hence Chris@0: // this will never be reached. Listed for sake of completeness. Chris@0: break; Chris@0: Chris@0: case 'candidate': Chris@0: // Nothing to do for the typical in-place editor: it should not be Chris@0: // visible yet. Except when we come from the 'invalid' state, then we Chris@0: // clean up. Chris@0: if (from === 'invalid') { Chris@0: this.removeValidationErrors(); Chris@0: } Chris@0: break; Chris@0: Chris@0: case 'highlighted': Chris@0: // Nothing to do for the typical in-place editor: it should not be Chris@0: // visible yet. Chris@0: break; Chris@0: Chris@0: case 'activating': { Chris@0: // The user has indicated he wants to do in-place editing: if Chris@0: // something needs to be loaded (CSS/JavaScript/server data/…), then Chris@0: // do so at this stage, and once the in-place editor is ready, Chris@0: // set the 'active' state. A "loading" indicator will be shown in the Chris@0: // UI for as long as the field remains in this state. Chris@0: const loadDependencies = function (callback) { Chris@0: // Do the loading here. Chris@0: callback(); Chris@0: }; Chris@0: loadDependencies(() => { Chris@0: fieldModel.set('state', 'active'); Chris@0: }); Chris@0: break; Chris@0: } Chris@0: Chris@0: case 'active': Chris@0: // The user can now actually use the in-place editor. Chris@0: break; Chris@0: Chris@0: case 'changed': Chris@0: // Nothing to do for the typical in-place editor. The UI will show an Chris@0: // indicator that the field has changed. Chris@0: break; Chris@0: Chris@0: case 'saving': Chris@0: // When the user has indicated he wants to save his changes to this Chris@0: // field, this state will be entered. If the previous saving attempt Chris@0: // resulted in validation errors, the previous state will be Chris@0: // 'invalid'. Clean up those validation errors while the user is Chris@0: // saving. Chris@0: if (from === 'invalid') { Chris@0: this.removeValidationErrors(); Chris@0: } Chris@0: this.save(); Chris@0: break; Chris@0: Chris@0: case 'saved': Chris@0: // Nothing to do for the typical in-place editor. Immediately after Chris@0: // being saved, a field will go to the 'candidate' state, where it Chris@0: // should no longer be visible (after all, the field will then again Chris@0: // just be a *candidate* to be in-place edited). Chris@0: break; Chris@0: Chris@0: case 'invalid': Chris@0: // The modified field value was attempted to be saved, but there were Chris@0: // validation errors. Chris@0: this.showValidationErrors(); Chris@0: break; Chris@0: } Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Reverts the modified value to the original, before editing started. Chris@0: */ Chris@0: revert() { Chris@0: // A no-op by default; each editor should implement reverting itself. Chris@0: // Note that if the in-place editor does not cause the FieldModel's Chris@0: // element to be modified, then nothing needs to happen. Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Saves the modified value in the in-place editor for this field. Chris@0: */ Chris@0: save() { Chris@0: const fieldModel = this.fieldModel; Chris@0: const editorModel = this.model; Chris@0: const backstageId = `quickedit_backstage-${this.fieldModel.id.replace(/[/[\]_\s]/g, '-')}`; Chris@0: Chris@0: function fillAndSubmitForm(value) { Chris@0: const $form = $(`#${backstageId}`).find('form'); Chris@0: // Fill in the value in any that isn't hidden or a submit Chris@0: // button. Chris@0: $form.find(':input[type!="hidden"][type!="submit"]:not(select)') Chris@0: // Don't mess with the node summary. Chris@0: .not('[name$="\\[summary\\]"]').val(value); Chris@0: // Submit the form. Chris@0: $form.find('.quickedit-form-submit').trigger('click.quickedit'); Chris@0: } Chris@0: Chris@0: const formOptions = { Chris@0: fieldID: this.fieldModel.get('fieldID'), Chris@0: $el: this.$el, Chris@0: nocssjs: true, Chris@0: other_view_modes: fieldModel.findOtherViewModes(), Chris@0: // Reset an existing entry for this entity in the PrivateTempStore (if Chris@0: // any) when saving the field. Logically speaking, this should happen in Chris@0: // a separate request because this is an entity-level operation, not a Chris@0: // field-level operation. But that would require an additional request, Chris@0: // that might not even be necessary: it is only when a user saves a Chris@0: // first changed field for an entity that this needs to happen: Chris@0: // precisely now! Chris@0: reset: !this.fieldModel.get('entity').get('inTempStore'), Chris@0: }; Chris@0: Chris@0: const self = this; Chris@0: Drupal.quickedit.util.form.load(formOptions, (form, ajax) => { Chris@0: // Create a backstage area for storing forms that are hidden from view Chris@0: // (hence "backstage" — since the editing doesn't happen in the form, it Chris@0: // happens "directly" in the content, the form is only used for saving). Chris@0: const $backstage = $(Drupal.theme('quickeditBackstage', { id: backstageId })).appendTo('body'); Chris@0: // Hidden forms are stuffed into the backstage container for this field. Chris@0: const $form = $(form).appendTo($backstage); Chris@0: // Disable the browser's HTML5 validation; we only care about server- Chris@0: // side validation. (Not disabling this will actually cause problems Chris@0: // because browsers don't like to set HTML5 validation errors on hidden Chris@0: // forms.) Chris@0: $form.prop('novalidate', true); Chris@0: const $submit = $form.find('.quickedit-form-submit'); Chris@0: self.formSaveAjax = Drupal.quickedit.util.form.ajaxifySaving(formOptions, $submit); Chris@0: Chris@0: function removeHiddenForm() { Chris@0: Drupal.quickedit.util.form.unajaxifySaving(self.formSaveAjax); Chris@0: delete self.formSaveAjax; Chris@0: $backstage.remove(); Chris@0: } Chris@0: Chris@0: // Successfully saved. Chris@0: self.formSaveAjax.commands.quickeditFieldFormSaved = function (ajax, response, status) { Chris@0: removeHiddenForm(); Chris@0: // First, transition the state to 'saved'. Chris@0: fieldModel.set('state', 'saved'); Chris@0: // Second, set the 'htmlForOtherViewModes' attribute, so that when Chris@0: // this field is rerendered, the change can be propagated to other Chris@0: // instances of this field, which may be displayed in different view Chris@0: // modes. Chris@0: fieldModel.set('htmlForOtherViewModes', response.other_view_modes); Chris@0: // Finally, set the 'html' attribute on the field model. This will Chris@0: // cause the field to be rerendered. Chris@0: fieldModel.set('html', response.data); Chris@0: }; Chris@0: Chris@0: // Unsuccessfully saved; validation errors. Chris@0: self.formSaveAjax.commands.quickeditFieldFormValidationErrors = function (ajax, response, status) { Chris@0: removeHiddenForm(); Chris@0: editorModel.set('validationErrors', response.data); Chris@0: fieldModel.set('state', 'invalid'); Chris@0: }; Chris@0: Chris@0: // The quickeditFieldForm AJAX command is only called upon loading the Chris@0: // form for the first time, and when there are validation errors in the Chris@0: // form; Form API then marks which form items have errors. This is Chris@0: // useful for the form-based in-place editor, but pointless for any Chris@0: // other: the form itself won't be visible at all anyway! So, we just Chris@0: // ignore it. Chris@0: self.formSaveAjax.commands.quickeditFieldForm = function () {}; Chris@0: Chris@0: fillAndSubmitForm(editorModel.get('currentValue')); Chris@0: }); Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Shows validation error messages. Chris@0: * Chris@0: * Should be called when the state is changed to 'invalid'. Chris@0: */ Chris@0: showValidationErrors() { Chris@0: const $errors = $('
') Chris@0: .append(this.model.get('validationErrors')); Chris@0: this.getEditedElement() Chris@0: .addClass('quickedit-validation-error') Chris@0: .after($errors); Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Cleans up validation error messages. Chris@0: * Chris@0: * Should be called when the state is changed to 'candidate' or 'saving'. In Chris@0: * the case of the latter: the user has modified the value in the in-place Chris@0: * editor again to attempt to save again. In the case of the latter: the Chris@0: * invalid value was discarded. Chris@0: */ Chris@0: removeValidationErrors() { Chris@0: this.getEditedElement() Chris@0: .removeClass('quickedit-validation-error') Chris@0: .next('.quickedit-validation-errors') Chris@0: .remove(); Chris@0: }, Chris@0: Chris@0: }); Chris@0: }(jQuery, Backbone, Drupal));