diff options
author | Tyler Schicke <tyler_schicke@brown.edu> | 2019-05-07 16:29:02 -0400 |
---|---|---|
committer | Tyler Schicke <tyler_schicke@brown.edu> | 2019-05-07 16:29:02 -0400 |
commit | 14c776b6d30e0bc0d5b3712f28e4b9f1170eae3b (patch) | |
tree | 5255d8cce8a72a5b09cc1ad58661e2176295467a /src/debug | |
parent | e19fdbba4cf672aee5bfb59b91b6162431d146d3 (diff) | |
parent | 26141a697ae52a7edf3cc6845ce2153111f8860e (diff) |
Merge branch 'master' of github-tsch-brown:browngraphicslab/Dash-Web into new_search
Diffstat (limited to 'src/debug')
-rw-r--r-- | src/debug/Test.tsx | 91 | ||||
-rw-r--r-- | src/debug/Viewer.tsx | 368 |
2 files changed, 216 insertions, 243 deletions
diff --git a/src/debug/Test.tsx b/src/debug/Test.tsx index 7d72a1ba0..57221aa39 100644 --- a/src/debug/Test.tsx +++ b/src/debug/Test.tsx @@ -1,48 +1,41 @@ import * as React from 'react'; import * as ReactDOM from 'react-dom'; -import { serialize, deserialize, map } from 'serializr'; -import { URLField, Doc, createSchema, makeInterface, makeStrictInterface, List, ListSpec } from '../fields/NewDoc'; import { SerializationHelper } from '../client/util/SerializationHelper'; -import { Search } from '../server/Search'; -import { restProperty } from 'babel-types'; -import * as rp from 'request-promise'; +import { createSchema, makeInterface, makeStrictInterface, listSpec } from '../new_fields/Schema'; +import { ImageField } from '../new_fields/URLField'; +import { Doc } from '../new_fields/Doc'; +import { List } from '../new_fields/List'; + const schema1 = createSchema({ hello: "number", test: "string", fields: "boolean", - url: URLField, + url: ImageField, testDoc: Doc }); -const TestDoc = makeInterface(schema1); -type TestDoc = makeInterface<typeof schema1>; +type TestDoc = makeInterface<[typeof schema1]>; +const TestDoc: (doc?: Doc) => TestDoc = makeInterface(schema1); const schema2 = createSchema({ - hello: URLField, + hello: ImageField, test: "boolean", - fields: { List: "number" } as ListSpec<number>, + fields: listSpec("number"), url: "number", - testDoc: URLField + testDoc: ImageField }); const Test2Doc = makeStrictInterface(schema2); type Test2Doc = makeStrictInterface<typeof schema2>; -const schema3 = createSchema({ - test: "boolean", -}); - -const Test3Doc = makeStrictInterface(schema3); -type Test3Doc = makeStrictInterface<typeof schema3>; - const assert = (bool: boolean) => { if (!bool) throw new Error(); }; class Test extends React.Component { onClick = () => { - const url = new URLField(new URL("http://google.com")); + const url = new ImageField(new URL("http://google.com")); const doc = new Doc(); const doc2 = new Doc(); doc.hello = 5; @@ -52,48 +45,34 @@ class Test extends React.Component { //doc.testDoc = doc2; - // const test1: TestDoc = TestDoc(doc); - // const test2: Test2Doc = Test2Doc(doc); - // assert(test1.hello === 5); - // assert(test1.fields === undefined); - // assert(test1.test === "hello doc"); - // assert(test1.url === url); - // //assert(test1.testDoc === doc2); - // test1.myField = 20; - // assert(test1.myField === 20); - - // assert(test2.hello === undefined); - // // assert(test2.fields === "test"); - // assert(test2.test === undefined); - // assert(test2.url === undefined); - // assert(test2.testDoc === undefined); - // test2.url = 35; - // assert(test2.url === 35); - // const l = new List<number>(); - // //TODO push, and other array functions don't go through the proxy - // l.push(1); - // //TODO currently length, and any other string fields will get serialized - // l.length = 3; - // l[2] = 5; - // console.log(l.slice()); - // console.log(SerializationHelper.Serialize(l)); - } + const test1: TestDoc = TestDoc(doc); + assert(test1.hello === 5); + assert(test1.fields === undefined); + assert(test1.test === "hello doc"); + assert(test1.url === url); + assert(test1.testDoc === doc2); + test1.myField = 20; + assert(test1.myField === 20); - onEnter = async (e: any) => { - var key = e.keyCode || e.which; - if (key === 13) { - var query = e.target.value; - await rp.get('http://localhost:1050/search', { - qs: { - query - } - }); - } + const test2: Test2Doc = Test2Doc(doc); + assert(test2.hello === undefined); + // assert(test2.fields === "test"); + assert(test2.test === undefined); + assert(test2.url === undefined); + assert(test2.testDoc === undefined); + test2.url = 35; + assert(test2.url === 35); + const l = new List<Doc>(); + //TODO push, and other array functions don't go through the proxy + l.push(doc2); + //TODO currently length, and any other string fields will get serialized + doc.list = l; + console.log(l.slice()); } render() { return <div><button onClick={this.onClick}>Click me</button> - <input onKeyPress={this.onEnter}></input> + {/* <input onKeyPress={this.onEnter}></input> */} </div>; } } diff --git a/src/debug/Viewer.tsx b/src/debug/Viewer.tsx index 857da1ebb..4cac09dee 100644 --- a/src/debug/Viewer.tsx +++ b/src/debug/Viewer.tsx @@ -3,190 +3,184 @@ import "normalize.css"; import * as React from 'react'; import * as ReactDOM from 'react-dom'; import { observer } from 'mobx-react'; -import { Document } from '../fields/Document'; -import { BasicField } from '../fields/BasicField'; -import { ListField } from '../fields/ListField'; -import { Key } from '../fields/Key'; -import { Opt, Field } from '../fields/Field'; -import { Server } from '../client/Server'; - -configure({ - enforceActions: "observed" -}); - -@observer -class FieldViewer extends React.Component<{ field: BasicField<any> }> { - render() { - return <span>{JSON.stringify(this.props.field.Data)} ({this.props.field.Id})</span>; - } -} - -@observer -class KeyViewer extends React.Component<{ field: Key }> { - render() { - return this.props.field.Name; - } -} - -@observer -class ListViewer extends React.Component<{ field: ListField<Field> }>{ - @observable - expanded = false; - - render() { - let content; - if (this.expanded) { - content = ( - <div> - {this.props.field.Data.map(field => <DebugViewer fieldId={field.Id} key={field.Id} />)} - </div> - ); - } else { - content = <>[...] ({this.props.field.Id})</>; - } - return ( - <div> - <button onClick={action(() => this.expanded = !this.expanded)}>Toggle</button> - {content} - </div > - ); - } -} - -@observer -class DocumentViewer extends React.Component<{ field: Document }> { - private keyMap: ObservableMap<string, Key> = new ObservableMap; - - private disposer?: Lambda; - - componentDidMount() { - let f = () => { - Array.from(this.props.field._proxies.keys()).forEach(id => { - if (!this.keyMap.has(id)) { - Server.GetField(id, (field) => { - if (field && field instanceof Key) { - this.keyMap.set(id, field); - } - }); - } - }); - }; - this.disposer = this.props.field._proxies.observe(f); - f(); - } - - componentWillUnmount() { - if (this.disposer) { - this.disposer(); - } - } - - render() { - let fields = Array.from(this.props.field._proxies.entries()).map(kv => { - let key = this.keyMap.get(kv[0]); - return ( - <div key={kv[0]}> - <b>({key ? key.Name : kv[0]}): </b> - <DebugViewer fieldId={kv[1]}></DebugViewer> - </div> - ); - }); - return ( - <div> - Document ({this.props.field.Id}) - <div style={{ paddingLeft: "25px" }}> - {fields} - </div> - </div> - ); - } -} - -@observer -class DebugViewer extends React.Component<{ fieldId: string }> { - @observable - private field?: Field; - - @observable - private error?: string; - - constructor(props: { fieldId: string }) { - super(props); - this.update(); - } - - update() { - Server.GetField(this.props.fieldId, action((field: Opt<Field>) => { - this.field = field; - if (!field) { - this.error = `Field with id ${this.props.fieldId} not found`; - } - })); - - } - - render() { - let content; - if (this.field) { - // content = this.field.ToJson(); - if (this.field instanceof ListField) { - content = (<ListViewer field={this.field} />); - } else if (this.field instanceof Document) { - content = (<DocumentViewer field={this.field} />); - } else if (this.field instanceof BasicField) { - content = (<FieldViewer field={this.field} />); - } else if (this.field instanceof Key) { - content = (<KeyViewer field={this.field} />); - } else { - content = (<span>Unrecognized field type</span>); - } - } else if (this.error) { - content = <span>Field <b>{this.props.fieldId}</b> not found <button onClick={() => this.update()}>Refresh</button></span>; - } else { - content = <span>Field loading: {this.props.fieldId}</span>; - } - return content; - } -} - -@observer -class Viewer extends React.Component { - @observable - private idToAdd: string = ''; - - @observable - private ids: string[] = []; - - @action - inputOnChange = (e: React.ChangeEvent<HTMLInputElement>) => { - this.idToAdd = e.target.value; - } - - @action - onKeyPress = (e: React.KeyboardEvent<HTMLDivElement>) => { - if (e.key === "Enter") { - this.ids.push(this.idToAdd); - this.idToAdd = ""; - } - } - - render() { - return ( - <> - <input value={this.idToAdd} - onChange={this.inputOnChange} - onKeyDown={this.onKeyPress} /> - <div> - {this.ids.map(id => <DebugViewer fieldId={id} key={id}></DebugViewer>)} - </div> - </> - ); - } -} - -ReactDOM.render(( - <div style={{ position: "absolute", width: "100%", height: "100%" }}> - <Viewer /> - </div>), - document.getElementById('root') -);
\ No newline at end of file + +// configure({ +// enforceActions: "observed" +// }); + +// @observer +// class FieldViewer extends React.Component<{ field: BasicField<any> }> { +// render() { +// return <span>{JSON.stringify(this.props.field.Data)} ({this.props.field.Id})</span>; +// } +// } + +// @observer +// class KeyViewer extends React.Component<{ field: Key }> { +// render() { +// return this.props.field.Name; +// } +// } + +// @observer +// class ListViewer extends React.Component<{ field: ListField<Field> }>{ +// @observable +// expanded = false; + +// render() { +// let content; +// if (this.expanded) { +// content = ( +// <div> +// {this.props.field.Data.map(field => <DebugViewer fieldId={field.Id} key={field.Id} />)} +// </div> +// ); +// } else { +// content = <>[...] ({this.props.field.Id})</>; +// } +// return ( +// <div> +// <button onClick={action(() => this.expanded = !this.expanded)}>Toggle</button> +// {content} +// </div > +// ); +// } +// } + +// @observer +// class DocumentViewer extends React.Component<{ field: Document }> { +// private keyMap: ObservableMap<string, Key> = new ObservableMap; + +// private disposer?: Lambda; + +// componentDidMount() { +// let f = () => { +// Array.from(this.props.field._proxies.keys()).forEach(id => { +// if (!this.keyMap.has(id)) { +// Server.GetField(id, (field) => { +// if (field && field instanceof Key) { +// this.keyMap.set(id, field); +// } +// }); +// } +// }); +// }; +// this.disposer = this.props.field._proxies.observe(f); +// f(); +// } + +// componentWillUnmount() { +// if (this.disposer) { +// this.disposer(); +// } +// } + +// render() { +// let fields = Array.from(this.props.field._proxies.entries()).map(kv => { +// let key = this.keyMap.get(kv[0]); +// return ( +// <div key={kv[0]}> +// <b>({key ? key.Name : kv[0]}): </b> +// <DebugViewer fieldId={kv[1]}></DebugViewer> +// </div> +// ); +// }); +// return ( +// <div> +// Document ({this.props.field.Id}) +// <div style={{ paddingLeft: "25px" }}> +// {fields} +// </div> +// </div> +// ); +// } +// } + +// @observer +// class DebugViewer extends React.Component<{ fieldId: string }> { +// @observable +// private field?: Field; + +// @observable +// private error?: string; + +// constructor(props: { fieldId: string }) { +// super(props); +// this.update(); +// } + +// update() { +// Server.GetField(this.props.fieldId, action((field: Opt<Field>) => { +// this.field = field; +// if (!field) { +// this.error = `Field with id ${this.props.fieldId} not found`; +// } +// })); + +// } + +// render() { +// let content; +// if (this.field) { +// // content = this.field.ToJson(); +// if (this.field instanceof ListField) { +// content = (<ListViewer field={this.field} />); +// } else if (this.field instanceof Document) { +// content = (<DocumentViewer field={this.field} />); +// } else if (this.field instanceof BasicField) { +// content = (<FieldViewer field={this.field} />); +// } else if (this.field instanceof Key) { +// content = (<KeyViewer field={this.field} />); +// } else { +// content = (<span>Unrecognized field type</span>); +// } +// } else if (this.error) { +// content = <span>Field <b>{this.props.fieldId}</b> not found <button onClick={() => this.update()}>Refresh</button></span>; +// } else { +// content = <span>Field loading: {this.props.fieldId}</span>; +// } +// return content; +// } +// } + +// @observer +// class Viewer extends React.Component { +// @observable +// private idToAdd: string = ''; + +// @observable +// private ids: string[] = []; + +// @action +// inputOnChange = (e: React.ChangeEvent<HTMLInputElement>) => { +// this.idToAdd = e.target.value; +// } + +// @action +// onKeyPress = (e: React.KeyboardEvent<HTMLDivElement>) => { +// if (e.key === "Enter") { +// this.ids.push(this.idToAdd); +// this.idToAdd = ""; +// } +// } + +// render() { +// return ( +// <> +// <input value={this.idToAdd} +// onChange={this.inputOnChange} +// onKeyDown={this.onKeyPress} /> +// <div> +// {this.ids.map(id => <DebugViewer fieldId={id} key={id}></DebugViewer>)} +// </div> +// </> +// ); +// } +// } + +// ReactDOM.render(( +// <div style={{ position: "absolute", width: "100%", height: "100%" }}> +// <Viewer /> +// </div>), +// document.getElementById('root') +// );
\ No newline at end of file |