add Required and RequiredDeep to pieDb, add config functions

This commit is contained in:
Yokozuna59 2023-06-19 17:45:38 +03:00
parent a92571d588
commit c894c1f5b5
2 changed files with 47 additions and 21 deletions

View File

@ -1,6 +1,6 @@
import { log } from '../../logger.js'; import { log } from '../../logger.js';
import { parseDirective as _parseDirective } from '../../directiveUtils.js'; import { parseDirective as _parseDirective } from '../../directiveUtils.js';
import { getConfig } from '../../config.js'; import { getConfig as commonGetConfig } from '../../config.js';
import { sanitizeText } from '../common/common.js'; import { sanitizeText } from '../common/common.js';
import { import {
setAccTitle, setAccTitle,
@ -13,14 +13,15 @@ import {
} from '../../commonDb.js'; } from '../../commonDb.js';
import type { ParseDirectiveDefinition } from '../../diagram-api/types.js'; import type { ParseDirectiveDefinition } from '../../diagram-api/types.js';
import type { PieFields, PieDb, Sections, PieDiagramConfig } from './pieTypes.js'; import type { PieFields, PieDb, Sections, PieDiagramConfig } from './pieTypes.js';
import type { RequiredDeep } from 'type-fest';
export const DEFAULT_PIE_CONFIG: PieDiagramConfig = { export const DEFAULT_PIE_CONFIG: Required<PieDiagramConfig> = {
useMaxWidth: true, useMaxWidth: true,
useWidth: 1200, useWidth: 1200,
textPosition: 0.75, textPosition: 0.75,
} as const; } as const;
export const DEFAULT_PIE_DB: PieFields = { export const DEFAULT_PIE_DB: RequiredDeep<PieFields> = {
sections: {}, sections: {},
showData: false, showData: false,
config: DEFAULT_PIE_CONFIG, config: DEFAULT_PIE_CONFIG,
@ -28,18 +29,38 @@ export const DEFAULT_PIE_DB: PieFields = {
let sections: Sections = DEFAULT_PIE_DB.sections; let sections: Sections = DEFAULT_PIE_DB.sections;
let showData: boolean = DEFAULT_PIE_DB.showData; let showData: boolean = DEFAULT_PIE_DB.showData;
const config: PieDiagramConfig = { const config: Required<PieDiagramConfig> = {
useWidth: DEFAULT_PIE_DB.config.useWidth, useWidth: DEFAULT_PIE_DB.config.useWidth,
useMaxWidth: DEFAULT_PIE_DB.config.useMaxWidth, useMaxWidth: DEFAULT_PIE_DB.config.useMaxWidth,
textPosition: DEFAULT_PIE_DB.config.textPosition, textPosition: DEFAULT_PIE_DB.config.textPosition,
}; };
export const parseDirective: ParseDirectiveDefinition = (statement, context, type) => { const setConfig = (conf: PieDiagramConfig): void => {
config.useWidth = conf.useWidth ?? DEFAULT_PIE_CONFIG.useWidth;
config.useMaxWidth = conf.useMaxWidth ?? DEFAULT_PIE_CONFIG.useMaxWidth;
config.textPosition = conf.textPosition ?? DEFAULT_PIE_CONFIG.textPosition;
};
const getConfig = (): Required<PieDiagramConfig> => config;
const reset = (): void => {
config.useWidth = DEFAULT_PIE_CONFIG.useWidth;
config.useMaxWidth = DEFAULT_PIE_CONFIG.useMaxWidth;
config.textPosition = DEFAULT_PIE_CONFIG.textPosition;
};
const parseDirective: ParseDirectiveDefinition = (statement, context, type) => {
_parseDirective(this, statement, context, type); _parseDirective(this, statement, context, type);
}; };
const clear = (): void => {
sections = JSON.parse(JSON.stringify(DEFAULT_PIE_DB.sections));
showData = DEFAULT_PIE_DB.showData;
commonClear();
};
const addSection = (label: string, value: number): void => { const addSection = (label: string, value: number): void => {
label = sanitizeText(label, getConfig()); label = sanitizeText(label, commonGetConfig());
if (sections[label] === undefined) { if (sections[label] === undefined) {
sections[label] = value; sections[label] = value;
log.debug(`added new section: ${label}, with value: ${value}`); log.debug(`added new section: ${label}, with value: ${value}`);
@ -48,12 +69,6 @@ const addSection = (label: string, value: number): void => {
const getSections = (): Sections => sections; const getSections = (): Sections => sections;
const setShowData = (toggle: boolean): void => {
showData = toggle;
};
const getShowData = (): boolean => showData;
const cleanupValue = (value: string): number => { const cleanupValue = (value: string): number => {
if (value.substring(0, 1) === ':') { if (value.substring(0, 1) === ':') {
value = value.substring(1).trim(); value = value.substring(1).trim();
@ -63,22 +78,26 @@ const cleanupValue = (value: string): number => {
} }
}; };
const clear = (): void => { const setShowData = (toggle: boolean): void => {
sections = JSON.parse(JSON.stringify(DEFAULT_PIE_DB.sections)); showData = toggle;
showData = DEFAULT_PIE_DB.showData;
commonClear();
}; };
const getShowData = (): boolean => showData;
export const db: PieDb = { export const db: PieDb = {
clear, setConfig,
getConfig: () => getConfig().pie, getConfig,
reset,
parseDirective, parseDirective,
clear,
setDiagramTitle, setDiagramTitle,
getDiagramTitle, getDiagramTitle,
setAccTitle, setAccTitle,
getAccTitle, getAccTitle,
setAccDescription, setAccDescription,
getAccDescription, getAccDescription,
addSection, addSection,
getSections, getSections,
cleanupValue, cleanupValue,

View File

@ -47,18 +47,25 @@ export interface D3Sections {
} }
export interface PieDb extends DiagramDB { export interface PieDb extends DiagramDB {
clear: () => void; // config
getConfig: () => PieDiagramConfig | undefined; setConfig: (config: PieDiagramConfig) => void;
getConfig: () => Required<PieDiagramConfig>;
reset: () => void;
// common db
parseDirective: ParseDirectiveDefinition; parseDirective: ParseDirectiveDefinition;
clear: () => void;
setDiagramTitle: (title: string) => void; setDiagramTitle: (title: string) => void;
getDiagramTitle: () => string; getDiagramTitle: () => string;
setAccTitle: (title: string) => void; setAccTitle: (title: string) => void;
getAccTitle: () => string; getAccTitle: () => string;
setAccDescription: (describetion: string) => void; setAccDescription: (describetion: string) => void;
getAccDescription: () => string; getAccDescription: () => string;
// diagram db
addSection: (label: string, value: number) => void; addSection: (label: string, value: number) => void;
cleanupValue: (value: string) => number;
getSections: () => Sections; getSections: () => Sections;
cleanupValue: (value: string) => number;
setShowData: (toggle: boolean) => void; setShowData: (toggle: boolean) => void;
getShowData: () => boolean; getShowData: () => boolean;
} }