@@ -350,6 +350,26 @@ MM.Item.fromJSON = function(data) {
350
350
return new this ( ) . fromJSON ( data ) ;
351
351
}
352
352
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
+
353
373
/**
354
374
* Only when creating a new item. To merge existing items, use .mergeWith().
355
375
*/
@@ -374,24 +394,57 @@ MM.Item.prototype.fromJSON = function(data) {
374
394
return this ;
375
395
}
376
396
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 ;
381
404
}
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 ;
392
409
}
393
410
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 ( ) ; }
395
448
}
396
449
397
450
MM . Item . prototype . clone = function ( ) {
@@ -407,11 +460,11 @@ MM.Item.prototype.clone = function() {
407
460
}
408
461
409
462
MM . Item . prototype . update = function ( doNotRecurse ) {
410
- MM . publish ( "item-change" , this ) ;
411
-
412
463
var map = this . getMap ( ) ;
413
464
if ( ! map || ! map . isVisible ( ) ) { return this ; }
414
465
466
+ MM . publish ( "item-change" , this ) ;
467
+
415
468
if ( this . _autoShape ) { /* check for changed auto-shape */
416
469
var autoShape = this . _getAutoShape ( ) ;
417
470
if ( autoShape != this . _shape ) {
@@ -820,18 +873,22 @@ MM.Map.fromJSON = function(data) {
820
873
return new this ( ) . fromJSON ( data ) ;
821
874
}
822
875
823
- MM . Map . prototype . fromJSON = function ( data ) {
824
- this . _setRoot ( MM . Item . fromJSON ( data . root ) ) ;
825
- return this ;
826
- }
827
-
828
876
MM . Map . prototype . toJSON = function ( ) {
829
877
var data = {
830
878
root : this . _root . toJSON ( )
831
879
} ;
832
880
return data ;
833
881
}
834
882
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
+
835
892
MM . Map . prototype . isVisible = function ( ) {
836
893
return this . _visible ;
837
894
}
@@ -4241,9 +4298,7 @@ MM.UI.Backend.Firebase.handleMessage = function(message, publisher, data) {
4241
4298
4242
4299
case "firebase-change" :
4243
4300
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 ) ;
4247
4302
} else { /* FIXME */
4248
4303
console . log ( "remote data disappeared" ) ;
4249
4304
}
0 commit comments