{"version":3,"file":"view.mjs","sources":["../../../lib/ui/navigation/view.ts"],"sourcesContent":["/*\n * SPDX-FileCopyrightText: 2022 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 { IColumn } from './column.ts'\n\nimport isSvg from 'is-svg'\nimport { checkOptionalProperty } from '../../utils/objectValidation.ts'\nimport { validateColumn } from './column.ts'\n\nexport type ContentsWithRoot = {\n\tfolder: IFolder\n\tcontents: INode[]\n}\n\nexport interface IGetContentsOptions {\n\t/**\n\t * Abort signal to be able to cancel the request.\n\t *\n\t *@see https://developer.mozilla.org/en-US/docs/Web/API/AbortController\n\t */\n\tsignal: AbortSignal\n}\n\nexport interface IView {\n\t/** Unique view ID */\n\tid: string\n\t/** Translated view name */\n\tname: string\n\t/** Translated accessible description of the view */\n\tcaption?: string\n\n\t/** Translated title of the empty view */\n\temptyTitle?: string\n\t/** Translated description of the empty view */\n\temptyCaption?: string\n\t/**\n\t * Custom implementation of the empty view.\n\t * If set and no content is found for the current view,\n\t * then this method is called with the container element\n\t * where to render your empty view implementation.\n\t *\n\t * @param div - The container element to render into\n\t */\n\temptyView?: (div: HTMLDivElement) => void\n\n\t/**\n\t * Method return the content of the provided path.\n\t *\n\t * This method _must_ also return the current directory\n\t * information alongside with its content.\n\t *\n\t * An [abort signal](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) is provided\n\t * to be able to cancel the request if the user change directory.\n\t */\n\tgetContents(path: string, options: IGetContentsOptions): Promise<ContentsWithRoot>\n\n\t/**\n\t * If set then the view will be hidden from the navigation unless its the active view.\n\t */\n\thidden?: true\n\n\t/** The view icon as an inline svg */\n\ticon: string\n\n\t/**\n\t * The view order.\n\t * If not set will be natural sorted by view name.\n\t */\n\torder?: number\n\n\t/**\n\t * Custom params to give to the router on click\n\t * If defined, will be treated as a dummy view and\n\t * will just redirect and not fetch any contents.\n\t */\n\tparams?: Record<string, string>\n\n\t/**\n\t * This view column(s). Name and actions are\n\t * by default always included\n\t */\n\tcolumns?: IColumn[]\n\n\t/** The parent unique ID */\n\tparent?: string\n\t/** This view is sticky (sent at the bottom) */\n\tsticky?: boolean\n\n\t/**\n\t * This view has children and is expanded (by default)\n\t * or not. This will be overridden by user config.\n\t */\n\texpanded?: boolean\n\n\t/**\n\t * Will be used as default if the user\n\t * haven't customized their sorting column\n\t */\n\tdefaultSortKey?: string\n\n\t/**\n\t * Method called to load child views if any\n\t */\n\tloadChildViews?: (view: IView) => Promise<void>\n}\n\nexport class View implements IView {\n\tprivate _view: IView\n\n\tconstructor(view: IView) {\n\t\tvalidateView(view)\n\t\tthis._view = view\n\t}\n\n\tget id() {\n\t\treturn this._view.id\n\t}\n\n\tget name() {\n\t\treturn this._view.name\n\t}\n\n\tget caption() {\n\t\treturn this._view.caption\n\t}\n\n\tget emptyTitle() {\n\t\treturn this._view.emptyTitle\n\t}\n\n\tget emptyCaption() {\n\t\treturn this._view.emptyCaption\n\t}\n\n\tget getContents() {\n\t\treturn this._view.getContents\n\t}\n\n\tget hidden() {\n\t\treturn this._view.hidden\n\t}\n\n\tget icon() {\n\t\treturn this._view.icon\n\t}\n\n\tset icon(icon) {\n\t\tthis._view.icon = icon\n\t}\n\n\tget order() {\n\t\treturn this._view.order\n\t}\n\n\tset order(order) {\n\t\tthis._view.order = order\n\t}\n\n\tget params() {\n\t\treturn this._view.params\n\t}\n\n\tset params(params) {\n\t\tthis._view.params = params\n\t}\n\n\tget columns() {\n\t\treturn this._view.columns\n\t}\n\n\tget emptyView() {\n\t\treturn this._view.emptyView\n\t}\n\n\tget parent() {\n\t\treturn this._view.parent\n\t}\n\n\tget sticky() {\n\t\treturn this._view.sticky\n\t}\n\n\tget expanded() {\n\t\treturn this._view.expanded\n\t}\n\n\tset expanded(expanded: boolean | undefined) {\n\t\tthis._view.expanded = expanded\n\t}\n\n\tget defaultSortKey() {\n\t\treturn this._view.defaultSortKey\n\t}\n\n\tget loadChildViews() {\n\t\treturn this._view.loadChildViews\n\t}\n}\n\n/**\n * Validate a view interface to check all required properties are satisfied.\n *\n * @param view the view to check\n * @throws {Error} if the view is not valid\n */\nexport function validateView(view: IView) {\n\tif (!view.icon || typeof view.icon !== 'string' || !isSvg(view.icon)) {\n\t\tthrow new Error('View icon is required and must be a valid svg string')\n\t}\n\n\tif (!view.id || typeof view.id !== 'string') {\n\t\tthrow new Error('View id is required and must be a string')\n\t}\n\n\tif (!view.getContents || typeof view.getContents !== 'function') {\n\t\tthrow new Error('View getContents is required and must be a function')\n\t}\n\n\tif (!view.name || typeof view.name !== 'string') {\n\t\tthrow new Error('View name is required and must be a string')\n\t}\n\n\t// optional properties type checks\n\n\tcheckOptionalProperty(view, 'caption', 'string')\n\tcheckOptionalProperty(view, 'columns', 'array')\n\tcheckOptionalProperty(view, 'defaultSortKey', 'string')\n\tcheckOptionalProperty(view, 'emptyCaption', 'string')\n\tcheckOptionalProperty(view, 'emptyTitle', 'string')\n\tcheckOptionalProperty(view, 'emptyView', 'function')\n\tcheckOptionalProperty(view, 'expanded', 'boolean')\n\tcheckOptionalProperty(view, 'hidden', 'boolean')\n\tcheckOptionalProperty(view, 'loadChildViews', 'function')\n\tcheckOptionalProperty(view, 'order', 'number')\n\tcheckOptionalProperty(view, 'params', 'object')\n\tcheckOptionalProperty(view, 'parent', 'string')\n\tcheckOptionalProperty(view, 'sticky', 'boolean')\n\n\tif (view.columns) {\n\t\t// we cannot use `instanceof` here because if the Navigation and the Column class are loaded by different apps\n\t\t// (Navigation is set by files app and Column by a 3rd party app),\n\t\t// the `instanceof` check will fail even if the object has the correct shape because they are different classes in memory.\n\t\tview.columns.forEach(validateColumn)\n\t\tconst columnIds = view.columns.reduce((set, column) => set.add(column.id), new Set<string>())\n\t\tif (columnIds.size !== view.columns.length) {\n\t\t\tthrow new Error('View columns must have unique ids')\n\t\t}\n\t}\n}\n"],"names":[],"mappings":";;;AA6GO,MAAM,KAAsB;AAAA,EAC1B;AAAA,EAER,YA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