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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
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";
import { string } from "prop-types";
export namespace LinkUtils {
export function findOppositeAnchor(link: Doc, anchor: Doc): Doc {
if (Doc.AreProtosEqual(anchor, Cast(link.anchor1, Doc, new Doc))) {
return Cast(link.anchor2, Doc, new Doc);
} else {
return Cast(link.anchor1, Doc, new Doc);
}
}
// export function getAnchorGroups(link: Doc, anchor: Doc): Doc[] {
// let groups;
// if (Doc.AreProtosEqual(anchor, Cast(link.anchor1, Doc, new Doc))) {
// groups = Cast(link.anchor1Groups, listSpec(Doc), []);
// } else {
// groups = Cast(link.anchor2Groups, listSpec(Doc), []);
// }
// if (groups instanceof Doc[]) {
// return groups;
// } else {
// return [];
// }
// // if (Doc.AreProtosEqual(anchor, Cast(link.anchor1, Doc, new Doc))) {
// // returnCast(link.anchor1Groups, listSpec(Doc), []);
// // } else {
// // return Cast(link.anchor2Groups, listSpec(Doc), []);
// // }
// }
export function setAnchorGroups(link: Doc, anchor: Doc, groups: Doc[]) {
// console.log("setting groups for anchor", anchor["title"]);
if (Doc.AreProtosEqual(anchor, Cast(link.anchor1, Doc, new Doc))) {
link.anchor1Groups = new List<Doc>(groups);
} else {
link.anchor2Groups = new List<Doc>(groups);
}
}
export function removeGroupFromAnchor(link: Doc, anchor: Doc, groupType: string) {
let groups = [];
if (Doc.AreProtosEqual(anchor, Cast(link.anchor1, Doc, new Doc))) {
groups = Cast(link.proto!.anchor1Groups, listSpec(Doc), []);
} else {
groups = Cast(link.proto!.anchor2Groups, listSpec(Doc), []);
}
let newGroups: Doc[] = [];
groups.forEach(groupDoc => {
if (groupDoc instanceof Doc && StrCast(groupDoc.type) !== groupType) {
newGroups.push(groupDoc);
}
});
LinkUtils.setAnchorGroups(link, anchor, newGroups);
}
}
/*
* link doc:
* - anchor1: doc
* - anchor1page: number
* - anchor1groups: list of group docs representing the groups anchor1 categorizes this link/anchor2 in
* - anchor2: doc
* - anchor2page: number
* - anchor2groups: list of group docs representing the groups anchor2 categorizes this link/anchor1 in
*
* group doc:
* - type: string representing the group type/name/category
* - metadata: doc representing the metadata kvps
*
* metadata doc:
* - user defined kvps
*/
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> = []; // list of link docs
@observable public allGroups: Map<string, Array<string>> = new Map(); // map of group type to list of its metadata keys
public findAllRelatedLinks(anchor: Doc): Array<Doc> {
return LinkManager.Instance.allLinks.filter(
link => Doc.AreProtosEqual(anchor, Cast(link.anchor1, Doc, new Doc)) || Doc.AreProtosEqual(anchor, Cast(link.anchor2, Doc, new Doc)));
}
// returns map of group type to anchor's links in that group type
public findRelatedGroupedLinks(anchor: Doc): Map<string, Array<Doc>> {
let related = this.findAllRelatedLinks(anchor);
let anchorGroups = new Map<string, Array<Doc>>();
related.forEach(link => {
// get groups of given anchor categorizes this link/opposite anchor in
let groups = (Doc.AreProtosEqual(anchor, Cast(link.anchor1, Doc, new Doc))) ? Cast(link.anchor1Groups, listSpec(Doc), []) : Cast(link.anchor2Groups, listSpec(Doc), []);
if (groups.length > 0) {
groups.forEach(groupDoc => {
if (groupDoc instanceof Doc) {
let groupType = StrCast(groupDoc.type);
let group = anchorGroups.get(groupType); // TODO: clean this up lol
if (group) group.push(link);
else group = [link];
anchorGroups.set(groupType, group);
} else {
// promise doc
}
});
}
else {
// if link is in no groups then put it in default group
let group = anchorGroups.get("*");
if (group) group.push(link);
else group = [link];
anchorGroups.set("*", group);
}
});
return anchorGroups;
}
public findMetadataInGroup(groupType: string) {
let md: Doc[] = [];
let allLinks = LinkManager.Instance.allLinks;
// for every link find its groups
// allLinks.forEach(linkDoc => {
// let anchor1groups = LinkManager.Instance.findRelatedGroupedLinks(Cast(linkDoc["anchor1"], Doc, new Doc));
// if (anchor1groups.get(groupType)) {
// md.push(linkDoc["anchor1"]["group"])
// }
// })
allLinks.forEach(linkDoc => {
let anchor1Groups = Cast(linkDoc.anchor1Groups, listSpec(Doc), []);
let anchor2Groups = Cast(linkDoc.anchor2Groups, listSpec(Doc), []);
anchor1Groups.forEach(groupDoc => {
if (groupDoc instanceof Doc) {
if (StrCast(groupDoc.type) === groupType) {
md.push(Cast(groupDoc.metadata, Doc, new Doc));
}
} else {
// TODO: promise
}
});
anchor2Groups.forEach(groupDoc => {
if (groupDoc instanceof Doc) {
if (StrCast(groupDoc.type) === groupType) {
md.push(Cast(groupDoc.metadata, Doc, new Doc));
}
} else {
// TODO: promise
}
});
});
return md;
}
public deleteGroup(groupType: string) {
let deleted = LinkManager.Instance.allGroups.delete(groupType);
if (deleted) {
LinkManager.Instance.allLinks.forEach(linkDoc => {
LinkUtils.removeGroupFromAnchor(linkDoc, Cast(linkDoc.anchor1, Doc, new Doc), groupType);
LinkUtils.removeGroupFromAnchor(linkDoc, Cast(linkDoc.anchor2, Doc, new Doc), groupType);
});
}
}
}
|