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