Skip to content

Commit

Permalink
firebase sync experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
ondras committed Jun 19, 2014
1 parent d26b67e commit 699e7b5
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 50 deletions.
105 changes: 80 additions & 25 deletions my-mind.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,26 @@ MM.Item.fromJSON = function(data) {
return new this().fromJSON(data);
}

MM.Item.prototype.toJSON = function() {
var data = {
id: this._id,
text: this.getText()
}

if (this._side) { data.side = this._side; }
if (this._color) { data.color = this._color; }
if (this._value) { data.value = this._value; }
if (this._status) { data.status = this._status; }
if (this._layout) { data.layout = this._layout.id; }
if (!this._autoShape) { data.shape = this._shape.id; }
if (this._collapsed) { data.collapsed = 1; }
if (this._children.length) {
data.children = this._children.map(function(child) { return child.toJSON(); });
}

return data;
}

/**
* Only when creating a new item. To merge existing items, use .mergeWith().
*/
Expand All @@ -374,24 +394,57 @@ MM.Item.prototype.fromJSON = function(data) {
return this;
}

MM.Item.prototype.toJSON = function() {
var data = {
id: this._id,
text: this.getText()
MM.Item.prototype.mergeWith = function(data) {
var dirty = false;
if (this.getText() != data.text) { this.setText(data.text); }

if (this._side != data.side) {
this._side = data.side;
dirty = true;
}

if (this._side) { data.side = this._side; }
if (this._color) { data.color = this._color; }
if (this._value) { data.value = this._value; }
if (this._status) { data.status = this._status; }
if (this._layout) { data.layout = this._layout.id; }
if (!this._autoShape) { data.shape = this._shape.id; }
if (this._collapsed) { data.collapsed = 1; }
if (this._children.length) {
data.children = this._children.map(function(child) { return child.toJSON(); });

if (this._color != data.color) {
this._color = data.color;
dirty = true;
}

return data;
if (this._value != data.value) {
this._value = data.value;
dirty = true;
}

if (this._status != data.status) {
this._status = data.status;
dirty = true;
}

if (this._collapsed != !!data.collapsed) { this[this._collapsed ? "expand" : "collapse"](); }

if (this.getOwnLayout() != data.layout) {
this._layout = MM.Layout.getById(data.layout);
dirty = true;
}

var s = (this._autoShape ? null : this._shape.id);
if (s != data.shape) { this.setShape(MM.Shape.getById(data.shape)); }

/* FIXME children - co kdyz je nekdo z nas zrovna aktivni, nerkuli editovatelny? */
(data.children || []).forEach(function(child, index) {
if (index >= this._children.length) { /* new child */
this.insertChild(MM.Item.fromJSON(child));
dirty = true;
} else { /* existing child */
var myChild = this._children[index];
if (myChild.getId() == child.id) { /* recursive merge */
myChild.mergeWith(child);
} else { /* changed; replace */
this._children[index] = MM.Item.fromJSON(child);
dirty = true;
}
}
}, this);

if (dirty) { this.update(); }
}

MM.Item.prototype.clone = function() {
Expand All @@ -407,11 +460,11 @@ MM.Item.prototype.clone = function() {
}

MM.Item.prototype.update = function(doNotRecurse) {
MM.publish("item-change", this);

var map = this.getMap();
if (!map || !map.isVisible()) { return this; }

MM.publish("item-change", this);

if (this._autoShape) { /* check for changed auto-shape */
var autoShape = this._getAutoShape();
if (autoShape != this._shape) {
Expand Down Expand Up @@ -820,18 +873,22 @@ MM.Map.fromJSON = function(data) {
return new this().fromJSON(data);
}

MM.Map.prototype.fromJSON = function(data) {
this._setRoot(MM.Item.fromJSON(data.root));
return this;
}

MM.Map.prototype.toJSON = function() {
var data = {
root: this._root.toJSON()
};
return data;
}

MM.Map.prototype.fromJSON = function(data) {
this._setRoot(MM.Item.fromJSON(data.root));
return this;
}

MM.Map.prototype.mergeWith = function(data) {
this._root.mergeWith(data.root);
}

MM.Map.prototype.isVisible = function() {
return this._visible;
}
Expand Down Expand Up @@ -4241,9 +4298,7 @@ MM.UI.Backend.Firebase.handleMessage = function(message, publisher, data) {

case "firebase-change":
if (data) {
console.log("remote data changed");
console.log(data);
//FIXME MM.App.map.mergeWith(data);
MM.App.map.mergeWith(data);
} else { /* FIXME */
console.log("remote data disappeared");
}
Expand Down
87 changes: 70 additions & 17 deletions src/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ MM.Item.fromJSON = function(data) {
return new this().fromJSON(data);
}

MM.Item.prototype.toJSON = function() {
var data = {
id: this._id,
text: this.getText()
}

if (this._side) { data.side = this._side; }
if (this._color) { data.color = this._color; }
if (this._value) { data.value = this._value; }
if (this._status) { data.status = this._status; }
if (this._layout) { data.layout = this._layout.id; }
if (!this._autoShape) { data.shape = this._shape.id; }
if (this._collapsed) { data.collapsed = 1; }
if (this._children.length) {
data.children = this._children.map(function(child) { return child.toJSON(); });
}

return data;
}

/**
* Only when creating a new item. To merge existing items, use .mergeWith().
*/
Expand All @@ -85,24 +105,57 @@ MM.Item.prototype.fromJSON = function(data) {
return this;
}

MM.Item.prototype.toJSON = function() {
var data = {
id: this._id,
text: this.getText()
MM.Item.prototype.mergeWith = function(data) {
var dirty = false;
if (this.getText() != data.text) { this.setText(data.text); }

if (this._side != data.side) {
this._side = data.side;
dirty = true;
}

if (this._side) { data.side = this._side; }
if (this._color) { data.color = this._color; }
if (this._value) { data.value = this._value; }
if (this._status) { data.status = this._status; }
if (this._layout) { data.layout = this._layout.id; }
if (!this._autoShape) { data.shape = this._shape.id; }
if (this._collapsed) { data.collapsed = 1; }
if (this._children.length) {
data.children = this._children.map(function(child) { return child.toJSON(); });

if (this._color != data.color) {
this._color = data.color;
dirty = true;
}

return data;
if (this._value != data.value) {
this._value = data.value;
dirty = true;
}

if (this._status != data.status) {
this._status = data.status;
dirty = true;
}

if (this._collapsed != !!data.collapsed) { this[this._collapsed ? "expand" : "collapse"](); }

if (this.getOwnLayout() != data.layout) {
this._layout = MM.Layout.getById(data.layout);
dirty = true;
}

var s = (this._autoShape ? null : this._shape.id);
if (s != data.shape) { this.setShape(MM.Shape.getById(data.shape)); }

/* FIXME children - co kdyz je nekdo z nas zrovna aktivni, nerkuli editovatelny? */
(data.children || []).forEach(function(child, index) {
if (index >= this._children.length) { /* new child */
this.insertChild(MM.Item.fromJSON(child));
dirty = true;
} else { /* existing child */
var myChild = this._children[index];
if (myChild.getId() == child.id) { /* recursive merge */
myChild.mergeWith(child);
} else { /* changed; replace */
this._children[index] = MM.Item.fromJSON(child);
dirty = true;
}
}
}, this);

if (dirty) { this.update(); }
}

MM.Item.prototype.clone = function() {
Expand All @@ -118,11 +171,11 @@ MM.Item.prototype.clone = function() {
}

MM.Item.prototype.update = function(doNotRecurse) {
MM.publish("item-change", this);

var map = this.getMap();
if (!map || !map.isVisible()) { return this; }

MM.publish("item-change", this);

if (this._autoShape) { /* check for changed auto-shape */
var autoShape = this._getAutoShape();
if (autoShape != this._shape) {
Expand Down
14 changes: 9 additions & 5 deletions src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ MM.Map.fromJSON = function(data) {
return new this().fromJSON(data);
}

MM.Map.prototype.fromJSON = function(data) {
this._setRoot(MM.Item.fromJSON(data.root));
return this;
}

MM.Map.prototype.toJSON = function() {
var data = {
root: this._root.toJSON()
};
return data;
}

MM.Map.prototype.fromJSON = function(data) {
this._setRoot(MM.Item.fromJSON(data.root));
return this;
}

MM.Map.prototype.mergeWith = function(data) {
this._root.mergeWith(data.root);
}

MM.Map.prototype.isVisible = function() {
return this._visible;
}
Expand Down
4 changes: 1 addition & 3 deletions src/ui.backend.firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ MM.UI.Backend.Firebase.handleMessage = function(message, publisher, data) {

case "firebase-change":
if (data) {
console.log("remote data changed");
console.log(data);
//FIXME MM.App.map.mergeWith(data);
MM.App.map.mergeWith(data);
} else { /* FIXME */
console.log("remote data disappeared");
}
Expand Down

0 comments on commit 699e7b5

Please sign in to comment.