{"version":3,"file":"SidebarTab.mjs","sources":["../../../lib/ui/sidebar/SidebarTab.ts"],"sourcesContent":["/*\n * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport type { IFolder, INode } from '../../node/index.ts'\nimport type { IView } from '../navigation/view.ts'\n\nimport isSvg from 'is-svg'\nimport { scopedGlobals } from '../../globalScope.ts'\nimport logger from '../../utils/logger.ts'\n\nexport interface ISidebarContext {\n\t/**\n\t * The active node in the sidebar\n\t */\n\tnode: INode\n\n\t/**\n\t * The current open folder in the files app.\n\t *\n\t * Only set if the sidebar is rendered within the files app,\n\t * apps rendering the sidebar within their own app have no open folder.\n\t */\n\tfolder?: IFolder\n\n\t/**\n\t * The currently active view.\n\t *\n\t * Only set if the sidebar is rendered within the files app,\n\t * apps rendering the sidebar within their own app have no active view.\n\t */\n\tview?: IView\n}\n\n/**\n * This component describes the custom web component that should be registered for a sidebar tab.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/Web_components\n * @see https://vuejs.org/guide/extras/web-components#building-custom-elements-with-vue\n */\nexport interface SidebarTabComponent extends ISidebarContext {\n\t/**\n\t * The active state of the sidebar tab.\n\t * It will be set to true if this component is the currently active tab.\n\t */\n\tactive: boolean\n}\n\n/**\n * The instance type of a sidebar tab web component.\n */\nexport type SidebarTabComponentInstance = SidebarTabComponent & HTMLElement\n\n/**\n * Implementation of a custom sidebar tab within the files app.\n */\nexport interface ISidebarTab {\n\t/**\n\t * Unique id of the sidebar tab.\n\t * This has to conform to the HTML id attribute specification.\n\t */\n\tid: string\n\n\t/**\n\t * The localized name of the sidebar tab.\n\t */\n\tdisplayName: string\n\n\t/**\n\t * The icon, as SVG, of the sidebar tab.\n\t */\n\ticonSvgInline: string\n\n\t/**\n\t * The order of this tab.\n\t * Use a low number to make this tab ordered in front.\n\t */\n\torder: number\n\n\t/**\n\t * The tag name of the web component.\n\t *\n\t * The web component must be defined using this name with `CustomElementRegistry.define()`,\n\t * either on initialization or within the `onInit` callback (preferred).\n\t * When rendering the sidebar tab, the files app will wait for the component to be defined in the registry (`customElements.whenDefined()`).\n\t *\n\t * To avoid name clashes the name has to start with your appid (e.g. `your_app`).\n\t * So in addition with the web component naming rules a good name would be `your_app-files-sidebar-tab`.\n\t */\n\ttagName: string\n\n\t/**\n\t * Callback to check if the sidebar tab should be shown for the selected node.\n\t *\n\t * If not provided, the tab will always be shown.\n\t *\n\t * @param context - The current context of the files app\n\t */\n\tenabled?: (context: ISidebarContext) => boolean\n\n\t/**\n\t * Called when the sidebar tab is active and rendered the first time in the sidebar.\n\t * This should be used to register the web componen (`CustomElementRegistry.define()`).\n\t *\n\t * The sidebar itself will anyways wait for the component to be defined in the registry (`customElements.whenDefined()`).\n\t * But also will wait for the promise returned by this method to resolve before rendering the tab.\n\t */\n\tonInit?: () => Promise<void>\n}\n\n/**\n * Register a new sidebar tab for the files app.\n *\n * @param tab - The sidebar tab to register\n * @throws {Error} If the provided tab is not a valid sidebar tab and thus cannot be registered.\n */\nexport function registerSidebarTab(tab: ISidebarTab): void {\n\tvalidateSidebarTab(tab)\n\n\tscopedGlobals.filesSidebarTabs ??= new Map<string, ISidebarTab>()\n\tif (scopedGlobals.filesSidebarTabs.has(tab.id)) {\n\t\tlogger.warn(`Sidebar tab with id \"${tab.id}\" already registered. Skipping.`)\n\t\treturn\n\t}\n\tscopedGlobals.filesSidebarTabs.set(tab.id, tab)\n\tlogger.debug(`New sidebar tab with id \"${tab.id}\" registered.`)\n}\n\n/**\n * Get all currently registered sidebar tabs.\n */\nexport function getSidebarTabs(): ISidebarTab[] {\n\tif (scopedGlobals.filesSidebarTabs) {\n\t\treturn [...scopedGlobals.filesSidebarTabs.values()]\n\t}\n\treturn []\n}\n\n/**\n * Check if a given sidebar tab objects implements all necessary fields.\n *\n * @param tab - The sidebar tab to validate\n * @throws {Error} If the provided tab is not valid\n */\nfunction validateSidebarTab(tab: ISidebarTab): void {\n\tif (typeof tab !== 'object') {\n\t\tthrow new Error('Sidebar tab is not an object')\n\t}\n\n\tif (!tab.id || (typeof tab.id !== 'string') || tab.id !== CSS.escape(tab.id)) {\n\t\tthrow new Error('Sidebar tabs need to have an id conforming to the HTML id attribute specifications')\n\t}\n\n\tif (!tab.tagName || typeof tab.tagName !== 'string') {\n\t\tthrow new Error('Sidebar tabs need to have the tagName name set')\n\t}\n\n\tif (!tab.tagName.match(/^[a-z][a-z0-9-_]+$/)) {\n\t\tthrow new Error('Sidebar tab \"tagName\" is invalid')\n\t}\n\n\tif (!tab.displayName || typeof tab.displayName !== 'string') {\n\t\tthrow new Error('Sidebar tabs need to have a name set')\n\t}\n\n\tif (typeof tab.iconSvgInline !== 'string' || !isSvg(tab.iconSvgInline)) {\n\t\tthrow new Error('Sidebar tabs need to have an valid SVG icon')\n\t}\n\n\tif (typeof tab.order !== 'number') {\n\t\tthrow new Error('Sidebar tabs need to have a numeric order set')\n\t}\n\n\tif (tab.enabled && typeof tab.enabled !== 'function') {\n\t\tthrow new Error('Sidebar tab \"enabled\" is not a function')\n\t}\n\n\tif (tab.onInit && typeof tab.onInit !== 'function') {\n\t\tthrow new Error('Sidebar tab \"onInit\" is not a function')\n\t}\n}\n"],"names":[],"mappings":";;;AAqHO,SAAS,mBAAmB,KAAwB;AAC1D,qBAAmB,GAAG;AAEtB,gBAAc,yCAAyB,IAAA;AACvC,MAAI,cAAc,iBAAiB,IAAI,IAAI,EAAE,GAAG;AAC/C,WAAO,KAAK,wBAAwB,IAAI,EAAE,iCAAiC;AAC3E;AAAA,EACD;AACA,gBAAc,iBAAiB,IAAI,IAAI,IAAI,GAAG;AAC9C,SAAO,MAAM,4BAA4B,IAAI,EAAE,eAAe;AAC/D;AAKO,SAAS,iBAAgC;AAC/C,MAAI,cAAc,kBAAkB;AACnC,WAAO,CAAC,GAAG,cAAc,iBAAiB,QAAQ;AAAA,EACnD;AACA,SAAO,CAAA;AACR;AAQA,SAAS,mBAAmB,KAAwB;AACnD,MAA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