From 6007fc2754244a1cc6cf5acc8c156d1640139506 Mon Sep 17 00:00:00 2001 From: alokhyland Date: Wed, 12 Jun 2024 15:37:04 +0530 Subject: [PATCH] ELEMENTS-1672: Apply text-overflow and tooltip to multiple selected items in nuxeo-selectivity --- ui/widgets/nuxeo-selectivity.js | 78 ++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 6 deletions(-) diff --git a/ui/widgets/nuxeo-selectivity.js b/ui/widgets/nuxeo-selectivity.js index 4f3f5ba69..83e570a0e 100644 --- a/ui/widgets/nuxeo-selectivity.js +++ b/ui/widgets/nuxeo-selectivity.js @@ -25,6 +25,8 @@ import { html } from '@polymer/polymer/lib/utils/html-tag.js'; import { mixinBehaviors } from '@polymer/polymer/lib/legacy/class.js'; import {I18nBehavior} from "../nuxeo-i18n-behavior"; import '@polymer/iron-icon/iron-icon.js'; +import { IronResizableBehavior } from '@polymer/iron-resizable-behavior/iron-resizable-behavior.js'; +import './nuxeo-tooltip.js'; /** * @license @@ -2439,7 +2441,8 @@ typedArrayTags[weakMapTag] = false; resultsHtml = this.selectivity.template('tagExists'); } else if (!resultsHtml && !options.add) { resultsHtml = this.selectivity.template('noResults', { term: options.term }); - } + } + if (resultsHtml) { this.resultsContainer.innerHTML = ''; removeElement(this.$('.selectivity-loading')); @@ -3430,8 +3433,42 @@ typedArrayTags[weakMapTag] = false; )); this.input.parentNode.insertBefore(el, this.input); + const textElement = el.querySelector('.multipleSelectedItemText'); + if(textElement){ + const containerElement = this._getHTMLRootNode(textElement) + let containerElementWidth = this._calculateElementWidth(containerElement) + const childNodes = Array.from(textElement.children); + const otherElementWidth = childNodes.reduce((totalWidth, currentValue) => { + if (currentValue !== this && !currentValue.shadowRoot) { + totalWidth += currentValue.offsetWidth; + } + return totalWidth; + }, 1); + containerElementWidth -= otherElementWidth ; + el.setAttribute('style', `max-width:${containerElementWidth}px`); + } + + }, + + _getHTMLRootNode(element){ + let currentElement = element; + const parentElement = currentElement.closest(".selectivity-multiple-input-container") + return parentElement; }, + + + _calculateElementWidth(element){ + const currrentElement = getComputedStyle(element); + const paddingX = parseFloat(currrentElement.paddingLeft) + parseFloat(currrentElement.paddingRight); + const borderX = parseFloat(currrentElement.borderLeftWidth) + parseFloat(currrentElement.borderRightWidth); + const scrollBarWidth = element.offsetWidth - element.clientWidth; + const elementWidth = (element .offsetWidth - paddingX - borderX - scrollBarWidth -2) ; + return elementWidth + }, + + + /** * @private */ @@ -5756,7 +5793,6 @@ typedArrayTags[weakMapTag] = false; newValue, (data) => { this._data = this.validateData(data); - if (options.triggerChange !== false) { this.triggerChange(); } @@ -6582,7 +6618,7 @@ typedArrayTags[weakMapTag] = false; * @demo demo/nuxeo-selectivity/index.html */ class SelectivityElement - extends mixinBehaviors([I18nBehavior, IronFormElementBehavior, IronValidatableBehavior], Nuxeo.Element) { + extends mixinBehaviors([I18nBehavior, IronFormElementBehavior, IronValidatableBehavior, IronResizableBehavior], Nuxeo.Element) { static get is() { return 'nuxeo-selectivity'; @@ -7107,6 +7143,11 @@ typedArrayTags[weakMapTag] = false; height: 2px; background-color: var(--nuxeo-primary-color, #0066ff); } + .selectivity-text-container { + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + } @@ -7170,10 +7211,18 @@ typedArrayTags[weakMapTag] = false; multipleSelectedItem: (opts) => { const extraClass = opts.highlighted ? ' highlighted' : ''; - return `${opts.removable ? + const toolTipElement = ` + ${this.selectionFormatter(opts.item || opts)} + `; + return (` + + ${opts.removable ? `` - : ``}${this.selectionFormatter(opts.item || opts)}` + : ``}${this.selectionFormatter(opts.item || opts)} + + ${toolTipElement} + `) }, }, }; @@ -7188,6 +7237,7 @@ typedArrayTags[weakMapTag] = false; options.allowClear = !this.required && !this.multiple; // allowClear only works on single-value mode } + if (this.data) { options.items = this._wrap(this.data); } @@ -7264,8 +7314,12 @@ typedArrayTags[weakMapTag] = false; }); this._visibilityObserver.observe(this); this._readonlyChanged(); + this.multiple && this._scrollParent.addEventListener('dialog-opened',this._reCalculateWidth.bind(this)) + this.multiple && this.addEventListener('iron-resize', this._reCalculateWidth); } + + disconnectedCallback() { this.$.input.removeEventListener('selectivity-change', this._updateSelectionHandler); this._updateSelectionHandler = null; @@ -7273,9 +7327,21 @@ typedArrayTags[weakMapTag] = false; this._selectivity = null; this._visibilityObserver.unobserve(this); this._scrollParent.removeEventListener('scroll', this._updateDropdownPosition.bind(this)); + this.multiple && this._scrollParent.removeEventListener('dialog-opened',this._reCalculateWidth.bind(this)) + this.multiple && this.removeEventListener('iron-resize', this._reCalculateWidth); super.disconnectedCallback(); } + + _reCalculateWidth(){ + setTimeout(() => { + this.multiple && + this._selectivity && + this._selectivity.rerenderSelection(); + }, 0); + + } + _updateDropdownPosition() { if (this._selectivity) { this._selectivity.positionDropdown();