diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client/views/DocumentDecorations.tsx | 1 | ||||
-rw-r--r-- | src/client/views/Main.tsx | 5 | ||||
-rw-r--r-- | src/database.ts | 41 | ||||
-rw-r--r-- | src/fields/BasicField.ts | 6 | ||||
-rw-r--r-- | src/fields/Document.ts | 15 | ||||
-rw-r--r-- | src/fields/DocumentReference.ts | 10 | ||||
-rw-r--r-- | src/fields/Field.ts | 43 | ||||
-rw-r--r-- | src/fields/ImageField.ts | 14 | ||||
-rw-r--r-- | src/fields/Key.ts | 14 | ||||
-rw-r--r-- | src/fields/ListField.ts | 15 | ||||
-rw-r--r-- | src/fields/NumberField.ts | 14 | ||||
-rw-r--r-- | src/fields/RichTextField.ts | 14 | ||||
-rw-r--r-- | src/fields/TextField.ts | 14 | ||||
-rw-r--r-- | src/server/Message.ts | 66 | ||||
-rw-r--r-- | src/server/index.ts | 3 |
15 files changed, 212 insertions, 63 deletions
diff --git a/src/client/views/DocumentDecorations.tsx b/src/client/views/DocumentDecorations.tsx index 8a94bff36..564cb59f6 100644 --- a/src/client/views/DocumentDecorations.tsx +++ b/src/client/views/DocumentDecorations.tsx @@ -146,7 +146,6 @@ export class DocumentDecorations extends React.Component { <div id="documentDecorations-bottomLeftResizer" className="documentDecorations-resizer" onPointerDown={this.onPointerDown} onContextMenu={(e) => e.preventDefault()}></div> <div id="documentDecorations-bottomResizer" className="documentDecorations-resizer" onPointerDown={this.onPointerDown} onContextMenu={(e) => e.preventDefault()}></div> <div id="documentDecorations-bottomRightResizer" className="documentDecorations-resizer" onPointerDown={this.onPointerDown} onContextMenu={(e) => e.preventDefault()}></div> - </div> ) } diff --git a/src/client/views/Main.tsx b/src/client/views/Main.tsx index da3576066..6a065327b 100644 --- a/src/client/views/Main.tsx +++ b/src/client/views/Main.tsx @@ -13,8 +13,7 @@ import "./Main.scss"; import { ContextMenu } from './ContextMenu'; import { DocumentView } from './nodes/DocumentView'; import { ImageField } from '../../fields/ImageField'; -import { CompileScript } from './util/Scripting'; -import { database } from './database'; +import { CompileScript } from './../util/Scripting'; configure({ @@ -41,8 +40,6 @@ document.addEventListener("pointerdown", action(function (e: PointerEvent) { //runInAction(() => { - let db = new database(); - db.update('1', '2', '3'); let doc1 = Documents.TextDocument({ title: "hello" }); let doc2 = doc1.MakeDelegate(); doc2.Set(KS.X, new NumberField(150)); diff --git a/src/database.ts b/src/database.ts deleted file mode 100644 index a822b15bf..000000000 --- a/src/database.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { action, configure } from 'mobx'; -import * as mongodb from 'mongodb'; - -export class database { - private MongoClient = mongodb.MongoClient; - private url = 'mongodb://localhost:27017/website'; - - public update(id: string, field: string, value: string) { - this.MongoClient.connect(this.url, (err, db) => { - let collection = db.db().collection('documents'); - collection.update({ "id": id }, { $set: { field: value } }); - db.close(); - }); - } - - public delete(id: string) { - this.MongoClient.connect(this.url, (err, db) => { - let collection = db.db().collection('documents'); - collection.remove({ "id": id }); - db.close(); - }); - } - - public insert(kvpairs: JSON) { - this.MongoClient.connect(this.url, (err, db) => { - let collection = db.db().collection('documents'); - collection.insert(kvpairs, () => { }); - db.close(); - }); - } - - public getDocument(id: string) { - var result: Array<JSON>; - this.MongoClient.connect(this.url, (err, db) => { - let collection = db.db().collection('documents'); - collection.find({ "id": id }).toArray((err, db) => { result = db }); - db.close(); - return result[0]; - }); - } -} diff --git a/src/fields/BasicField.ts b/src/fields/BasicField.ts index fb5cc773e..40ead0953 100644 --- a/src/fields/BasicField.ts +++ b/src/fields/BasicField.ts @@ -1,9 +1,9 @@ -import { Field } from "./Field" +import { Field, FIELD_ID } from "./Field" import { observable, computed, action } from "mobx"; export abstract class BasicField<T> extends Field { - constructor(data: T) { - super(); + constructor(data: T, id: FIELD_ID = undefined) { + super(id); this.data = data; } diff --git a/src/fields/Document.ts b/src/fields/Document.ts index 6f9752a8e..e32e03070 100644 --- a/src/fields/Document.ts +++ b/src/fields/Document.ts @@ -6,6 +6,7 @@ import { TextField } from "./TextField"; import { ListField } from "./ListField"; import { findDOMNode } from "react-dom"; import { Server } from "../client/Server"; +import { Types } from "../server/Message"; export class Document extends Field { public fields: ObservableMap<Key, Opt<Field>> = new ObservableMap(); @@ -155,5 +156,19 @@ export class Document extends Field { throw new Error("Method not implemented."); } + ToJson(): { type: Types, data: [string, string][], id: string } { + let fields: [string, string][] = [] + this._proxies.forEach((field, key) => { + if (field) { + fields.push([key.Name, field as string]) + } + }); + + return { + type: Types.Document, + data: fields, + id: this.Id as string + } + } }
\ No newline at end of file diff --git a/src/fields/DocumentReference.ts b/src/fields/DocumentReference.ts index 983b162a3..ab4c4644e 100644 --- a/src/fields/DocumentReference.ts +++ b/src/fields/DocumentReference.ts @@ -1,6 +1,7 @@ -import { Field, Opt, FieldValue } from "./Field"; +import { Field, Opt, FieldValue, FIELD_ID } from "./Field"; import { Document } from "./Document"; import { Key } from "./Key"; +import { Types } from "../server/Message"; export class DocumentReference extends Field { get Key(): Key { @@ -41,4 +42,11 @@ export class DocumentReference extends Field { return ""; } + ToJson(): { type: Types, data: FIELD_ID, id: string } { + return { + type: Types.DocumentReference, + data: this.document.Id, + id: this.Id as string + } + } }
\ No newline at end of file diff --git a/src/fields/Field.ts b/src/fields/Field.ts index 6adee9b61..f55a80db4 100644 --- a/src/fields/Field.ts +++ b/src/fields/Field.ts @@ -1,5 +1,14 @@ import { Utils } from "../Utils"; +import { Types } from "../server/Message"; +import { NumberField } from "./NumberField"; +import { TextField } from "./TextField"; +import { RichTextField } from "./RichTextField"; +import { KeyStore } from "./Key"; +import { ImageField } from "./ImageField"; +import { ListField } from "./ListField"; +import { Document } from "./Document"; +import { Server } from "../client/Server"; export function Cast<T extends Field>(field: FieldValue<Field>, ctor: { new(): T }): Opt<T> { if (field) { @@ -55,4 +64,38 @@ export abstract class Field { abstract Copy(): Field; + abstract ToJson(): { id: string, type: Types, data: any } + + public static FromJson(obj: { id: string, type: number, data: any }): Field { + let data: any = obj.data + let id: string = obj.id + + switch (obj.type) { + case Types.Number: + return new NumberField(data, id) + case Types.Text: + return new TextField(data, id) + case Types.RichText: + return new RichTextField(data, id) + case Types.Key: + return KeyStore.Get(data) + case Types.Image: + return new ImageField(data, id) + case Types.List: + return new ListField(data, id) + case Types.Document: + let doc: Document = new Document(id) + let fields: [string, string][] = data as [string, string][] + fields.forEach(element => { + let keyName: string = element[0] + let valueId: string = element[1] + let key = KeyStore.Get(keyName) + Server.GetField(valueId, (field: Field) => { + doc.Set(key, field) + }) + }); + return doc + } + return new TextField(data, id) + } }
\ No newline at end of file diff --git a/src/fields/ImageField.ts b/src/fields/ImageField.ts index d82260f54..8ffc6d680 100644 --- a/src/fields/ImageField.ts +++ b/src/fields/ImageField.ts @@ -1,9 +1,10 @@ import { BasicField } from "./BasicField"; -import { Field } from "./Field"; +import { Field, FIELD_ID } from "./Field"; +import { Types } from "../server/Message"; export class ImageField extends BasicField<URL> { - constructor(data: URL | undefined = undefined) { - super(data == undefined ? new URL("http://cs.brown.edu/~bcz/bob_fettucine.jpg") : data); + constructor(data: URL | undefined = undefined, id: FIELD_ID = undefined) { + super(data == undefined ? new URL("http://cs.brown.edu/~bcz/bob_fettucine.jpg") : data, id); } toString(): string { @@ -18,4 +19,11 @@ export class ImageField extends BasicField<URL> { return new ImageField(this.Data); } + ToJson(): { type: Types, data: URL, id: string } { + return { + type: Types.Image, + data: this.Data, + id: this.Id as string + } + } }
\ No newline at end of file diff --git a/src/fields/Key.ts b/src/fields/Key.ts index 993102613..a7303f351 100644 --- a/src/fields/Key.ts +++ b/src/fields/Key.ts @@ -1,6 +1,7 @@ -import { Field } from "./Field" +import { Field, FIELD_ID } from "./Field" import { Utils } from "../Utils"; import { observable } from "mobx"; +import { Types } from "../server/Message"; export class Key extends Field { private name: string; @@ -31,6 +32,13 @@ export class Key extends Field { return name; } + ToJson(): { type: Types, data: string, id: string } { + return { + type: Types.Key, + data: this.name, + id: this.Id as string + } + } } export namespace KeyStore { @@ -50,4 +58,8 @@ export namespace KeyStore { export const LayoutKeys = new Key("LayoutKeys"); export const LayoutFields = new Key("LayoutFields"); export const ColumnsKey = new Key("SchemaColumns"); + + export function Get(name: string): Key { + return new Key(name) + } }
\ No newline at end of file diff --git a/src/fields/ListField.ts b/src/fields/ListField.ts index 8843338c1..925f8c7f4 100644 --- a/src/fields/ListField.ts +++ b/src/fields/ListField.ts @@ -1,9 +1,10 @@ -import { Field } from "./Field"; +import { Field, FIELD_ID } from "./Field"; import { BasicField } from "./BasicField"; +import { Types } from "../server/Message"; export class ListField<T extends Field> extends BasicField<T[]> { - constructor(data: T[] = []) { - super(data.slice()); + constructor(data: T[] = [], id: FIELD_ID = undefined) { + super(data.slice(), id); } ToScriptString(): string { @@ -13,4 +14,12 @@ export class ListField<T extends Field> extends BasicField<T[]> { Copy(): Field { return new ListField<T>(this.Data); } + + ToJson(): { type: Types, data: T[], id: string } { + return { + type: Types.List, + data: this.Data, + id: this.Id as string + } + } }
\ No newline at end of file diff --git a/src/fields/NumberField.ts b/src/fields/NumberField.ts index 03926d696..22abb23e9 100644 --- a/src/fields/NumberField.ts +++ b/src/fields/NumberField.ts @@ -1,8 +1,10 @@ import { BasicField } from "./BasicField" +import { Types } from "../server/Message"; +import { FIELD_ID } from "./Field"; export class NumberField extends BasicField<number> { - constructor(data: number = 0) { - super(data); + constructor(data: number = 0, id: FIELD_ID = undefined) { + super(data, id); } ToScriptString(): string { @@ -12,4 +14,12 @@ export class NumberField extends BasicField<number> { Copy() { return new NumberField(this.Data); } + + ToJson(): { id: string, type: Types, data: number } { + return { + id: this.Id as string, + type: Types.Number, + data: this.Data + } + } }
\ No newline at end of file diff --git a/src/fields/RichTextField.ts b/src/fields/RichTextField.ts index 4a77c669c..f7c3f2430 100644 --- a/src/fields/RichTextField.ts +++ b/src/fields/RichTextField.ts @@ -1,8 +1,10 @@ import { BasicField } from "./BasicField"; +import { Types } from "../server/Message"; +import { FIELD_ID } from "./Field"; export class RichTextField extends BasicField<string> { - constructor(data: string = "") { - super(data); + constructor(data: string = "", id: FIELD_ID = undefined) { + super(data, id); } ToScriptString(): string { @@ -13,4 +15,12 @@ export class RichTextField extends BasicField<string> { return new RichTextField(this.Data); } + ToJson(): { type: Types, data: string, id: string } { + return { + type: Types.RichText, + data: this.Data, + id: this.Id as string + } + } + }
\ No newline at end of file diff --git a/src/fields/TextField.ts b/src/fields/TextField.ts index 11d2ed7cd..5f2cd1db8 100644 --- a/src/fields/TextField.ts +++ b/src/fields/TextField.ts @@ -1,8 +1,10 @@ import { BasicField } from "./BasicField" +import { FIELD_ID } from "./Field"; +import { Types } from "../server/Message"; export class TextField extends BasicField<string> { - constructor(data: string = "") { - super(data); + constructor(data: string = "", id: FIELD_ID = undefined) { + super(data, id); } ToScriptString(): string { @@ -12,4 +14,12 @@ export class TextField extends BasicField<string> { Copy() { return new TextField(this.Data); } + + ToJson(): { type: Types, data: string, id: string } { + return { + type: Types.Text, + data: this.Data, + id: this.Id as string + } + } } diff --git a/src/server/Message.ts b/src/server/Message.ts index 15329249d..44df7be1c 100644 --- a/src/server/Message.ts +++ b/src/server/Message.ts @@ -47,6 +47,72 @@ export class GetFieldArgs { } } +export enum Types { + Number, List, Key, Image, Document, Text, RichText, DocumentReference +} + +export class DocumentTransfer implements Transferable { + readonly type = Types.Document + + constructor(readonly id: string) { } +} + +export class ImageTransfer implements Transferable { + readonly type = Types.Image + + constructor(readonly id: string) { } +} + +export class KeyTransfer implements Transferable { + name: string + readonly id: string + readonly type = Types.Key + + constructor(i: string, n: string) { + this.name = n + this.id = i + } +} + +export class ListTransfer implements Transferable { + type = Types.List; + + constructor(readonly id: string) { } +} + +export class NumberTransfer implements Transferable { + readonly type = Types.Number + + constructor(readonly value: number, readonly id: string) { } +} + +export class TextTransfer implements Transferable { + value: string + readonly id: string + readonly type = Types.Text + + constructor(t: string, i: string) { + this.value = t + this.id = i + } +} + +export class RichTextTransfer implements Transferable { + value: string + readonly id: string + readonly type = Types.Text + + constructor(t: string, i: string) { + this.value = t + this.id = i + } +} + +interface Transferable { + readonly id: string + readonly type: Types +} + export namespace MessageStore { export const Foo = new Message("Foo", String); export const Bar = new Message("Bar", String); diff --git a/src/server/index.ts b/src/server/index.ts index 0f2409fbb..f5a0fcfe2 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -10,6 +10,7 @@ import { Socket } from 'socket.io'; import { Utils } from '../Utils'; import { ObservableMap } from 'mobx'; import { FIELD_ID, Field } from '../fields/Field'; +import { Database } from './database'; const config = require('../../webpack.config') const compiler = webpack(config) const port = 1050; // default port to listen @@ -55,6 +56,7 @@ server.on("connection", function (socket: Socket) { function barReceived(guid: String) { clients[guid.toString()] = new Client(guid.toString()); + Database.Instance.print() } function addDocument(document: Document) { @@ -62,6 +64,7 @@ function addDocument(document: Document) { } function setField(newValue: SetFieldArgs) { + Database.Instance.update(newValue.field, newValue.value) if (FieldStore.get(newValue.field)) { FieldStore.get(newValue.field)!.TrySetValue(newValue.value); } |