Skip to content
Snippets Groups Projects
transform.ts 2.89 KiB
Newer Older
David Sehnal's avatar
David Sehnal committed
/**
 * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
 *
 * @author David Sehnal <david.sehnal@gmail.com>
 */

David Sehnal's avatar
David Sehnal committed
import { StateObject } from './object';
David Sehnal's avatar
David Sehnal committed
import { Transformer } from './transformer';
import { UUID } from 'mol-util';
export interface Transform<A extends StateObject = StateObject, B extends StateObject = StateObject, P extends {} = {}> {
    readonly parent: Transform.Ref,
David Sehnal's avatar
David Sehnal committed
    readonly transformer: Transformer<A, B, P>,
    readonly props: Transform.Props,
    readonly ref: Transform.Ref,
David Sehnal's avatar
David Sehnal committed
    readonly version: string
David Sehnal's avatar
David Sehnal committed
}

export namespace Transform {
David Sehnal's avatar
David Sehnal committed
    export type Ref = string
David Sehnal's avatar
David Sehnal committed

    export const RootRef = '-=root=-' as Ref;
David Sehnal's avatar
David Sehnal committed

    export interface Props {
        tag?: string
        isGhost?: boolean,
        isBinding?: boolean,
        // determine if the corresponding cell can be deleted by the user.
        isLocked?: boolean
    }

    export interface Options {
        ref?: string,
David Sehnal's avatar
David Sehnal committed
        props?: Props
    export function create<A extends StateObject, B extends StateObject, P extends {} = {}>(parent: Ref, transformer: Transformer<A, B, P>, params?: P, options?: Options): Transform<A, B, P> {
        const ref = options && options.ref ? options.ref : UUID.create22() as string as Ref;
            parent,
            props: (options && options.props) || { },
David Sehnal's avatar
David Sehnal committed
            version: UUID.create22()
    export function withParams<T>(t: Transform, params: any): Transform {
        return { ...t, params, version: UUID.create22() };
    }

David Sehnal's avatar
David Sehnal committed
    export function createRoot(): Transform {
        return create(RootRef, Transformer.ROOT, {}, { ref: RootRef });
        parent: string,
        transformer: string,
        params: any,
        props: Props,
David Sehnal's avatar
David Sehnal committed
        version: string
    }

    function _id(x: any) { return x; }
    export function toJSON(t: Transform): Serialized {
        const pToJson = t.transformer.definition.customSerialization
            ? t.transformer.definition.customSerialization.toJSON
            : _id;
        return {
            parent: t.parent,
            transformer: t.transformer.id,
            params: t.params ? pToJson(t.params) : void 0,
            props: t.props,
David Sehnal's avatar
David Sehnal committed
            version: t.version
        };
    }

    export function fromJSON(t: Serialized): Transform {
        const transformer = Transformer.get(t.transformer);
        const pFromJson = transformer.definition.customSerialization
            ? transformer.definition.customSerialization.toJSON
            : _id;
        return {
            parent: t.parent as Ref,
            params: t.params ? pFromJson(t.params) : void 0,
            props: t.props,
David Sehnal's avatar
David Sehnal committed
            ref: t.ref as Ref,
David Sehnal's avatar
David Sehnal committed
            version: t.version