-
Notifications
You must be signed in to change notification settings - Fork 6
/
jsmind.js
2930 lines (2687 loc) · 101 KB
/
jsmind.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Released under BSD License
* Copyright (c) 2014-2016 [email protected]
*
* Project Home:
* https://github.com/hizzgdev/jsmind/
*/
;(function($w){
'use strict';
// set 'jsMind' as the library name.
// __name__ should be a const value, Never try to change it easily.
var __name__ = 'jsMind';
// library version
var __version__ = '0.4.6';
// author
var __author__ = '[email protected]';
// an noop function define
var _noop = function(){};
var logger = (typeof console === 'undefined')?{
log:_noop, debug:_noop, error:_noop, warn:_noop, info:_noop
}:console;
// check global variables
if(typeof module === 'undefined' || !module.exports){
if(typeof $w[__name__] != 'undefined'){
logger.log(__name__+' has been already exist.');
return;
}
}
// shortcut of methods in dom
var $d = $w.document;
var $g = function(id){return $d.getElementById(id);};
var $c = function(tag){return $d.createElement(tag);};
var $t = function(n,t){if(n.hasChildNodes()){n.firstChild.nodeValue = t;}else{n.appendChild($d.createTextNode(t));}};
var $h = function(n,t){n.innerHTML = t;};
// detect isElement
var $i = function(el){return !!el&&(typeof el==='object')&&(el.nodeType===1)&&(typeof el.style==='object')&&(typeof el.ownerDocument==='object');};
if(typeof String.prototype.startsWith != 'function'){String.prototype.startsWith=function(p){return this.slice(0,p.length)===p;};}
var DEFAULT_OPTIONS = {
container : '', // id of the container
editable : false, // you can change it in your options
theme : null,
mode :'full', // full or side
support_html : true,
view:{
hmargin:100,
vmargin:50,
line_width:2,
line_color:'#3598DB'
},
layout:{
hspace:50,
vspace:20,
pspace:13
},
default_event_handle:{
enable_mousedown_handle:true,
enable_click_handle:true,
enable_dblclick_handle:true
},
shortcut:{
enable:true,
handles:{
},
mapping:{
addchild : 45, // Insert
addbrother : 13, // Enter
editnode : 113,// F2
delnode : 46, // Delete
toggle : 32, // Space
// left : 37, // Left
// up : 38, // Up
// right : 39, // Right
// down : 40, // Down
}
},
};
// core object
var jm = function(options){
jm.current = this;
this.version = __version__;
var opts = {};
jm.util.json.merge(opts, DEFAULT_OPTIONS);
jm.util.json.merge(opts, options);
if(!opts.container){
logger.error('the options.container should not be null or empty.');
return;
}
this.options = opts;
this.inited = false;
this.mind = null;
this.event_handles = [];
this.init();
};
// ============= static object =============================================
jm.direction = {left:-1,center:0,right:1};
jm.event_type = {show:1,resize:2,edit:3,select:4};
jm.node = function(sId,iIndex,sTopic,oData,bIsRoot,oParent,eDirection,bExpanded){
if(!sId){logger.error('invalid nodeid');return;}
if(typeof iIndex != 'number'){logger.error('invalid node index');return;}
if(typeof bExpanded === 'undefined'){bExpanded = true;}
this.id = sId;
this.index = iIndex;
this.topic = sTopic;
this.data = oData || {};
this.isroot = bIsRoot;
this.parent = oParent;
this.direction = eDirection;
this.expanded = !!bExpanded;
this.children = [];
this._data = {};
};
jm.node.compare=function(node1,node2){
// '-1' is alwary the last
var r = 0;
var i1 = node1.index;
var i2 = node2.index;
if(i1>=0 && i2>=0){
r = i1-i2;
}else if(i1==-1 && i2==-1){
r = 0;
}else if(i1==-1){
r = 1;
}else if(i2==-1){
r = -1;
}else{
r = 0;
}
//logger.debug(i1+' <> '+i2+' = '+r);
return r;
};
jm.node.inherited=function(pnode,node){
if(!!pnode && !!node){
if(pnode.id === node.id){
return true;
}
if(pnode.isroot){
return true;
}
var pid = pnode.id;
var p = node;
while(!p.isroot){
p = p.parent;
if(p.id === pid){
return true;
}
}
}
return false;
};
jm.node.prototype = {
get_location:function(){
var vd = this._data.view;
return {
x:vd.abs_x,
y:vd.abs_y
};
},
get_size:function(){
var vd = this._data.view;
return {
w:vd.width,
h:vd.height
}
}
};
jm.mind = function(){
this.name = null;
this.author = null;
this.version = null;
this.root = null;
this.selected = null;
this.nodes = {};
};
jm.mind.prototype = {
get_node:function(nodeid){
if(nodeid in this.nodes){
return this.nodes[nodeid];
}else{
logger.warn('the node[id='+nodeid+'] can not be found');
return null;
}
},
set_root:function(nodeid, topic, data){
if(this.root == null){
this.root = new jm.node(nodeid, 0, topic, data, true);
this._put_node(this.root);
}else{
logger.error('root node is already exist');
}
},
add_node:function(parent_node, nodeid, topic, data, idx, direction, expanded){
if(!jm.util.is_node(parent_node)){
var the_parent_node = this.get_node(parent_node);
if(!the_parent_node){
logger.error('the parent_node[id='+parent_node+'] can not be found.');
return null;
}else{
return this.add_node(the_parent_node, nodeid, topic, data, idx, direction, expanded);
}
}
var nodeindex = idx || -1;
var node = null;
if(parent_node.isroot){
var d = jm.direction.right;
if(isNaN(direction)){
var children = parent_node.children;
var children_len = children.length;
var r = 0;
for(var i=0;i<children_len;i++){if(children[i].direction === jm.direction.left){r--;}else{r++;}}
d = (children_len > 1 && r > 0) ? jm.direction.left : jm.direction.right
}else{
d = (direction != jm.direction.left) ? jm.direction.right : jm.direction.left;
}
node = new jm.node(nodeid,nodeindex,topic,data,false,parent_node,d,expanded);
}else{
node = new jm.node(nodeid,nodeindex,topic,data,false,parent_node,parent_node.direction,expanded);
}
if(this._put_node(node)){
parent_node.children.push(node);
this._reindex(parent_node);
}else{
logger.error('fail, the nodeid \''+node.id+'\' has been already exist.');
node = null;
}
return node;
},
insert_node_before:function(node_before, nodeid, topic, data){
if(!jm.util.is_node(node_before)){
var the_node_before = this.get_node(node_before);
if(!the_node_before){
logger.error('the node_before[id='+node_before+'] can not be found.');
return null;
}else{
return this.insert_node_before(the_node_before, nodeid, topic, data);
}
}
var node_index = node_before.index-0.5;
return this.add_node(node_before.parent, nodeid, topic, data, node_index);
},
get_node_before:function(node){
if(!jm.util.is_node(node)){
var the_node = this.get_node(node);
if(!the_node){
logger.error('the node[id='+node+'] can not be found.');
return null;
}else{
return this.get_node_before(the_node);
}
}
if(node.isroot){return null;}
var idx = node.index - 2;
if(idx >= 0){
return node.parent.children[idx];
}else{
return null;
}
},
insert_node_after:function(node_after, nodeid, topic, data){
if(!jm.util.is_node(node_after)){
var the_node_after = this.get_node(node_before);
if(!the_node_after){
logger.error('the node_after[id='+node_after+'] can not be found.');
return null;
}else{
return this.insert_node_after(the_node_after, nodeid, topic, data);
}
}
var node_index = node_after.index + 0.5;
return this.add_node(node_after.parent, nodeid, topic, data, node_index);
},
get_node_after:function(node){
if(!jm.util.is_node(node)){
var the_node = this.get_node(node);
if(!the_node){
logger.error('the node[id='+node+'] can not be found.');
return null;
}else{
return this.get_node_after(the_node);
}
}
if(node.isroot){return null;}
var idx = node.index;
var brothers = node.parent.children;
if(brothers.length >= idx){
return node.parent.children[idx];
}else{
return null;
}
},
move_node:function(node, beforeid, parentid, direction){
if(!jm.util.is_node(node)){
var the_node = this.get_node(node);
if(!the_node){
logger.error('the node[id='+node+'] can not be found.');
return null;
}else{
return this.move_node(the_node, beforeid, parentid, direction);
}
}
if(!parentid){
parentid = node.parent.id;
}
return this._move_node(node, beforeid, parentid, direction);
},
_flow_node_direction:function(node,direction){
if(typeof direction === 'undefined'){
direction = node.direction;
}else{
node.direction = direction;
}
var len = node.children.length;
while(len--){
this._flow_node_direction(node.children[len],direction);
}
},
_move_node_internal:function(node, beforeid){
if(!!node && !!beforeid){
if(beforeid == '_last_'){
node.index = -1;
this._reindex(node.parent);
}else if(beforeid == '_first_'){
node.index = 0;
this._reindex(node.parent);
}else{
var node_before = (!!beforeid)?this.get_node(beforeid):null;
if(node_before!=null && node_before.parent!=null && node_before.parent.id==node.parent.id){
node.index = node_before.index - 0.5;
this._reindex(node.parent);
}
}
}
return node;
},
_move_node:function(node, beforeid, parentid, direction){
if(!!node && !!parentid){
if(node.parent.id != parentid){
// remove from parent's children
var sibling = node.parent.children;
var si = sibling.length;
while(si--){
if(sibling[si].id == node.id){
sibling.splice(si,1);
break;
}
}
node.parent = this.get_node(parentid);
node.parent.children.push(node);
}
if(node.parent.isroot){
if(direction == jsMind.direction.left){
node.direction = direction;
}else{
node.direction = jm.direction.right;
}
}else{
node.direction = node.parent.direction;
}
this._move_node_internal(node, beforeid);
this._flow_node_direction(node);
}
return node;
},
remove_node:function(node){
if(!jm.util.is_node(node)){
var the_node = this.get_node(node);
if(!the_node){
logger.error('the node[id='+node+'] can not be found.');
return false;
}else{
return this.remove_node(the_node);
}
}
if(!node){
logger.error('fail, the node can not be found');
return false;
}
if(node.isroot){
logger.error('fail, can not remove root node');
return false;
}
if(this.selected!=null && this.selected.id == node.id){
this.selected = null;
}
// clean all subordinate nodes
var children = node.children;
var ci = children.length;
while(ci--){
this.remove_node(children[ci]);
}
// clean all children
children.length = 0;
// remove from parent's children
var sibling = node.parent.children;
var si = sibling.length;
while(si--){
if(sibling[si].id == node.id){
sibling.splice(si,1);
break;
}
}
// remove from global nodes
delete this.nodes[node.id];
// clean all properties
for(var k in node){
delete node[k];
}
// remove it's self
node = null;
//delete node;
return true;
},
_put_node:function(node){
if(node.id in this.nodes){
logger.warn('the nodeid \''+node.id+'\' has been already exist.');
return false;
}else{
this.nodes[node.id] = node;
return true;
}
},
_reindex:function(node){
if(node instanceof jm.node){
node.children.sort(jm.node.compare);
for(var i=0;i<node.children.length;i++){
node.children[i].index = i+1;
}
}
},
};
jm.format = {
node_tree:{
example:{
"meta":{
"name":__name__,
"author":__author__,
"version":__version__
},
"format":"node_tree",
"data":{"id":"root","topic":"Welcome to EAF MindMap"}
},
get_mind:function(source){
var df = jm.format.node_tree;
var mind = new jm.mind();
mind.name = source.meta.name;
mind.author = source.meta.author;
mind.version = source.meta.version;
df._parse(mind,source.data);
return mind;
},
get_data:function(mind){
var df = jm.format.node_tree;
var json = {};
json.meta = {
name : mind.name,
author : mind.author,
version : mind.version
};
json.format = 'node_tree';
json.data = df._buildnode(mind.root);
return json;
},
_parse:function(mind, node_root){
var df = jm.format.node_tree;
var data = df._extract_data(node_root);
mind.set_root(node_root.id, node_root.topic, data);
if('children' in node_root){
var children = node_root.children;
for(var i=0;i<children.length;i++){
df._extract_subnode(mind, mind.root, children[i]);
}
}
},
_extract_data:function(node_json){
var data = {};
for(var k in node_json){
if(k == 'id' || k=='topic' || k=='children' || k=='direction' || k=='expanded'){
continue;
}
data[k] = node_json[k];
}
return data;
},
_extract_subnode:function(mind, node_parent, node_json){
var df = jm.format.node_tree;
var data = df._extract_data(node_json);
var d = null;
if(node_parent.isroot){
d = node_json.direction == 'left'?jm.direction.left:jm.direction.right;
}
var node = mind.add_node(node_parent, node_json.id, node_json.topic, data, null, d, node_json.expanded);
if('children' in node_json){
var children = node_json.children;
for(var i=0;i<children.length;i++){
df._extract_subnode(mind, node, children[i]);
}
}
},
_buildnode:function(node){
var df = jm.format.node_tree;
if(!(node instanceof jm.node)){return;}
var o = {
id : node.id,
topic : node.topic,
expanded : node.expanded
};
if(!!node.parent && node.parent.isroot){
o.direction = node.direction == jm.direction.left?'left':'right';
}
if(node.data != null){
var node_data = node.data;
for(var k in node_data){
o[k] = node_data[k];
}
}
var children = node.children;
if(children.length > 0){
o.children = [];
for(var i=0;i<children.length;i++){
o.children.push(df._buildnode(children[i]));
}
}
return o;
}
},
node_array:{
example:{
"meta":{
"name":__name__,
"author":__author__,
"version":__version__
},
"format":"node_array",
"data":[
{"id":"root","topic":"Welcome to EAF MindMap", "isroot":true}
]
},
get_mind:function(source){
var df = jm.format.node_array;
var mind = new jm.mind();
mind.name = source.meta.name;
mind.author = source.meta.author;
mind.version = source.meta.version;
df._parse(mind,source.data);
return mind;
},
get_data:function(mind){
var df = jm.format.node_array;
var json = {};
json.meta = {
name : mind.name,
author : mind.author,
version : mind.version
};
json.format = 'node_array';
json.data = [];
df._array(mind,json.data);
return json;
},
_parse:function(mind, node_array){
var df = jm.format.node_array;
var narray = node_array.slice(0);
// reverse array for improving looping performance
narray.reverse();
var root_id = df._extract_root(mind, narray);
if(!!root_id){
df._extract_subnode(mind, root_id, narray);
}else{
logger.error('root node can not be found');
}
},
_extract_root:function(mind, node_array){
var df = jm.format.node_array;
var i = node_array.length;
while(i--){
if('isroot' in node_array[i] && node_array[i].isroot){
var root_json = node_array[i];
var data = df._extract_data(root_json);
mind.set_root(root_json.id,root_json.topic,data);
node_array.splice(i,1);
return root_json.id;
}
}
return null;
},
_extract_subnode:function(mind, parentid, node_array){
var df = jm.format.node_array;
var i = node_array.length;
var node_json = null;
var data = null;
var extract_count = 0;
while(i--){
node_json = node_array[i];
if(node_json.parentid == parentid){
data = df._extract_data(node_json);
var d = null;
var node_direction = node_json.direction;
if(!!node_direction){
d = node_direction == 'left'?jm.direction.left:jm.direction.right;
}
mind.add_node(parentid, node_json.id, node_json.topic, data, null, d, node_json.expanded);
node_array.splice(i,1);
extract_count ++;
var sub_extract_count = df._extract_subnode(mind, node_json.id, node_array);
if(sub_extract_count > 0){
// reset loop index after extract subordinate node
i = node_array.length;
extract_count += sub_extract_count;
}
}
}
return extract_count;
},
_extract_data:function(node_json){
var data = {};
for(var k in node_json){
if(k == 'id' || k=='topic' || k=='parentid' || k=='isroot' || k=='direction' || k=='expanded'){
continue;
}
data[k] = node_json[k];
}
return data;
},
_array:function(mind, node_array){
var df = jm.format.node_array;
df._array_node(mind.root, node_array);
},
_array_node:function(node, node_array){
var df = jm.format.node_array;
if(!(node instanceof jm.node)){return;}
var o = {
id : node.id,
topic : node.topic,
expanded : node.expanded
};
if(!!node.parent){
o.parentid = node.parent.id;
}
if(node.isroot){
o.isroot = true;
}
if(!!node.parent && node.parent.isroot){
o.direction = node.direction == jm.direction.left?'left':'right';
}
if(node.data != null){
var node_data = node.data;
for(var k in node_data){
o[k] = node_data[k];
}
}
node_array.push(o);
var ci = node.children.length;
for(var i=0;i<ci;i++){
df._array_node(node.children[i], node_array);
}
},
},
freemind:{
example:{
"meta":{
"name":__name__,
"author":__author__,
"version":__version__
},
"format":"freemind",
"data":"<map version=\"1.0.1\"><node ID=\"root\" TEXT=\"freemind Example\"/></map>"
},
get_mind:function(source){
var df = jm.format.freemind;
var mind = new jm.mind();
mind.name = source.meta.name;
mind.author = source.meta.author;
mind.version = source.meta.version;
var xml = source.data;
var xml_doc = df._parse_xml(xml);
var xml_root = df._find_root(xml_doc);
df._load_node(mind, null, xml_root);
return mind;
},
get_data:function(mind){
var df = jm.format.freemind;
var json = {};
json.meta = {
name : mind.name,
author : mind.author,
version : mind.version
};
json.format = 'freemind';
var xmllines = [];
xmllines.push('<map version=\"1.0.1\">');
df._buildmap(mind.root, xmllines);
xmllines.push('</map>');
json.data = xmllines.join(' ');
return json;
},
_parse_xml:function(xml){
var xml_doc = null;
if (window.DOMParser){
var parser = new DOMParser();
xml_doc = parser.parseFromString(xml,'text/xml');
}else{ // Internet Explorer
xml_doc = new ActiveXObject('Microsoft.XMLDOM');
xml_doc.async = false;
xml_doc.loadXML(xml);
}
return xml_doc;
},
_find_root:function(xml_doc){
var nodes = xml_doc.childNodes;
var node = null;
var root = null;
var n = null;
for(var i=0;i<nodes.length;i++){
n = nodes[i];
if(n.nodeType == 1 && n.tagName == 'map'){
node = n;
break;
}
}
if(!!node){
var ns = node.childNodes;
node = null;
for(var i=0;i<ns.length;i++){
n = ns[i];
if(n.nodeType == 1 && n.tagName == 'node'){
node = n;
break;
}
}
}
return node;
},
_load_node:function(mind, parent_id, xml_node){
var df = jm.format.freemind;
var node_id = xml_node.getAttribute('ID');
var node_topic = xml_node.getAttribute('TEXT');
// look for richcontent
if(node_topic == null){
var topic_children = xml_node.childNodes;
var topic_child = null;
for(var i=0;i<topic_children.length;i++){
topic_child = topic_children[i];
//logger.debug(topic_child.tagName);
if(topic_child.nodeType == 1 && topic_child.tagName === 'richcontent'){
node_topic = topic_child.textContent;
break;
}
}
}
var node_data = df._load_attributes(xml_node);
var node_expanded = ('expanded' in node_data)?(node_data.expanded == 'true') : true;
delete node_data.expanded;
var node_position = xml_node.getAttribute('POSITION');
var node_direction = null;
if(!!node_position){
node_direction = node_position=='left'?jm.direction.left:jm.direction.right;
}
//logger.debug(node_position +':'+ node_direction);
if(!!parent_id){
mind.add_node(parent_id, node_id, node_topic, node_data, null, node_direction, node_expanded);
}else{
mind.set_root(node_id, node_topic, node_data);
}
var children = xml_node.childNodes;
var child = null;
for(var i=0;i<children.length;i++){
child = children[i];
if(child.nodeType == 1 && child.tagName == 'node'){
df._load_node(mind, node_id, child);
}
}
},
_load_attributes:function(xml_node){
var children = xml_node.childNodes;
var attr = null;
var attr_data = {};
for(var i=0;i<children.length;i++){
attr = children[i];
if(attr.nodeType == 1 && attr.tagName === 'attribute'){
attr_data[attr.getAttribute('NAME')] = attr.getAttribute('VALUE');
}
}
return attr_data;
},
_buildmap:function(node, xmllines){
var df = jm.format.freemind;
var pos = null;
if(!!node.parent && node.parent.isroot){
pos = node.direction === jm.direction.left?'left':'right';
}
xmllines.push('<node');
xmllines.push('ID=\"'+node.id+'\"');
if(!!pos){
xmllines.push('POSITION=\"'+pos+'\"');
}
xmllines.push('TEXT=\"'+node.topic+'\">');
// store expanded status as an attribute
xmllines.push('<attribute NAME=\"expanded\" VALUE=\"'+node.expanded+'\"/>');
// for attributes
var node_data = node.data;
if(node_data != null){
for(var k in node_data){
xmllines.push('<attribute NAME=\"'+k+'\" VALUE=\"'+node_data[k]+'\"/>');
}
}
// for children
var children = node.children;
for(var i=0;i<children.length;i++){
df._buildmap(children[i], xmllines);
}
xmllines.push('</node>');
},
},
};
// ============= utility object =============================================
jm.util = {
is_node: function(node){
return !!node && node instanceof jm.node;
},
ajax:{
_xhr:function(){
var xhr = null;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else{
try{
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}catch(e){}
}
return xhr;
},
_eurl:function(url){
return encodeURIComponent(url);
},
request:function(url,param,method,callback,fail_callback){
var a = jm.util.ajax;
var p = null;
var tmp_param = [];
for(var k in param){
tmp_param.push(a._eurl(k)+'='+a._eurl(param[k]));
}
if(tmp_param.length>0){
p = tmp_param.join('&');
}
var xhr = a._xhr();
if(!xhr){return;}
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.status == 200 || xhr.status == 0){
if(typeof callback === 'function'){
var data = jm.util.json.string2json(xhr.responseText);
if(data != null){
callback(data);
}else{
callback(xhr.responseText);
}
}
}else{
if(typeof fail_callback === 'function'){
fail_callback(xhr);
}else{
logger.error('xhr request failed.',xhr);
}
}
}
}
method = method || 'GET';
xhr.open(method,url,true);
xhr.setRequestHeader('If-Modified-Since','0');
if(method == 'POST'){
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=utf-8');
xhr.send(p);
}else{
xhr.send();
}
},
get:function(url,callback){
return jm.util.ajax.request(url,{},'GET',callback);
},
post:function(url,param,callback){
return jm.util.ajax.request(url,param,'POST',callback);
}
},
dom:{
//target,eventType,handler
add_event:function(t,e,h){
if(!!t.addEventListener){
t.addEventListener(e,h,false);
}else{
t.attachEvent('on'+e,h);
}
}
},
canvas:{
bezierto: function(ctx,x1,y1,x2,y2){
ctx.beginPath();
ctx.moveTo(x1,y1);
ctx.bezierCurveTo(x1+(x2-x1)*2/3,y1,x1,y2,x2,y2);
ctx.stroke();
},
lineto : function(ctx,x1,y1,x2,y2){
ctx.beginPath();
ctx.moveTo(x1,y1);
ctx.lineTo(x2,y2);
ctx.stroke();
},
clear:function(ctx,x,y,w,h){
ctx.clearRect(x,y,w,h);
}
},
file:{
read:function(file_data,fn_callback){
var reader = new FileReader();
reader.onload = function(){
if(typeof fn_callback === 'function'){
fn_callback(this.result, file_data.name);
}
};
reader.readAsText(file_data);
},
save:function(file_data, type, name) {
var blob;
if (typeof $w.Blob === 'function') {
blob = new Blob([file_data], {type: type});
} else {
var BlobBuilder = $w.BlobBuilder || $w.MozBlobBuilder || $w.WebKitBlobBuilder || $w.MSBlobBuilder;
var bb = new BlobBuilder();
bb.append(file_data);
blob = bb.getBlob(type);
}
if (navigator.msSaveBlob) {
navigator.msSaveBlob(blob, name);
} else {
var URL = $w.URL || $w.webkitURL;
var bloburl = URL.createObjectURL(blob);
var anchor = $c('a');
if ('download' in anchor) {
anchor.style.visibility = 'hidden';