annotate core/modules/quickedit/js/views/EditorView.es6.js @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
rev   line source
Chris@0 1 /**
Chris@0 2 * @file
Chris@0 3 * An abstract Backbone View that controls an in-place editor.
Chris@0 4 */
Chris@0 5
Chris@0 6 (function ($, Backbone, Drupal) {
Chris@0 7 Drupal.quickedit.EditorView = Backbone.View.extend(/** @lends Drupal.quickedit.EditorView# */{
Chris@0 8
Chris@0 9 /**
Chris@0 10 * A base implementation that outlines the structure for in-place editors.
Chris@0 11 *
Chris@0 12 * Specific in-place editor implementations should subclass (extend) this
Chris@0 13 * View and override whichever method they deem necessary to override.
Chris@0 14 *
Chris@0 15 * Typically you would want to override this method to set the
Chris@0 16 * originalValue attribute in the FieldModel to such a value that your
Chris@0 17 * in-place editor can revert to the original value when necessary.
Chris@0 18 *
Chris@0 19 * @example
Chris@0 20 * <caption>If you override this method, you should call this
Chris@0 21 * method (the parent class' initialize()) first.</caption>
Chris@0 22 * Drupal.quickedit.EditorView.prototype.initialize.call(this, options);
Chris@0 23 *
Chris@0 24 * @constructs
Chris@0 25 *
Chris@0 26 * @augments Backbone.View
Chris@0 27 *
Chris@0 28 * @param {object} options
Chris@0 29 * An object with the following keys:
Chris@0 30 * @param {Drupal.quickedit.EditorModel} options.model
Chris@0 31 * The in-place editor state model.
Chris@0 32 * @param {Drupal.quickedit.FieldModel} options.fieldModel
Chris@0 33 * The field model.
Chris@0 34 *
Chris@0 35 * @see Drupal.quickedit.EditorModel
Chris@0 36 * @see Drupal.quickedit.editors.plain_text
Chris@0 37 */
Chris@0 38 initialize(options) {
Chris@0 39 this.fieldModel = options.fieldModel;
Chris@0 40 this.listenTo(this.fieldModel, 'change:state', this.stateChange);
Chris@0 41 },
Chris@0 42
Chris@0 43 /**
Chris@0 44 * @inheritdoc
Chris@0 45 */
Chris@0 46 remove() {
Chris@0 47 // The el property is the field, which should not be removed. Remove the
Chris@0 48 // pointer to it, then call Backbone.View.prototype.remove().
Chris@0 49 this.setElement();
Chris@0 50 Backbone.View.prototype.remove.call(this);
Chris@0 51 },
Chris@0 52
Chris@0 53 /**
Chris@0 54 * Returns the edited element.
Chris@0 55 *
Chris@0 56 * For some single cardinality fields, it may be necessary or useful to
Chris@0 57 * not in-place edit (and hence decorate) the DOM element with the
Chris@0 58 * data-quickedit-field-id attribute (which is the field's wrapper), but a
Chris@0 59 * specific element within the field's wrapper.
Chris@0 60 * e.g. using a WYSIWYG editor on a body field should happen on the DOM
Chris@0 61 * element containing the text itself, not on the field wrapper.
Chris@0 62 *
Chris@0 63 * @return {jQuery}
Chris@0 64 * A jQuery-wrapped DOM element.
Chris@0 65 *
Chris@0 66 * @see Drupal.quickedit.editors.plain_text
Chris@0 67 */
Chris@0 68 getEditedElement() {
Chris@0 69 return this.$el;
Chris@0 70 },
Chris@0 71
Chris@0 72 /**
Chris@0 73 *
Chris@0 74 * @return {object}
Chris@0 75 * Returns 3 Quick Edit UI settings that depend on the in-place editor:
Chris@0 76 * - Boolean padding: indicates whether padding should be applied to the
Chris@0 77 * edited element, to guarantee legibility of text.
Chris@0 78 * - Boolean unifiedToolbar: provides the in-place editor with the ability
Chris@0 79 * to insert its own toolbar UI into Quick Edit's tightly integrated
Chris@0 80 * toolbar.
Chris@0 81 * - Boolean fullWidthToolbar: indicates whether Quick Edit's tightly
Chris@0 82 * integrated toolbar should consume the full width of the element,
Chris@0 83 * rather than being just long enough to accommodate a label.
Chris@0 84 */
Chris@0 85 getQuickEditUISettings() {
Chris@0 86 return { padding: false, unifiedToolbar: false, fullWidthToolbar: false, popup: false };
Chris@0 87 },
Chris@0 88
Chris@0 89 /**
Chris@0 90 * Determines the actions to take given a change of state.
Chris@0 91 *
Chris@0 92 * @param {Drupal.quickedit.FieldModel} fieldModel
Chris@0 93 * The quickedit `FieldModel` that holds the state.
Chris@0 94 * @param {string} state
Chris@0 95 * The state of the associated field. One of
Chris@0 96 * {@link Drupal.quickedit.FieldModel.states}.
Chris@0 97 */
Chris@0 98 stateChange(fieldModel, state) {
Chris@0 99 const from = fieldModel.previous('state');
Chris@0 100 const to = state;
Chris@0 101 switch (to) {
Chris@0 102 case 'inactive':
Chris@0 103 // An in-place editor view will not yet exist in this state, hence
Chris@0 104 // this will never be reached. Listed for sake of completeness.
Chris@0 105 break;
Chris@0 106
Chris@0 107 case 'candidate':
Chris@0 108 // Nothing to do for the typical in-place editor: it should not be
Chris@0 109 // visible yet. Except when we come from the 'invalid' state, then we
Chris@0 110 // clean up.
Chris@0 111 if (from === 'invalid') {
Chris@0 112 this.removeValidationErrors();
Chris@0 113 }
Chris@0 114 break;
Chris@0 115
Chris@0 116 case 'highlighted':
Chris@0 117 // Nothing to do for the typical in-place editor: it should not be
Chris@0 118 // visible yet.
Chris@0 119 break;
Chris@0 120
Chris@0 121 case 'activating': {
Chris@0 122 // The user has indicated he wants to do in-place editing: if
Chris@0 123 // something needs to be loaded (CSS/JavaScript/server data/…), then
Chris@0 124 // do so at this stage, and once the in-place editor is ready,
Chris@0 125 // set the 'active' state. A "loading" indicator will be shown in the
Chris@0 126 // UI for as long as the field remains in this state.
Chris@0 127 const loadDependencies = function (callback) {
Chris@0 128 // Do the loading here.
Chris@0 129 callback();
Chris@0 130 };
Chris@0 131 loadDependencies(() => {
Chris@0 132 fieldModel.set('state', 'active');
Chris@0 133 });
Chris@0 134 break;
Chris@0 135 }
Chris@0 136
Chris@0 137 case 'active':
Chris@0 138 // The user can now actually use the in-place editor.
Chris@0 139 break;
Chris@0 140
Chris@0 141 case 'changed':
Chris@0 142 // Nothing to do for the typical in-place editor. The UI will show an
Chris@0 143 // indicator that the field has changed.
Chris@0 144 break;
Chris@0 145
Chris@0 146 case 'saving':
Chris@0 147 // When the user has indicated he wants to save his changes to this
Chris@0 148 // field, this state will be entered. If the previous saving attempt
Chris@0 149 // resulted in validation errors, the previous state will be
Chris@0 150 // 'invalid'. Clean up those validation errors while the user is
Chris@0 151 // saving.
Chris@0 152 if (from === 'invalid') {
Chris@0 153 this.removeValidationErrors();
Chris@0 154 }
Chris@0 155 this.save();
Chris@0 156 break;
Chris@0 157
Chris@0 158 case 'saved':
Chris@0 159 // Nothing to do for the typical in-place editor. Immediately after
Chris@0 160 // being saved, a field will go to the 'candidate' state, where it
Chris@0 161 // should no longer be visible (after all, the field will then again
Chris@0 162 // just be a *candidate* to be in-place edited).
Chris@0 163 break;
Chris@0 164
Chris@0 165 case 'invalid':
Chris@0 166 // The modified field value was attempted to be saved, but there were
Chris@0 167 // validation errors.
Chris@0 168 this.showValidationErrors();
Chris@0 169 break;
Chris@0 170 }
Chris@0 171 },
Chris@0 172
Chris@0 173 /**
Chris@0 174 * Reverts the modified value to the original, before editing started.
Chris@0 175 */
Chris@0 176 revert() {
Chris@0 177 // A no-op by default; each editor should implement reverting itself.
Chris@0 178 // Note that if the in-place editor does not cause the FieldModel's
Chris@0 179 // element to be modified, then nothing needs to happen.
Chris@0 180 },
Chris@0 181
Chris@0 182 /**
Chris@0 183 * Saves the modified value in the in-place editor for this field.
Chris@0 184 */
Chris@0 185 save() {
Chris@0 186 const fieldModel = this.fieldModel;
Chris@0 187 const editorModel = this.model;
Chris@0 188 const backstageId = `quickedit_backstage-${this.fieldModel.id.replace(/[/[\]_\s]/g, '-')}`;
Chris@0 189
Chris@0 190 function fillAndSubmitForm(value) {
Chris@0 191 const $form = $(`#${backstageId}`).find('form');
Chris@0 192 // Fill in the value in any <input> that isn't hidden or a submit
Chris@0 193 // button.
Chris@0 194 $form.find(':input[type!="hidden"][type!="submit"]:not(select)')
Chris@0 195 // Don't mess with the node summary.
Chris@0 196 .not('[name$="\\[summary\\]"]').val(value);
Chris@0 197 // Submit the form.
Chris@0 198 $form.find('.quickedit-form-submit').trigger('click.quickedit');
Chris@0 199 }
Chris@0 200
Chris@0 201 const formOptions = {
Chris@0 202 fieldID: this.fieldModel.get('fieldID'),
Chris@0 203 $el: this.$el,
Chris@0 204 nocssjs: true,
Chris@0 205 other_view_modes: fieldModel.findOtherViewModes(),
Chris@0 206 // Reset an existing entry for this entity in the PrivateTempStore (if
Chris@0 207 // any) when saving the field. Logically speaking, this should happen in
Chris@0 208 // a separate request because this is an entity-level operation, not a
Chris@0 209 // field-level operation. But that would require an additional request,
Chris@0 210 // that might not even be necessary: it is only when a user saves a
Chris@0 211 // first changed field for an entity that this needs to happen:
Chris@0 212 // precisely now!
Chris@0 213 reset: !this.fieldModel.get('entity').get('inTempStore'),
Chris@0 214 };
Chris@0 215
Chris@0 216 const self = this;
Chris@0 217 Drupal.quickedit.util.form.load(formOptions, (form, ajax) => {
Chris@0 218 // Create a backstage area for storing forms that are hidden from view
Chris@0 219 // (hence "backstage" — since the editing doesn't happen in the form, it
Chris@0 220 // happens "directly" in the content, the form is only used for saving).
Chris@0 221 const $backstage = $(Drupal.theme('quickeditBackstage', { id: backstageId })).appendTo('body');
Chris@0 222 // Hidden forms are stuffed into the backstage container for this field.
Chris@0 223 const $form = $(form).appendTo($backstage);
Chris@0 224 // Disable the browser's HTML5 validation; we only care about server-
Chris@0 225 // side validation. (Not disabling this will actually cause problems
Chris@0 226 // because browsers don't like to set HTML5 validation errors on hidden
Chris@0 227 // forms.)
Chris@0 228 $form.prop('novalidate', true);
Chris@0 229 const $submit = $form.find('.quickedit-form-submit');
Chris@0 230 self.formSaveAjax = Drupal.quickedit.util.form.ajaxifySaving(formOptions, $submit);
Chris@0 231
Chris@0 232 function removeHiddenForm() {
Chris@0 233 Drupal.quickedit.util.form.unajaxifySaving(self.formSaveAjax);
Chris@0 234 delete self.formSaveAjax;
Chris@0 235 $backstage.remove();
Chris@0 236 }
Chris@0 237
Chris@0 238 // Successfully saved.
Chris@0 239 self.formSaveAjax.commands.quickeditFieldFormSaved = function (ajax, response, status) {
Chris@0 240 removeHiddenForm();
Chris@0 241 // First, transition the state to 'saved'.
Chris@0 242 fieldModel.set('state', 'saved');
Chris@0 243 // Second, set the 'htmlForOtherViewModes' attribute, so that when
Chris@0 244 // this field is rerendered, the change can be propagated to other
Chris@0 245 // instances of this field, which may be displayed in different view
Chris@0 246 // modes.
Chris@0 247 fieldModel.set('htmlForOtherViewModes', response.other_view_modes);
Chris@0 248 // Finally, set the 'html' attribute on the field model. This will
Chris@0 249 // cause the field to be rerendered.
Chris@0 250 fieldModel.set('html', response.data);
Chris@0 251 };
Chris@0 252
Chris@0 253 // Unsuccessfully saved; validation errors.
Chris@0 254 self.formSaveAjax.commands.quickeditFieldFormValidationErrors = function (ajax, response, status) {
Chris@0 255 removeHiddenForm();
Chris@0 256 editorModel.set('validationErrors', response.data);
Chris@0 257 fieldModel.set('state', 'invalid');
Chris@0 258 };
Chris@0 259
Chris@0 260 // The quickeditFieldForm AJAX command is only called upon loading the
Chris@0 261 // form for the first time, and when there are validation errors in the
Chris@0 262 // form; Form API then marks which form items have errors. This is
Chris@0 263 // useful for the form-based in-place editor, but pointless for any
Chris@0 264 // other: the form itself won't be visible at all anyway! So, we just
Chris@0 265 // ignore it.
Chris@0 266 self.formSaveAjax.commands.quickeditFieldForm = function () {};
Chris@0 267
Chris@0 268 fillAndSubmitForm(editorModel.get('currentValue'));
Chris@0 269 });
Chris@0 270 },
Chris@0 271
Chris@0 272 /**
Chris@0 273 * Shows validation error messages.
Chris@0 274 *
Chris@0 275 * Should be called when the state is changed to 'invalid'.
Chris@0 276 */
Chris@0 277 showValidationErrors() {
Chris@0 278 const $errors = $('<div class="quickedit-validation-errors"></div>')
Chris@0 279 .append(this.model.get('validationErrors'));
Chris@0 280 this.getEditedElement()
Chris@0 281 .addClass('quickedit-validation-error')
Chris@0 282 .after($errors);
Chris@0 283 },
Chris@0 284
Chris@0 285 /**
Chris@0 286 * Cleans up validation error messages.
Chris@0 287 *
Chris@0 288 * Should be called when the state is changed to 'candidate' or 'saving'. In
Chris@0 289 * the case of the latter: the user has modified the value in the in-place
Chris@0 290 * editor again to attempt to save again. In the case of the latter: the
Chris@0 291 * invalid value was discarded.
Chris@0 292 */
Chris@0 293 removeValidationErrors() {
Chris@0 294 this.getEditedElement()
Chris@0 295 .removeClass('quickedit-validation-error')
Chris@0 296 .next('.quickedit-validation-errors')
Chris@0 297 .remove();
Chris@0 298 },
Chris@0 299
Chris@0 300 });
Chris@0 301 }(jQuery, Backbone, Drupal));