chromatic-lattice/src/features/bars/bar.ts

141 lines
4 KiB
TypeScript
Raw Normal View History

2022-03-04 03:39:48 +00:00
import BarComponent from "features/bars/Bar.vue";
2022-01-14 04:25:47 +00:00
import {
CoercableComponent,
Component,
GatherProps,
2022-01-14 04:25:47 +00:00
getUniqueID,
Replace,
setDefault,
StyleValue,
Visibility
2022-03-04 03:39:48 +00:00
} from "features/feature";
import { DecimalSource } from "util/bignum";
2022-01-14 04:25:47 +00:00
import {
Computable,
GetComputableType,
GetComputableTypeWithDefault,
processComputable,
ProcessedComputable
2022-03-04 03:39:48 +00:00
} from "util/computed";
import { createLazyProxy } from "util/proxies";
2022-01-14 04:25:47 +00:00
export const BarType = Symbol("Bar");
export enum Direction {
Up = "Up",
Down = "Down",
Left = "Left",
Right = "Right",
Default = "Up"
}
export interface BarOptions {
visibility?: Computable<Visibility>;
width: Computable<number>;
height: Computable<number>;
direction: Computable<Direction>;
style?: Computable<StyleValue>;
classes?: Computable<Record<string, boolean>>;
borderStyle?: Computable<StyleValue>;
baseStyle?: Computable<StyleValue>;
textStyle?: Computable<StyleValue>;
fillStyle?: Computable<StyleValue>;
progress: Computable<DecimalSource>;
display?: Computable<CoercableComponent>;
mark?: Computable<boolean | string>;
}
2022-03-09 01:40:51 +00:00
export interface BaseBar {
2022-01-14 04:25:47 +00:00
id: string;
type: typeof BarType;
[Component]: typeof BarComponent;
[GatherProps]: () => Record<string, unknown>;
2022-01-14 04:25:47 +00:00
}
export type Bar<T extends BarOptions> = Replace<
T & BaseBar,
{
visibility: GetComputableTypeWithDefault<T["visibility"], Visibility.Visible>;
width: GetComputableType<T["width"]>;
height: GetComputableType<T["height"]>;
direction: GetComputableType<T["direction"]>;
style: GetComputableType<T["style"]>;
classes: GetComputableType<T["classes"]>;
borderStyle: GetComputableType<T["borderStyle"]>;
baseStyle: GetComputableType<T["baseStyle"]>;
textStyle: GetComputableType<T["textStyle"]>;
fillStyle: GetComputableType<T["fillStyle"]>;
progress: GetComputableType<T["progress"]>;
display: GetComputableType<T["display"]>;
mark: GetComputableType<T["mark"]>;
}
>;
export type GenericBar = Replace<
Bar<BarOptions>,
{
visibility: ProcessedComputable<Visibility>;
}
>;
export function createBar<T extends BarOptions>(optionsFunc: () => T & ThisType<Bar<T>>): Bar<T> {
return createLazyProxy(() => {
const bar: T & Partial<BaseBar> = optionsFunc();
bar.id = getUniqueID("bar-");
bar.type = BarType;
bar[Component] = BarComponent;
2022-01-14 04:25:47 +00:00
processComputable(bar as T, "visibility");
setDefault(bar, "visibility", Visibility.Visible);
processComputable(bar as T, "width");
processComputable(bar as T, "height");
processComputable(bar as T, "direction");
processComputable(bar as T, "style");
processComputable(bar as T, "classes");
processComputable(bar as T, "borderStyle");
processComputable(bar as T, "baseStyle");
processComputable(bar as T, "textStyle");
processComputable(bar as T, "fillStyle");
processComputable(bar as T, "progress");
processComputable(bar as T, "display");
processComputable(bar as T, "mark");
2022-01-14 04:25:47 +00:00
bar[GatherProps] = function (this: GenericBar) {
const {
progress,
width,
height,
direction,
display,
visibility,
style,
classes,
borderStyle,
textStyle,
baseStyle,
fillStyle,
mark,
id
} = this;
return {
progress,
width,
height,
direction,
display,
visibility,
style,
classes,
borderStyle,
textStyle,
baseStyle,
fillStyle,
mark,
id
};
};
return bar as unknown as Bar<T>;
});
2022-01-14 04:25:47 +00:00
}