Chris@0: /** Chris@0: * @file Chris@0: * A Backbone View that provides an entity level toolbar. Chris@0: */ Chris@0: Chris@4: (function($, _, Backbone, Drupal, debounce) { Chris@4: Drupal.quickedit.EntityToolbarView = Backbone.View.extend( Chris@4: /** @lends Drupal.quickedit.EntityToolbarView# */ { Chris@4: /** Chris@4: * @type {jQuery} Chris@4: */ Chris@4: _fieldToolbarRoot: null, Chris@0: Chris@4: /** Chris@4: * @return {object} Chris@4: * A map of events. Chris@4: */ Chris@4: events() { Chris@4: const map = { Chris@4: 'click button.action-save': 'onClickSave', Chris@4: 'click button.action-cancel': 'onClickCancel', Chris@4: mouseenter: 'onMouseenter', Chris@4: }; Chris@4: return map; Chris@4: }, Chris@0: Chris@4: /** Chris@4: * @constructs Chris@4: * Chris@4: * @augments Backbone.View Chris@4: * Chris@4: * @param {object} options Chris@4: * Options to construct the view. Chris@4: * @param {Drupal.quickedit.AppModel} options.appModel Chris@4: * A quickedit `AppModel` to use in the view. Chris@4: */ Chris@4: initialize(options) { Chris@4: const that = this; Chris@4: this.appModel = options.appModel; Chris@4: this.$entity = $(this.model.get('el')); Chris@0: Chris@4: // Rerender whenever the entity state changes. Chris@4: this.listenTo( Chris@4: this.model, Chris@4: 'change:isActive change:isDirty change:state', Chris@4: this.render, Chris@4: ); Chris@4: // Also rerender whenever a different field is highlighted or activated. Chris@4: this.listenTo( Chris@4: this.appModel, Chris@4: 'change:highlightedField change:activeField', Chris@4: this.render, Chris@4: ); Chris@4: // Rerender when a field of the entity changes state. Chris@4: this.listenTo( Chris@4: this.model.get('fields'), Chris@4: 'change:state', Chris@4: this.fieldStateChange, Chris@4: ); Chris@0: Chris@4: // Reposition the entity toolbar as the viewport and the position within Chris@4: // the viewport changes. Chris@4: $(window).on( Chris@4: 'resize.quickedit scroll.quickedit drupalViewportOffsetChange.quickedit', Chris@4: debounce($.proxy(this.windowChangeHandler, this), 150), Chris@4: ); Chris@0: Chris@4: // Adjust the fence placement within which the entity toolbar may be Chris@4: // positioned. Chris@4: $(document).on( Chris@4: 'drupalViewportOffsetChange.quickedit', Chris@4: (event, offsets) => { Chris@4: if (that.$fence) { Chris@4: that.$fence.css(offsets); Chris@4: } Chris@4: }, Chris@4: ); Chris@0: Chris@4: // Set the entity toolbar DOM element as the el for this view. Chris@4: const $toolbar = this.buildToolbarEl(); Chris@4: this.setElement($toolbar); Chris@4: this._fieldToolbarRoot = $toolbar Chris@4: .find('.quickedit-toolbar-field') Chris@4: .get(0); Chris@4: Chris@4: // Initial render. Chris@4: this.render(); Chris@4: }, Chris@4: Chris@4: /** Chris@4: * @inheritdoc Chris@4: * Chris@4: * @return {Drupal.quickedit.EntityToolbarView} Chris@4: * The entity toolbar view. Chris@4: */ Chris@4: render() { Chris@4: if (this.model.get('isActive')) { Chris@4: // If the toolbar container doesn't exist, create it. Chris@4: const $body = $('body'); Chris@4: if ($body.children('#quickedit-entity-toolbar').length === 0) { Chris@4: $body.append(this.$el); Chris@4: } Chris@4: // The fence will define a area on the screen that the entity toolbar Chris@4: // will be position within. Chris@4: if ($body.children('#quickedit-toolbar-fence').length === 0) { Chris@4: this.$fence = $(Drupal.theme('quickeditEntityToolbarFence')) Chris@4: .css(Drupal.displace()) Chris@4: .appendTo($body); Chris@4: } Chris@4: // Adds the entity title to the toolbar. Chris@4: this.label(); Chris@4: Chris@4: // Show the save and cancel buttons. Chris@4: this.show('ops'); Chris@4: // If render is being called and the toolbar is already visible, just Chris@4: // reposition it. Chris@4: this.position(); Chris@0: } Chris@0: Chris@4: // The save button text and state varies with the state of the entity Chris@4: // model. Chris@4: const $button = this.$el.find('.quickedit-button.action-save'); Chris@4: const isDirty = this.model.get('isDirty'); Chris@4: // Adjust the save button according to the state of the model. Chris@4: switch (this.model.get('state')) { Chris@4: // Quick editing is active, but no field is being edited. Chris@4: case 'opened': Chris@4: // The saving throbber is not managed by AJAX system. The Chris@4: // EntityToolbarView manages this visual element. Chris@4: $button Chris@4: .removeClass('action-saving icon-throbber icon-end') Chris@4: .text(Drupal.t('Save')) Chris@4: .removeAttr('disabled') Chris@4: .attr('aria-hidden', !isDirty); Chris@0: break; Chris@0: Chris@4: // The changes to the fields of the entity are being committed. Chris@4: case 'committing': Chris@4: $button Chris@4: .addClass('action-saving icon-throbber icon-end') Chris@4: .text(Drupal.t('Saving')) Chris@4: .attr('disabled', 'disabled'); Chris@0: break; Chris@0: Chris@4: default: Chris@4: $button.attr('aria-hidden', true); Chris@4: break; Chris@4: } Chris@4: Chris@4: return this; Chris@4: }, Chris@4: Chris@4: /** Chris@4: * @inheritdoc Chris@4: */ Chris@4: remove() { Chris@4: // Remove additional DOM elements controlled by this View. Chris@4: this.$fence.remove(); Chris@4: Chris@4: // Stop listening to additional events. Chris@4: $(window).off( Chris@4: 'resize.quickedit scroll.quickedit drupalViewportOffsetChange.quickedit', Chris@4: ); Chris@4: $(document).off('drupalViewportOffsetChange.quickedit'); Chris@4: Chris@4: Backbone.View.prototype.remove.call(this); Chris@4: }, Chris@4: Chris@4: /** Chris@4: * Repositions the entity toolbar on window scroll and resize. Chris@4: * Chris@4: * @param {jQuery.Event} event Chris@4: * The scroll or resize event. Chris@4: */ Chris@4: windowChangeHandler(event) { Chris@4: this.position(); Chris@4: }, Chris@4: Chris@4: /** Chris@4: * Determines the actions to take given a change of state. Chris@4: * Chris@4: * @param {Drupal.quickedit.FieldModel} model Chris@4: * The `FieldModel` model. Chris@4: * @param {string} state Chris@4: * The state of the associated field. One of Chris@4: * {@link Drupal.quickedit.FieldModel.states}. Chris@4: */ Chris@4: fieldStateChange(model, state) { Chris@4: switch (state) { Chris@4: case 'active': Chris@4: this.render(); Chris@0: break; Chris@0: Chris@4: case 'invalid': Chris@4: this.render(); Chris@0: break; Chris@0: } Chris@4: }, Chris@0: Chris@0: /** Chris@4: * Uses the jQuery.ui.position() method to position the entity toolbar. Chris@0: * Chris@4: * @param {HTMLElement} [element] Chris@4: * The element against which the entity toolbar is positioned. Chris@0: */ Chris@4: position(element) { Chris@4: clearTimeout(this.timer); Chris@4: Chris@4: const that = this; Chris@4: // Vary the edge of the positioning according to the direction of language Chris@4: // in the document. Chris@4: const edge = document.documentElement.dir === 'rtl' ? 'right' : 'left'; Chris@4: // A time unit to wait until the entity toolbar is repositioned. Chris@4: let delay = 0; Chris@4: // Determines what check in the series of checks below should be Chris@4: // evaluated. Chris@4: let check = 0; Chris@4: // When positioned against an active field that has padding, we should Chris@4: // ignore that padding when positioning the toolbar, to not unnecessarily Chris@4: // move the toolbar horizontally, which feels annoying. Chris@4: let horizontalPadding = 0; Chris@4: let of; Chris@4: let activeField; Chris@4: let highlightedField; Chris@4: // There are several elements in the page that the entity toolbar might be Chris@4: // positioned against. They are considered below in a priority order. Chris@4: do { Chris@4: switch (check) { Chris@4: case 0: Chris@4: // Position against a specific element. Chris@4: of = element; Chris@4: break; Chris@4: Chris@4: case 1: Chris@4: // Position against a form container. Chris@4: activeField = Drupal.quickedit.app.model.get('activeField'); Chris@4: of = Chris@4: activeField && Chris@4: activeField.editorView && Chris@4: activeField.editorView.$formContainer && Chris@4: activeField.editorView.$formContainer.find('.quickedit-form'); Chris@4: break; Chris@4: Chris@4: case 2: Chris@4: // Position against an active field. Chris@4: of = Chris@4: activeField && Chris@4: activeField.editorView && Chris@4: activeField.editorView.getEditedElement(); Chris@4: if ( Chris@4: activeField && Chris@4: activeField.editorView && Chris@4: activeField.editorView.getQuickEditUISettings().padding Chris@4: ) { Chris@4: horizontalPadding = 5; Chris@4: } Chris@4: break; Chris@4: Chris@4: case 3: Chris@4: // Position against a highlighted field. Chris@4: highlightedField = Drupal.quickedit.app.model.get( Chris@4: 'highlightedField', Chris@4: ); Chris@4: of = Chris@4: highlightedField && Chris@4: highlightedField.editorView && Chris@4: highlightedField.editorView.getEditedElement(); Chris@4: delay = 250; Chris@4: break; Chris@4: Chris@4: default: { Chris@4: const fieldModels = this.model.get('fields').models; Chris@4: let topMostPosition = 1000000; Chris@4: let topMostField = null; Chris@4: // Position against the topmost field. Chris@4: for (let i = 0; i < fieldModels.length; i++) { Chris@4: const pos = fieldModels[i].get('el').getBoundingClientRect() Chris@4: .top; Chris@4: if (pos < topMostPosition) { Chris@4: topMostPosition = pos; Chris@4: topMostField = fieldModels[i]; Chris@4: } Chris@4: } Chris@4: of = topMostField.get('el'); Chris@4: delay = 50; Chris@4: break; Chris@4: } Chris@0: } Chris@4: // Prepare to check the next possible element to position against. Chris@4: check++; Chris@4: } while (!of); Chris@4: Chris@4: /** Chris@4: * Refines the positioning algorithm of jquery.ui.position(). Chris@4: * Chris@4: * Invoked as the 'using' callback of jquery.ui.position() in Chris@4: * positionToolbar(). Chris@4: * Chris@4: * @param {*} view Chris@4: * The view the positions will be calculated from. Chris@4: * @param {object} suggested Chris@4: * A hash of top and left values for the position that should be set. It Chris@4: * can be forwarded to .css() or .animate(). Chris@4: * @param {object} info Chris@4: * The position and dimensions of both the 'my' element and the 'of' Chris@4: * elements, as well as calculations to their relative position. This Chris@4: * object contains the following properties: Chris@4: * @param {object} info.element Chris@4: * A hash that contains information about the HTML element that will be Chris@4: * positioned. Also known as the 'my' element. Chris@4: * @param {object} info.target Chris@4: * A hash that contains information about the HTML element that the Chris@4: * 'my' element will be positioned against. Also known as the 'of' Chris@4: * element. Chris@4: */ Chris@4: function refinePosition(view, suggested, info) { Chris@4: // Determine if the pointer should be on the top or bottom. Chris@4: const isBelow = suggested.top > info.target.top; Chris@4: info.element.element.toggleClass( Chris@4: 'quickedit-toolbar-pointer-top', Chris@4: isBelow, Chris@4: ); Chris@4: // Don't position the toolbar past the first or last editable field if Chris@4: // the entity is the target. Chris@4: if (view.$entity[0] === info.target.element[0]) { Chris@4: // Get the first or last field according to whether the toolbar is Chris@4: // above or below the entity. Chris@4: const $field = view.$entity Chris@4: .find('.quickedit-editable') Chris@4: .eq(isBelow ? -1 : 0); Chris@4: if ($field.length > 0) { Chris@4: suggested.top = isBelow Chris@4: ? $field.offset().top + $field.outerHeight(true) Chris@4: : $field.offset().top - info.element.element.outerHeight(true); Chris@4: } Chris@4: } Chris@4: // Don't let the toolbar go outside the fence. Chris@4: const fenceTop = view.$fence.offset().top; Chris@4: const fenceHeight = view.$fence.height(); Chris@4: const toolbarHeight = info.element.element.outerHeight(true); Chris@4: if (suggested.top < fenceTop) { Chris@4: suggested.top = fenceTop; Chris@4: } else if (suggested.top + toolbarHeight > fenceTop + fenceHeight) { Chris@4: suggested.top = fenceTop + fenceHeight - toolbarHeight; Chris@4: } Chris@4: // Position the toolbar. Chris@4: info.element.element.css({ Chris@4: left: Math.floor(suggested.left), Chris@4: top: Math.floor(suggested.top), Chris@4: }); Chris@0: } Chris@4: Chris@4: /** Chris@4: * Calls the jquery.ui.position() method on the $el of this view. Chris@4: */ Chris@4: function positionToolbar() { Chris@4: that.$el Chris@4: .position({ Chris@4: my: `${edge} bottom`, Chris@4: // Move the toolbar 1px towards the start edge of the 'of' element, Chris@4: // plus any horizontal padding that may have been added to the Chris@4: // element that is being added, to prevent unwanted horizontal Chris@4: // movement. Chris@4: at: `${edge}+${1 + horizontalPadding} top`, Chris@4: of, Chris@4: collision: 'flipfit', Chris@4: using: refinePosition.bind(null, that), Chris@4: within: that.$fence, Chris@4: }) Chris@4: // Resize the toolbar to match the dimensions of the field, up to a Chris@4: // maximum width that is equal to 90% of the field's width. Chris@4: .css({ Chris@4: 'max-width': Chris@4: document.documentElement.clientWidth < 450 Chris@4: ? document.documentElement.clientWidth Chris@4: : 450, Chris@4: // Set a minimum width of 240px for the entity toolbar, or the width Chris@4: // of the client if it is less than 240px, so that the toolbar Chris@4: // never folds up into a squashed and jumbled mess. Chris@4: 'min-width': Chris@4: document.documentElement.clientWidth < 240 Chris@4: ? document.documentElement.clientWidth Chris@4: : 240, Chris@4: width: '100%', Chris@4: }); Chris@0: } Chris@4: Chris@4: // Uses the jQuery.ui.position() method. Use a timeout to move the toolbar Chris@4: // only after the user has focused on an editable for 250ms. This prevents Chris@4: // the toolbar from jumping around the screen. Chris@4: this.timer = setTimeout(() => { Chris@4: // Render the position in the next execution cycle, so that animations Chris@4: // on the field have time to process. This is not strictly speaking, a Chris@4: // guarantee that all animations will be finished, but it's a simple Chris@4: // way to get better positioning without too much additional code. Chris@4: _.defer(positionToolbar); Chris@4: }, delay); Chris@4: }, Chris@0: Chris@0: /** Chris@4: * Set the model state to 'saving' when the save button is clicked. Chris@4: * Chris@4: * @param {jQuery.Event} event Chris@4: * The click event. Chris@0: */ Chris@4: onClickSave(event) { Chris@4: event.stopPropagation(); Chris@4: event.preventDefault(); Chris@4: // Save the model. Chris@4: this.model.set('state', 'committing'); Chris@4: }, Chris@0: Chris@4: /** Chris@4: * Sets the model state to candidate when the cancel button is clicked. Chris@4: * Chris@4: * @param {jQuery.Event} event Chris@4: * The click event. Chris@4: */ Chris@4: onClickCancel(event) { Chris@4: event.preventDefault(); Chris@4: this.model.set('state', 'deactivating'); Chris@4: }, Chris@0: Chris@4: /** Chris@4: * Clears the timeout that will eventually reposition the entity toolbar. Chris@4: * Chris@4: * Without this, it may reposition itself, away from the user's cursor! Chris@4: * Chris@4: * @param {jQuery.Event} event Chris@4: * The mouse event. Chris@4: */ Chris@4: onMouseenter(event) { Chris@4: clearTimeout(this.timer); Chris@4: }, Chris@0: Chris@4: /** Chris@4: * Builds the entity toolbar HTML; attaches to DOM; sets starting position. Chris@4: * Chris@4: * @return {jQuery} Chris@4: * The toolbar element. Chris@4: */ Chris@4: buildToolbarEl() { Chris@4: const $toolbar = $( Chris@4: Drupal.theme('quickeditEntityToolbar', { Chris@4: id: 'quickedit-entity-toolbar', Chris@4: }), Chris@4: ); Chris@0: Chris@4: $toolbar Chris@4: .find('.quickedit-toolbar-entity') Chris@4: // Append the "ops" toolgroup into the toolbar. Chris@4: .prepend( Chris@4: Drupal.theme('quickeditToolgroup', { Chris@4: classes: ['ops'], Chris@4: buttons: [ Chris@4: { Chris@4: label: Drupal.t('Save'), Chris@4: type: 'submit', Chris@4: classes: 'action-save quickedit-button icon', Chris@4: attributes: { Chris@4: 'aria-hidden': true, Chris@4: }, Chris@4: }, Chris@4: { Chris@4: label: Drupal.t('Close'), Chris@4: classes: Chris@4: 'action-cancel quickedit-button icon icon-close icon-only', Chris@4: }, Chris@4: ], Chris@4: }), Chris@4: ); Chris@0: Chris@4: // Give the toolbar a sensible starting position so that it doesn't Chris@4: // animate on to the screen from a far off corner. Chris@4: $toolbar.css({ Chris@0: left: this.$entity.offset().left, Chris@0: top: this.$entity.offset().top, Chris@0: }); Chris@0: Chris@4: return $toolbar; Chris@4: }, Chris@4: Chris@4: /** Chris@4: * Returns the DOM element that fields will attach their toolbars to. Chris@4: * Chris@4: * @return {jQuery} Chris@4: * The DOM element that fields will attach their toolbars to. Chris@4: */ Chris@4: getToolbarRoot() { Chris@4: return this._fieldToolbarRoot; Chris@4: }, Chris@4: Chris@4: /** Chris@4: * Generates a state-dependent label for the entity toolbar. Chris@4: */ Chris@4: label() { Chris@4: // The entity label. Chris@4: let label = ''; Chris@4: const entityLabel = this.model.get('label'); Chris@4: Chris@4: // Label of an active field, if it exists. Chris@4: const activeField = Drupal.quickedit.app.model.get('activeField'); Chris@4: const activeFieldLabel = Chris@4: activeField && activeField.get('metadata').label; Chris@4: // Label of a highlighted field, if it exists. Chris@4: const highlightedField = Drupal.quickedit.app.model.get( Chris@4: 'highlightedField', Chris@4: ); Chris@4: const highlightedFieldLabel = Chris@4: highlightedField && highlightedField.get('metadata').label; Chris@4: // The label is constructed in a priority order. Chris@4: if (activeFieldLabel) { Chris@4: label = Drupal.theme('quickeditEntityToolbarLabel', { Chris@4: entityLabel, Chris@4: fieldLabel: activeFieldLabel, Chris@4: }); Chris@4: } else if (highlightedFieldLabel) { Chris@4: label = Drupal.theme('quickeditEntityToolbarLabel', { Chris@4: entityLabel, Chris@4: fieldLabel: highlightedFieldLabel, Chris@4: }); Chris@4: } else { Chris@4: // @todo Add XSS regression test coverage in https://www.drupal.org/node/2547437 Chris@4: label = Drupal.checkPlain(entityLabel); Chris@4: } Chris@4: Chris@4: this.$el.find('.quickedit-toolbar-label').html(label); Chris@4: }, Chris@4: Chris@4: /** Chris@4: * Adds classes to a toolgroup. Chris@4: * Chris@4: * @param {string} toolgroup Chris@4: * A toolgroup name. Chris@4: * @param {string} classes Chris@4: * A string of space-delimited class names that will be applied to the Chris@4: * wrapping element of the toolbar group. Chris@4: */ Chris@4: addClass(toolgroup, classes) { Chris@4: this._find(toolgroup).addClass(classes); Chris@4: }, Chris@4: Chris@4: /** Chris@4: * Removes classes from a toolgroup. Chris@4: * Chris@4: * @param {string} toolgroup Chris@4: * A toolgroup name. Chris@4: * @param {string} classes Chris@4: * A string of space-delimited class names that will be removed from the Chris@4: * wrapping element of the toolbar group. Chris@4: */ Chris@4: removeClass(toolgroup, classes) { Chris@4: this._find(toolgroup).removeClass(classes); Chris@4: }, Chris@4: Chris@4: /** Chris@4: * Finds a toolgroup. Chris@4: * Chris@4: * @param {string} toolgroup Chris@4: * A toolgroup name. Chris@4: * Chris@4: * @return {jQuery} Chris@4: * The toolgroup DOM element. Chris@4: */ Chris@4: _find(toolgroup) { Chris@4: return this.$el.find( Chris@4: `.quickedit-toolbar .quickedit-toolgroup.${toolgroup}`, Chris@4: ); Chris@4: }, Chris@4: Chris@4: /** Chris@4: * Shows a toolgroup. Chris@4: * Chris@4: * @param {string} toolgroup Chris@4: * A toolgroup name. Chris@4: */ Chris@4: show(toolgroup) { Chris@4: this.$el.removeClass('quickedit-animate-invisible'); Chris@4: }, Chris@0: }, Chris@4: ); Chris@4: })(jQuery, _, Backbone, Drupal, Drupal.debounce);