aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/LinkManager.tsx
blob: 1064eaa22c1850b8b72e521cac6f146a81d4a3da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { observable, computed, action } from "mobx";
import React = require("react");
import { SelectionManager } from "../../util/SelectionManager";
import { observer } from "mobx-react";
import './LinkEditor.scss';
import { props } from "bluebird";
import { DocumentView } from "./DocumentView";
import { link } from "fs";
import { StrCast, Cast } from "../../../new_fields/Types";
import { Doc } from "../../../new_fields/Doc";
import { listSpec } from "../../../new_fields/Schema";


export class LinkManager {
    private static _instance: LinkManager;
    public static get Instance(): LinkManager {
        return this._instance || (this._instance = new this());
    }
    private constructor() {
    }

    @observable
    public allLinks: Array<Doc> = [];

    public findAllRelatedLinks(source: Doc): Array<Doc> {
        let related = LinkManager.Instance.allLinks.filter(
            link => Doc.AreProtosEqual(source, Cast(link.linkedFrom, Doc, new Doc)) || Doc.AreProtosEqual(source, Cast(link.linkedTo, Doc, new Doc)));
        return related;
    }

    public findRelatedGroupedLinks(source: Doc): Map<string, Array<Doc>> {
        let related = this.findAllRelatedLinks(source);
        let categories = new Map();
        related.forEach(link => {
            let group = categories.get(link.linkTags);
            if (group) group.push(link);
            else group = [link];
            categories.set(link.linkTags, group);
        })
        return categories;
    }

    public findOppositeAnchor(link: Doc, source: Doc): Doc {
        if (Doc.AreProtosEqual(source, Cast(link.linkedFrom, Doc, new Doc))) {
            return Cast(link.linkedTo, Doc, new Doc);
        } else {
            return Cast(link.linkedFrom, Doc, new Doc);
        }
    }

}