import { observable, computed, action, trace } from "mobx"; import React = require("react"); import { observer } from "mobx-react"; import './LinkEditor.scss'; import { StrCast, Cast, FieldValue } from "../../../new_fields/Types"; import { Doc } from "../../../new_fields/Doc"; import { LinkManager } from "../../util/LinkManager"; import { Docs } from "../../documents/Documents"; import { Utils } from "../../../Utils"; import { faArrowLeft, faEllipsisV, faTable, faTrash, faCog, faExchangeAlt, faTimes, faPlus } from '@fortawesome/free-solid-svg-icons'; import { library } from "@fortawesome/fontawesome-svg-core"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { SetupDrag } from "../../util/DragManager"; import { SchemaHeaderField, RandomPastel } from "../../../new_fields/SchemaHeaderField"; library.add(faArrowLeft, faEllipsisV, faTable, faTrash, faCog, faExchangeAlt, faTimes, faPlus); interface GroupTypesDropdownProps { groupType: string; setGroupType: (group: string) => void; } // this dropdown could be generalized @observer class GroupTypesDropdown extends React.Component { @observable private _searchTerm: string = this.props.groupType; @observable private _groupType: string = this.props.groupType; @observable private _isEditing: boolean = false; @action createGroup = (groupType: string): void => { this.props.setGroupType(groupType); LinkManager.Instance.addGroupType(groupType); } @action onChange = (val: string): void => { this._searchTerm = val; this._groupType = val; this._isEditing = true; } @action onKeyDown = (e: React.KeyboardEvent): void => { if (e.key === "Enter") { const allGroupTypes = Array.from(LinkManager.Instance.getAllGroupTypes()); const groupOptions = allGroupTypes.filter(groupType => groupType.toUpperCase().indexOf(this._searchTerm.toUpperCase()) > -1); const exactFound = groupOptions.findIndex(groupType => groupType.toUpperCase() === this._searchTerm.toUpperCase()); if (exactFound > -1) { const groupType = groupOptions[exactFound]; this.props.setGroupType(groupType); this._groupType = groupType; } else { this.createGroup(this._searchTerm); this._groupType = this._searchTerm; } this._searchTerm = this._groupType; this._isEditing = false; } } @action onOptionClick = (value: string, createNew: boolean): void => { if (createNew) { this.createGroup(this._searchTerm); this._groupType = this._searchTerm; } else { this.props.setGroupType(value); this._groupType = value; } this._searchTerm = this._groupType; this._isEditing = false; } @action onButtonPointerDown = (): void => { this._isEditing = true; } renderOptions = (): JSX.Element[] | JSX.Element => { if (this._searchTerm === "") return <>; const allGroupTypes = Array.from(LinkManager.Instance.getAllGroupTypes()); const groupOptions = allGroupTypes.filter(groupType => groupType.toUpperCase().indexOf(this._searchTerm.toUpperCase()) > -1); const exactFound = groupOptions.findIndex(groupType => groupType.toUpperCase() === this._searchTerm.toUpperCase()) > -1; const options = groupOptions.map(groupType => { const ref = React.createRef(); return
this.onOptionClick(groupType, false)}>{groupType}
; }); // if search term does not already exist as a group type, give option to create new group type if (!exactFound && this._searchTerm !== "") { const ref = React.createRef(); options.push(
this.onOptionClick(this._searchTerm, true)}>Define new "{this._searchTerm}" relationship
); } return options; } render() { if (this._isEditing || this._groupType === "") { return (
this.onChange(e.target.value)} onKeyDown={this.onKeyDown} autoFocus>
{this.renderOptions()}
); } else { return ; } } } interface LinkMetadataEditorProps { id: string; groupType: string; mdDoc: Doc; mdKey: string; mdValue: string; changeMdIdKey: (id: string, newKey: string) => void; } @observer class LinkMetadataEditor extends React.Component { @observable private _key: string = this.props.mdKey; @observable private _value: string = this.props.mdValue; @observable private _keyError: boolean = false; @action setMetadataKey = (value: string): void => { const groupMdKeys = LinkManager.Instance.getMetadataKeysInGroup(this.props.groupType); // don't allow user to create existing key const newIndex = groupMdKeys.findIndex(key => key.toUpperCase() === value.toUpperCase()); if (newIndex > -1) { this._keyError = true; this._key = value; return; } else { this._keyError = false; } // set new value for key const currIndex = groupMdKeys.findIndex(key => { return StrCast(key).toUpperCase() === this._key.toUpperCase(); }); if (currIndex === -1) console.error("LinkMetadataEditor: key was not found"); groupMdKeys[currIndex] = value; this.props.changeMdIdKey(this.props.id, value); this._key = value; LinkManager.Instance.setMetadataKeysForGroup(this.props.groupType, [...groupMdKeys]); } @action setMetadataValue = (value: string): void => { if (!this._keyError) { this._value = value; this.props.mdDoc[this._key] = value; } } @action removeMetadata = (): void => { const groupMdKeys = LinkManager.Instance.getMetadataKeysInGroup(this.props.groupType); const index = groupMdKeys.findIndex(key => key.toUpperCase() === this._key.toUpperCase()); if (index === -1) console.error("LinkMetadataEditor: key was not found"); groupMdKeys.splice(index, 1); LinkManager.Instance.setMetadataKeysForGroup(this.props.groupType, groupMdKeys); this._key = ""; } render() { return (
this.setMetadataKey(e.target.value)}>: this.setMetadataValue(e.target.value)}>
); } } interface LinkGroupEditorProps { sourceDoc: Doc; linkDoc: Doc; groupDoc: Doc; } @observer export class LinkGroupEditor extends React.Component { private _metadataIds: Map = new Map(); constructor(props: LinkGroupEditorProps) { super(props); const groupMdKeys = LinkManager.Instance.getMetadataKeysInGroup(StrCast(props.groupDoc.type)); groupMdKeys.forEach(key => { this._metadataIds.set(key, Utils.GenerateGuid()); }); } @action setGroupType = (groupType: string): void => { this.props.groupDoc.type = groupType; } removeGroupFromLink = (groupType: string): void => { LinkManager.Instance.removeGroupFromAnchor(this.props.linkDoc, this.props.sourceDoc, groupType); } deleteGroup = (groupType: string): void => { LinkManager.Instance.deleteGroupType(groupType); } copyGroup = async (groupType: string): Promise => { const sourceGroupDoc = this.props.groupDoc; const sourceMdDoc = await Cast(sourceGroupDoc.metadata, Doc); if (!sourceMdDoc) return; const destDoc = LinkManager.Instance.getOppositeAnchor(this.props.linkDoc, this.props.sourceDoc); // let destGroupList = LinkManager.Instance.getAnchorGroups(this.props.linkDoc, destDoc); const keys = LinkManager.Instance.getMetadataKeysInGroup(groupType); // create new metadata doc with copied kvp const destMdDoc = new Doc(); destMdDoc.anchor1 = StrCast(sourceMdDoc.anchor2); destMdDoc.anchor2 = StrCast(sourceMdDoc.anchor1); keys.forEach(key => { const val = sourceMdDoc[key] === undefined ? "" : StrCast(sourceMdDoc[key]); destMdDoc[key] = val; }); // create new group doc with new metadata doc const destGroupDoc = new Doc(); destGroupDoc.type = groupType; destGroupDoc.metadata = destMdDoc; if (destDoc) { LinkManager.Instance.addGroupToAnchor(this.props.linkDoc, destDoc, destGroupDoc, true); } } @action addMetadata = (groupType: string): void => { this._metadataIds.set("new key", Utils.GenerateGuid()); const mdKeys = LinkManager.Instance.getMetadataKeysInGroup(groupType); // only add "new key" if there is no other key with value "new key"; prevents spamming if (mdKeys.indexOf("new key") === -1) mdKeys.push("new key"); LinkManager.Instance.setMetadataKeysForGroup(groupType, mdKeys); } // for key rendering purposes changeMdIdKey = (id: string, newKey: string) => { this._metadataIds.set(newKey, id); } renderMetadata = (): JSX.Element[] => { const metadata: Array = []; const groupDoc = this.props.groupDoc; const mdDoc = FieldValue(Cast(groupDoc.metadata, Doc)); if (!mdDoc) { return []; } const groupType = StrCast(groupDoc.type); const groupMdKeys = LinkManager.Instance.getMetadataKeysInGroup(groupType); groupMdKeys.forEach((key) => { const val = StrCast(mdDoc[key]); metadata.push( ); }); return metadata; } viewGroupAsTable = (groupType: string): JSX.Element => { const keys = LinkManager.Instance.getMetadataKeysInGroup(groupType); const index = keys.indexOf(""); if (index > -1) keys.splice(index, 1); const cols = ["anchor1", "anchor2", ...[...keys]].map(c => new SchemaHeaderField(c, "#f1efeb")); const docs: Doc[] = LinkManager.Instance.getAllMetadataDocsInGroup(groupType); const createTable = action(() => Docs.Create.SchemaDocument(cols, docs, { _width: 500, _height: 300, title: groupType + " table" })); const ref = React.createRef(); return
; } render() { const groupType = StrCast(this.props.groupDoc.type); // if ((groupType && LinkManager.Instance.getMetadataKeysInGroup(groupType).length > 0) || groupType === "") { let buttons; if (groupType === "") { buttons = ( <> ); } else { buttons = ( <> {this.viewGroupAsTable(groupType)} ); } return (

type:

{this.renderMetadata().length > 0 ?

metadata:

: <>} {this.renderMetadata()}
{buttons}
); } } interface LinkEditorProps { sourceDoc: Doc; linkDoc: Doc; showLinks: () => void; } @observer export class LinkEditor extends React.Component { @action deleteLink = (): void => { LinkManager.Instance.deleteLink(this.props.linkDoc); this.props.showLinks(); } @action addGroup = (): void => { // create new metadata document for group const mdDoc = new Doc(); mdDoc.anchor1 = this.props.sourceDoc.title; const opp = LinkManager.Instance.getOppositeAnchor(this.props.linkDoc, this.props.sourceDoc); if (opp) { mdDoc.anchor2 = opp.title; } // create new group document const groupDoc = new Doc(); groupDoc.type = ""; groupDoc.metadata = mdDoc; LinkManager.Instance.addGroupToAnchor(this.props.linkDoc, this.props.sourceDoc, groupDoc); } render() { const destination = LinkManager.Instance.getOppositeAnchor(this.props.linkDoc, this.props.sourceDoc); const groupList = LinkManager.Instance.getAnchorGroups(this.props.linkDoc, this.props.sourceDoc); const groups = groupList.map(groupDoc => { return ; }); if (destination) { return (

editing link to: {destination.proto!.title}

Relationships:
{groups.length > 0 ? groups :
There are currently no relationships associated with this link.
}
); } } }