2023-04-25 07:34:10 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2023 Hardcore Engineering Inc.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2023-11-20 10:01:43 +00:00
|
|
|
import core, { type PluginConfiguration, SortingOrder } from '@hcengineering/core'
|
|
|
|
import { type Plugin, type Resource, getResourcePlugin } from '@hcengineering/platform'
|
2023-06-22 04:55:57 +00:00
|
|
|
import { get, writable } from 'svelte/store'
|
2023-04-25 07:34:10 +00:00
|
|
|
import { createQuery } from '.'
|
2023-06-22 04:55:57 +00:00
|
|
|
import { location as platformLocation } from '@hcengineering/ui'
|
2023-04-25 07:34:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export class ConfigurationManager {
|
2023-11-20 10:01:43 +00:00
|
|
|
constructor (
|
|
|
|
readonly list: PluginConfiguration[],
|
|
|
|
readonly configuration: Map<Plugin, PluginConfiguration>
|
|
|
|
) {}
|
2023-04-25 07:34:10 +00:00
|
|
|
|
|
|
|
has (plugin: Plugin): boolean {
|
|
|
|
return this.configuration.get(plugin)?.enabled !== false
|
|
|
|
}
|
|
|
|
|
2024-01-04 10:37:11 +00:00
|
|
|
hasResource<T>(resource?: Resource<T> | null): boolean | undefined {
|
2023-04-25 07:34:10 +00:00
|
|
|
if (resource == null) {
|
|
|
|
return false
|
|
|
|
}
|
2024-01-04 10:37:11 +00:00
|
|
|
try {
|
|
|
|
return this.has(getResourcePlugin(resource))
|
|
|
|
} catch {}
|
2023-04-25 07:34:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Issue status live query
|
|
|
|
export let configuration = new ConfigurationManager([], new Map())
|
|
|
|
export const configurationStore = writable<ConfigurationManager>(configuration)
|
|
|
|
|
|
|
|
const configQuery = createQuery(true)
|
|
|
|
|
2023-06-22 04:55:57 +00:00
|
|
|
let hashString = ''
|
|
|
|
let workspaceId: string = ''
|
|
|
|
|
2023-04-25 07:34:10 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2024-01-04 10:37:11 +00:00
|
|
|
export function hasResource<T> (resource?: Resource<T>): boolean | undefined {
|
2023-04-25 07:34:10 +00:00
|
|
|
return configuration.hasResource(resource)
|
|
|
|
}
|
|
|
|
|
|
|
|
configQuery.query(
|
|
|
|
core.class.PluginConfiguration,
|
|
|
|
{},
|
|
|
|
(res) => {
|
2023-06-22 04:55:57 +00:00
|
|
|
const newHash = res.map((it) => `${it.pluginId}=${it.enabled ? '+' : '-'}`).join('&')
|
|
|
|
const wsId = get(platformLocation).path[1]
|
|
|
|
|
|
|
|
if (hashString !== '' && hashString !== newHash && workspaceId !== '' && workspaceId === wsId) {
|
|
|
|
// Configuration is changed for same workspace.
|
2023-04-25 07:34:10 +00:00
|
|
|
location.reload()
|
|
|
|
}
|
2023-06-22 04:55:57 +00:00
|
|
|
workspaceId = wsId
|
|
|
|
hashString = newHash
|
2023-04-25 07:34:10 +00:00
|
|
|
configuration = new ConfigurationManager(res, new Map(res.map((it) => [it.pluginId, it])))
|
|
|
|
configurationStore.set(configuration)
|
|
|
|
},
|
|
|
|
{ sort: { label: SortingOrder.Ascending } }
|
|
|
|
)
|