aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/LinkManager.ts
blob: a6d6493958e21b389921df509e076ba0f272853b (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { observable, computed, action } from "mobx";
import React = require("react");
import { SelectionManager } from "./SelectionManager";
import { observer } from "mobx-react";
import { props } from "bluebird";
import { DocumentView } from "../views/nodes/DocumentView";
import { link } from "fs";
import { StrCast, Cast } from "../../new_fields/Types";
import { Doc } from "../../new_fields/Doc";
import { listSpec } from "../../new_fields/Schema";
import { List } from "../../new_fields/List";


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.anchor1, Doc, new Doc)) || Doc.AreProtosEqual(source, Cast(link.anchor2, 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 => {
            // get groups of given doc
            let groups = (Doc.AreProtosEqual(source, Cast(link.anchor1, Doc, new Doc))) ? Cast(link.anchor1Groups, listSpec(Doc), []) : Cast(link.anchor2Groups, listSpec(Doc), []);
            if (groups) {
                if (groups.length > 0) {
                    groups.forEach(groupDoc => {
                        if (groupDoc instanceof Doc) {
                            let groupType = StrCast(groupDoc.proto!.type);
                            let group = categories.get(groupType); // TODO: clean this up lol
                            if (group) group.push(link);
                            else group = [link];
                            categories.set(groupType, group);
                        } else {
                            // promise doc
                        }

                    })
                }
                else {
                    // if link is in no groups then put it in default group
                    let group = categories.get("*");
                    if (group) group.push(link);
                    else group = [link];
                    categories.set("*", group);
                }
            }


            //     let anchor = this.findOppositeAnchor(link, source);
            //     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.anchor1, Doc, new Doc))) {
            return Cast(link.anchor2, Doc, new Doc);
        } else {
            return Cast(link.anchor1, Doc, new Doc);
        }
    }

    // public findAnchorTags(link: Doc, source: Doc): Doc[] {
    //     if (source)
    // }

}