2021-08-06 13:18:50 +03:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
|
|
|
// Copyright © 2021 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.
|
|
|
|
//
|
|
|
|
|
2021-12-14 12:24:14 +03:00
|
|
|
import core from '@anticrm/core'
|
2021-11-18 15:48:05 +03:00
|
|
|
import type { Plugin, Asset, Resource, IntlString } from '@anticrm/platform'
|
2021-08-06 13:18:50 +03:00
|
|
|
import { plugin } from '@anticrm/platform'
|
2021-12-14 12:24:14 +03:00
|
|
|
import type { Ref, Mixin, UXObject, Space, FindOptions, Class, Doc, Arr, State, Client, Obj, DoneState, AttachedDoc, WonState, LostState, TxOperations } from '@anticrm/core'
|
2021-08-06 13:18:50 +03:00
|
|
|
|
2021-11-18 15:48:05 +03:00
|
|
|
import type { AnyComponent, AnySvelteComponent } from '@anticrm/ui'
|
2021-08-06 13:18:50 +03:00
|
|
|
|
2021-08-07 08:39:49 +03:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-06 13:18:50 +03:00
|
|
|
export interface AttributeEditor extends Class<Doc> {
|
|
|
|
editor: AnyComponent
|
|
|
|
}
|
|
|
|
|
2021-08-07 08:39:49 +03:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-06 13:18:50 +03:00
|
|
|
export interface AttributePresenter extends Class<Doc> {
|
|
|
|
presenter: AnyComponent
|
|
|
|
}
|
|
|
|
|
2021-09-07 11:36:50 +03:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface KanbanCard extends Class<Doc> {
|
|
|
|
card: AnyComponent
|
|
|
|
}
|
|
|
|
|
2021-09-08 19:42:40 +03:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface ObjectEditor extends Class<Doc> {
|
|
|
|
editor: AnyComponent
|
|
|
|
}
|
|
|
|
|
2021-08-07 08:39:49 +03:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-06 13:18:50 +03:00
|
|
|
export interface ViewletDescriptor extends Doc, UXObject {
|
|
|
|
component: AnyComponent
|
|
|
|
}
|
|
|
|
|
2021-08-07 08:39:49 +03:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-06 13:18:50 +03:00
|
|
|
export interface Viewlet extends Doc {
|
|
|
|
attachTo: Ref<Class<Space>>
|
|
|
|
descriptor: Ref<ViewletDescriptor>
|
|
|
|
open: AnyComponent
|
|
|
|
options?: FindOptions<Doc>
|
|
|
|
config: any
|
|
|
|
}
|
|
|
|
|
2021-09-25 19:48:22 +03:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface Action extends Doc, UXObject {
|
|
|
|
action: Resource<(doc: Doc) => Promise<void>>
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface ActionTarget extends Doc {
|
|
|
|
target: Ref<Class<Doc>>
|
|
|
|
action: Ref<Action>
|
|
|
|
}
|
|
|
|
|
2021-12-14 12:24:14 +03:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface Sequence extends Doc {
|
|
|
|
attachedTo: Ref<Class<Doc>>
|
|
|
|
sequence: number
|
|
|
|
}
|
|
|
|
|
2021-10-08 17:49:46 +03:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface Kanban extends Doc {
|
|
|
|
attachedTo: Ref<Space>
|
2021-10-09 18:02:32 +03:00
|
|
|
states: Arr<Ref<State>>
|
2021-12-14 12:24:14 +03:00
|
|
|
doneStates: Arr<Ref<DoneState>>
|
2021-10-08 17:49:46 +03:00
|
|
|
order: Arr<Ref<Doc>>
|
|
|
|
}
|
|
|
|
|
2021-10-23 13:09:39 +03:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-12-14 12:24:14 +03:00
|
|
|
export interface StateTemplate extends AttachedDoc, State {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface DoneStateTemplate extends AttachedDoc, DoneState {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface WonStateTemplate extends DoneStateTemplate, WonState {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface LostStateTemplate extends DoneStateTemplate, LostState {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface KanbanTemplate extends Doc {
|
|
|
|
title: string
|
|
|
|
states: Arr<Ref<StateTemplate>>
|
|
|
|
doneStates: Arr<Ref<DoneStateTemplate>>
|
|
|
|
statesC: number
|
|
|
|
doneStatesC: number
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface KanbanTemplateSpace extends Space {
|
|
|
|
icon: AnyComponent
|
2021-10-23 13:09:39 +03:00
|
|
|
}
|
|
|
|
|
2021-08-07 08:39:49 +03:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-06 13:18:50 +03:00
|
|
|
export const viewId = 'view' as Plugin
|
|
|
|
|
2021-12-08 12:09:51 +03:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export type BuildModelKey = string | {
|
|
|
|
presenter: AnyComponent
|
|
|
|
label: string
|
|
|
|
}
|
|
|
|
|
2021-11-18 15:48:05 +03:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface AttributeModel {
|
|
|
|
key: string
|
|
|
|
label: IntlString
|
2021-12-03 13:16:16 +03:00
|
|
|
_class: Ref<Class<Doc>>
|
2021-11-18 15:48:05 +03:00
|
|
|
presenter: AnySvelteComponent
|
2021-12-08 12:09:51 +03:00
|
|
|
// Extra properties for component
|
|
|
|
props?: Record<string, any>
|
2021-11-18 15:48:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface BuildModelOptions {
|
|
|
|
client: Client
|
|
|
|
_class: Ref<Class<Obj>>
|
2021-12-08 12:09:51 +03:00
|
|
|
keys: BuildModelKey[]
|
2021-11-18 15:48:05 +03:00
|
|
|
options?: FindOptions<Doc>
|
|
|
|
ignoreMissing?: boolean
|
|
|
|
}
|
|
|
|
|
2021-12-14 12:24:14 +03:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
const view = plugin(viewId, {
|
2021-08-06 13:18:50 +03:00
|
|
|
mixin: {
|
|
|
|
AttributeEditor: '' as Ref<Mixin<AttributeEditor>>,
|
2021-09-07 11:36:50 +03:00
|
|
|
AttributePresenter: '' as Ref<Mixin<AttributePresenter>>,
|
2021-09-08 19:42:40 +03:00
|
|
|
KanbanCard: '' as Ref<Mixin<KanbanCard>>,
|
|
|
|
ObjectEditor: '' as Ref<Mixin<ObjectEditor>>
|
2021-08-06 13:18:50 +03:00
|
|
|
},
|
|
|
|
class: {
|
|
|
|
ViewletDescriptor: '' as Ref<Class<ViewletDescriptor>>,
|
2021-09-25 19:48:22 +03:00
|
|
|
Viewlet: '' as Ref<Class<Viewlet>>,
|
|
|
|
Action: '' as Ref<Class<Action>>,
|
2021-10-08 17:49:46 +03:00
|
|
|
ActionTarget: '' as Ref<Class<ActionTarget>>,
|
2021-10-23 13:09:39 +03:00
|
|
|
Kanban: '' as Ref<Class<Kanban>>,
|
2021-12-14 12:24:14 +03:00
|
|
|
Sequence: '' as Ref<Class<Sequence>>,
|
|
|
|
StateTemplate: '' as Ref<Class<StateTemplate>>,
|
|
|
|
DoneStateTemplate: '' as Ref<Class<DoneStateTemplate>>,
|
|
|
|
WonStateTemplate: '' as Ref<Class<WonStateTemplate>>,
|
|
|
|
LostStateTemplate: '' as Ref<Class<LostStateTemplate>>,
|
|
|
|
KanbanTemplate: '' as Ref<Class<KanbanTemplate>>,
|
|
|
|
KanbanTemplateSpace: '' as Ref<Class<KanbanTemplateSpace>>
|
2021-08-06 13:18:50 +03:00
|
|
|
},
|
|
|
|
viewlet: {
|
2021-09-06 11:30:31 +03:00
|
|
|
Table: '' as Ref<ViewletDescriptor>,
|
|
|
|
Kanban: '' as Ref<ViewletDescriptor>
|
2021-08-06 13:18:50 +03:00
|
|
|
},
|
2021-10-23 13:09:39 +03:00
|
|
|
space: {
|
|
|
|
Sequence: '' as Ref<Space>
|
|
|
|
},
|
2021-08-06 13:18:50 +03:00
|
|
|
icon: {
|
2021-09-06 11:30:31 +03:00
|
|
|
Table: '' as Asset,
|
2021-11-29 14:41:15 +03:00
|
|
|
Kanban: '' as Asset,
|
2021-12-13 12:05:46 +03:00
|
|
|
Delete: '' as Asset,
|
|
|
|
Move: '' as Asset
|
2021-12-14 12:24:14 +03:00
|
|
|
},
|
|
|
|
string: {
|
|
|
|
Delete: '' as IntlString
|
2021-08-06 13:18:50 +03:00
|
|
|
}
|
|
|
|
})
|
2021-12-14 12:24:14 +03:00
|
|
|
export default view
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export async function createKanban (client: Client & TxOperations, attachedTo: Ref<Space>, templateId?: Ref<KanbanTemplate>): Promise<Ref<Kanban>> {
|
|
|
|
if (templateId === undefined) {
|
|
|
|
return await client.createDoc(view.class.Kanban, attachedTo, {
|
|
|
|
attachedTo,
|
|
|
|
states: [],
|
|
|
|
doneStates: await Promise.all([
|
|
|
|
client.createDoc(core.class.WonState, attachedTo, {
|
|
|
|
title: 'Won'
|
|
|
|
}),
|
|
|
|
client.createDoc(core.class.LostState, attachedTo, {
|
|
|
|
title: 'Lost'
|
|
|
|
})
|
|
|
|
]),
|
|
|
|
order: []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const template = await client.findOne(view.class.KanbanTemplate, { _id: templateId })
|
|
|
|
|
|
|
|
if (template === undefined) {
|
|
|
|
throw Error(`Failed to find target kanban template: ${templateId}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
const tmplStates = await client.findAll(view.class.StateTemplate, { attachedTo: template._id })
|
|
|
|
const states = await Promise.all(
|
|
|
|
template.states
|
|
|
|
.map((id) => tmplStates.find((x) => x._id === id))
|
|
|
|
.filter((tstate): tstate is StateTemplate => tstate !== undefined)
|
|
|
|
.map(async (state) => await client.createDoc(core.class.State, attachedTo, { color: state.color, title: state.title }))
|
|
|
|
)
|
|
|
|
|
|
|
|
const doneClassMap = new Map<Ref<Class<DoneStateTemplate>>, Ref<Class<DoneState>>>([
|
|
|
|
[view.class.WonStateTemplate, core.class.WonState],
|
|
|
|
[view.class.LostStateTemplate, core.class.LostState]
|
|
|
|
])
|
|
|
|
const tmplDoneStates = await client.findAll(view.class.DoneStateTemplate, { attachedTo: template._id })
|
|
|
|
const doneStates = (await Promise.all(
|
|
|
|
template.doneStates
|
|
|
|
.map((id) => tmplDoneStates.find((x) => x._id === id))
|
|
|
|
.filter((tstate): tstate is DoneStateTemplate => tstate !== undefined)
|
|
|
|
.map(async (state) => {
|
|
|
|
const cl = doneClassMap.get(state._class)
|
|
|
|
|
|
|
|
if (cl === undefined) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return await client.createDoc(cl, attachedTo, { title: state.title })
|
|
|
|
})
|
|
|
|
)).filter((x): x is Ref<DoneState> => x !== undefined)
|
|
|
|
|
|
|
|
return await client.createDoc(view.class.Kanban, attachedTo, {
|
|
|
|
attachedTo,
|
|
|
|
states,
|
|
|
|
doneStates,
|
|
|
|
order: []
|
|
|
|
})
|
|
|
|
}
|