2021-08-06 08:31:17 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
2023-09-19 17:21:22 +00:00
|
|
|
// Copyright © 2021, 2023 Hardcore Engineering Inc.
|
2022-02-03 09:03:53 +00:00
|
|
|
//
|
2021-08-06 08:31:17 +00:00
|
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License. You may
|
|
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
2022-02-03 09:03:53 +00:00
|
|
|
//
|
2021-08-06 08:31:17 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
2022-02-03 09:03:53 +00:00
|
|
|
//
|
2021-08-06 08:31:17 +00:00
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2024-01-11 14:46:11 +00:00
|
|
|
import type { Class, Client, Doc, Ref, Space, TxOperations } from '@hcengineering/core'
|
2022-09-21 08:08:25 +00:00
|
|
|
import core from '@hcengineering/core'
|
2023-05-19 11:46:05 +00:00
|
|
|
import type { Workspace } from '@hcengineering/login'
|
2023-11-03 09:11:12 +00:00
|
|
|
import login, { loginId } from '@hcengineering/login'
|
2023-09-06 06:45:19 +00:00
|
|
|
import { getResource, setMetadata } from '@hcengineering/platform'
|
2023-05-19 11:46:05 +00:00
|
|
|
import preference from '@hcengineering/preference'
|
2023-09-06 06:45:19 +00:00
|
|
|
import { closeClient, getClient } from '@hcengineering/presentation'
|
2023-11-03 09:11:12 +00:00
|
|
|
import presentation from '@hcengineering/presentation/src/plugin'
|
2023-09-06 06:45:19 +00:00
|
|
|
import {
|
|
|
|
closePanel,
|
|
|
|
fetchMetadataLocalStorage,
|
|
|
|
getCurrentLocation,
|
|
|
|
navigate,
|
|
|
|
setMetadataLocalStorage
|
|
|
|
} from '@hcengineering/ui'
|
2023-05-19 11:46:05 +00:00
|
|
|
import view from '@hcengineering/view'
|
2023-11-20 10:01:43 +00:00
|
|
|
import workbench, { type Application, type NavigatorModel } from '@hcengineering/workbench'
|
2022-12-30 08:43:26 +00:00
|
|
|
import { writable } from 'svelte/store'
|
2024-01-11 14:46:11 +00:00
|
|
|
|
2022-04-29 05:27:17 +00:00
|
|
|
export function getSpecialSpaceClass (model: NavigatorModel): Array<Ref<Class<Space>>> {
|
2022-04-25 17:15:13 +00:00
|
|
|
const spaceResult = model.spaces.map((x) => x.spaceClass)
|
2022-06-10 18:36:47 +00:00
|
|
|
const result = (model.specials ?? [])
|
|
|
|
.map((it) => it.spaceClass)
|
|
|
|
.filter((it) => it !== undefined && !spaceResult.includes(it))
|
2022-03-04 09:01:46 +00:00
|
|
|
return spaceResult.concat(result as Array<Ref<Class<Space>>>)
|
|
|
|
}
|
2022-04-25 17:15:13 +00:00
|
|
|
|
2022-04-29 05:27:17 +00:00
|
|
|
export async function getSpaceName (client: Client, space: Space): Promise<string> {
|
2022-04-25 17:15:13 +00:00
|
|
|
const hierarchy = client.getHierarchy()
|
|
|
|
const clazz = hierarchy.getClass(space._class)
|
|
|
|
const nameMixin = hierarchy.as(clazz, view.mixin.SpaceName)
|
|
|
|
|
2022-04-29 05:27:17 +00:00
|
|
|
if (nameMixin?.getName !== undefined) {
|
2022-04-25 17:15:13 +00:00
|
|
|
const getSpaceName = await getResource(nameMixin.getName)
|
|
|
|
const name = await getSpaceName(client, space)
|
|
|
|
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
return space.name
|
|
|
|
}
|
2022-05-04 08:08:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export async function doNavigate (
|
|
|
|
doc: Doc | Doc[],
|
|
|
|
evt: Event | undefined,
|
|
|
|
props: {
|
|
|
|
mode: 'app' | 'special' | 'space'
|
2022-06-29 08:08:59 +00:00
|
|
|
application?: string
|
2022-05-04 08:08:12 +00:00
|
|
|
special?: string
|
|
|
|
spaceSpecial?: string
|
|
|
|
space?: Ref<Space>
|
|
|
|
// If no space is selected, select first space from list
|
|
|
|
spaceClass?: Ref<Class<Space>>
|
2023-06-03 03:49:20 +00:00
|
|
|
query?: Record<string, string | null>
|
2022-05-04 08:08:12 +00:00
|
|
|
}
|
|
|
|
): Promise<void> {
|
|
|
|
evt?.preventDefault()
|
|
|
|
|
|
|
|
closePanel()
|
|
|
|
const loc = getCurrentLocation()
|
|
|
|
const client = getClient()
|
|
|
|
switch (props.mode) {
|
|
|
|
case 'app':
|
2022-07-06 07:02:01 +00:00
|
|
|
loc.path[2] = props.application ?? ''
|
2022-05-04 08:08:12 +00:00
|
|
|
if (props.special !== undefined) {
|
2022-07-06 07:02:01 +00:00
|
|
|
loc.path[3] = props.special
|
|
|
|
loc.path.length = 4
|
2022-05-04 08:08:12 +00:00
|
|
|
} else {
|
2022-07-06 07:02:01 +00:00
|
|
|
loc.path.length = 3
|
2022-05-04 08:08:12 +00:00
|
|
|
}
|
2023-06-03 03:49:20 +00:00
|
|
|
loc.query = props.query
|
|
|
|
loc.fragment = undefined
|
2022-05-04 08:08:12 +00:00
|
|
|
navigate(loc)
|
|
|
|
break
|
|
|
|
case 'special':
|
2022-07-06 07:02:01 +00:00
|
|
|
if (props.application !== undefined && loc.path[2] !== props.application) {
|
|
|
|
loc.path[2] = props.application
|
2022-05-04 08:08:12 +00:00
|
|
|
}
|
2022-07-06 07:02:01 +00:00
|
|
|
loc.path[3] = props.special ?? ''
|
|
|
|
loc.path.length = 4
|
2023-06-03 03:49:20 +00:00
|
|
|
loc.query = props.query
|
|
|
|
loc.fragment = undefined
|
2022-05-04 08:08:12 +00:00
|
|
|
navigate(loc)
|
|
|
|
break
|
|
|
|
case 'space': {
|
|
|
|
if (props.space !== undefined) {
|
2022-07-06 07:02:01 +00:00
|
|
|
loc.path[3] = props.space
|
2022-05-16 11:41:22 +00:00
|
|
|
} else {
|
|
|
|
if (doc !== undefined && !Array.isArray(doc) && client.getHierarchy().isDerived(doc._class, core.class.Space)) {
|
2022-07-06 07:02:01 +00:00
|
|
|
loc.path[3] = doc._id
|
2022-05-16 11:41:22 +00:00
|
|
|
}
|
2022-05-04 08:08:12 +00:00
|
|
|
}
|
|
|
|
if (props.spaceSpecial !== undefined) {
|
2022-07-06 07:02:01 +00:00
|
|
|
loc.path[4] = props.spaceSpecial
|
2022-05-04 08:08:12 +00:00
|
|
|
}
|
|
|
|
if (props.spaceClass !== undefined) {
|
2022-07-06 07:02:01 +00:00
|
|
|
const ex = await client.findOne(props.spaceClass, { _id: loc.path[3] as Ref<Space> })
|
2022-05-04 08:08:12 +00:00
|
|
|
if (ex === undefined) {
|
|
|
|
const r = await client.findOne(props.spaceClass, {})
|
|
|
|
if (r !== undefined) {
|
2022-07-06 07:02:01 +00:00
|
|
|
loc.path[3] = r._id
|
2022-05-04 08:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-06 07:02:01 +00:00
|
|
|
loc.path.length = 5
|
2023-06-03 03:49:20 +00:00
|
|
|
loc.query = props.query
|
|
|
|
loc.fragment = undefined
|
2022-05-04 08:08:12 +00:00
|
|
|
navigate(loc)
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-01 15:48:20 +00:00
|
|
|
|
|
|
|
export async function hideApplication (app: Application): Promise<void> {
|
|
|
|
const client = getClient()
|
|
|
|
|
|
|
|
await client.createDoc(workbench.class.HiddenApplication, preference.space.Preference, {
|
|
|
|
attachedTo: app._id
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function showApplication (app: Application): Promise<void> {
|
|
|
|
const client = getClient()
|
|
|
|
|
|
|
|
const current = await client.findOne(workbench.class.HiddenApplication, { attachedTo: app._id })
|
|
|
|
if (current !== undefined) {
|
|
|
|
await client.remove(current)
|
|
|
|
}
|
|
|
|
}
|
2022-12-30 08:43:26 +00:00
|
|
|
|
|
|
|
export const workspacesStore = writable<Workspace[]>([])
|
2023-07-04 01:50:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export async function buildNavModel (
|
|
|
|
client: TxOperations,
|
|
|
|
currentApplication?: Application
|
|
|
|
): Promise<NavigatorModel | undefined> {
|
|
|
|
let newNavModel = currentApplication?.navigatorModel
|
|
|
|
if (currentApplication !== undefined) {
|
|
|
|
const models = await client.findAll(workbench.class.ApplicationNavModel, { extends: currentApplication._id })
|
|
|
|
for (const nm of models) {
|
|
|
|
const spaces = newNavModel?.spaces ?? []
|
|
|
|
// Check for extending
|
|
|
|
for (const sp of spaces) {
|
|
|
|
const extend = (nm.spaces ?? []).find((p) => p.id === sp.id)
|
|
|
|
if (extend !== undefined) {
|
|
|
|
sp.label = sp.label ?? extend.label
|
|
|
|
sp.createComponent = sp.createComponent ?? extend.createComponent
|
|
|
|
sp.addSpaceLabel = sp.addSpaceLabel ?? extend.addSpaceLabel
|
|
|
|
sp.icon = sp.icon ?? extend.icon
|
|
|
|
sp.visibleIf = sp.visibleIf ?? extend.visibleIf
|
|
|
|
sp.specials = [...(sp.specials ?? []), ...(extend.specials ?? [])]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const newSpaces = (nm.spaces ?? []).filter((it) => !spaces.some((sp) => sp.id === it.id))
|
|
|
|
newNavModel = {
|
|
|
|
spaces: [...spaces, ...newSpaces],
|
|
|
|
specials: [...(newNavModel?.specials ?? []), ...(nm.specials ?? [])],
|
|
|
|
aside: newNavModel?.aside ?? nm?.aside
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newNavModel
|
|
|
|
}
|
2023-09-06 06:45:19 +00:00
|
|
|
|
|
|
|
export function signOut (): void {
|
|
|
|
const tokens = fetchMetadataLocalStorage(login.metadata.LoginTokens)
|
|
|
|
if (tokens !== null) {
|
|
|
|
const loc = getCurrentLocation()
|
|
|
|
const l = loc.path[1]
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
|
|
delete tokens[l]
|
|
|
|
setMetadataLocalStorage(login.metadata.LoginTokens, tokens)
|
|
|
|
}
|
|
|
|
setMetadata(presentation.metadata.Token, null)
|
|
|
|
setMetadataLocalStorage(login.metadata.LoginEndpoint, null)
|
|
|
|
setMetadataLocalStorage(login.metadata.LoginEmail, null)
|
|
|
|
void closeClient()
|
|
|
|
navigate({ path: [loginId] })
|
|
|
|
}
|