aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/LinkManager.ts
diff options
context:
space:
mode:
authorFawn <fangrui_tong@brown.edu>2019-06-14 15:24:35 -0400
committerFawn <fangrui_tong@brown.edu>2019-06-14 15:24:35 -0400
commitf54496c7aa930e385e77aaf37df8d51db733e3a2 (patch)
tree6c96d5fe127bf65888511d2529fa2e7f8dc1e385 /src/client/util/LinkManager.ts
parent8bcd3567df7c49523638f0b935ecd09b1acad45d (diff)
cleaned up link code
Diffstat (limited to 'src/client/util/LinkManager.ts')
-rw-r--r--src/client/util/LinkManager.ts166
1 files changed, 55 insertions, 111 deletions
diff --git a/src/client/util/LinkManager.ts b/src/client/util/LinkManager.ts
index 929fcbf21..aaed7388d 100644
--- a/src/client/util/LinkManager.ts
+++ b/src/client/util/LinkManager.ts
@@ -1,71 +1,9 @@
-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 { observable } from "mobx";
import { StrCast, Cast } from "../../new_fields/Types";
-import { Doc } from "../../new_fields/Doc";
+import { Doc, DocListCast } from "../../new_fields/Doc";
import { listSpec } from "../../new_fields/Schema";
import { List } from "../../new_fields/List";
-import { string } from "prop-types";
-import { Docs } from "../documents/Documents";
-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 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);
-
- let print: string[] = [];
- Cast(link.anchor1Groups, listSpec(Doc), []).forEach(doc => {
- if (doc instanceof Doc) {
- print.push(StrCast(doc.type));
- }
- });
- console.log("set anchor's groups as", print);
- } else {
- link.anchor2Groups = new List<Doc>(groups);
-
- let print: string[] = [];
- Cast(link.anchor2Groups, listSpec(Doc), []).forEach(doc => {
- if (doc instanceof Doc) {
- print.push(StrCast(doc.type));
- }
- });
- console.log("set anchor's groups as", print);
- }
- }
-
- export function removeGroupFromAnchor(link: Doc, anchor: Doc, groupType: string) {
- let groups = Doc.AreProtosEqual(anchor, Cast(link.anchor1, Doc, new Doc)) ?
- Cast(link.proto!.anchor1Groups, listSpec(Doc), []) : Cast(link.proto!.anchor2Groups, listSpec(Doc), []);
-
- let newGroups: Doc[] = [];
- groups.forEach(groupDoc => {
- if (groupDoc instanceof Doc && StrCast(groupDoc.type) !== groupType) {
- newGroups.push(groupDoc);
- } // TODO: promise
- });
-
- // let grouptypes: string[] = [];
- // newGroups.forEach(groupDoc => {
- // grouptypes.push(StrCast(groupDoc.type));
- // });
- // console.log("remove anchor's groups as", grouptypes);
-
- LinkUtils.setAnchorGroups(link, anchor, newGroups);
- }
-}
/*
* link doc:
@@ -94,6 +32,7 @@ export class LinkManager {
@observable public allLinks: Array<Doc> = []; // list of link docs
@observable public groupMetadataKeys: Map<string, Array<string>> = new Map(); // map of group type to list of its metadata keys
+ // finds all links that contain the given anchor
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)));
@@ -102,27 +41,19 @@ export class LinkManager {
// 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 => {
+ let groups = LinkManager.Instance.getAnchorGroups(link, anchor);
- // 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
- }
-
+ let groupType = StrCast(groupDoc.type);
+ let group = anchorGroups.get(groupType);
+ if (group) group.push(link);
+ else group = [link];
+ anchorGroups.set(groupType, group);
});
- }
- else {
+ } else {
// if link is in no groups then put it in default group
let group = anchorGroups.get("*");
if (group) group.push(link);
@@ -134,52 +65,38 @@ export class LinkManager {
return anchorGroups;
}
- public findMetadataInGroup(groupType: string) {
+ // returns a list of all metadata docs associated with the given group type
+ public findAllMetadataDocsInGroup(groupType: string): Array<Doc> {
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
- }
- });
-
+ let anchor1Groups = LinkManager.Instance.getAnchorGroups(linkDoc, Cast(linkDoc.anchor1, Doc, new Doc));
+ let anchor2Groups = LinkManager.Instance.getAnchorGroups(linkDoc, Cast(linkDoc.anchor2, Doc, new Doc));
+ anchor1Groups.forEach(groupDoc => { if (StrCast(groupDoc.type).toUpperCase() === groupType.toUpperCase()) md.push(Cast(groupDoc.metadata, Doc, new Doc)); });
+ anchor2Groups.forEach(groupDoc => { if (StrCast(groupDoc.type).toUpperCase() === groupType.toUpperCase()) md.push(Cast(groupDoc.metadata, Doc, new Doc)); });
});
return md;
}
- public deleteGroup(groupType: string) {
+ // removes all group docs from all links with the given group type
+ public deleteGroup(groupType: string): void {
let deleted = LinkManager.Instance.groupMetadataKeys.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);
+ LinkManager.Instance.removeGroupFromAnchor(linkDoc, Cast(linkDoc.anchor1, Doc, new Doc), groupType);
+ LinkManager.Instance.removeGroupFromAnchor(linkDoc, Cast(linkDoc.anchor2, Doc, new Doc), groupType);
});
}
}
+ // removes group doc of given group type only from given anchor on given link
+ public removeGroupFromAnchor(linkDoc: Doc, anchor: Doc, groupType: string) {
+ let groups = LinkManager.Instance.getAnchorGroups(linkDoc, anchor);
+ let newGroups = groups.filter(groupDoc => StrCast(groupDoc.type).toUpperCase() !== groupType.toUpperCase());
+ LinkManager.Instance.setAnchorGroups(linkDoc, anchor, newGroups);
+ }
+
+ // checks if a link with the given anchors exists
public doesLinkExist(anchor1: Doc, anchor2: Doc) {
let allLinks = LinkManager.Instance.allLinks;
let index = allLinks.findIndex(linkDoc => {
@@ -189,4 +106,31 @@ export class LinkManager {
return index !== -1;
}
+ // finds the opposite anchor of a given anchor in a link
+ public findOppositeAnchor(linkDoc: Doc, anchor: Doc): Doc {
+ if (Doc.AreProtosEqual(anchor, Cast(linkDoc.anchor1, Doc, new Doc))) {
+ return Cast(linkDoc.anchor2, Doc, new Doc);
+ } else {
+ return Cast(linkDoc.anchor1, Doc, new Doc);
+ }
+ }
+
+ // 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, new Doc))) {
+ return DocListCast(linkDoc.anchor1Groups);
+ } else {
+ return DocListCast(linkDoc.anchor2Groups);
+ }
+ }
+
+ // sets the groups of the given anchor in the given link
+ public setAnchorGroups(linkDoc: Doc, anchor: Doc, groups: Doc[]) {
+ if (Doc.AreProtosEqual(anchor, Cast(linkDoc.anchor1, Doc, new Doc))) {
+ linkDoc.anchor1Groups = new List<Doc>(groups);
+ } else {
+ linkDoc.anchor2Groups = new List<Doc>(groups);
+ }
+ }
+
} \ No newline at end of file