Chris@0: /** Chris@0: * @file Chris@0: * A Backbone View that decorates the in-place edited element. Chris@0: */ Chris@0: Chris@17: (function($, Backbone, Drupal) { Chris@17: Drupal.quickedit.FieldDecorationView = Backbone.View.extend( Chris@17: /** @lends Drupal.quickedit.FieldDecorationView# */ { Chris@17: /** Chris@17: * @type {null} Chris@17: */ Chris@17: _widthAttributeIsEmpty: null, Chris@0: Chris@17: /** Chris@17: * @type {object} Chris@17: */ Chris@17: events: { Chris@17: 'mouseenter.quickedit': 'onMouseEnter', Chris@17: 'mouseleave.quickedit': 'onMouseLeave', Chris@17: click: 'onClick', Chris@17: 'tabIn.quickedit': 'onMouseEnter', Chris@17: 'tabOut.quickedit': 'onMouseLeave', Chris@17: }, Chris@0: 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.EditorView} options.editorView Chris@17: * The editor object view. Chris@17: */ Chris@17: initialize(options) { Chris@17: this.editorView = options.editorView; Chris@0: Chris@17: this.listenTo(this.model, 'change:state', this.stateChange); Chris@17: this.listenTo( Chris@17: this.model, Chris@17: 'change:isChanged change:inTempStore', Chris@17: this.renderChanged, Chris@17: ); Chris@17: }, Chris@0: Chris@17: /** Chris@17: * @inheritdoc Chris@17: */ Chris@17: remove() { Chris@17: // The el property is the field, which should not be removed. Remove the Chris@17: // pointer to it, then call Backbone.View.prototype.remove(). Chris@17: this.setElement(); Chris@17: Backbone.View.prototype.remove.call(this); Chris@17: }, Chris@0: Chris@17: /** Chris@17: * Determines the actions to take given a change of state. Chris@17: * Chris@17: * @param {Drupal.quickedit.FieldModel} model Chris@17: * The `FieldModel` model. Chris@17: * @param {string} state Chris@17: * The state of the associated field. One of Chris@17: * {@link Drupal.quickedit.FieldModel.states}. Chris@17: */ Chris@17: stateChange(model, state) { Chris@17: const from = model.previous('state'); Chris@17: const to = state; Chris@17: switch (to) { Chris@17: case 'inactive': Chris@17: this.undecorate(); Chris@17: break; Chris@0: Chris@17: case 'candidate': Chris@17: this.decorate(); Chris@17: if (from !== 'inactive') { Chris@17: this.stopHighlight(); Chris@17: if (from !== 'highlighted') { Chris@17: this.model.set('isChanged', false); Chris@17: this.stopEdit(); Chris@17: } Chris@17: } Chris@17: this._unpad(); Chris@17: break; Chris@0: Chris@17: case 'highlighted': Chris@17: this.startHighlight(); Chris@17: break; Chris@17: Chris@17: case 'activating': Chris@17: // NOTE: this state is not used by every editor! It's only used by Chris@17: // those that need to interact with the server. Chris@17: this.prepareEdit(); Chris@17: break; Chris@17: Chris@17: case 'active': Chris@17: if (from !== 'activating') { Chris@17: this.prepareEdit(); Chris@0: } Chris@17: if (this.editorView.getQuickEditUISettings().padding) { Chris@17: this._pad(); Chris@17: } Chris@17: break; Chris@0: Chris@17: case 'changed': Chris@17: this.model.set('isChanged', true); Chris@17: break; Chris@0: Chris@17: case 'saving': Chris@17: break; Chris@0: Chris@17: case 'saved': Chris@17: break; Chris@0: Chris@17: case 'invalid': Chris@17: break; Chris@17: } Chris@17: }, Chris@0: Chris@17: /** Chris@17: * Adds a class to the edited element that indicates whether the field has Chris@17: * been changed by the user (i.e. locally) or the field has already been Chris@17: * changed and stored before by the user (i.e. remotely, stored in Chris@17: * PrivateTempStore). Chris@17: */ Chris@17: renderChanged() { Chris@17: this.$el.toggleClass( Chris@17: 'quickedit-changed', Chris@17: this.model.get('isChanged') || this.model.get('inTempStore'), Chris@17: ); Chris@17: }, Chris@0: Chris@17: /** Chris@17: * Starts hover; transitions to 'highlight' state. Chris@17: * Chris@17: * @param {jQuery.Event} event Chris@17: * The mouse event. Chris@17: */ Chris@17: onMouseEnter(event) { Chris@17: const that = this; Chris@17: that.model.set('state', 'highlighted'); Chris@17: event.stopPropagation(); Chris@17: }, Chris@0: Chris@17: /** Chris@17: * Stops hover; transitions to 'candidate' state. Chris@17: * Chris@17: * @param {jQuery.Event} event Chris@17: * The mouse event. Chris@17: */ Chris@17: onMouseLeave(event) { Chris@17: const that = this; Chris@17: that.model.set('state', 'candidate', { reason: 'mouseleave' }); Chris@17: event.stopPropagation(); Chris@17: }, Chris@0: Chris@17: /** Chris@17: * Transition to 'activating' stage. Chris@17: * Chris@17: * @param {jQuery.Event} event Chris@17: * The click event. Chris@17: */ Chris@17: onClick(event) { Chris@17: this.model.set('state', 'activating'); Chris@17: event.preventDefault(); Chris@17: event.stopPropagation(); Chris@17: }, Chris@0: Chris@17: /** Chris@17: * Adds classes used to indicate an elements editable state. Chris@17: */ Chris@17: decorate() { Chris@17: this.$el.addClass('quickedit-candidate quickedit-editable'); Chris@17: }, Chris@0: Chris@17: /** Chris@17: * Removes classes used to indicate an elements editable state. Chris@17: */ Chris@17: undecorate() { Chris@17: this.$el.removeClass( Chris@17: 'quickedit-candidate quickedit-editable quickedit-highlighted quickedit-editing', Chris@17: ); Chris@17: }, Chris@0: Chris@17: /** Chris@17: * Adds that class that indicates that an element is highlighted. Chris@17: */ Chris@17: startHighlight() { Chris@17: // Animations. Chris@17: const that = this; Chris@17: // Use a timeout to grab the next available animation frame. Chris@17: that.$el.addClass('quickedit-highlighted'); Chris@17: }, Chris@0: Chris@17: /** Chris@17: * Removes the class that indicates that an element is highlighted. Chris@17: */ Chris@17: stopHighlight() { Chris@17: this.$el.removeClass('quickedit-highlighted'); Chris@17: }, Chris@0: Chris@17: /** Chris@17: * Removes the class that indicates that an element as editable. Chris@17: */ Chris@17: prepareEdit() { Chris@17: this.$el.addClass('quickedit-editing'); Chris@0: Chris@17: // Allow the field to be styled differently while editing in a pop-up Chris@17: // in-place editor. Chris@17: if (this.editorView.getQuickEditUISettings().popup) { Chris@17: this.$el.addClass('quickedit-editor-is-popup'); Chris@17: } Chris@17: }, Chris@0: Chris@17: /** Chris@17: * Removes the class that indicates that an element is being edited. Chris@17: * Chris@17: * Reapplies the class that indicates that a candidate editable element is Chris@17: * again available to be edited. Chris@17: */ Chris@17: stopEdit() { Chris@17: this.$el.removeClass('quickedit-highlighted quickedit-editing'); Chris@0: Chris@17: // Done editing in a pop-up in-place editor; remove the class. Chris@17: if (this.editorView.getQuickEditUISettings().popup) { Chris@17: this.$el.removeClass('quickedit-editor-is-popup'); Chris@17: } Chris@0: Chris@17: // Make the other editors show up again. Chris@17: $('.quickedit-candidate').addClass('quickedit-editable'); Chris@17: }, Chris@0: Chris@17: /** Chris@17: * Adds padding around the editable element to make it pop visually. Chris@17: */ Chris@17: _pad() { Chris@17: // Early return if the element has already been padded. Chris@17: if (this.$el.data('quickedit-padded')) { Chris@17: return; Chris@17: } Chris@17: const self = this; Chris@0: Chris@17: // Add 5px padding for readability. This means we'll freeze the current Chris@17: // width and *then* add 5px padding, hence ensuring the padding is added Chris@17: // "on the outside". Chris@17: // 1) Freeze the width (if it's not already set); don't use animations. Chris@17: if (this.$el[0].style.width === '') { Chris@17: this._widthAttributeIsEmpty = true; Chris@17: this.$el Chris@17: .addClass('quickedit-animate-disable-width') Chris@17: .css('width', this.$el.width()); Chris@17: } Chris@0: Chris@17: // 2) Add padding; use animations. Chris@17: const posProp = this._getPositionProperties(this.$el); Chris@17: setTimeout(() => { Chris@17: // Re-enable width animations (padding changes affect width too!). Chris@17: self.$el.removeClass('quickedit-animate-disable-width'); Chris@0: Chris@17: // Pad the editable. Chris@17: self.$el Chris@17: .css({ Chris@17: position: 'relative', Chris@17: top: `${posProp.top - 5}px`, Chris@17: left: `${posProp.left - 5}px`, Chris@17: 'padding-top': `${posProp['padding-top'] + 5}px`, Chris@17: 'padding-left': `${posProp['padding-left'] + 5}px`, Chris@17: 'padding-right': `${posProp['padding-right'] + 5}px`, Chris@17: 'padding-bottom': `${posProp['padding-bottom'] + 5}px`, Chris@17: 'margin-bottom': `${posProp['margin-bottom'] - 10}px`, Chris@17: }) Chris@17: .data('quickedit-padded', true); Chris@17: }, 0); Chris@17: }, Chris@0: Chris@17: /** Chris@17: * Removes the padding around the element being edited when editing ceases. Chris@17: */ Chris@17: _unpad() { Chris@17: // Early return if the element has not been padded. Chris@17: if (!this.$el.data('quickedit-padded')) { Chris@17: return; Chris@17: } Chris@17: const self = this; Chris@0: Chris@17: // 1) Set the empty width again. Chris@17: if (this._widthAttributeIsEmpty) { Chris@17: this.$el.addClass('quickedit-animate-disable-width').css('width', ''); Chris@17: } Chris@0: Chris@17: // 2) Remove padding; use animations (these will run simultaneously with) Chris@17: // the fading out of the toolbar as its gets removed). Chris@17: const posProp = this._getPositionProperties(this.$el); Chris@17: setTimeout(() => { Chris@17: // Re-enable width animations (padding changes affect width too!). Chris@17: self.$el.removeClass('quickedit-animate-disable-width'); Chris@0: Chris@17: // Unpad the editable. Chris@17: self.$el.css({ Chris@0: position: 'relative', Chris@0: top: `${posProp.top + 5}px`, Chris@0: left: `${posProp.left + 5}px`, Chris@0: 'padding-top': `${posProp['padding-top'] - 5}px`, Chris@0: 'padding-left': `${posProp['padding-left'] - 5}px`, Chris@0: 'padding-right': `${posProp['padding-right'] - 5}px`, Chris@0: 'padding-bottom': `${posProp['padding-bottom'] - 5}px`, Chris@0: 'margin-bottom': `${posProp['margin-bottom'] + 10}px`, Chris@0: }); Chris@17: }, 0); Chris@17: // Remove the marker that indicates that this field has padding. This is Chris@17: // done outside the timed out function above so that we don't get numerous Chris@17: // queued functions that will remove padding before the data marker has Chris@17: // been removed. Chris@17: this.$el.removeData('quickedit-padded'); Chris@17: }, Chris@17: Chris@17: /** Chris@17: * Gets the top and left properties of an element. Chris@17: * Chris@17: * Convert extraneous values and information into numbers ready for Chris@17: * subtraction. Chris@17: * Chris@17: * @param {jQuery} $e Chris@17: * The element to get position properties from. Chris@17: * Chris@17: * @return {object} Chris@17: * An object containing css values for the needed properties. Chris@17: */ Chris@17: _getPositionProperties($e) { Chris@17: let p; Chris@17: const r = {}; Chris@17: const props = [ Chris@17: 'top', Chris@17: 'left', Chris@17: 'bottom', Chris@17: 'right', Chris@17: 'padding-top', Chris@17: 'padding-left', Chris@17: 'padding-right', Chris@17: 'padding-bottom', Chris@17: 'margin-bottom', Chris@17: ]; Chris@17: Chris@17: const propCount = props.length; Chris@17: for (let i = 0; i < propCount; i++) { Chris@17: p = props[i]; Chris@17: r[p] = parseInt(this._replaceBlankPosition($e.css(p)), 10); Chris@17: } Chris@17: return r; Chris@17: }, Chris@17: Chris@17: /** Chris@17: * Replaces blank or 'auto' CSS `position: ` values with "0px". Chris@17: * Chris@17: * @param {string} [pos] Chris@17: * The value for a CSS position declaration. Chris@17: * Chris@17: * @return {string} Chris@17: * A CSS value that is valid for `position`. Chris@17: */ Chris@17: _replaceBlankPosition(pos) { Chris@17: if (pos === 'auto' || !pos) { Chris@17: pos = '0px'; Chris@17: } Chris@17: return pos; Chris@17: }, Chris@0: }, Chris@17: ); Chris@17: })(jQuery, Backbone, Drupal);