From a3d027fd64801c72fe4e390e208af498bb59b636 Mon Sep 17 00:00:00 2001 From: alokhyland Date: Sun, 28 Apr 2024 12:05:28 +0530 Subject: [PATCH] ELEMENTS-1731: fix display of long username under activity section --- ui/widgets/nuxeo-user-tag.js | 88 +++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/ui/widgets/nuxeo-user-tag.js b/ui/widgets/nuxeo-user-tag.js index fcf27f033..ee1ed21df 100644 --- a/ui/widgets/nuxeo-user-tag.js +++ b/ui/widgets/nuxeo-user-tag.js @@ -19,6 +19,7 @@ import { html } from '@polymer/polymer/lib/utils/html-tag.js'; import { mixinBehaviors } from '@polymer/polymer/lib/legacy/class.js'; import '@nuxeo/nuxeo-elements/nuxeo-element.js'; import '@polymer/polymer/lib/elements/dom-if.js'; +import { IronResizableBehavior } from '@polymer/iron-resizable-behavior/iron-resizable-behavior.js'; import { RoutingBehavior } from '../nuxeo-routing-behavior.js'; import './nuxeo-tag.js'; import './nuxeo-user-avatar.js'; @@ -36,7 +37,7 @@ import './nuxeo-tooltip.js'; * @memberof Nuxeo * @demo demo/nuxeo-user-tag/index.html */ - class UserTag extends mixinBehaviors([RoutingBehavior], Nuxeo.Element) { + class UserTag extends mixinBehaviors([RoutingBehavior, IronResizableBehavior], Nuxeo.Element) { static get template() { return html`
@@ -75,11 +91,16 @@ import './nuxeo-tooltip.js'; font-size="10" font-weight="500" fetch-avatar$="[[fetchAvatar]]" + class="user-avatar" > @@ -94,6 +115,13 @@ import './nuxeo-tooltip.js'; + + +
`; @@ -177,6 +205,62 @@ import './nuxeo-tooltip.js'; _preventPropagation(e) { e.stopPropagation(); } + + _getUserTagClass(user) { + const userFullName = this._name(user); + const nameContainsWhiteSpace = /\s/.test(userFullName); + return nameContainsWhiteSpace ? 'user-tag-wrap' : 'user-tag-nowrap'; + } + + _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; + return elementWidth; + } + + _getHTMLRootNode(element) { + let currentElement = element; + while (currentElement.parentNode instanceof DocumentFragment) { + currentElement = currentElement.parentNode.host; + } + return currentElement.parentNode; + } + + connectedCallback() { + super.connectedCallback(); + this.addEventListener('dom-change', this._layout); + this.addEventListener('iron-resize', this._layout); + } + + disconnectedCallback() { + super.disconnectedCallback(); + this.removeEventListener('dom-change', this._layout); + this.removeEventListener('iron-resize', this._layout); + } + + _layout() { + if (this && this.parentNode) { + const selectedElement = this; + const parentElement = this._getHTMLRootNode(selectedElement); + let elementWidth = this._calculateElementWidth(parentElement); + const childNodes = Array.from(parentElement.children); + const userAvatar = Array.from(selectedElement.shadowRoot.querySelectorAll('.user-avatar')); + const userAvatarWidth = userAvatar[0].offsetWidth; + const totalAvatarWidth = userAvatar.length * userAvatarWidth; + const otherElementWidth = childNodes.reduce((totalWidth, currentValue) => { + if (currentValue !== this && !currentValue.shadowRoot) { + totalWidth += this._calculateElementWidth(currentValue); + } + return totalWidth; + }, 0); + elementWidth -= otherElementWidth + totalAvatarWidth; + const userTagElement = this.shadowRoot.querySelector('.username-container'); + if (userTagElement && elementWidth > 0) userTagElement.setAttribute('style', `max-width:${elementWidth}px`); + } + } } customElements.define(UserTag.is, UserTag);