From a7815253023c5aca2c05b6e1f7dcb791bec23b61 Mon Sep 17 00:00:00 2001 From: alokhyland Date: Sun, 28 Apr 2024 12:09:12 +0530 Subject: [PATCH] ELEMENTS-1731-BACKPORT: fix display of long username under activity section --- ui/widgets/nuxeo-user-tag.js | 90 ++++++++++++++++++++++++++++++++++-- 1 file changed, 86 insertions(+), 4 deletions(-) diff --git a/ui/widgets/nuxeo-user-tag.js b/ui/widgets/nuxeo-user-tag.js index d927e1916c..048a66849f 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` -
+
@@ -89,7 +113,7 @@ import './nuxeo-tooltip.js'; @@ -177,6 +201,64 @@ 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; + + // Element width and height minus padding and border + 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 toatlAvatarWidth = userAvatar.length * userAvatarWidth; + const otherElementWidth = childNodes.reduce((totalWidth, currentValue) => { + if (currentValue !== this && !currentValue.shadowRoot) { + totalWidth += this._calculateElementWidth(currentValue); + } + return totalWidth; + }, 0); + elementWidth -= otherElementWidth + toatlAvatarWidth; + const userTagElement = this.shadowRoot.querySelector('.username-container'); + if (userTagElement) userTagElement.setAttribute('style', `max-width:${elementWidth}px`); + } + } } customElements.define(UserTag.is, UserTag);