Ensure that `Owned Files` is not switchable when no files are owned When plugin registers itself for tab display even if `nothing` is returned Gerrit still shows the pointer and allows clicking at it, redirecting to the empty tab when done. Traverse through the shadowRoots down to the tab and disable/enable it when needed. As a result it is no longer possible to select an empty tab without content. Change-Id: Ib9da5fe0ed8ba564f8d62de2f8b13179ae232a4e
diff --git a/owners/web/gr-owned-files.ts b/owners/web/gr-owned-files.ts index 5f5d919..56274d4 100644 --- a/owners/web/gr-owned-files.ts +++ b/owners/web/gr-owned-files.ts
@@ -71,11 +71,36 @@ @customElement(OWNED_FILES_TAB_HEADER) export class OwnedFilesTabHeader extends OwnedFilesCommon { static override get styles() { - return [...OwnedFilesCommon.commonStyles()]; + return [ + ...OwnedFilesCommon.commonStyles(), + css` + [hidden] { + display: none; + } + `, + ]; } override render() { - if (this.hidden) return nothing; + // even if `nothing` is returned Gerrit still shows the pointer and allows + // clicking at it, redirecting to the empty tab when done; traverse through + // the shadowRoots down to the tab and disable/enable it when needed + const tabParent = document + .querySelector('#pg-app') + ?.shadowRoot?.querySelector('#app-element') + ?.shadowRoot?.querySelector('main > gr-change-view') + ?.shadowRoot?.querySelector( + '#tabs > paper-tab[data-name="change-view-tab-header-owners"]' + ); + if (this.hidden) { + if (tabParent && !tabParent.getAttribute('disabled')) { + tabParent.setAttribute('disabled', 'disabled'); + } + return nothing; + } + if (tabParent && tabParent.getAttribute('disabled')) { + tabParent.removeAttribute('disabled'); + } return html`<div>Owned Files</div>`; } }