aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Utils.ts17
-rw-r--r--src/client/Server.ts4
-rw-r--r--src/client/SocketStub.ts30
-rw-r--r--src/client/documents/Documents.ts11
-rw-r--r--src/client/views/Main.tsx112
-rw-r--r--src/fields/BasicField.ts2
-rw-r--r--src/fields/Document.ts13
-rw-r--r--src/fields/DocumentReference.ts5
-rw-r--r--src/fields/Field.ts43
-rw-r--r--src/fields/ImageField.ts5
-rw-r--r--src/fields/Key.ts5
-rw-r--r--src/fields/ListField.ts5
-rw-r--r--src/fields/NumberField.ts5
-rw-r--r--src/fields/RichTextField.ts5
-rw-r--r--src/fields/TextField.ts5
-rw-r--r--src/server/Message.ts42
-rw-r--r--src/server/ServerUtil.ts60
-rw-r--r--src/server/database.ts60
-rw-r--r--src/server/index.ts20
19 files changed, 301 insertions, 148 deletions
diff --git a/src/Utils.ts b/src/Utils.ts
index f735ddc73..f07c644b7 100644
--- a/src/Utils.ts
+++ b/src/Utils.ts
@@ -1,16 +1,27 @@
import v4 = require('uuid/v4');
import v5 = require("uuid/v5");
import { Socket } from 'socket.io';
-import { Message } from './server/Message';
+import { Message, Types } from './server/Message';
+import { Field } from './fields/Field';
+import { TextField } from './fields/TextField';
+import { NumberField } from './fields/NumberField';
+import { RichTextField } from './fields/RichTextField';
+import { Key } from './fields/Key';
+import { ImageField } from './fields/ImageField';
+import { ListField } from './fields/ListField';
+import { Document } from './fields/Document';
+import { Server } from './client/Server';
export class Utils {
public static GenerateGuid(): string {
- return v4();
+ return v4()
+ // return new Buffer(v4()).toString("hex").substr(0, 24);
}
public static GenerateDeterministicGuid(seed: string): string {
- return v5(seed, v5.URL);
+ return v5(seed, v5.URL)
+ // return new Buffer(v5(seed, v5.URL)).toString("hex").substr(0, 24);
}
public static GetScreenTransform(ele: HTMLElement): { scale: number, translateX: number, translateY: number } {
diff --git a/src/client/Server.ts b/src/client/Server.ts
index 30fc57c93..a08fc2e87 100644
--- a/src/client/Server.ts
+++ b/src/client/Server.ts
@@ -48,8 +48,8 @@ export class Server {
public static DeleteDocumentField(doc: Document, key: Key) {
SocketStub.SEND_DELETE_DOCUMENT_FIELD(doc, key);
}
- public static SetFieldValue(field: Field, value: any) {
- SocketStub.SEND_SET_FIELD(field, value);
+ public static UpdateField(field: Field) {
+ SocketStub.SEND_SET_FIELD(field);
}
static connected(message: string) {
diff --git a/src/client/SocketStub.ts b/src/client/SocketStub.ts
index bdf326cd8..03c0131ba 100644
--- a/src/client/SocketStub.ts
+++ b/src/client/SocketStub.ts
@@ -2,7 +2,7 @@ import { Field, FIELD_ID } from "../fields/Field"
import { Key, KeyStore } from "../fields/Key"
import { ObservableMap, action } from "mobx";
import { Document } from "../fields/Document"
-import { MessageStore, SetFieldArgs, GetFieldArgs } from "../server/Message";
+import { MessageStore, SetFieldArgs, GetFieldArgs, DocumentTransfer, Types } from "../server/Message";
import { Utils } from "../Utils";
import { Server } from "./Server";
@@ -15,13 +15,21 @@ export class SocketStub {
// ...SOCKET(ADD_DOCUMENT, serialied document)
// server stores each document field in its repository of stored fields
- document.fields.forEach((f, key) => this.FieldStore.set((f as Field).Id, f as Field));
-
- // server stores stripped down document (w/ only field id proxies) in the field store
- this.FieldStore.set(document.Id, new Document(document.Id));
- document.fields.forEach((f, key) => (this.FieldStore.get(document.Id) as Document)._proxies.set(key.Id, (f as Field).Id));
-
- // Server.Socket.emit(MessageStore.AddDocument.Message, document)
+ // document.fields.forEach((f, key) => this.FieldStore.set((f as Field).Id, f as Field));
+ // let strippedDoc = new Document(document.Id);
+ // document.fields.forEach((f, key) => {
+ // if (f) {
+ // // let args: SetFieldArgs = new SetFieldArgs(f.Id, f.GetValue())
+ // let args: Transferable = f.ToJson()
+ // Utils.Emit(Server.Socket, MessageStore.SetField, args)
+ // }
+ // })
+
+ // // server stores stripped down document (w/ only field id proxies) in the field store
+ // this.FieldStore.set(document.Id, new Document(document.Id));
+ // document.fields.forEach((f, key) => (this.FieldStore.get(document.Id) as Document)._proxies.set(key.Id, (f as Field).Id));
+
+ Utils.Emit(Server.Socket, MessageStore.AddDocument, new DocumentTransfer(document.ToJson()))
}
public static SEND_FIELD_REQUEST(fieldid: FIELD_ID, callback: (field: Field) => void) {
@@ -45,6 +53,7 @@ export class SocketStub {
// server adds the field to its repository of fields
this.FieldStore.set(value.Id, value);
+ // Utils.Emit(Server.Socket, MessageStore.AddDocument, new DocumentTransfer(doc.ToJson()))
}
public static SEND_DELETE_DOCUMENT_FIELD(doc: Document, key: Key) {
@@ -58,13 +67,12 @@ export class SocketStub {
document._proxies.delete(key.Id);
}
- public static SEND_SET_FIELD(field: Field, value: any) {
+ public static SEND_SET_FIELD(field: Field) {
// Send a request to set the value of a field
// ...SOCKET(SET_FIELD, field id, serialized field value)
// Server updates the value of the field in its fieldstore
- let msg: SetFieldArgs = new SetFieldArgs(field.Id as string, value)
- Utils.Emit(Server.Socket, MessageStore.SetField, msg)
+ Utils.Emit(Server.Socket, MessageStore.SetField, field.ToJson())
}
}
diff --git a/src/client/documents/Documents.ts b/src/client/documents/Documents.ts
index 6925234fe..460bf9b23 100644
--- a/src/client/documents/Documents.ts
+++ b/src/client/documents/Documents.ts
@@ -120,19 +120,20 @@ export namespace Documents {
imageProto.Set(KeyStore.Layout, new TextField(ImageBox.LayoutString()));
// imageProto.SetField(KeyStore.Layout, new TextField('<div style={"background-image: " + {Data}} />'));
imageProto.Set(KeyStore.LayoutKeys, new ListField([KeyStore.Data]));
- Server.AddDocument(imageProto);
+ // Server.AddDocument(imageProto);
return imageProto;
}
- return Server.GetField(imageProtoId) as Document;
+ return new Document();
+ // return Server.GetField(imageProtoId) as Document;
}
export function ImageDocument(url: string, options: DocumentOptions = {}): Document {
let doc = GetImagePrototype().MakeDelegate();
setupOptions(doc, options);
doc.Set(KeyStore.Data, new ImageField(new URL(url)));
- Server.AddDocument(doc);
- var sdoc = Server.GetField(doc.Id) as Document;
- return sdoc;
+ // Server.AddDocument(doc);
+ // var sdoc = Server.GetField(doc.Id) as Document;
+ return doc;
}
let collectionProto: Document;
diff --git a/src/client/views/Main.tsx b/src/client/views/Main.tsx
index 6a065327b..197529c91 100644
--- a/src/client/views/Main.tsx
+++ b/src/client/views/Main.tsx
@@ -39,54 +39,84 @@ document.addEventListener("pointerdown", action(function (e: PointerEvent) {
//runInAction(() =>
-{
- let doc1 = Documents.TextDocument({ title: "hello" });
- let doc2 = doc1.MakeDelegate();
- doc2.Set(KS.X, new NumberField(150));
- doc2.Set(KS.Y, new NumberField(20));
- let doc3 = Documents.ImageDocument("https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg", {
- x: 450, y: 100, title: "cat 1"
- });
- doc3.Set(KeyStore.Data, new ImageField);
- const schemaDocs = Array.from(Array(5).keys()).map(v => Documents.ImageDocument("https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg", {
- x: 50 + 100 * v, y: 50, width: 100, height: 100, title: "cat" + v
- }));
- schemaDocs[0].SetData(KS.Author, "Tyler", TextField);
- schemaDocs[4].SetData(KS.Author, "Bob", TextField);
- schemaDocs.push(doc2);
- const doc7 = Documents.SchemaDocument(schemaDocs)
- const docset = [doc1, doc2, doc3, doc7];
- let doc4 = Documents.CollectionDocument(docset, {
- x: 0, y: 400, title: "mini collection"
- });
- // let doc5 = Documents.ImageDocument("https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg", {
- // x: 650, y: 500, width: 600, height: 600, title: "cat 2"
- // });
- let docset2 = new Array<Document>(doc4);//, doc1, doc3);
- let doc6 = Documents.CollectionDocument(docset2, {
- x: 350, y: 100, width: 600, height: 600, title: "docking collection"
- });
- let mainNodes = null;// mainContainer.GetFieldT(KeyStore.Data, ListField);
- if (!mainNodes) {
- mainNodes = new ListField<Document>();
- }
- // mainNodes.Data.push(doc6);
- // mainNodes.Data.push(doc2);
- mainNodes.Data.push(doc4);
- mainNodes.Data.push(doc3);
- // mainNodes.Data.push(doc5);
- // mainNodes.Data.push(doc1);
- //mainNodes.Data.push(doc2);
- //mainNodes.Data.push(doc6);
- mainContainer.Set(KeyStore.Data, mainNodes);
-}
+// let doc1 = Documents.TextDocument({ title: "hello" });
+// let doc2 = doc1.MakeDelegate();
+// doc2.Set(KS.X, new NumberField(150));
+// doc2.Set(KS.Y, new NumberField(20));
+// let doc3 = Documents.ImageDocument("https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg", {
+// x: 450, y: 100, title: "cat 1"
+// });
+// doc3.Set(KeyStore.Data, new ImageField);
+// const schemaDocs = Array.from(Array(5).keys()).map(v => Documents.ImageDocument("https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg", {
+// x: 50 + 100 * v, y: 50, width: 100, height: 100, title: "cat" + v
+// }));
+// schemaDocs[0].SetData(KS.Author, "Tyler", TextField);
+// schemaDocs[4].SetData(KS.Author, "Bob", TextField);
+// schemaDocs.push(doc2);
+// const doc7 = Documents.SchemaDocument(schemaDocs)
+const docset: Document[] = [];
+let doc4 = Documents.CollectionDocument(docset, {
+ x: 0, y: 400, title: "mini collection"
+});
+mainContainer = doc4;
+// let doc5 = Documents.ImageDocument("https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg", {
+// x: 650, y: 500, width: 600, height: 600, title: "cat 2"
+// });
+// let docset2 = new Array<Document>(doc4);//, doc1, doc3);
+// let doc6 = Documents.CollectionDocument(docset2, {
+// x: 350, y: 100, width: 600, height: 600, title: "docking collection"
+// });
+// let mainNodes = mainContainer.GetOrCreate(KeyStore.Data, ListField);
+// mainNodes.Data.push(doc6);
+// mainNodes.Data.push(doc2);
+// mainNodes.Data.push(doc4);
+// mainNodes.Data.push(doc3);
+// mainNodes.Data.push(doc5);
+// mainNodes.Data.push(doc1);
+//mainNodes.Data.push(doc2);
+//mainNodes.Data.push(doc6);
+// mainContainer.Set(KeyStore.Data, mainNodes);
//}
//);
+let addImageNode = action(() => {
+ doc4.GetList<Document>(KeyStore.Data, []).push(Documents.ImageDocument("https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg", {
+ x: 0, y: 300, width: 200, height: 200, title: "added note"
+ }));
+})
+let addTextNode = action(() => {
+ doc4.GetList<Document>(KeyStore.Data, []).push(Documents.TextDocument({
+ x: 0, y: 300, width: 200, height: 200, title: "added note"
+ }));
+})
+let addColNode = action(() => {
+ doc4.GetList<Document>(KeyStore.Data, []).push(Documents.CollectionDocument([], {
+ x: 0, y: 300, width: 200, height: 200, title: "added note"
+ }));
+})
+
ReactDOM.render((
<div style={{ position: "absolute", width: "100%", height: "100%" }}>
<DocumentView Document={mainContainer} ContainingCollectionView={undefined} DocumentView={undefined} />
<DocumentDecorations />
<ContextMenu />
+ <button style={{
+ position: 'absolute',
+ bottom: '0px',
+ left: '0px',
+ width: '150px'
+ }} onClick={addImageNode}>Add Image</button>
+ <button style={{
+ position: 'absolute',
+ bottom: '25px',
+ left: '0px',
+ width: '150px'
+ }} onClick={addTextNode}>Add Text</button>
+ <button style={{
+ position: 'absolute',
+ bottom: '50px',
+ left: '0px',
+ width: '150px'
+ }} onClick={addColNode}>Add Collection</button>
</div>),
document.getElementById('root')); \ No newline at end of file
diff --git a/src/fields/BasicField.ts b/src/fields/BasicField.ts
index 40ead0953..9476f5d21 100644
--- a/src/fields/BasicField.ts
+++ b/src/fields/BasicField.ts
@@ -1,5 +1,6 @@
import { Field, FIELD_ID } from "./Field"
import { observable, computed, action } from "mobx";
+import { Server } from "../client/Server";
export abstract class BasicField<T> extends Field {
constructor(data: T, id: FIELD_ID = undefined) {
@@ -21,6 +22,7 @@ export abstract class BasicField<T> extends Field {
return;
}
this.data = value;
+ Server.UpdateField(this);
}
@action
diff --git a/src/fields/Document.ts b/src/fields/Document.ts
index 99cd03813..56ac3c471 100644
--- a/src/fields/Document.ts
+++ b/src/fields/Document.ts
@@ -7,6 +7,8 @@ import { ListField } from "./ListField";
import { findDOMNode } from "react-dom";
import { Server } from "../client/Server";
import { Types } from "../server/Message";
+import { ObjectID } from "bson";
+import { Utils } from "../Utils";
export class Document extends Field {
public fields: ObservableMap<Key, Opt<Field>> = new ObservableMap();
@@ -87,11 +89,12 @@ export class Document extends Field {
Set(key: Key, field: Field | undefined): void {
if (field) {
this.fields.set(key, field);
- Server.AddDocumentField(this, key, field);
+ // Server.AddDocumentField(this, key, field);
} else {
this.fields.delete(key);
- Server.DeleteDocumentField(this, key);
+ // Server.DeleteDocumentField(this, key);
}
+ Server.UpdateField(this);
}
@action
@@ -101,7 +104,7 @@ export class Document extends Field {
//if (field != WAITING) { // do we want to wait for the field to come back from the server to set it, or do we overwrite?
if (field instanceof ctor) {
field.Data = value;
- Server.SetFieldValue(field, value);
+ // Server.SetFieldValue(field, value);
} else if (!field || replaceWrongType) {
let newField = new ctor();
newField.Data = value;
@@ -156,7 +159,7 @@ export class Document extends Field {
throw new Error("Method not implemented.");
}
- ToJson(): { type: Types, data: [string, string][], id: string } {
+ ToJson(): { type: Types, data: [string, string][], _id: ObjectID } {
let fields: [string, string][] = []
this._proxies.forEach((field, key) => {
if (field) {
@@ -167,7 +170,7 @@ export class Document extends Field {
return {
type: Types.Document,
data: fields,
- id: this.Id as string
+ _id: this.Id
}
}
diff --git a/src/fields/DocumentReference.ts b/src/fields/DocumentReference.ts
index ab4c4644e..b3d8e059d 100644
--- a/src/fields/DocumentReference.ts
+++ b/src/fields/DocumentReference.ts
@@ -2,6 +2,7 @@ import { Field, Opt, FieldValue, FIELD_ID } from "./Field";
import { Document } from "./Document";
import { Key } from "./Key";
import { Types } from "../server/Message";
+import { ObjectID } from "bson";
export class DocumentReference extends Field {
get Key(): Key {
@@ -42,11 +43,11 @@ export class DocumentReference extends Field {
return "";
}
- ToJson(): { type: Types, data: FIELD_ID, id: string } {
+ ToJson(): { type: Types, data: FIELD_ID, _id: ObjectID } {
return {
type: Types.DocumentReference,
data: this.document.Id,
- id: this.Id as string
+ _id: new ObjectID(this.Id)
}
}
} \ No newline at end of file
diff --git a/src/fields/Field.ts b/src/fields/Field.ts
index 1a5cfb29c..4b9d996ac 100644
--- a/src/fields/Field.ts
+++ b/src/fields/Field.ts
@@ -9,6 +9,7 @@ import { ImageField } from "./ImageField";
import { ListField } from "./ListField";
import { Document } from "./Document";
import { Server } from "../client/Server";
+import { ObjectID } from "bson";
export function Cast<T extends Field>(field: FieldValue<Field>, ctor: { new(): T }): Opt<T> {
if (field) {
@@ -64,45 +65,5 @@ 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 new Key(data, id)
- 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 => {
- doc._proxies.set(element[0], element[1]);
- let keyId: string = element[0]
- let valueId: string = element[1]
- Server.GetField(keyId, (key: Field) => {
- if (key instanceof Key) {
- Server.GetField(valueId, (field: Field) => {
- doc.Set(key as Key, field)
- })
- }
- else {
- console.log("how did you get a key that isnt a key wtf")
- }
- })
- });
- return doc
- }
- return new TextField(data, id)
- }
+ abstract ToJson(): { _id: ObjectID, type: Types, data: any }
} \ No newline at end of file
diff --git a/src/fields/ImageField.ts b/src/fields/ImageField.ts
index 8ffc6d680..30fe0970b 100644
--- a/src/fields/ImageField.ts
+++ b/src/fields/ImageField.ts
@@ -1,6 +1,7 @@
import { BasicField } from "./BasicField";
import { Field, FIELD_ID } from "./Field";
import { Types } from "../server/Message";
+import { ObjectID } from "bson";
export class ImageField extends BasicField<URL> {
constructor(data: URL | undefined = undefined, id: FIELD_ID = undefined) {
@@ -19,11 +20,11 @@ export class ImageField extends BasicField<URL> {
return new ImageField(this.Data);
}
- ToJson(): { type: Types, data: URL, id: string } {
+ ToJson(): { type: Types, data: URL, _id: ObjectID } {
return {
type: Types.Image,
data: this.Data,
- id: this.Id as string
+ _id: new ObjectID(this.Id)
}
}
} \ No newline at end of file
diff --git a/src/fields/Key.ts b/src/fields/Key.ts
index 084b444a6..ffcbfc8b4 100644
--- a/src/fields/Key.ts
+++ b/src/fields/Key.ts
@@ -2,6 +2,7 @@ import { Field, FIELD_ID } from "./Field"
import { Utils } from "../Utils";
import { observable } from "mobx";
import { Types } from "../server/Message";
+import { ObjectID } from "bson";
export class Key extends Field {
private name: string;
@@ -32,11 +33,11 @@ export class Key extends Field {
return name;
}
- ToJson(): { type: Types, data: string, id: string } {
+ ToJson(): { type: Types, data: string, _id: ObjectID } {
return {
type: Types.Key,
data: this.name,
- id: this.Id as string
+ _id: new ObjectID(this.Id)
}
}
}
diff --git a/src/fields/ListField.ts b/src/fields/ListField.ts
index 925f8c7f4..e98ced902 100644
--- a/src/fields/ListField.ts
+++ b/src/fields/ListField.ts
@@ -1,6 +1,7 @@
import { Field, FIELD_ID } from "./Field";
import { BasicField } from "./BasicField";
import { Types } from "../server/Message";
+import { ObjectId } from "bson";
export class ListField<T extends Field> extends BasicField<T[]> {
constructor(data: T[] = [], id: FIELD_ID = undefined) {
@@ -15,11 +16,11 @@ export class ListField<T extends Field> extends BasicField<T[]> {
return new ListField<T>(this.Data);
}
- ToJson(): { type: Types, data: T[], id: string } {
+ ToJson(): { type: Types, data: T[], _id: ObjectId } {
return {
type: Types.List,
data: this.Data,
- id: this.Id as string
+ _id: new ObjectId(this.Id)
}
}
} \ No newline at end of file
diff --git a/src/fields/NumberField.ts b/src/fields/NumberField.ts
index 22abb23e9..ce07a18b8 100644
--- a/src/fields/NumberField.ts
+++ b/src/fields/NumberField.ts
@@ -1,6 +1,7 @@
import { BasicField } from "./BasicField"
import { Types } from "../server/Message";
import { FIELD_ID } from "./Field";
+import { ObjectID } from "bson";
export class NumberField extends BasicField<number> {
constructor(data: number = 0, id: FIELD_ID = undefined) {
@@ -15,9 +16,9 @@ export class NumberField extends BasicField<number> {
return new NumberField(this.Data);
}
- ToJson(): { id: string, type: Types, data: number } {
+ ToJson(): { _id: ObjectID, type: Types, data: number } {
return {
- id: this.Id as string,
+ _id: new ObjectID(this.Id),
type: Types.Number,
data: this.Data
}
diff --git a/src/fields/RichTextField.ts b/src/fields/RichTextField.ts
index f7c3f2430..a7ea1f2ce 100644
--- a/src/fields/RichTextField.ts
+++ b/src/fields/RichTextField.ts
@@ -1,6 +1,7 @@
import { BasicField } from "./BasicField";
import { Types } from "../server/Message";
import { FIELD_ID } from "./Field";
+import { ObjectID } from "bson";
export class RichTextField extends BasicField<string> {
constructor(data: string = "", id: FIELD_ID = undefined) {
@@ -15,11 +16,11 @@ export class RichTextField extends BasicField<string> {
return new RichTextField(this.Data);
}
- ToJson(): { type: Types, data: string, id: string } {
+ ToJson(): { type: Types, data: string, _id: ObjectID } {
return {
type: Types.RichText,
data: this.Data,
- id: this.Id as string
+ _id: new ObjectID(this.Id)
}
}
diff --git a/src/fields/TextField.ts b/src/fields/TextField.ts
index 5f2cd1db8..692b746d1 100644
--- a/src/fields/TextField.ts
+++ b/src/fields/TextField.ts
@@ -1,6 +1,7 @@
import { BasicField } from "./BasicField"
import { FIELD_ID } from "./Field";
import { Types } from "../server/Message";
+import { ObjectID } from "bson";
export class TextField extends BasicField<string> {
constructor(data: string = "", id: FIELD_ID = undefined) {
@@ -15,11 +16,11 @@ export class TextField extends BasicField<string> {
return new TextField(this.Data);
}
- ToJson(): { type: Types, data: string, id: string } {
+ ToJson(): { type: Types, data: string, _id: ObjectID } {
return {
type: Types.Text,
data: this.Data,
- id: this.Id as string
+ _id: new ObjectID(this.Id)
}
}
}
diff --git a/src/server/Message.ts b/src/server/Message.ts
index 44df7be1c..0391b6671 100644
--- a/src/server/Message.ts
+++ b/src/server/Message.ts
@@ -1,10 +1,10 @@
import { Utils } from "../Utils";
import { FIELD_ID, Field } from "../fields/Field";
+import { ObjectId } from "bson";
export class Message<T> {
private name: string;
private guid: string;
- readonly ArgsCtor: new (...args: any) => T;
get Name(): string {
return this.name;
@@ -14,10 +14,9 @@ export class Message<T> {
return this.guid
}
- constructor(name: string, ctor: new (...args: any) => T) {
+ constructor(name: string) {
this.name = name;
this.guid = Utils.GenerateDeterministicGuid(name)
- this.ArgsCtor = ctor;
}
GetValue() {
@@ -53,70 +52,73 @@ export enum Types {
export class DocumentTransfer implements Transferable {
readonly type = Types.Document
+ _id: ObjectId;
- constructor(readonly id: string) { }
+ constructor(readonly obj: { type: Types, data: [string, string][], _id: ObjectId }) {
+ this._id = obj._id
+ }
}
export class ImageTransfer implements Transferable {
readonly type = Types.Image
- constructor(readonly id: string) { }
+ constructor(readonly _id: ObjectId) { }
}
export class KeyTransfer implements Transferable {
name: string
- readonly id: string
+ readonly _id: ObjectId
readonly type = Types.Key
constructor(i: string, n: string) {
this.name = n
- this.id = i
+ this._id = new ObjectId(i)
}
}
export class ListTransfer implements Transferable {
type = Types.List;
- constructor(readonly id: string) { }
+ constructor(readonly _id: ObjectId) { }
}
export class NumberTransfer implements Transferable {
readonly type = Types.Number
- constructor(readonly value: number, readonly id: string) { }
+ constructor(readonly value: number, readonly _id: ObjectId) { }
}
export class TextTransfer implements Transferable {
value: string
- readonly id: string
+ readonly _id: ObjectId
readonly type = Types.Text
constructor(t: string, i: string) {
this.value = t
- this.id = i
+ this._id = new ObjectId(i)
}
}
export class RichTextTransfer implements Transferable {
value: string
- readonly id: string
+ readonly _id: ObjectId
readonly type = Types.Text
constructor(t: string, i: string) {
this.value = t
- this.id = i
+ this._id = new ObjectId(i)
}
}
-interface Transferable {
- readonly id: string
+export interface Transferable {
+ readonly _id: ObjectId
readonly type: Types
}
export namespace MessageStore {
- export const Foo = new Message("Foo", String);
- export const Bar = new Message("Bar", String);
- export const AddDocument = new Message("Add Document", TestMessageArgs);
- export const SetField = new Message("Set Field", SetFieldArgs)
- export const GetField = new Message("Get Field", GetFieldArgs)
+ export const Foo = new Message<string>("Foo");
+ export const Bar = new Message<string>("Bar");
+ export const AddDocument = new Message<DocumentTransfer>("Add Document");
+ export const SetField = new Message<{ _id: ObjectId, data: any, type: Types }>("Set Field")
+ export const GetField = new Message<GetFieldArgs>("Get Field")
} \ No newline at end of file
diff --git a/src/server/ServerUtil.ts b/src/server/ServerUtil.ts
new file mode 100644
index 000000000..6757615fb
--- /dev/null
+++ b/src/server/ServerUtil.ts
@@ -0,0 +1,60 @@
+import { Field } from './../fields/Field';
+import { TextField } from './../fields/TextField';
+import { NumberField } from './../fields/NumberField';
+import { RichTextField } from './../fields/RichTextField';
+import { Key } from './../fields/Key';
+import { ImageField } from './../fields/ImageField';
+import { ListField } from './../fields/ListField';
+import { Document } from './../fields/Document';
+import { Server } from './../client/Server';
+import { Types } from './Message';
+import { Utils } from '../Utils';
+
+export class ServerUtils {
+ public static FromJson(json: string): Field {
+ let obj = JSON.parse(json)
+ let data: any = obj.data
+ let id: string = obj.id
+ let type: Types = obj.type
+
+ if (!(data && id && type != undefined)) {
+ console.log("how did you manage to get an object that doesn't have a data or an id?")
+ return new TextField("Something to fill the space", Utils.GenerateGuid());
+ }
+
+ switch (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 new Key(data, id)
+ 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 => {
+ doc._proxies.set(element[0], element[1]);
+ let keyId: string = element[0]
+ let valueId: string = element[1]
+ Server.GetField(keyId, (key: Field) => {
+ if (key instanceof Key) {
+ Server.GetField(valueId, (field: Field) => {
+ doc.Set(key as Key, field)
+ })
+ }
+ else {
+ console.log("how did you get a key that isnt a key wtf")
+ }
+ })
+ });
+ return doc
+ }
+ return new TextField(data, id)
+ }
+} \ No newline at end of file
diff --git a/src/server/database.ts b/src/server/database.ts
new file mode 100644
index 000000000..72ddbc82c
--- /dev/null
+++ b/src/server/database.ts
@@ -0,0 +1,60 @@
+import { action, configure } from 'mobx';
+import * as mongodb from 'mongodb';
+import { ObjectID } from 'mongodb';
+import { Transferable } from './Message';
+import { Utils } from '../Utils';
+
+export class Database {
+ public static Instance = new Database()
+ private MongoClient = mongodb.MongoClient;
+ private url = 'mongodb://localhost:27017/Dash';
+
+ public update(id: mongodb.ObjectID, value: any) {
+ this.MongoClient.connect(this.url, (err, db) => {
+ let collection = db.db().collection('documents');
+ collection.update({ _id: id }, { $set: 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: any) {
+ this.MongoClient.connect(this.url, (err, db) => {
+ let collection = db.db().collection('documents');
+ collection.insertOne(kvpairs, () => { });
+ db.close();
+ });
+ }
+
+ public getDocument(id: mongodb.ObjectID): string | undefined {
+ var result: JSON;
+ this.MongoClient.connect(this.url, (err, db) => {
+ if (err) {
+ console.log(err)
+ return undefined
+ }
+ let collection = db.db().collection('documents');
+ collection.findOne({ _id: Utils.GenerateDeterministicGuid(id.toHexString()) }, (err: any, res: any) => result = res)
+ console.log(result)
+ db.close();
+ if (!result) {
+ console.log("not found")
+ return undefined
+ }
+ console.log("found")
+ return result;
+ });
+ return undefined
+ }
+
+ public print() {
+ console.log("db says hi!")
+ }
+}
diff --git a/src/server/index.ts b/src/server/index.ts
index f5a0fcfe2..98d897d2f 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -4,13 +4,15 @@ import * as webpack from 'webpack'
import * as wdm from 'webpack-dev-middleware';
import * as whm from 'webpack-hot-middleware';
import * as path from 'path'
-import { MessageStore, Message, SetFieldArgs, GetFieldArgs } from "./Message";
+import { MessageStore, Message, SetFieldArgs, GetFieldArgs, Transferable } from "./Message";
import { Client } from './Client';
import { Socket } from 'socket.io';
import { Utils } from '../Utils';
import { ObservableMap } from 'mobx';
import { FIELD_ID, Field } from '../fields/Field';
import { Database } from './database';
+import { ServerUtils } from './ServerUtil';
+import { ObjectID } from 'mongodb';
const config = require('../../webpack.config')
const compiler = webpack(config)
const port = 1050; // default port to listen
@@ -63,16 +65,22 @@ 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);
+function setField(newValue: Transferable) {
+ console.log(newValue._id)
+ if (Database.Instance.getDocument(newValue._id)) {
+ Database.Instance.update(newValue._id, newValue)
+ }
+ else {
+ Database.Instance.insert(newValue)
}
}
function getField([fieldRequest, callback]: [GetFieldArgs, (field: Field) => void]) {
let fieldid: string = fieldRequest.field
- callback(FieldStore.get(fieldid) as Field)
+ let result: string | undefined = Database.Instance.getDocument(new ObjectID(fieldid))
+ if (result) {
+ let fromJson: Field = ServerUtils.FromJson(result)
+ }
}
server.listen(serverPort);