aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/LinkManager.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/util/LinkManager.ts')
-rw-r--r--src/client/util/LinkManager.ts106
1 files changed, 29 insertions, 77 deletions
diff --git a/src/client/util/LinkManager.ts b/src/client/util/LinkManager.ts
index 1ba6cff6d..c3ab7c6e7 100644
--- a/src/client/util/LinkManager.ts
+++ b/src/client/util/LinkManager.ts
@@ -25,94 +25,52 @@ export class LinkManager {
public static currentLink: Opt<Doc>;
- public static get Instance(): LinkManager {
- return this._instance || (this._instance = new this());
- }
-
- private constructor() {
- }
-
+ public static get Instance(): LinkManager { return this._instance || (this._instance = new this()); }
- public getAllLinks(): Doc[] {
- const lset = new Set<Doc>(DocListCast(Doc.LinkDBDoc().data));
- SharingManager.Instance.users.forEach(user => {
- DocListCast(user.linkDatabase?.data).map(doc => {
- lset.add(doc);
- });
- });
- return Array.from(lset);
- }
+ public addLink(linkDoc: Doc) { return Doc.AddDocToList(Doc.LinkDBDoc(), "data", linkDoc); }
- public addLink(linkDoc: Doc): boolean {
- return Doc.AddDocToList(Doc.LinkDBDoc(), "data", linkDoc);
- }
+ public deleteLink(linkDoc: Doc) { return Doc.RemoveDocFromList(Doc.LinkDBDoc(), "data", linkDoc); }
- public deleteLink(linkDoc: Doc): boolean {
- return Doc.RemoveDocFromList(Doc.LinkDBDoc(), "data", linkDoc);
- }
+ public deleteAllLinksOnAnchor(anchor: Doc) { LinkManager.Instance.getAllRelatedLinks(anchor).forEach(linkDoc => LinkManager.Instance.deleteLink(linkDoc)); }
- // finds all links that contain the given anchor
- public getAllDirectLinks(anchor: Doc): Doc[] {
- const related = LinkManager.Instance.getAllLinks().filter(link => link).filter(link => {
- const a1 = Cast(link.anchor1, Doc, null);
- const a2 = Cast(link.anchor2, Doc, null);
- const protomatch1 = Doc.AreProtosEqual(anchor, a1);
- const protomatch2 = Doc.AreProtosEqual(anchor, a2);
- return ((a1?.author !== undefined && a2?.author !== undefined) || link.author === Doc.CurrentUserEmail) && (protomatch1 || protomatch2 || Doc.AreProtosEqual(link, anchor));
- });
- return related;
- }
+ public getAllRelatedLinks(anchor: Doc) { return this.relatedLinker(anchor); } // finds all links that contain the given anchor
- relatedLinker = computedFn(function realtedLinker(this: any, anchor: Doc) {
- const related = LinkManager.Instance.getAllDirectLinks(anchor);
- DocListCast(anchor[Doc.LayoutFieldKey(anchor) + "-annotations"]).map(anno => {
- related.push(...LinkManager.Instance.getAllRelatedLinks(anno));
- });
- return related;
- }.bind(this), true);
+ public getAllDirectLinks(anchor: Doc): Doc[] { return this.directLinker(anchor); } // finds all links that contain the given anchor
- // finds all links that contain the given anchor
- public getAllRelatedLinks(anchor: Doc): Doc[] {
- return this.relatedLinker(anchor);
- }
+ public getAllLinks(): Doc[] { return this.allLinks(); }
- public deleteAllLinksOnAnchor(anchor: Doc) {
- const related = LinkManager.Instance.getAllRelatedLinks(anchor);
- related.forEach(linkDoc => LinkManager.Instance.deleteLink(linkDoc));
- }
+ allLinks = computedFn(function allLinks(this: any) {
+ const lset = new Set<Doc>(DocListCast(Doc.LinkDBDoc().data));
+ SharingManager.Instance.users.forEach(user => DocListCast(user.linkDatabase?.data).map(doc => lset.add(doc)));
+ return Array.from(lset);
+ }, true);
- // gets the groups associates with an anchor in a link
- public getAnchorGroups(linkDoc: Doc, anchor?: Doc): Array<Doc> {
- if (Doc.AreProtosEqual(anchor, Cast(linkDoc.anchor1, Doc, null))) {
- return DocListCast(linkDoc.anchor1Groups);
- } else {
- return DocListCast(linkDoc.anchor2Groups);
- }
- }
- public addGroupToAnchor(linkDoc: Doc, anchor: Doc, groupDoc: Doc, replace: boolean = false) {
- Doc.GetProto(linkDoc).linkRelationship = groupDoc.linkRelationship;
- }
+ directLinker = computedFn(function directLinker(this: any, anchor: Doc) {
+ return LinkManager.Instance.getAllLinks().filter(link => {
+ const a1 = Cast(link?.anchor1, Doc, null);
+ const a2 = Cast(link?.anchor2, Doc, null);
+ return link && ((a1?.author !== undefined && a2?.author !== undefined) || link.author === Doc.CurrentUserEmail) && (Doc.AreProtosEqual(anchor, a1) || Doc.AreProtosEqual(anchor, a2) || Doc.AreProtosEqual(link, anchor));
+ });
+ }, true);
- // removes group doc of given group type only from given anchor on given link
- public removeGroupFromAnchor(linkDoc: Doc, anchor: Doc, groupType: string) {
- Doc.GetProto(linkDoc).linkRelationship = "-ungrouped-";
- }
+ relatedLinker = computedFn(function relatedLinker(this: any, anchor: Doc) {
+ const related = LinkManager.Instance.directLinker(anchor);
+ DocListCast(anchor[Doc.LayoutFieldKey(anchor) + "-annotations"]).map(anno => related.push(...LinkManager.Instance.getAllRelatedLinks(anno)));
+ return related;
+ }, true);
// returns map of group type to anchor's links in that group type
public getRelatedGroupedLinks(anchor: Doc): Map<string, Array<Doc>> {
- const related = this.getAllRelatedLinks(anchor);
const anchorGroups = new Map<string, Array<Doc>>();
- related.forEach(link => {
+ this.getAllRelatedLinks(anchor).forEach(link => {
if (!link.linkRelationship || link?.linkRelationship !== "-ungrouped-") {
const group = anchorGroups.get(StrCast(link.linkRelationship));
anchorGroups.set(StrCast(link.linkRelationship), group ? [...group, link] : [link]);
-
} else {
// if link is in no groups then put it in default group
const group = anchorGroups.get("*");
anchorGroups.set("*", group ? [...group, link] : [link]);
}
-
});
return anchorGroups;
}
@@ -120,21 +78,15 @@ export class LinkManager {
// returns a list of all metadata docs associated with the given group type
public getAllMetadataDocsInGroup(groupType: string): Array<Doc> {
const md: Doc[] = [];
- const allLinks = LinkManager.Instance.getAllLinks();
- allLinks.forEach(linkDoc => {
- if (StrCast(linkDoc.linkRelationship).toUpperCase() === groupType.toUpperCase()) { md.push(linkDoc); }
- });
+ LinkManager.Instance.getAllLinks().forEach(linkDoc => StrCast(linkDoc.linkRelationship).toUpperCase() === groupType.toUpperCase() && md.push(linkDoc));
return md;
}
// checks if a link with the given anchors exists
public doesLinkExist(anchor1: Doc, anchor2: Doc): boolean {
- const allLinks = LinkManager.Instance.getAllLinks();
- const index = allLinks.findIndex(linkDoc => {
- return (Doc.AreProtosEqual(Cast(linkDoc.anchor1, Doc, null), anchor1) && Doc.AreProtosEqual(Cast(linkDoc.anchor2, Doc, null), anchor2)) ||
- (Doc.AreProtosEqual(Cast(linkDoc.anchor1, Doc, null), anchor2) && Doc.AreProtosEqual(Cast(linkDoc.anchor2, Doc, null), anchor1));
- });
- return index !== -1;
+ return -1 !== LinkManager.Instance.getAllLinks().findIndex(linkDoc =>
+ (Doc.AreProtosEqual(Cast(linkDoc.anchor1, Doc, null), anchor1) && Doc.AreProtosEqual(Cast(linkDoc.anchor2, Doc, null), anchor2)) ||
+ (Doc.AreProtosEqual(Cast(linkDoc.anchor1, Doc, null), anchor2) && Doc.AreProtosEqual(Cast(linkDoc.anchor2, Doc, null), anchor1)));
}
// finds the opposite anchor of a given anchor in a link