{"version":3,"file":"navigation.mjs","sources":["../../../lib/ui/navigation/navigation.ts"],"sourcesContent":["/**\n * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport type { IView } from './view.ts'\n\nimport { TypedEventTarget } from 'typescript-event-target'\nimport { scopedGlobals } from '../../globalScope.ts'\nimport { validateView } from './view.ts'\n\n/**\n * The event is emitted when the navigation view was updated.\n * It contains the new active view in the `detail` attribute.\n */\ninterface UpdateActiveViewEvent extends CustomEvent<IView | null> {\n\ttype: 'updateActive'\n}\n\n/**\n * This event is emitted when the list of registered views is changed\n */\ninterface UpdateViewsEvent extends CustomEvent<never> {\n\ttype: 'update'\n}\n\n/**\n * The files navigation manages the available and active views\n *\n * Custom views for the files app can be registered (examples are the favorites views or the shared-with-you view).\n * It is also possible to listen on changes of the registered views or when the current active view is changed.\n *\n * @example\n * ```js\n * const navigation = getNavigation()\n * navigation.addEventListener('update', () => {\n *     // This will be called whenever a new view is registered or a view is removed\n *     const viewNames = navigation.views.map((view) => view.name)\n *     console.warn('Registered views changed', viewNames)\n * })\n * // Or you can react to changes of the current active view\n * navigation.addEventListener('updateActive', (event) => {\n *     // This will be called whenever the active view changed\n *     const newView = event.detail // you could also use `navigation.active`\n *     console.warn('Active view changed to ' + newView.name)\n * })\n * ```\n */\nexport class Navigation extends TypedEventTarget<{ updateActive: UpdateActiveViewEvent, update: UpdateViewsEvent }> {\n\tprivate _views: IView[] = []\n\tprivate _currentView: IView | null = null\n\n\t/**\n\t * Register a new view on the navigation\n\t *\n\t * @param views The views to register\n\t * @throws {Error} if a view with the same id is already registered\n\t * @throws {Error} if the registered view is invalid\n\t */\n\tregister(...views: IView[]): void {\n\t\tfor (const view of views) {\n\t\t\tif (this._views.find((search) => search.id === view.id)) {\n\t\t\t\tthrow new Error(`IView id ${view.id} is already registered`)\n\t\t\t}\n\t\t\tvalidateView(view)\n\t\t}\n\n\t\tthis._views.push(...views)\n\t\tthis.dispatchTypedEvent('update', new CustomEvent<never>('update') as UpdateViewsEvent)\n\t}\n\n\t/**\n\t * Remove a registered view\n\t *\n\t * @param id The id of the view to remove\n\t */\n\tremove(id: string): void {\n\t\tconst index = this._views.findIndex((view) => view.id === id)\n\t\tif (index !== -1) {\n\t\t\tthis._views.splice(index, 1)\n\t\t\tthis.dispatchTypedEvent('update', new CustomEvent('update') as UpdateViewsEvent)\n\t\t}\n\t}\n\n\t/**\n\t * Set the currently active view\n\t *\n\t * @param id - The id of the view to set as active\n\t * @throws {Error} If no view with the given id was registered\n\t * @fires UpdateActiveViewEvent\n\t */\n\tsetActive(id: string | null): void {\n\t\tif (id === null) {\n\t\t\tthis._currentView = null\n\t\t} else {\n\t\t\tconst view = this._views.find(({ id: viewId }) => viewId === id)\n\t\t\tif (!view) {\n\t\t\t\tthrow new Error(`No view with ${id} registered`)\n\t\t\t}\n\t\t\tthis._currentView = view\n\t\t}\n\n\t\tconst event = new CustomEvent<IView | null>('updateActive', { detail: this._currentView })\n\t\tthis.dispatchTypedEvent('updateActive', event as UpdateActiveViewEvent)\n\t}\n\n\t/**\n\t * The currently active files view\n\t */\n\tget active(): IView | null {\n\t\treturn this._currentView\n\t}\n\n\t/**\n\t * All registered views\n\t */\n\tget views(): IView[] {\n\t\treturn this._views\n\t}\n}\n\n/**\n * Get the current files navigation\n */\nexport function getNavigation(): Navigation {\n\tscopedGlobals.navigation ??= new Navigation()\n\treturn scopedGlobals.navigation\n}\n"],"names":[],"mappings":";;;AAgDO,MAAM,mBAAmB,iBAAoF;AAAA,EAC3G,SAAkB,CAAA;AAAA,EAClB,eAA6B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASrC,YAAY,OAAsB;AACjC,eAAW,QAAQ,OAAO;AACzB,UAAI,KAAK,OAAO,KAAK,CAAC,WAAW,OAAO,OAAO,KAAK,EAAE,GAAG;AACxD,cAAM,IAAI,MAAM,YAAY,KAAK,EAAE,wBAAwB;AAAA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