Skip to content

Commit 699e7b5

Browse files
committed
firebase sync experiments
1 parent d26b67e commit 699e7b5

File tree

4 files changed

+160
-50
lines changed

4 files changed

+160
-50
lines changed

my-mind.js

+80-25
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,26 @@ MM.Item.fromJSON = function(data) {
350350
return new this().fromJSON(data);
351351
}
352352

353+
MM.Item.prototype.toJSON = function() {
354+
var data = {
355+
id: this._id,
356+
text: this.getText()
357+
}
358+
359+
if (this._side) { data.side = this._side; }
360+
if (this._color) { data.color = this._color; }
361+
if (this._value) { data.value = this._value; }
362+
if (this._status) { data.status = this._status; }
363+
if (this._layout) { data.layout = this._layout.id; }
364+
if (!this._autoShape) { data.shape = this._shape.id; }
365+
if (this._collapsed) { data.collapsed = 1; }
366+
if (this._children.length) {
367+
data.children = this._children.map(function(child) { return child.toJSON(); });
368+
}
369+
370+
return data;
371+
}
372+
353373
/**
354374
* Only when creating a new item. To merge existing items, use .mergeWith().
355375
*/
@@ -374,24 +394,57 @@ MM.Item.prototype.fromJSON = function(data) {
374394
return this;
375395
}
376396

