Skip to content
Snippets Groups Projects
link.ts 1.16 KiB
Newer Older
  • Learn to ignore specific revisions
  • import {GraphElement} from "./graphelement";
    import {Graph} from "./graph";
    import {Node} from "./node";
    import {GLOBAL_PARAMS} from "../helper/serializableitem";
    
    const LINK_PARAMS = ["source", "target", ...GLOBAL_PARAMS];
    const LINK_SIM_PARAMS = ["index"];
    
    export class Link extends GraphElement {
        public source: Node;
        public target: Node;
    
        constructor(graph: Graph) {
            super(graph);
            this.isLink = true;
        }
    
        public delete() {
            return this.graph.deleteLink(this);
        }
    
        public add(graph: Graph = this.graph) {
            this.graph = graph;
            return this.graph.addLink(this);
        }
    
        /**
         * Determines if the given node is part of the link structure.
         * @param node Node to check for.
         * @returns True, if node is either source or target node of link.
         */
        public contains(node: Node): boolean {
            return this.source === node || this.target === node;
        }
    
        public serialize(): any {
            return this.serializeProperties(LINK_PARAMS);
        }
    
        public getCleanInstance(): any {
            return {
                ...this.serialize(),
                ...this.serializeProperties(LINK_SIM_PARAMS)
            };
        }
    }