Newer
Older
/**
* 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';
export interface Transform<A extends StateObject = StateObject, B extends StateObject = StateObject, P extends {} = {}> {
readonly ref: Transform.Ref,
David Sehnal
committed
readonly params?: P,
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,
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;
props: (options && options.props) || { },
David Sehnal
committed
params,
export function withParams<T>(t: Transform, params: any): Transform {
return { ...t, params, version: UUID.create22() };
}
export function createRoot(): Transform {
return create(RootRef, Transformer.ROOT, {}, { ref: RootRef });
}
export interface Serialized {
transformer: string,
params: any,
}
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 {
transformer: t.transformer.id,
David Sehnal
committed
params: t.params ? pToJson(t.params) : void 0,
};
}
export function fromJSON(t: Serialized): Transform {
const transformer = Transformer.get(t.transformer);
const pFromJson = transformer.definition.customSerialization
? transformer.definition.customSerialization.toJSON
: _id;
return {
David Sehnal
committed
params: t.params ? pFromJson(t.params) : void 0,