1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- /**
- * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
- *
- * @author David Sehnal <david.sehnal@gmail.com>
- */
- import { UUID } from 'mol-util';
- import { Transform } from './transform';
- export { StateObject, StateObjectCell }
- interface StateObject<D = any, T extends StateObject.Type = { name: string, typeClass: any }> {
- readonly id: UUID,
- readonly type: T,
- readonly data: D,
- readonly label: string,
- readonly description?: string,
- }
- namespace StateObject {
- export function factory<T extends Type>() {
- return <D = { }>(type: T) => create<D, T>(type);
- }
- export type Type<Cls extends string = string> = { name: string, typeClass: Cls }
- export type Ctor = { new(...args: any[]): StateObject, type: any }
- export function create<Data, T extends Type>(type: T) {
- return class implements StateObject<Data, T> {
- static type = type;
- static is(obj?: StateObject): obj is StateObject<Data, T> { return !!obj && type === obj.type; }
- id = UUID.create22();
- type = type;
- label: string;
- description?: string;
- constructor(public data: Data, props?: { label: string, description?: string }) {
- this.label = props && props.label || type.name;
- this.description = props && props.description;
- }
- }
- }
- }
- interface StateObjectCell {
- transform: Transform,
- // Which object was used as a parent to create data in this cell
- sourceRef: Transform.Ref | undefined,
- version: string
- status: StateObjectCell.Status,
- errorText?: string,
- obj?: StateObject
- }
- namespace StateObjectCell {
- export type Status = 'ok' | 'error' | 'pending' | 'processing'
- export interface State {
- isHidden: boolean,
- isCollapsed: boolean
- }
- export const DefaultState: State = { isHidden: false, isCollapsed: false };
- export function areStatesEqual(a: State, b: State) {
- return a.isHidden !== b.isHidden || a.isCollapsed !== b.isCollapsed;
- }
- export function isStateChange(a: State, b?: Partial<State>) {
- if (!b) return false;
- if (typeof b.isCollapsed !== 'undefined' && a.isCollapsed !== b.isCollapsed) return true;
- if (typeof b.isHidden !== 'undefined' && a.isHidden !== b.isHidden) return true;
- return false;
- }
- }
|