From 2d4e1ac97f87c3e5a7be58291542829d68a19669 Mon Sep 17 00:00:00 2001 From: Bob Zeleznik Date: Tue, 26 Feb 2019 19:35:26 -0500 Subject: simplified WebBox. --- src/fields/Document.ts | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/fields/Document.ts') diff --git a/src/fields/Document.ts b/src/fields/Document.ts index 6193ea56c..5b91de6ed 100644 --- a/src/fields/Document.ts +++ b/src/fields/Document.ts @@ -8,6 +8,7 @@ import { ListField } from "./ListField"; import { Server } from "../client/Server"; import { Types } from "../server/Message"; import { UndoManager } from "../client/util/UndoManager"; +import { HtmlField } from "./HtmlField"; export class Document extends Field { public fields: ObservableMap = new ObservableMap(); @@ -125,6 +126,10 @@ export class Document extends Field { return vval; } + GetHtml(key: Key, defaultVal: string): string { + return this.GetData(key, HtmlField, defaultVal); + } + GetNumber(key: Key, defaultVal: number): number { return this.GetData(key, NumberField, defaultVal); } -- cgit v1.2.3-70-g09d2 From 9c9aea92d9a330f5826735c3c3e07ec35b325cc2 Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Thu, 28 Feb 2019 00:55:04 -0500 Subject: Added GetOrCreateAsync --- src/fields/Document.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/fields/Document.ts') diff --git a/src/fields/Document.ts b/src/fields/Document.ts index 5b91de6ed..0c156b282 100644 --- a/src/fields/Document.ts +++ b/src/fields/Document.ts @@ -102,6 +102,25 @@ export class Document extends Field { return false; } + GetOrCreateAsync(key: Key, ctor: { new(): T }, callback: (field: T) => void): void { + //This currently doesn't deal with prototypes + if (this._proxies.has(key.Id)) { + Server.GetDocumentField(this, key, (field) => { + if (field && field instanceof ctor) { + callback(field); + } else { + let newField = new ctor(); + this.Set(key, newField); + callback(newField); + } + }); + } else { + let newField = new ctor(); + this.Set(key, newField); + callback(newField); + } + } + GetT(key: Key, ctor: { new(...args: any[]): T }, ignoreProto: boolean = false): FieldValue { var getfield = this.Get(key, ignoreProto); if (getfield != FieldWaiting) { -- cgit v1.2.3-70-g09d2 From 5e01c84f8545ce85e288c49562aa61b26e1bf00d Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Tue, 5 Mar 2019 00:35:54 -0500 Subject: Started adding some comments --- src/client/views/EditableView.tsx | 23 +++++++++++++++++++++-- src/fields/Document.ts | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) (limited to 'src/fields/Document.ts') diff --git a/src/client/views/EditableView.tsx b/src/client/views/EditableView.tsx index 88ef67afa..84b1b91c3 100644 --- a/src/client/views/EditableView.tsx +++ b/src/client/views/EditableView.tsx @@ -3,12 +3,30 @@ import { observer } from 'mobx-react'; import { observable, action } from 'mobx'; export interface EditableProps { + /** + * Called to get the initial value for editing + * */ GetValue(): string; + + /** + * Called to apply changes + * @param value - The string entered by the user to set the value to + * @returns `true` if setting the value was successful, `false` otherwise + * */ SetValue(value: string): boolean; + + /** + * The contents to render when not editing + */ contents: any; height: number } +/** + * Customizable view that can be given an arbitrary view to render normally, + * but can also be edited with customizable functions to get a string version + * of the content, and set the value based on the entered string. + */ @observer export class EditableView extends React.Component { @observable @@ -17,8 +35,9 @@ export class EditableView extends React.Component { @action onKeyDown = (e: React.KeyboardEvent) => { if (e.key == "Enter" && !e.ctrlKey) { - this.props.SetValue(e.currentTarget.value); - this.editing = false; + if (this.props.SetValue(e.currentTarget.value)) { + this.editing = false; + } } else if (e.key == "Escape") { this.editing = false; } diff --git a/src/fields/Document.ts b/src/fields/Document.ts index 0c156b282..2e873439c 100644 --- a/src/fields/Document.ts +++ b/src/fields/Document.ts @@ -38,6 +38,22 @@ export class Document extends Field { return this.GetText(KeyStore.Title, ""); } + /** + * Get the field in the document associated with the given key. If the + * associated field has not yet been filled in from the server, a request + * to the server will automatically be sent, the value will be filled in + * when the request is completed, and {@link Field.ts#FieldWaiting} will be returned. + * @param key - The key of the value to get + * @param ignoreProto - If true, ignore any prototype this document + * might have and only search for the value on this immediate document. + * If false (default), search up the prototype chain, starting at this document, + * for a document that has a field associated with the given key, and return the first + * one found. + * + * @returns If the document does not have a field associated with the given key, returns `undefined`. + * If the document does have an associated field, but the field has not been fetched from the server, returns {@link Field.ts#FieldWaiting}. + * If the document does have an associated field, and the field has not been fetched from the server, returns the associated field. + */ Get(key: Key, ignoreProto: boolean = false): FieldValue { let field: FieldValue; if (ignoreProto) { @@ -93,7 +109,17 @@ export class Document extends Field { return field; } + /** + * Tries to get the field associated with the given key, and if there is an + * associated field, calls the given callback with that field. + * @param key - The key of the value to get + * @param callback - A function that will be called with the associated field, if it exists, + * once it is fetched from the server (this may be immediately if the field has already been fetched). + * Note: The callback will not be called if there is no associated field. + * @returns `true` if the field exists on the document and `callback` will be called, and `false` otherwise + */ GetAsync(key: Key, callback: (field: Field) => void): boolean { + //TODO: This should probably check if this.fields contains the key before calling Server.GetDocumentField //This currently doesn't deal with prototypes if (this._proxies.has(key.Id)) { Server.GetDocumentField(this, key, callback); @@ -102,6 +128,12 @@ export class Document extends Field { return false; } + /** + * Same as {@link Document#GetAsync}, except a field of the given type + * will be created if there is no field associated with the given key, + * or the field associated with the given key is not of the given type. + * @param ctor - Constructor of the field type to get. E.g., TextField, ImageField, etc. + */ GetOrCreateAsync(key: Key, ctor: { new(): T }, callback: (field: T) => void): void { //This currently doesn't deal with prototypes if (this._proxies.has(key.Id)) { @@ -121,6 +153,13 @@ export class Document extends Field { } } + /** + * Same as {@link Document#Get}, except that it will additionally + * check if the field is of the given type. + * @param ctor - Constructor of the field type to get. E.g., `TextField`, `ImageField`, etc. + * @returns Same as {@link Document#Get}, except will return `undefined` + * if there is an associated field but it is of the wrong type. + */ GetT(key: Key, ctor: { new(...args: any[]): T }, ignoreProto: boolean = false): FieldValue { var getfield = this.Get(key, ignoreProto); if (getfield != FieldWaiting) { -- cgit v1.2.3-70-g09d2 From 2a7ecc8e179a8020c291da9ed84c877402dbd2f9 Mon Sep 17 00:00:00 2001 From: Hannah Chow Date: Thu, 7 Mar 2019 01:11:32 -0500 Subject: delete working --- src/client/views/nodes/DocumentView.tsx | 4 ++-- src/client/views/nodes/LinkBox.tsx | 23 ++++++++++++++++++----- src/client/views/nodes/LinkMenu.tsx | 1 + src/fields/Document.ts | 8 +++++++- 4 files changed, 28 insertions(+), 8 deletions(-) (limited to 'src/fields/Document.ts') diff --git a/src/client/views/nodes/DocumentView.tsx b/src/client/views/nodes/DocumentView.tsx index 220f7017e..c9afbb150 100644 --- a/src/client/views/nodes/DocumentView.tsx +++ b/src/client/views/nodes/DocumentView.tsx @@ -214,9 +214,9 @@ export class DocumentView extends React.Component { linkDoc.Set(KeyStore.LinkTags, new TextField("Default")); sourceDoc.GetOrCreateAsync(KeyStore.LinkedToDocs, ListField, field => { (field as ListField).Data.push(linkDoc) }); - linkDoc.GetOrCreateAsync(KeyStore.LinkedToDocs, ListField, field => { (field as ListField).Data.push(destDoc) }); + linkDoc.Set(KeyStore.LinkedToDocs, destDoc); destDoc.GetOrCreateAsync(KeyStore.LinkedFromDocs, ListField, field => { (field as ListField).Data.push(linkDoc) }); - linkDoc.GetOrCreateAsync(KeyStore.LinkedFromDocs, ListField, field => { (field as ListField).Data.push(sourceDoc) }); + linkDoc.Set(KeyStore.LinkedFromDocs, sourceDoc); diff --git a/src/client/views/nodes/LinkBox.tsx b/src/client/views/nodes/LinkBox.tsx index d6cb1f612..d493c55e7 100644 --- a/src/client/views/nodes/LinkBox.tsx +++ b/src/client/views/nodes/LinkBox.tsx @@ -31,11 +31,24 @@ export class LinkBox extends React.Component { onDeleteButtonPressed = (e: React.PointerEvent): void => { console.log("delete down"); e.stopPropagation(); - let linkToDoc: Document = this.props.linkDoc.GetData(KeyStore.LinkedToDocs, ListField, [])[0]; - let linkFromDoc: Document = this.props.linkDoc.GetData(KeyStore.LinkedFromDocs, ListField, [])[0]; - - // let linkToDocFromDocs: Document[] = linkToDoc.GetData(KeyStore.LinkedFromDocs, ListField, []); - // linkToDocFromDocs. + this.props.linkDoc.GetTAsync(KeyStore.LinkedFromDocs, Document, field => { + if (field) { + field.GetTAsync>(KeyStore.LinkedToDocs, ListField, field => { + if (field) { + field.Data.splice(field.Data.indexOf(this.props.linkDoc)); + } + }) + } + }); + this.props.linkDoc.GetTAsync(KeyStore.LinkedToDocs, Document, field => { + if (field) { + field.GetTAsync>(KeyStore.LinkedFromDocs, ListField, field => { + if (field) { + field.Data.splice(field.Data.indexOf(this.props.linkDoc)); + } + }) + } + }); } render() { diff --git a/src/client/views/nodes/LinkMenu.tsx b/src/client/views/nodes/LinkMenu.tsx index 4a1f49864..d0909e266 100644 --- a/src/client/views/nodes/LinkMenu.tsx +++ b/src/client/views/nodes/LinkMenu.tsx @@ -34,6 +34,7 @@ export class LinkMenu extends React.Component { })} {linkFrom.map(link => { + let name = link.GetData(KeyStore.Title, TextField, new String); return })} diff --git a/src/fields/Document.ts b/src/fields/Document.ts index 2e873439c..25e239417 100644 --- a/src/fields/Document.ts +++ b/src/fields/Document.ts @@ -1,6 +1,6 @@ import { Key } from "./Key" import { KeyStore } from "./KeyStore"; -import { Field, Cast, FieldWaiting, FieldValue, FieldId } from "./Field" +import { Field, Cast, FieldWaiting, FieldValue, FieldId, Opt } from "./Field" import { NumberField } from "./NumberField"; import { ObservableMap, computed, action } from "mobx"; import { TextField } from "./TextField"; @@ -128,6 +128,12 @@ export class Document extends Field { return false; } + GetTAsync(key: Key, ctor: { new(): T }, callback: (field: Opt) => void): boolean { + return this.GetAsync(key, (field) => { + callback(Cast(field, ctor)); + }) + } + /** * Same as {@link Document#GetAsync}, except a field of the given type * will be created if there is no field associated with the given key, -- cgit v1.2.3-70-g09d2