From 95ea0aab98be11f978dddada102bfde53f05edc9 Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Tue, 21 Mar 2023 22:08:45 +0600 Subject: [PATCH] Default values (#2792) Signed-off-by: Denis Bykhov --- models/core/src/core.ts | 1 + packages/core/src/classes.ts | 6 ++ packages/core/src/utils.ts | 25 +++++++- .../src/components/CreateOrganization.svelte | 5 +- .../src/components/CreateApplication.svelte | 3 + .../src/components/CreateCandidate.svelte | 6 +- .../src/components/CreateVacancy.svelte | 13 +++- plugins/setting-assets/lang/en.json | 3 +- plugins/setting-assets/lang/ru.json | 3 +- .../src/components/CreateAttribute.svelte | 5 +- .../src/components/EditAttribute.svelte | 6 ++ .../typeEditors/EnumTypeEditor.svelte | 59 +++++++++++++------ plugins/setting-resources/src/plugin.ts | 3 +- .../src/components/CreateIssue.svelte | 6 +- .../src/components/EditDoc.svelte | 2 - plugins/view-resources/src/index.ts | 3 +- 16 files changed, 118 insertions(+), 31 deletions(-) diff --git a/models/core/src/core.ts b/models/core/src/core.ts index 135bb41eb9..7748405e34 100644 --- a/models/core/src/core.ts +++ b/models/core/src/core.ts @@ -152,6 +152,7 @@ export class TAttribute extends TDoc implements AnyAttribute { type!: Type label!: IntlString isCustom?: boolean + defaultValue?: any } @Model(core.class.Type, core.class.Obj, DOMAIN_MODEL) diff --git a/packages/core/src/classes.ts b/packages/core/src/classes.ts index 4fa6644eac..888d4866d5 100644 --- a/packages/core/src/classes.ts +++ b/packages/core/src/classes.ts @@ -115,6 +115,7 @@ export interface Attribute extends Doc, UXObject { index?: IndexKind shortLabel?: IntlString isCustom?: boolean + defaultValue?: any // Extra customization properties [key: string]: any @@ -192,6 +193,11 @@ export type Data = Omit */ export type AttachedData = Omit +/** + * @public + */ +export type DocData = T extends AttachedDoc ? AttachedData : Data + // T Y P E S /** diff --git a/packages/core/src/utils.ts b/packages/core/src/utils.ts index 16b06a78aa..2ef4a6ae28 100644 --- a/packages/core/src/utils.ts +++ b/packages/core/src/utils.ts @@ -13,9 +13,10 @@ // limitations under the License. // -import { Account, AnyAttribute, Class, Doc, DocIndexState, IndexKind, Obj, Ref } from './classes' -import { FindResult } from './storage' +import { Account, AnyAttribute, Class, Doc, DocData, DocIndexState, IndexKind, Obj, Ref } from './classes' import core from './component' +import { Hierarchy } from './hierarchy' +import { FindResult } from './storage' function toHex (value: number, chars: number): string { const result = value.toString(16) @@ -199,3 +200,23 @@ export function concatLink (host: string, path: string): string { return `${host}${path}` } } + +/** + * @public + */ +export function fillDefaults ( + hierarchy: Hierarchy, + object: DocData | T, + _class: Ref> +): DocData | T { + const baseClass = hierarchy.isDerived(_class, core.class.AttachedDoc) ? core.class.AttachedDoc : core.class.Doc + const attributes = hierarchy.getAllAttributes(_class, baseClass) + for (const attribute of attributes) { + if (attribute[1].defaultValue !== undefined) { + if ((object as any)[attribute[0]] === undefined) { + ;(object as any)[attribute[0]] = attribute[1].defaultValue + } + } + } + return object +} diff --git a/plugins/contact-resources/src/components/CreateOrganization.svelte b/plugins/contact-resources/src/components/CreateOrganization.svelte index d5018bb3de..6b3cdbd1f4 100644 --- a/plugins/contact-resources/src/components/CreateOrganization.svelte +++ b/plugins/contact-resources/src/components/CreateOrganization.svelte @@ -14,7 +14,7 @@ --> diff --git a/plugins/setting-resources/src/components/EditAttribute.svelte b/plugins/setting-resources/src/components/EditAttribute.svelte index d154dc5441..4d2562adca 100644 --- a/plugins/setting-resources/src/components/EditAttribute.svelte +++ b/plugins/setting-resources/src/components/EditAttribute.svelte @@ -26,6 +26,7 @@ let name: string let type: Type | undefined = attribute.type let index: IndexKind | undefined = attribute.index + let defaultValue: any | undefined = attribute.defaultValue let is: AnyComponent | undefined const client = getClient() @@ -40,6 +41,9 @@ if (newLabel !== attribute.label) { update.label = newLabel } + if (defaultValue !== attribute.defaultValue) { + update.defaultValue = defaultValue + } if (!exist) { if (index !== attribute.index) { update.index = index @@ -83,6 +87,7 @@ const handleChange = (e: any) => { type = e.detail?.type index = e.detail?.index + defaultValue = e.detail?.defaultValue } @@ -119,6 +124,7 @@ {is} props={{ type, + defaultValue, editable: !exist }} on:change={handleChange} diff --git a/plugins/setting-resources/src/components/typeEditors/EnumTypeEditor.svelte b/plugins/setting-resources/src/components/typeEditors/EnumTypeEditor.svelte index 7dc5d66bcd..6056131bd2 100644 --- a/plugins/setting-resources/src/components/typeEditors/EnumTypeEditor.svelte +++ b/plugins/setting-resources/src/components/typeEditors/EnumTypeEditor.svelte @@ -17,6 +17,7 @@ import { TypeEnum } from '@hcengineering/model' import presentation, { getClient } from '@hcengineering/presentation' import { Button, Label, showPopup } from '@hcengineering/ui' + import { EnumEditor } from '@hcengineering/view-resources' import { createEventDispatcher } from 'svelte' import setting from '../../plugin' import EnumSelect from './EnumSelect.svelte' @@ -24,11 +25,12 @@ export let type: EnumOf | undefined export let editable: boolean = true export let value: Enum | undefined + export let defaultValue: string | undefined const client = getClient() const dispatch = createEventDispatcher() - $: value && dispatch('change', { type: TypeEnum(value._id) }) + $: value && changeEnum(value) $: ref = value?._id ?? type?.of const create = { @@ -36,6 +38,11 @@ component: setting.component.EditEnum } + function changeEnum (value: Enum) { + type = TypeEnum(value._id) + dispatch('change', { type, defaultValue }) + } + async function updateSelected (ref: Ref | undefined) { value = ref !== undefined ? await client.findOne(core.class.Enum, { _id: ref }) : undefined } @@ -48,24 +55,42 @@ } -
-