377-
MM.Item.prototype.toJSON = function() {
378-
var data = {
379-
id: this._id,
380-
text: this.getText()
397+
MM.Item.prototype.mergeWith = function(data) {
398+
var dirty = false;
399+
if (this.getText() != data.text) { this.setText(data.text); }
400+
401+
if (this._side != data.side) {
402+
this._side = data.side;
403+
dirty = true;
381404
}
382-
383-
if (this._side) { data.side = this._side; }
384-
if (this._color) { data.color = this._color; }
385-
if (this._value) { data.value = this._value; }
386-
if (this._status) { data.status = this._status; }
387-
if (this._layout) { data.layout = this._layout.id; }
388-
if (!this._autoShape) { data.shape = this._shape.id; }
389-
if (this._collapsed) { data.collapsed = 1; }
390-
if (this._children.length) {
391-
data.children = this._children.map(function(child) { return child.toJSON(); });
405+
406+
if (this._color != data.color) {
407+
this._color = data.color;
408+
dirty = true;
392409
}
393410

394-
return data;
411+
if (this._value != data.value) {
412+
this._value = data.value;
413+
dirty = true;
414+
}
415+
416+
if (this._status != data.status) {
417+
this._status = data.status;
418+
dirty = true;
419+
}
420+
421+
if (this._collapsed != !!data.collapsed) { this[this._collapsed ? "expand" : "collapse"](); }
422+
423+
if (this.getOwnLayout() != data.layout) {
424+
this._layout = MM.Layout.getById(data.layout);
425+
dirty = true;
426+
}
427+
428+
var s = (this._autoShape ? null : this._shape.id);
429+
if (s != data.shape) { this.setShape(MM.Shape.getById(data.shape)); }
430+
431+
/* FIXME children - co kdyz je nekdo z nas zrovna aktivni, nerkuli editovatelny? */
432+
(data.children || []).forEach(function(child, index) {
433+
if (index >= this._children.length) { /* new child */
434+
this.insertChild(MM.Item.fromJSON(child));
435+
dirty = true;
436+
} else { /* existing child */
437+
var myChild = this._children[index];
438+
if (myChild.getId() == child.id) { /* recursive merge */
439+
myChild.mergeWith(child);
440+
} else { /* changed; replace */
441+
this._children[index] = MM.Item.fromJSON(child);
442+
dirty = true;
443+
}
444+
}
445+
}, this);
446+
447+
if (dirty) { this.update(); }
395448
}
396449

397450
MM.Item.prototype.clone = function() {
@@ -407,11 +460,11 @@ MM.Item.prototype.clone = function() {
407460
}
408461

409462
MM.Item.prototype.update = function(doNotRecurse) {
410-
MM.publish("item-change", this);
411-
412463
var map = this.getMap();
413464
if (!map || !map.isVisible()) { return this; }
414465

466+
MM.publish("item-change", this);
467+
415468
if (this._autoShape) { /* check for changed auto-shape */
416469
var autoShape = this._getAutoShape();
417470
if (autoShape != this._shape) {
@@ -820,18 +873,22 @@ MM.Map.fromJSON = function(data) {
820873
return new this().fromJSON(data);
821874
}
822875

823-
MM.Map.prototype.fromJSON = function(data) {
824-
this._setRoot(MM.Item.fromJSON(data.root));
825-
return this;
826-
}
827-
828876
MM.Map.prototype.toJSON = function() {
829877
var data = {
830878
root: this._root.toJSON()
831879
};
832880
return data;
833881
}
834882

883+
MM.Map.prototype.fromJSON = function(data) {
884+
this._setRoot(MM.Item.fromJSON(data.root));
885+
return this;
886+
}
887+
888+
MM.Map.prototype.mergeWith = function(data) {
889+
this._root.mergeWith(data.root);
890+
}
891+
835892
MM.Map.prototype.isVisible = function() {
836893
return this._visible;
837894
}
@@ -4241,9 +4298,7 @@ MM.UI.Backend.Firebase.handleMessage = function(message, publisher, data) {
42414298

42424299
case "firebase-change":
42434300
if (data) {
4244-
console.log("remote data changed");
4245-
console.log(data);
4246-
//FIXME MM.App.map.mergeWith(data);
4301+
MM.App.map.mergeWith(data);
42474302
} else { /* FIXME */
42484303
console.log("remote data disappeared");
42494304
}

src/item.js

+70-17
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ MM.Item.fromJSON = function(data) {
6161
return new this().fromJSON(data);
6262
}
6363

64+
MM.Item.prototype.toJSON = function() {
65+
var data = {
66+
id: this._id,
67+
text: this.getText()
68+
}
69+
70+
if (this._side) { data.side = this._side; }
71+
if (this._color) { data.color = this._color; }
72+
if (this._value) { data.value = this._value; }
73+
if (this._status) { data.status = this._status; }
74+
if (this._layout) { data.layout = this._layout.id; }
75+
if (!this._autoShape) { data.shape = this._shape.id; }
76+
if (this._collapsed) { data.collapsed = 1; }
77+
if (this._children.length) {
78+
data.children = this._children.map(function(child) { return child.toJSON(); });
79+
}
80+
81+
return data;
82+
}
83+
6484
/**
6585
* Only when creating a new item. To merge existing items, use .mergeWith().
6686
*/
@@ -85,24 +105,57 @@ MM.Item.prototype.fromJSON = function(data) {
85105
return this;
86106
}
87107

88-
MM.Item.prototype.toJSON = function() {
89-
var data = {
90-
id: this._id,
91-
text: this.getText()
108+
MM.Item.prototype.mergeWith = function(data) {
109+
var dirty = false;
110+
if (this.getText() != data.text) { this.setText(data.text); }
111+
112+
if (this._side != data.side) {
113+
this._side = data.side;
114+
dirty = true;
92115
}
93-
94-
if (this._side) { data.side = this._side; }
95-
if (this._color) { data.color = this._color; }
96-
if (this._value) { data.value = this._value; }
97-
if (this._status) { data.status = this._status; }
98-
if (this._layout) { data.layout = this._layout.id; }
99-
if (!this._autoShape) { data.shape = this._shape.id; }
100-
if (this._collapsed) { data.collapsed = 1; }
101-
if (this._children.length) {
102-
data.children = this._children.map(function(child) { return child.toJSON(); });
116+
117+
if (this._color != data.color) {
118+
this._color = data.color;
119+
dirty = true;
103120
}
104121

105-
return data;
122+
if (this._value != data.value) {
123+
this._value = data.value;
124+
dirty = true;
125+
}
126+
127+
if (this._status != data.status) {
128+
this._status = data.status;
129+
dirty = true;
130+
}
131+
132+
if (this._collapsed != !!data.collapsed) { this[this._collapsed ? "expand" : "collapse"](); }
133+
134+
if (this.getOwnLayout() != data.layout) {
135+
this._layout = MM.Layout.getById(data.layout);
136+
dirty = true;
137+
}
138+
139+
var s = (this._autoShape ? null : this._shape.id);
140+
if (s != data.shape) { this.setShape(MM.Shape.getById(data.shape)); }
141+
142+
/* FIXME children - co kdyz je nekdo z nas zrovna aktivni, nerkuli editovatelny? */
143+
(data.children || []).forEach(function(child, index) {
144+
if (index >= this._children.length) { /* new child */
145+
this.insertChild(MM.Item.fromJSON(child));
146+
dirty = true;
147+
} else { /* existing child */
148+
var myChild = this._children[index];
149+
if (myChild.getId() == child.id) { /* recursive merge */
150+
myChild.mergeWith(child);
151+
} else { /* changed; replace */
152+
this._children[index] = MM.Item.fromJSON(child);
153+
dirty = true;
154+
}
155+
}
156+
}, this);
157+
158+
if (dirty) { this.update(); }
106159
}
107160

108161
MM.Item.prototype.clone = function() {
@@ -118,11 +171,11 @@ MM.Item.prototype.clone = function() {
118171
}
119172

120173
MM.Item.prototype.update = function(doNotRecurse) {
121-
MM.publish("item-change", this);
122-
123174
var map = this.getMap();
124175
if (!map || !map.isVisible()) { return this; }
125176

177+
MM.publish("item-change", this);
178+
126179
if (this._autoShape) { /* check for changed auto-shape */
127180
var autoShape = this._getAutoShape();
128181
if (autoShape != this._shape) {

src/map.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,22 @@ MM.Map.fromJSON = function(data) {
1515
return new this().fromJSON(data);
1616
}
1717

18-
MM.Map.prototype.fromJSON = function(data) {
19-
this._setRoot(MM.Item.fromJSON(data.root));
20-
return this;
21-
}
22-
2318
MM.Map.prototype.toJSON = function() {
2419
var data = {
2520
root: this._root.toJSON()
2621
};
2722
return data;
2823
}
2924

25+
MM.Map.prototype.fromJSON = function(data) {
26+
this._setRoot(MM.Item.fromJSON(data.root));
27+
return this;
28+
}
29+
30+
MM.Map.prototype.mergeWith = function(data) {
31+
this._root.mergeWith(data.root);
32+
}
33+
3034
MM.Map.prototype.isVisible = function() {
3135
return this._visible;
3236
}

src/ui.backend.firebase.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ MM.UI.Backend.Firebase.handleMessage = function(message, publisher, data) {
7676

7777
case "firebase-change":
7878
if (data) {
79-
console.log("remote data changed");
80-
console.log(data);
81-
//FIXME MM.App.map.mergeWith(data);
79+
MM.App.map.mergeWith(data);
8280
} else { /* FIXME */
8381
console.log("remote data disappeared");
8482
}

0 commit comments

Comments
 (0)