jsmind 0.4.6 → 0.4.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/js/jsmind.js CHANGED
@@ -1,88 +1,100 @@
1
- /*
2
- * Released under BSD License
3
- * Copyright (c) 2014-2016 hizzgdev@163.com
1
+ /**
2
+ * @license BSD
3
+ * @copyright 2014-2022 hizzgdev@163.com
4
4
  *
5
5
  * Project Home:
6
6
  * https://github.com/hizzgdev/jsmind/
7
7
  */
8
8
 
9
- ;(function($w){
10
- 'use strict';
9
+ ; (function ($w) {
10
+ 'use strict';
11
11
  // set 'jsMind' as the library name.
12
12
  // __name__ should be a const value, Never try to change it easily.
13
13
  var __name__ = 'jsMind';
14
14
  // library version
15
- var __version__ = '0.4.6';
15
+ var __version__ = '0.4.10';
16
16
  // author
17
17
  var __author__ = 'hizzgdev@163.com';
18
18
 
19
19
  // an noop function define
20
- var _noop = function(){};
21
- var logger = (typeof console === 'undefined')?{
22
- log:_noop, debug:_noop, error:_noop, warn:_noop, info:_noop
23
- }:console;
20
+ var _noop = function () { };
21
+ var logger = (typeof console === 'undefined') ? {
22
+ log: _noop, debug: _noop, error: _noop, warn: _noop, info: _noop
23
+ } : console;
24
24
 
25
25
  // check global variables
26
- if(typeof module === 'undefined' || !module.exports){
27
- if(typeof $w[__name__] != 'undefined'){
28
- logger.log(__name__+' has been already exist.');
26
+ if (typeof module === 'undefined' || !module.exports) {
27
+ if (typeof $w[__name__] != 'undefined') {
28
+ logger.log(__name__ + ' has been already exist.');
29
29
  return;
30
30
  }
31
31
  }
32
32
 
33
33
  // shortcut of methods in dom
34
34
  var $d = $w.document;
35
- var $g = function(id){return $d.getElementById(id);};
36
- var $c = function(tag){return $d.createElement(tag);};
37
- var $t = function(n,t){if(n.hasChildNodes()){n.firstChild.nodeValue = t;}else{n.appendChild($d.createTextNode(t));}};
38
- var $h = function(n,t){n.innerHTML = t;};
35
+ var $g = function (id) { return $d.getElementById(id); };
36
+ var $c = function (tag) { return $d.createElement(tag); };
37
+ var $t = function (n, t) { if (n.hasChildNodes()) { n.firstChild.nodeValue = t; } else { n.appendChild($d.createTextNode(t)); } };
38
+
39
+ var $h = function (n, t) {
40
+ if (t instanceof HTMLElement) {
41
+ n.innerHTML = '';
42
+ n.appendChild(t);
43
+ } else {
44
+ n.innerHTML = t;
45
+ }
46
+ };
39
47
  // detect isElement
40
- var $i = function(el){return !!el&&(typeof el==='object')&&(el.nodeType===1)&&(typeof el.style==='object')&&(typeof el.ownerDocument==='object');};
41
- if(typeof String.prototype.startsWith != 'function'){String.prototype.startsWith=function(p){return this.slice(0,p.length)===p;};}
48
+ var $i = function (el) { return !!el && (typeof el === 'object') && (el.nodeType === 1) && (typeof el.style === 'object') && (typeof el.ownerDocument === 'object'); };
49
+ if (typeof String.prototype.startsWith != 'function') { String.prototype.startsWith = function (p) { return this.slice(0, p.length) === p; }; }
42
50
 
43
51
  var DEFAULT_OPTIONS = {
44
- container : '', // id of the container
45
- editable : false, // you can change it in your options
46
- theme : null,
47
- mode :'full', // full or side
48
- support_html : true,
49
-
50
- view:{
51
- hmargin:100,
52
- vmargin:50,
53
- line_width:2,
54
- line_color:'#555'
55
- },
56
- layout:{
57
- hspace:30,
58
- vspace:20,
59
- pspace:13
60
- },
61
- default_event_handle:{
62
- enable_mousedown_handle:true,
63
- enable_click_handle:true,
64
- enable_dblclick_handle:true
65
- },
66
- shortcut:{
67
- enable:true,
68
- handles:{
52
+ container: '', // id of the container
53
+ editable: false, // you can change it in your options
54
+ theme: null,
55
+ mode: 'full', // full or side
56
+ support_html: true,
57
+
58
+ view: {
59
+ engine: 'canvas',
60
+ hmargin: 100,
61
+ vmargin: 50,
62
+ line_width: 2,
63
+ line_color: '#555',
64
+ draggable: false, // drag the mind map with your mouse, when it's larger that the container
65
+ hide_scrollbars_when_draggable: false // hide container scrollbars, when mind map is larger than container and draggable option is true.
66
+ },
67
+ layout: {
68
+ hspace: 30,
69
+ vspace: 20,
70
+ pspace: 13
71
+ },
72
+ default_event_handle: {
73
+ enable_mousedown_handle: true,
74
+ enable_click_handle: true,
75
+ enable_dblclick_handle: true,
76
+ enable_mousewheel_handle: true
77
+ },
78
+ shortcut: {
79
+ enable: true,
80
+ handles: {
69
81
  },
70
- mapping:{
71
- addchild : 45, // Insert
72
- addbrother : 13, // Enter
73
- editnode : 113,// F2
74
- delnode : 46, // Delete
75
- toggle : 32, // Space
76
- left : 37, // Left
77
- up : 38, // Up
78
- right : 39, // Right
79
- down : 40, // Down
82
+ mapping: {
83
+ addchild: 45, // Insert
84
+ addbrother: 13, // Enter
85
+ editnode: 113,// F2
86
+ delnode: 46, // Delete
87
+ toggle: 32, // Space
88
+ left: 37, // Left
89
+ up: 38, // Up
90
+ right: 39, // Right
91
+ down: 40, // Down
80
92
  }
81
93
  },
82
94
  };
83
95
 
84
96
  // core object
85
- var jm = function(options){
97
+ var jm = function (options) {
86
98
  jm.current = this;
87
99
 
88
100
  this.version = __version__;
@@ -90,25 +102,26 @@
90
102
  jm.util.json.merge(opts, DEFAULT_OPTIONS);
91
103
  jm.util.json.merge(opts, options);
92
104
 
93
- if(!opts.container){
105
+ if (!opts.container) {
94
106
  logger.error('the options.container should not be null or empty.');
95
107
  return;
96
108
  }
97
109
  this.options = opts;
98
- this.inited = false;
110
+ this.initialized = false;
99
111
  this.mind = null;
100
112
  this.event_handles = [];
101
113
  this.init();
102
114
  };
103
115
 
104
116
  // ============= static object =============================================
105
- jm.direction = {left:-1,center:0,right:1};
106
- jm.event_type = {show:1,resize:2,edit:3,select:4};
107
-
108
- jm.node = function(sId,iIndex,sTopic,oData,bIsRoot,oParent,eDirection,bExpanded){
109
- if(!sId){logger.error('invalid nodeid');return;}
110
- if(typeof iIndex != 'number'){logger.error('invalid node index');return;}
111
- if(typeof bExpanded === 'undefined'){bExpanded = true;}
117
+ jm.direction = { left: -1, center: 0, right: 1 };
118
+ jm.event_type = { show: 1, resize: 2, edit: 3, select: 4 };
119
+ jm.key = { meta: 1 << 13, ctrl: 1 << 12, alt: 1 << 11, shift: 1 << 10 };
120
+
121
+ jm.node = function (sId, iIndex, sTopic, oData, bIsRoot, oParent, eDirection, bExpanded) {
122
+ if (!sId) { logger.error('invalid node id'); return; }
123
+ if (typeof iIndex != 'number') { logger.error('invalid node index'); return; }
124
+ if (typeof bExpanded === 'undefined') { bExpanded = true; }
112
125
  this.id = sId;
113
126
  this.index = iIndex;
114
127
  this.topic = sTopic;
@@ -121,39 +134,39 @@
121
134
  this._data = {};
122
135
  };
123
136
 
124
- jm.node.compare=function(node1,node2){
137
+ jm.node.compare = function (node1, node2) {
125
138
  // '-1' is alwary the last
126
139
  var r = 0;
127
140
  var i1 = node1.index;
128
141
  var i2 = node2.index;
129
- if(i1>=0 && i2>=0){
130
- r = i1-i2;
131
- }else if(i1==-1 && i2==-1){
142
+ if (i1 >= 0 && i2 >= 0) {
143
+ r = i1 - i2;
144
+ } else if (i1 == -1 && i2 == -1) {
132
145
  r = 0;
133
- }else if(i1==-1){
146
+ } else if (i1 == -1) {
134
147
  r = 1;
135
- }else if(i2==-1){
148
+ } else if (i2 == -1) {
136
149
  r = -1;
137
- }else{
150
+ } else {
138
151
  r = 0;
139
152
  }
140
153
  //logger.debug(i1+' <> '+i2+' = '+r);
141
154
  return r;
142
155
  };
143
156
 
144
- jm.node.inherited=function(pnode,node){
145
- if(!!pnode && !!node){
146
- if(pnode.id === node.id){
157
+ jm.node.inherited = function (pnode, node) {
158
+ if (!!pnode && !!node) {
159
+ if (pnode.id === node.id) {
147
160
  return true;
148
161
  }
149
- if(pnode.isroot){
162
+ if (pnode.isroot) {
150
163
  return true;
151
164
  }
152
165
  var pid = pnode.id;
153
166
  var p = node;
154
- while(!p.isroot){
167
+ while (!p.isroot) {
155
168
  p = p.parent;
156
- if(p.id === pid){
169
+ if (p.id === pid) {
157
170
  return true;
158
171
  }
159
172
  }
@@ -161,25 +174,29 @@
161
174
  return false;
162
175
  };
163
176
 
177
+ jm.node.is_node = function (n) {
178
+ return !!n && n instanceof jm.node;
179
+ };
180
+
164
181
  jm.node.prototype = {
165
- get_location:function(){
182
+ get_location: function () {
166
183
  var vd = this._data.view;
167
184
  return {
168
- x:vd.abs_x,
169
- y:vd.abs_y
185
+ x: vd.abs_x,
186
+ y: vd.abs_y
170
187
  };
171
188
  },
172
- get_size:function(){
189
+ get_size: function () {
173
190
  var vd = this._data.view;
174
191
  return {
175
- w:vd.width,
176
- h:vd.height
192
+ w: vd.width,
193
+ h: vd.height
177
194
  }
178
195
  }
179
196
  };
180
197
 
181
198
 
182
- jm.mind = function(){
199
+ jm.mind = function () {
183
200
  this.name = null;
184
201
  this.author = null;
185
202
  this.version = null;
@@ -189,167 +206,137 @@
189
206
  };
190
207
 
191
208
  jm.mind.prototype = {
192
- get_node:function(nodeid){
193
- if(nodeid in this.nodes){
209
+ get_node: function (nodeid) {
210
+ if (nodeid in this.nodes) {
194
211
  return this.nodes[nodeid];
195
- }else{
196
- logger.warn('the node[id='+nodeid+'] can not be found');
212
+ } else {
213
+ logger.warn('the node[id=' + nodeid + '] can not be found');
197
214
  return null;
198
215
  }
199
216
  },
200
217
 
201
- set_root:function(nodeid, topic, data){
202
- if(this.root == null){
218
+ set_root: function (nodeid, topic, data) {
219
+ if (this.root == null) {
203
220
  this.root = new jm.node(nodeid, 0, topic, data, true);
204
221
  this._put_node(this.root);
205
- }else{
222
+ return this.root;
223
+ } else {
206
224
  logger.error('root node is already exist');
225
+ return null;
207
226
  }
208
227
  },
209
228
 
210
- add_node:function(parent_node, nodeid, topic, data, idx, direction, expanded){
211
- if(!jm.util.is_node(parent_node)){
212
- var the_parent_node = this.get_node(parent_node);
213
- if(!the_parent_node){
214
- logger.error('the parent_node[id='+parent_node+'] can not be found.');
215
- return null;
216
- }else{
217
- return this.add_node(the_parent_node, nodeid, topic, data, idx, direction, expanded);
218
- }
229
+ add_node: function (parent_node, nodeid, topic, data, direction, expanded, idx) {
230
+ if (!jm.util.is_node(parent_node)) {
231
+ logger.error('the parent_node ' + parent_node + ' is not a node.');
232
+ return null;
219
233
  }
220
- var nodeindex = idx || -1;
221
- var node = null;
222
- if(parent_node.isroot){
223
- var d = jm.direction.right;
224
- if(isNaN(direction)){
225
- var children = parent_node.children;
226
- var children_len = children.length;
227
- var r = 0;
228
- for(var i=0;i<children_len;i++){if(children[i].direction === jm.direction.left){r--;}else{r++;}}
229
- d = (children_len > 1 && r > 0) ? jm.direction.left : jm.direction.right
230
- }else{
231
- d = (direction != jm.direction.left) ? jm.direction.right : jm.direction.left;
232
- }
233
- node = new jm.node(nodeid,nodeindex,topic,data,false,parent_node,d,expanded);
234
- }else{
235
- node = new jm.node(nodeid,nodeindex,topic,data,false,parent_node,parent_node.direction,expanded);
236
- }
237
- if(this._put_node(node)){
234
+ var node_index = idx || -1;
235
+ var node = new jm.node(nodeid, node_index, topic, data, false, parent_node, parent_node.direction, expanded);
236
+ if (parent_node.isroot) {
237
+ node.direction = direction || jm.direction.right;
238
+ }
239
+ if (this._put_node(node)) {
238
240
  parent_node.children.push(node);
239
241
  this._reindex(parent_node);
240
- }else{
241
- logger.error('fail, the nodeid \''+node.id+'\' has been already exist.');
242
+ } else {
243
+ logger.error('fail, the nodeid \'' + node.id + '\' has been already exist.');
242
244
  node = null;
243
245
  }
244
246
  return node;
245
247
  },
246
248
 
247
- insert_node_before:function(node_before, nodeid, topic, data){
248
- if(!jm.util.is_node(node_before)){
249
- var the_node_before = this.get_node(node_before);
250
- if(!the_node_before){
251
- logger.error('the node_before[id='+node_before+'] can not be found.');
252
- return null;
253
- }else{
254
- return this.insert_node_before(the_node_before, nodeid, topic, data);
255
- }
249
+ insert_node_before: function (node_before, nodeid, topic, data, direction) {
250
+ if (!jm.util.is_node(node_before)) {
251
+ logger.error('the node_before ' + node_before + ' is not a node.');
252
+ return null;
256
253
  }
257
- var node_index = node_before.index-0.5;
258
- return this.add_node(node_before.parent, nodeid, topic, data, node_index);
254
+ var node_index = node_before.index - 0.5;
255
+ return this.add_node(node_before.parent, nodeid, topic, data, direction, true, node_index);
259
256
  },
260
257
 
261
- get_node_before:function(node){
262
- if(!jm.util.is_node(node)){
258
+ get_node_before: function (node) {
259
+ if (!jm.util.is_node(node)) {
263
260
  var the_node = this.get_node(node);
264
- if(!the_node){
265
- logger.error('the node[id='+node+'] can not be found.');
261
+ if (!the_node) {
262
+ logger.error('the node[id=' + node + '] can not be found.');
266
263
  return null;
267
- }else{
264
+ } else {
268
265
  return this.get_node_before(the_node);
269
266
  }
270
267
  }
271
- if(node.isroot){return null;}
268
+ if (node.isroot) { return null; }
272
269
  var idx = node.index - 2;
273
- if(idx >= 0){
270
+ if (idx >= 0) {
274
271
  return node.parent.children[idx];
275
- }else{
272
+ } else {
276
273
  return null;
277
274
  }
278
275
  },
279
276
 
280
- insert_node_after:function(node_after, nodeid, topic, data){
281
- if(!jm.util.is_node(node_after)){
282
- var the_node_after = this.get_node(node_before);
283
- if(!the_node_after){
284
- logger.error('the node_after[id='+node_after+'] can not be found.');
285
- return null;
286
- }else{
287
- return this.insert_node_after(the_node_after, nodeid, topic, data);
288
- }
277
+ insert_node_after: function (node_after, nodeid, topic, data, direction) {
278
+ if (!jm.util.is_node(node_after)) {
279
+ logger.error('the node_after ' + node_after + ' is not a node.');
280
+ return null;
289
281
  }
290
282
  var node_index = node_after.index + 0.5;
291
- return this.add_node(node_after.parent, nodeid, topic, data, node_index);
283
+ return this.add_node(node_after.parent, nodeid, topic, data, direction, true, node_index);
292
284
  },
293
285
 
294
- get_node_after:function(node){
295
- if(!jm.util.is_node(node)){
286
+ get_node_after: function (node) {
287
+ if (!jm.util.is_node(node)) {
296
288
  var the_node = this.get_node(node);
297
- if(!the_node){
298
- logger.error('the node[id='+node+'] can not be found.');
289
+ if (!the_node) {
290
+ logger.error('the node[id=' + node + '] can not be found.');
299
291
  return null;
300
- }else{
292
+ } else {
301
293
  return this.get_node_after(the_node);
302
294
  }
303
295
  }
304
- if(node.isroot){return null;}
296
+ if (node.isroot) { return null; }
305
297
  var idx = node.index;
306
298
  var brothers = node.parent.children;
307
- if(brothers.length >= idx){
299
+ if (brothers.length > idx) {
308
300
  return node.parent.children[idx];
309
- }else{
301
+ } else {
310
302
  return null;
311
303
  }
312
304
  },
313
305
 
314
- move_node:function(node, beforeid, parentid, direction){
315
- if(!jm.util.is_node(node)){
316
- var the_node = this.get_node(node);
317
- if(!the_node){
318
- logger.error('the node[id='+node+'] can not be found.');
319
- return null;
320
- }else{
321
- return this.move_node(the_node, beforeid, parentid, direction);
322
- }
306
+ move_node: function (node, before_id, parent_id, direction) {
307
+ if (!jm.util.is_node(node)) {
308
+ logger.error('the parameter node ' + node + ' is not a node.');
309
+ return null;
323
310
  }
324
- if(!parentid){
325
- parentid = node.parent.id;
311
+ if (!parent_id) {
312
+ parent_id = node.parent.id;
326
313
  }
327
- return this._move_node(node, beforeid, parentid, direction);
314
+ return this._move_node(node, before_id, parent_id, direction);
328
315
  },
329
316
 
330
- _flow_node_direction:function(node,direction){
331
- if(typeof direction === 'undefined'){
317
+ _flow_node_direction: function (node, direction) {
318
+ if (typeof direction === 'undefined') {
332
319
  direction = node.direction;
333
- }else{
320
+ } else {
334
321
  node.direction = direction;
335
322
  }
336
323
  var len = node.children.length;
337
- while(len--){
338
- this._flow_node_direction(node.children[len],direction);
324
+ while (len--) {
325
+ this._flow_node_direction(node.children[len], direction);
339
326
  }
340
327
  },
341
328
 
342
- _move_node_internal:function(node, beforeid){
343
- if(!!node && !!beforeid){
344
- if(beforeid == '_last_'){
329
+ _move_node_internal: function (node, beforeid) {
330
+ if (!!node && !!beforeid) {
331
+ if (beforeid == '_last_') {
345
332
  node.index = -1;
346
333
  this._reindex(node.parent);
347
- }else if(beforeid == '_first_'){
334
+ } else if (beforeid == '_first_') {
348
335
  node.index = 0;
349
336
  this._reindex(node.parent);
350
- }else{
351
- var node_before = (!!beforeid)?this.get_node(beforeid):null;
352
- if(node_before!=null && node_before.parent!=null && node_before.parent.id==node.parent.id){
337
+ } else {
338
+ var node_before = (!!beforeid) ? this.get_node(beforeid) : null;
339
+ if (node_before != null && node_before.parent != null && node_before.parent.id == node.parent.id) {
353
340
  node.index = node_before.index - 0.5;
354
341
  this._reindex(node.parent);
355
342
  }
@@ -358,29 +345,34 @@
358
345
  return node;
359
346
  },
360
347
 
361
- _move_node:function(node, beforeid, parentid, direction){
362
- if(!!node && !!parentid){
363
- if(node.parent.id != parentid){
348
+ _move_node: function (node, beforeid, parentid, direction) {
349
+ if (!!node && !!parentid) {
350
+ var parent_node = this.get_node(parentid)
351
+ if (jm.node.inherited(node, parent_node)) {
352
+ logger.error('can not move a node to its children');
353
+ return null;
354
+ }
355
+ if (node.parent.id != parentid) {
364
356
  // remove from parent's children
365
357
  var sibling = node.parent.children;
366
358
  var si = sibling.length;
367
- while(si--){
368
- if(sibling[si].id == node.id){
369
- sibling.splice(si,1);
359
+ while (si--) {
360
+ if (sibling[si].id == node.id) {
361
+ sibling.splice(si, 1);
370
362
  break;
371
363
  }
372
364
  }
373
- node.parent = this.get_node(parentid);
374
- node.parent.children.push(node);
365
+ node.parent = parent_node;
366
+ parent_node.children.push(node);
375
367
  }
376
368
 
377
- if(node.parent.isroot){
378
- if(direction == jsMind.direction.left){
369
+ if (node.parent.isroot) {
370
+ if (direction == jm.direction.left) {
379
371
  node.direction = direction;
380
- }else{
372
+ } else {
381
373
  node.direction = jm.direction.right;
382
374
  }
383
- }else{
375
+ } else {
384
376
  node.direction = node.parent.direction;
385
377
  }
386
378
  this._move_node_internal(node, beforeid);
@@ -389,31 +381,22 @@
389
381
  return node;
390
382
  },
391
383
 
392
- remove_node:function(node){
393
- if(!jm.util.is_node(node)){
394
- var the_node = this.get_node(node);
395
- if(!the_node){
396
- logger.error('the node[id='+node+'] can not be found.');
397
- return false;
398
- }else{
399
- return this.remove_node(the_node);
400
- }
401
- }
402
- if(!node){
403
- logger.error('fail, the node can not be found');
384
+ remove_node: function (node) {
385
+ if (!jm.util.is_node(node)) {
386
+ logger.error('the parameter node ' + node + ' is not a node.');
404
387
  return false;
405
388
  }
406
- if(node.isroot){
389
+ if (node.isroot) {
407
390
  logger.error('fail, can not remove root node');
408
391
  return false;
409
392
  }
410
- if(this.selected!=null && this.selected.id == node.id){
393
+ if (this.selected != null && this.selected.id == node.id) {
411
394
  this.selected = null;
412
395
  }
413
396
  // clean all subordinate nodes
414
397
  var children = node.children;
415
398
  var ci = children.length;
416
- while(ci--){
399
+ while (ci--) {
417
400
  this.remove_node(children[ci]);
418
401
  }
419
402
  // clean all children
@@ -421,16 +404,16 @@
421
404
  // remove from parent's children
422
405
  var sibling = node.parent.children;
423
406
  var si = sibling.length;
424
- while(si--){
425
- if(sibling[si].id == node.id){
426
- sibling.splice(si,1);
407
+ while (si--) {
408
+ if (sibling[si].id == node.id) {
409
+ sibling.splice(si, 1);
427
410
  break;
428
411
  }
429
412
  }
430
413
  // remove from global nodes
431
414
  delete this.nodes[node.id];
432
415
  // clean all properties
433
- for(var k in node){
416
+ for (var k in node) {
434
417
  delete node[k];
435
418
  }
436
419
  // remove it's self
@@ -439,75 +422,75 @@
439
422
  return true;
440
423
  },
441
424
 
442
- _put_node:function(node){
443
- if(node.id in this.nodes){
444
- logger.warn('the nodeid \''+node.id+'\' has been already exist.');
425
+ _put_node: function (node) {
426
+ if (node.id in this.nodes) {
427
+ logger.warn('the nodeid \'' + node.id + '\' has been already exist.');
445
428
  return false;
446
- }else{
429
+ } else {
447
430
  this.nodes[node.id] = node;
448
431
  return true;
449
432
  }
450
433
  },
451
434
 
452
- _reindex:function(node){
453
- if(node instanceof jm.node){
435
+ _reindex: function (node) {
436
+ if (node instanceof jm.node) {
454
437
  node.children.sort(jm.node.compare);
455
- for(var i=0;i<node.children.length;i++){
456
- node.children[i].index = i+1;
438
+ for (var i = 0; i < node.children.length; i++) {
439
+ node.children[i].index = i + 1;
457
440
  }
458
441
  }
459
442
  },
460
443
  };
461
444
 
462
445
  jm.format = {
463
- node_tree:{
464
- example:{
465
- "meta":{
466
- "name":__name__,
467
- "author":__author__,
468
- "version":__version__
446
+ node_tree: {
447
+ example: {
448
+ "meta": {
449
+ "name": __name__,
450
+ "author": __author__,
451
+ "version": __version__
469
452
  },
470
- "format":"node_tree",
471
- "data":{"id":"root","topic":"jsMind Example"}
453
+ "format": "node_tree",
454
+ "data": { "id": "root", "topic": "jsMind Example" }
472
455
  },
473
- get_mind:function(source){
456
+ get_mind: function (source) {
474
457
  var df = jm.format.node_tree;
475
458
  var mind = new jm.mind();
476
459
  mind.name = source.meta.name;
477
460
  mind.author = source.meta.author;
478
461
  mind.version = source.meta.version;
479
- df._parse(mind,source.data);
462
+ df._parse(mind, source.data);
480
463
  return mind;
481
464
  },
482
- get_data:function(mind){
465
+ get_data: function (mind) {
483
466
  var df = jm.format.node_tree;
484
467
  var json = {};
485
468
  json.meta = {
486
- name : mind.name,
487
- author : mind.author,
488
- version : mind.version
469
+ name: mind.name,
470
+ author: mind.author,
471
+ version: mind.version
489
472
  };
490
473
  json.format = 'node_tree';
491
474
  json.data = df._buildnode(mind.root);
492
475
  return json;
493
476
  },
494
477
 
495
- _parse:function(mind, node_root){
478
+ _parse: function (mind, node_root) {
496
479
  var df = jm.format.node_tree;
497
480
  var data = df._extract_data(node_root);
498
481
  mind.set_root(node_root.id, node_root.topic, data);
499
- if('children' in node_root){
482
+ if ('children' in node_root) {
500
483
  var children = node_root.children;
501
- for(var i=0;i<children.length;i++){
484
+ for (var i = 0; i < children.length; i++) {
502
485
  df._extract_subnode(mind, mind.root, children[i]);
503
486
  }
504
487
  }
505
488
  },
506
489
 
507
- _extract_data:function(node_json){
490
+ _extract_data: function (node_json) {
508
491
  var data = {};
509
- for(var k in node_json){
510
- if(k == 'id' || k=='topic' || k=='children' || k=='direction' || k=='expanded'){
492
+ for (var k in node_json) {
493
+ if (k == 'id' || k == 'topic' || k == 'children' || k == 'direction' || k == 'expanded') {
511
494
  continue;
512
495
  }
513
496
  data[k] = node_json[k];
@@ -515,43 +498,43 @@
515
498
  return data;
516
499
  },
517
500
 
518
- _extract_subnode:function(mind, node_parent, node_json){
501
+ _extract_subnode: function (mind, node_parent, node_json) {
519
502
  var df = jm.format.node_tree;
520
503
  var data = df._extract_data(node_json);
521
504
  var d = null;
522
- if(node_parent.isroot){
523
- d = node_json.direction == 'left'?jm.direction.left:jm.direction.right;
505
+ if (node_parent.isroot) {
506
+ d = node_json.direction == 'left' ? jm.direction.left : jm.direction.right;
524
507
  }
525
- var node = mind.add_node(node_parent, node_json.id, node_json.topic, data, null, d, node_json.expanded);
526
- if('children' in node_json){
508
+ var node = mind.add_node(node_parent, node_json.id, node_json.topic, data, d, node_json.expanded);
509
+ if (!!node_json['children']) {
527
510
  var children = node_json.children;
528
- for(var i=0;i<children.length;i++){
511
+ for (var i = 0; i < children.length; i++) {
529
512
  df._extract_subnode(mind, node, children[i]);
530
513
  }
531
514
  }
532
515
  },
533
516
 
534
- _buildnode:function(node){
517
+ _buildnode: function (node) {
535
518
  var df = jm.format.node_tree;
536
- if(!(node instanceof jm.node)){return;}
519
+ if (!(node instanceof jm.node)) { return; }
537
520
  var o = {
538
- id : node.id,
539
- topic : node.topic,
540
- expanded : node.expanded
521
+ id: node.id,
522
+ topic: node.topic,
523
+ expanded: node.expanded
541
524
  };
542
- if(!!node.parent && node.parent.isroot){
543
- o.direction = node.direction == jm.direction.left?'left':'right';
525
+ if (!!node.parent && node.parent.isroot) {
526
+ o.direction = node.direction == jm.direction.left ? 'left' : 'right';
544
527
  }
545
- if(node.data != null){
528
+ if (node.data != null) {
546
529
  var node_data = node.data;
547
- for(var k in node_data){
530
+ for (var k in node_data) {
548
531
  o[k] = node_data[k];
549
532
  }
550
533
  }
551
534
  var children = node.children;
552
- if(children.length > 0){
535
+ if (children.length > 0) {
553
536
  o.children = [];
554
- for(var i=0;i<children.length;i++){
537
+ for (var i = 0; i < children.length; i++) {
555
538
  o.children.push(df._buildnode(children[i]));
556
539
  }
557
540
  }
@@ -559,91 +542,91 @@
559
542
  }
560
543
  },
561
544
 
562
- node_array:{
563
- example:{
564
- "meta":{
565
- "name":__name__,
566
- "author":__author__,
567
- "version":__version__
545
+ node_array: {
546
+ example: {
547
+ "meta": {
548
+ "name": __name__,
549
+ "author": __author__,
550
+ "version": __version__
568
551
  },
569
- "format":"node_array",
570
- "data":[
571
- {"id":"root","topic":"jsMind Example", "isroot":true}
552
+ "format": "node_array",
553
+ "data": [
554
+ { "id": "root", "topic": "jsMind Example", "isroot": true }
572
555
  ]
573
556
  },
574
557
 
575
- get_mind:function(source){
558
+ get_mind: function (source) {
576
559
  var df = jm.format.node_array;
577
560
  var mind = new jm.mind();
578
561
  mind.name = source.meta.name;
579
562
  mind.author = source.meta.author;
580
563
  mind.version = source.meta.version;
581
- df._parse(mind,source.data);
564
+ df._parse(mind, source.data);
582
565
  return mind;
583
566
  },
584
567
 
585
- get_data:function(mind){
568
+ get_data: function (mind) {
586
569
  var df = jm.format.node_array;
587
570
  var json = {};
588
571
  json.meta = {
589
- name : mind.name,
590
- author : mind.author,
591
- version : mind.version
572
+ name: mind.name,
573
+ author: mind.author,
574
+ version: mind.version
592
575
  };
593
576
  json.format = 'node_array';
594
577
  json.data = [];
595
- df._array(mind,json.data);
578
+ df._array(mind, json.data);
596
579
  return json;
597
580
  },
598
581
 
599
- _parse:function(mind, node_array){
582
+ _parse: function (mind, node_array) {
600
583
  var df = jm.format.node_array;
601
584
  var narray = node_array.slice(0);
602
585
  // reverse array for improving looping performance
603
586
  narray.reverse();
604
- var root_id = df._extract_root(mind, narray);
605
- if(!!root_id){
606
- df._extract_subnode(mind, root_id, narray);
607
- }else{
587
+ var root_node = df._extract_root(mind, narray);
588
+ if (!!root_node) {
589
+ df._extract_subnode(mind, root_node, narray);
590
+ } else {
608
591
  logger.error('root node can not be found');
609
592
  }
610
593
  },
611
594
 
612
- _extract_root:function(mind, node_array){
595
+ _extract_root: function (mind, node_array) {
613
596
  var df = jm.format.node_array;
614
597
  var i = node_array.length;
615
- while(i--){
616
- if('isroot' in node_array[i] && node_array[i].isroot){
598
+ while (i--) {
599
+ if ('isroot' in node_array[i] && node_array[i].isroot) {
617
600
  var root_json = node_array[i];
618
601
  var data = df._extract_data(root_json);
619
- mind.set_root(root_json.id,root_json.topic,data);
620
- node_array.splice(i,1);
621
- return root_json.id;
602
+ var node = mind.set_root(root_json.id, root_json.topic, data);
603
+ node_array.splice(i, 1);
604
+ return node;
622
605
  }
623
606
  }
624
607
  return null;
625
608
  },
626
609
 
627
- _extract_subnode:function(mind, parentid, node_array){
610
+ _extract_subnode: function (mind, parent_node, node_array) {
628
611
  var df = jm.format.node_array;
629
612
  var i = node_array.length;
630
613
  var node_json = null;
631
614
  var data = null;
632
615
  var extract_count = 0;
633
- while(i--){
616
+ while (i--) {
634
617
  node_json = node_array[i];
635
- if(node_json.parentid == parentid){
618
+ if (node_json.parentid == parent_node.id) {
636
619
  data = df._extract_data(node_json);
637
620
  var d = null;
638
621
  var node_direction = node_json.direction;
639
- if(!!node_direction){
640
- d = node_direction == 'left'?jm.direction.left:jm.direction.right;
622
+ if (!!node_direction) {
623
+ d = node_direction == 'left' ? jm.direction.left : jm.direction.right;
641
624
  }
642
- mind.add_node(parentid, node_json.id, node_json.topic, data, null, d, node_json.expanded);
643
- node_array.splice(i,1);
644
- extract_count ++;
645
- var sub_extract_count = df._extract_subnode(mind, node_json.id, node_array);
646
- if(sub_extract_count > 0){
625
+ var node = mind.add_node(parent_node, node_json.id, node_json.topic, data, d, node_json.expanded);
626
+ node_array.splice(i, 1);
627
+ extract_count++;
628
+ var sub_extract_count = df._extract_subnode(mind, node, node_array);
629
+ if (sub_extract_count > 0) {
647
630
  // reset loop index after extract subordinate node
648
631
  i = node_array.length;
649
632
  extract_count += sub_extract_count;
@@ -653,10 +636,10 @@
653
636
  return extract_count;
654
637
  },
655
638
 
656
- _extract_data:function(node_json){
639
+ _extract_data: function (node_json) {
657
640
  var data = {};
658
- for(var k in node_json){
659
- if(k == 'id' || k=='topic' || k=='parentid' || k=='isroot' || k=='direction' || k=='expanded'){
641
+ for (var k in node_json) {
642
+ if (k == 'id' || k == 'topic' || k == 'parentid' || k == 'isroot' || k == 'direction' || k == 'expanded') {
660
643
  continue;
661
644
  }
662
645
  data[k] = node_json[k];
@@ -664,53 +647,53 @@
664
647
  return data;
665
648
  },
666
649
 
667
- _array:function(mind, node_array){
650
+ _array: function (mind, node_array) {
668
651
  var df = jm.format.node_array;
669
652
  df._array_node(mind.root, node_array);
670
653
  },
671
654
 
672
- _array_node:function(node, node_array){
655
+ _array_node: function (node, node_array) {
673
656
  var df = jm.format.node_array;
674
- if(!(node instanceof jm.node)){return;}
657
+ if (!(node instanceof jm.node)) { return; }
675
658
  var o = {
676
- id : node.id,
677
- topic : node.topic,
678
- expanded : node.expanded
659
+ id: node.id,
660
+ topic: node.topic,
661
+ expanded: node.expanded
679
662
  };
680
- if(!!node.parent){
663
+ if (!!node.parent) {
681
664
  o.parentid = node.parent.id;
682
665
  }
683
- if(node.isroot){
666
+ if (node.isroot) {
684
667
  o.isroot = true;
685
668
  }
686
- if(!!node.parent && node.parent.isroot){
687
- o.direction = node.direction == jm.direction.left?'left':'right';
669
+ if (!!node.parent && node.parent.isroot) {
670
+ o.direction = node.direction == jm.direction.left ? 'left' : 'right';
688
671
  }
689
- if(node.data != null){
672
+ if (node.data != null) {
690
673
  var node_data = node.data;
691
- for(var k in node_data){
674
+ for (var k in node_data) {
692
675
  o[k] = node_data[k];
693
676
  }
694
677
  }
695
678
  node_array.push(o);
696
679
  var ci = node.children.length;
697
- for(var i=0;i<ci;i++){
680
+ for (var i = 0; i < ci; i++) {
698
681
  df._array_node(node.children[i], node_array);
699
682
  }
700
683
  },
701
684
  },
702
685
 
703
- freemind:{
704
- example:{
705
- "meta":{
706
- "name":__name__,
707
- "author":__author__,
708
- "version":__version__
686
+ freemind: {
687
+ example: {
688
+ "meta": {
689
+ "name": __name__,
690
+ "author": __author__,
691
+ "version": __version__
709
692
  },
710
- "format":"freemind",
711
- "data":"<map version=\"1.0.1\"><node ID=\"root\" TEXT=\"freemind Example\"/></map>"
693
+ "format": "freemind",
694
+ "data": "<map version=\"1.0.1\"><node ID=\"root\" TEXT=\"freemind Example\"/></map>"
712
695
  },
713
- get_mind:function(source){
696
+ get_mind: function (source) {
714
697
  var df = jm.format.freemind;
715
698
  var mind = new jm.mind();
716
699
  mind.name = source.meta.name;
@@ -723,13 +706,13 @@
723
706
  return mind;
724
707
  },
725
708
 
726
- get_data:function(mind){
709
+ get_data: function (mind) {
727
710
  var df = jm.format.freemind;
728
711
  var json = {};
729
712
  json.meta = {
730
- name : mind.name,
731
- author : mind.author,
732
- version : mind.version
713
+ name: mind.name,
714
+ author: mind.author,
715
+ version: mind.version
733
716
  };
734
717
  json.format = 'freemind';
735
718
  var xmllines = [];
@@ -740,12 +723,12 @@
740
723
  return json;
741
724
  },
742
725
 
743
- _parse_xml:function(xml){
726
+ _parse_xml: function (xml) {
744
727
  var xml_doc = null;
745
- if (window.DOMParser){
728
+ if (window.DOMParser) {
746
729
  var parser = new DOMParser();
747
- xml_doc = parser.parseFromString(xml,'text/xml');
748
- }else{ // Internet Explorer
730
+ xml_doc = parser.parseFromString(xml, 'text/xml');
731
+ } else { // Internet Explorer
749
732
  xml_doc = new ActiveXObject('Microsoft.XMLDOM');
750
733
  xml_doc.async = false;
751
734
  xml_doc.loadXML(xml);
@@ -753,24 +736,24 @@
753
736
  return xml_doc;
754
737
  },
755
738
 
756
- _find_root:function(xml_doc){
739
+ _find_root: function (xml_doc) {
757
740
  var nodes = xml_doc.childNodes;
758
741
  var node = null;
759
742
  var root = null;
760
743
  var n = null;
761
- for(var i=0;i<nodes.length;i++){
744
+ for (var i = 0; i < nodes.length; i++) {
762
745
  n = nodes[i];
763
- if(n.nodeType == 1 && n.tagName == 'map'){
746
+ if (n.nodeType == 1 && n.tagName == 'map') {
764
747
  node = n;
765
748
  break;
766
749
  }
767
750
  }
768
- if(!!node){
751
+ if (!!node) {
769
752
  var ns = node.childNodes;
770
753
  node = null;
771
- for(var i=0;i<ns.length;i++){
754
+ for (var i = 0; i < ns.length; i++) {
772
755
  n = ns[i];
773
- if(n.nodeType == 1 && n.tagName == 'node'){
756
+ if (n.nodeType == 1 && n.tagName == 'node') {
774
757
  node = n;
775
758
  break;
776
759
  }
@@ -779,88 +762,89 @@
779
762
  return node;
780
763
  },
781
764
 
782
- _load_node:function(mind, parent_id, xml_node){
765
+ _load_node: function (mind, parent_node, xml_node) {
783
766
  var df = jm.format.freemind;
784
767
  var node_id = xml_node.getAttribute('ID');
785
768
  var node_topic = xml_node.getAttribute('TEXT');
786
769
  // look for richcontent
787
- if(node_topic == null){
770
+ if (node_topic == null) {
788
771
  var topic_children = xml_node.childNodes;
789
772
  var topic_child = null;
790
- for(var i=0;i<topic_children.length;i++){
773
+ for (var i = 0; i < topic_children.length; i++) {
791
774
  topic_child = topic_children[i];
792
775
  //logger.debug(topic_child.tagName);
793
- if(topic_child.nodeType == 1 && topic_child.tagName === 'richcontent'){
776
+ if (topic_child.nodeType == 1 && topic_child.tagName === 'richcontent') {
794
777
  node_topic = topic_child.textContent;
795
778
  break;
796
779
  }
797
780
  }
798
781
  }
799
782
  var node_data = df._load_attributes(xml_node);
800
- var node_expanded = ('expanded' in node_data)?(node_data.expanded == 'true') : true;
783
+ var node_expanded = ('expanded' in node_data) ? (node_data.expanded == 'true') : true;
801
784
  delete node_data.expanded;
802
785
 
803
786
  var node_position = xml_node.getAttribute('POSITION');
804
787
  var node_direction = null;
805
- if(!!node_position){
806
- node_direction = node_position=='left'?jm.direction.left:jm.direction.right;
788
+ if (!!node_position) {
789
+ node_direction = node_position == 'left' ? jm.direction.left : jm.direction.right;
807
790
  }
808
791
  //logger.debug(node_position +':'+ node_direction);
809
- if(!!parent_id){
810
- mind.add_node(parent_id, node_id, node_topic, node_data, null, node_direction, node_expanded);
811
- }else{
812
- mind.set_root(node_id, node_topic, node_data);
792
+ var node = null;
793
+ if (!!parent_node) {
794
+ node = mind.add_node(parent_node, node_id, node_topic, node_data, node_direction, node_expanded);
795
+ } else {
796
+ node = mind.set_root(node_id, node_topic, node_data);
813
797
  }
814
798
  var children = xml_node.childNodes;
815
799
  var child = null;
816
- for(var i=0;i<children.length;i++){
800
+ for (var i = 0; i < children.length; i++) {
817
801
  child = children[i];
818
- if(child.nodeType == 1 && child.tagName == 'node'){
819
- df._load_node(mind, node_id, child);
802
+ if (child.nodeType == 1 && child.tagName == 'node') {
803
+ df._load_node(mind, node, child);
820
804
  }
821
805
  }
822
806
  },
823
807
 
824
- _load_attributes:function(xml_node){
808
+ _load_attributes: function (xml_node) {
825
809
  var children = xml_node.childNodes;
826
810
  var attr = null;
827
811
  var attr_data = {};
828
- for(var i=0;i<children.length;i++){
812
+ for (var i = 0; i < children.length; i++) {
829
813
  attr = children[i];
830
- if(attr.nodeType == 1 && attr.tagName === 'attribute'){
814
+ if (attr.nodeType == 1 && attr.tagName === 'attribute') {
831
815
  attr_data[attr.getAttribute('NAME')] = attr.getAttribute('VALUE');
832
816
  }
833
817
  }
834
818
  return attr_data;
835
819
  },
836
820
 
837
- _buildmap:function(node, xmllines){
821
+ _buildmap: function (node, xmllines) {
838
822
  var df = jm.format.freemind;
839
823
  var pos = null;
840
- if(!!node.parent && node.parent.isroot){
841
- pos = node.direction === jm.direction.left?'left':'right';
824
+ if (!!node.parent && node.parent.isroot) {
825
+ pos = node.direction === jm.direction.left ? 'left' : 'right';
842
826
  }
843
827
  xmllines.push('<node');
844
- xmllines.push('ID=\"'+node.id+'\"');
845
- if(!!pos){
846
- xmllines.push('POSITION=\"'+pos+'\"');
828
+ xmllines.push('ID=\"' + node.id + '\"');
829
+ if (!!pos) {
830
+ xmllines.push('POSITION=\"' + pos + '\"');
847
831
  }
848
- xmllines.push('TEXT=\"'+node.topic+'\">');
832
+ xmllines.push('TEXT=\"' + node.topic + '\">');
849
833
 
850
834
  // store expanded status as an attribute
851
- xmllines.push('<attribute NAME=\"expanded\" VALUE=\"'+node.expanded+'\"/>');
835
+ xmllines.push('<attribute NAME=\"expanded\" VALUE=\"' + node.expanded + '\"/>');
852
836
 
853
837
  // for attributes
854
838
  var node_data = node.data;
855
- if(node_data != null){
856
- for(var k in node_data){
857
- xmllines.push('<attribute NAME=\"'+k+'\" VALUE=\"'+node_data[k]+'\"/>');
839
+ if (node_data != null) {
840
+ for (var k in node_data) {
841
+ xmllines.push('<attribute NAME=\"' + k + '\" VALUE=\"' + node_data[k] + '\"/>');
858
842
  }
859
843
  }
860
844
 
861
845
  // for children
862
846
  var children = node.children;
863
- for(var i=0;i<children.length;i++){
847
+ for (var i = 0; i < children.length; i++) {
864
848
  df._buildmap(children[i], xmllines);
865
849
  }
866
850
 
@@ -872,118 +856,86 @@
872
856
  // ============= utility object =============================================
873
857
 
874
858
  jm.util = {
875
- is_node: function(node){
859
+ is_node: function (node) {
876
860
  return !!node && node instanceof jm.node;
877
861
  },
878
- ajax:{
879
- _xhr:function(){
880
- var xhr = null;
881
- if(window.XMLHttpRequest){
882
- xhr = new XMLHttpRequest();
883
- }else{
884
- try{
885
- xhr = new ActiveXObject('Microsoft.XMLHTTP');
886
- }catch(e){}
887
- }
888
- return xhr;
889
- },
890
- _eurl:function(url){
891
- return encodeURIComponent(url);
892
- },
893
- request:function(url,param,method,callback,fail_callback){
862
+ ajax: {
863
+ request: function (url, param, method, callback, fail_callback) {
894
864
  var a = jm.util.ajax;
895
865
  var p = null;
896
866
  var tmp_param = [];
897
- for(var k in param){
898
- tmp_param.push(a._eurl(k)+'='+a._eurl(param[k]));
867
+ for (var k in param) {
868
+ tmp_param.push(encodeURIComponent(k) + '=' + encodeURIComponent(param[k]));
899
869
  }
900
- if(tmp_param.length>0){
870
+ if (tmp_param.length > 0) {
901
871
  p = tmp_param.join('&');
902
872
  }
903
- var xhr = a._xhr();
904
- if(!xhr){return;}
905
- xhr.onreadystatechange = function(){
906
- if(xhr.readyState == 4){
907
- if(xhr.status == 200 || xhr.status == 0){
908
- if(typeof callback === 'function'){
873
+ var xhr = new XMLHttpRequest();
874
+ if (!xhr) { return; }
875
+ xhr.onreadystatechange = function () {
876
+ if (xhr.readyState == 4) {
877
+ if (xhr.status == 200 || xhr.status == 0) {
878
+ if (typeof callback === 'function') {
909
879
  var data = jm.util.json.string2json(xhr.responseText);
910
- if(data != null){
880
+ if (data != null) {
911
881
  callback(data);
912
- }else{
882
+ } else {
913
883
  callback(xhr.responseText);
914
884
  }
915
885
  }
916
- }else{
917
- if(typeof fail_callback === 'function'){
886
+ } else {
887
+ if (typeof fail_callback === 'function') {
918
888
  fail_callback(xhr);
919
- }else{
920
- logger.error('xhr request failed.',xhr);
889
+ } else {
890
+ logger.error('xhr request failed.', xhr);
921
891
  }
922
892
  }
923
893
  }
924
894
  }
925
895
  method = method || 'GET';
926
- xhr.open(method,url,true);
927
- xhr.setRequestHeader('If-Modified-Since','0');
928
- if(method == 'POST'){
929
- xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=utf-8');
896
+ xhr.open(method, url, true);
897
+ xhr.setRequestHeader('If-Modified-Since', '0');
898
+ if (method == 'POST') {
899
+ xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
930
900
  xhr.send(p);
931
- }else{
901
+ } else {
932
902
  xhr.send();
933
903
  }
934
904
  },
935
- get:function(url,callback){
936
- return jm.util.ajax.request(url,{},'GET',callback);
905
+ get: function (url, callback) {
906
+ return jm.util.ajax.request(url, {}, 'GET', callback);
937
907
  },
938
- post:function(url,param,callback){
939
- return jm.util.ajax.request(url,param,'POST',callback);
908
+ post: function (url, param, callback) {
909
+ return jm.util.ajax.request(url, param, 'POST', callback);
940
910
  }
941
911
  },
942
912
 
943
- dom:{
913
+ dom: {
944
914
  //target,eventType,handler
945
- add_event:function(t,e,h){
946
- if(!!t.addEventListener){
947
- t.addEventListener(e,h,false);
948
- }else{
949
- t.attachEvent('on'+e,h);
915
+ add_event: function (t, e, h) {
916
+ if (!!t.addEventListener) {
917
+ t.addEventListener(e, h, false);
918
+ } else {
919
+ t.attachEvent('on' + e, h);
950
920
  }
951
921
  }
952
922
  },
953
923
 
954
- canvas:{
955
- bezierto: function(ctx,x1,y1,x2,y2){
956
- ctx.beginPath();
957
- ctx.moveTo(x1,y1);
958
- ctx.bezierCurveTo(x1+(x2-x1)*2/3,y1,x1,y2,x2,y2);
959
- ctx.stroke();
960
- },
961
- lineto : function(ctx,x1,y1,x2,y2){
962
- ctx.beginPath();
963
- ctx.moveTo(x1,y1);
964
- ctx.lineTo(x2,y2);
965
- ctx.stroke();
966
- },
967
- clear:function(ctx,x,y,w,h){
968
- ctx.clearRect(x,y,w,h);
969
- }
970
- },
971
-
972
- file:{
973
- read:function(file_data,fn_callback){
924
+ file: {
925
+ read: function (file_data, fn_callback) {
974
926
  var reader = new FileReader();
975
- reader.onload = function(){
976
- if(typeof fn_callback === 'function'){
927
+ reader.onload = function () {
928
+ if (typeof fn_callback === 'function') {
977
929
  fn_callback(this.result, file_data.name);
978
930
  }
979
931
  };
980
932
  reader.readAsText(file_data);
981
933
  },
982
934
 
983
- save:function(file_data, type, name) {
935
+ save: function (file_data, type, name) {
984
936
  var blob;
985
937
  if (typeof $w.Blob === 'function') {
986
- blob = new Blob([file_data], {type: type});
938
+ blob = new Blob([file_data], { type: type });
987
939
  } else {
988
940
  var BlobBuilder = $w.BlobBuilder || $w.MozBlobBuilder || $w.WebKitBlobBuilder || $w.MSBlobBuilder;
989
941
  var bb = new BlobBuilder();
@@ -1012,42 +964,24 @@
1012
964
  }
1013
965
  },
1014
966
 
1015
- json:{
1016
- json2string:function(json){
1017
- if(!!JSON){
1018
- try{
1019
- var json_str = JSON.stringify(json);
1020
- return json_str;
1021
- }catch(e){
1022
- logger.warn(e);
1023
- logger.warn('can not convert to string');
1024
- return null;
1025
- }
1026
- }
967
+ json: {
968
+ json2string: function (json) {
969
+ return JSON.stringify(json);
1027
970
  },
1028
- string2json:function(json_str){
1029
- if(!!JSON){
1030
- try{
1031
- var json = JSON.parse(json_str);
1032
- return json;
1033
- }catch(e){
1034
- logger.warn(e);
1035
- logger.warn('can not parse to json');
1036
- return null;
1037
- }
1038
- }
971
+ string2json: function (json_str) {
972
+ return JSON.parse(json_str);
1039
973
  },
1040
- merge:function(b,a){
1041
- for(var o in a){
1042
- if(o in b){
1043
- if(typeof b[o] === 'object' &&
974
+ merge: function (b, a) {
975
+ for (var o in a) {
976
+ if (o in b) {
977
+ if (typeof b[o] === 'object' &&
1044
978
  Object.prototype.toString.call(b[o]).toLowerCase() == '[object object]' &&
1045
- !b[o].length){
979
+ !b[o].length) {
1046
980
  jm.util.json.merge(b[o], a[o]);
1047
- }else{
981
+ } else {
1048
982
  b[o] = a[o];
1049
983
  }
1050
- }else{
984
+ } else {
1051
985
  b[o] = a[o];
1052
986
  }
1053
987
  }
@@ -1055,40 +989,43 @@
1055
989
  }
1056
990
  },
1057
991
 
1058
- uuid:{
1059
- newid:function(){
1060
- return (new Date().getTime().toString(16)+Math.random().toString(16).substr(2)).substr(2,16);
992
+ uuid: {
993
+ newid: function () {
994
+ return (new Date().getTime().toString(16) + Math.random().toString(16).substring(2)).substring(2, 18);
1061
995
  }
1062
996
  },
1063
997
 
1064
- text:{
1065
- is_empty:function(s){
1066
- if(!s){return true;}
1067
- return s.replace(/\s*/,'').length == 0;
998
+ text: {
999
+ is_empty: function (s) {
1000
+ if (!s) { return true; }
1001
+ return s.replace(/\s*/, '').length == 0;
1068
1002
  }
1069
1003
  }
1070
1004
  };
1071
1005
 
1072
- jm.prototype={
1073
- init : function(){
1074
- if(this.inited){return;}
1075
- this.inited = true;
1006
+ jm.prototype = {
1007
+ init: function () {
1008
+ if (this.initialized) { return; }
1009
+ this.initialized = true;
1076
1010
 
1077
1011
  var opts = this.options;
1078
1012
 
1079
1013
  var opts_layout = {
1080
- mode:opts.mode,
1081
- hspace:opts.layout.hspace,
1082
- vspace:opts.layout.vspace,
1083
- pspace:opts.layout.pspace
1014
+ mode: opts.mode,
1015
+ hspace: opts.layout.hspace,
1016
+ vspace: opts.layout.vspace,
1017
+ pspace: opts.layout.pspace
1084
1018
  }
1085
1019
  var opts_view = {
1086
- container:opts.container,
1087
- support_html:opts.support_html,
1088
- hmargin:opts.view.hmargin,
1089
- vmargin:opts.view.vmargin,
1090
- line_width:opts.view.line_width,
1091
- line_color:opts.view.line_color
1020
+ container: opts.container,
1021
+ support_html: opts.support_html,
1022
+ engine: opts.view.engine,
1023
+ hmargin: opts.view.hmargin,
1024
+ vmargin: opts.view.vmargin,
1025
+ line_width: opts.view.line_width,
1026
+ line_color: opts.view.line_color,
1027
+ draggable: opts.view.draggable,
1028
+ hide_scrollbars_when_draggable: opts.view.hide_scrollbars_when_draggable
1092
1029
  };
1093
1030
  // create instance of function provider
1094
1031
  this.data = new jm.data_provider(this);
@@ -1106,186 +1043,206 @@
1106
1043
  jm.init_plugins(this);
1107
1044
  },
1108
1045
 
1109
- enable_edit:function(){
1046
+ enable_edit: function () {
1110
1047
  this.options.editable = true;
1111
1048
  },
1112
1049
 
1113
- disable_edit:function(){
1050
+ disable_edit: function () {
1114
1051
  this.options.editable = false;
1115
1052
  },
1116
1053
 
1117
1054
  // call enable_event_handle('dblclick')
1118
1055
  // options are 'mousedown', 'click', 'dblclick'
1119
- enable_event_handle: function(event_handle){
1120
- this.options.default_event_handle['enable_'+event_handle+'_handle'] = true;
1056
+ enable_event_handle: function (event_handle) {
1057
+ this.options.default_event_handle['enable_' + event_handle + '_handle'] = true;
1121
1058
  },
1122
1059
 
1123
1060
  // call disable_event_handle('dblclick')
1124
1061
  // options are 'mousedown', 'click', 'dblclick'
1125
- disable_event_handle: function(event_handle){
1126
- this.options.default_event_handle['enable_'+event_handle+'_handle'] = false;
1062
+ disable_event_handle: function (event_handle) {
1063
+ this.options.default_event_handle['enable_' + event_handle + '_handle'] = false;
1127
1064
  },
1128
1065
 
1129
- get_editable:function(){
1066
+ get_editable: function () {
1130
1067
  return this.options.editable;
1131
1068
  },
1132
1069
 
1133
- set_theme:function(theme){
1070
+ set_theme: function (theme) {
1134
1071
  var theme_old = this.options.theme;
1135
1072
  this.options.theme = (!!theme) ? theme : null;
1136
- if(theme_old != this.options.theme){
1073
+ if (theme_old != this.options.theme) {
1137
1074
  this.view.reset_theme();
1138
1075
  this.view.reset_custom_style();
1139
1076
  }
1140
1077
  },
1141
- _event_bind:function(){
1142
- this.view.add_event(this,'mousedown',this.mousedown_handle);
1143
- this.view.add_event(this,'click',this.click_handle);
1144
- this.view.add_event(this,'dblclick',this.dblclick_handle);
1078
+ _event_bind: function () {
1079
+ this.view.add_event(this, 'mousedown', this.mousedown_handle);
1080
+ this.view.add_event(this, 'click', this.click_handle);
1081
+ this.view.add_event(this, 'dblclick', this.dblclick_handle);
1082
+ this.view.add_event(this, "mousewheel", this.mousewheel_handle)
1145
1083
  },
1146
1084
 
1147
- mousedown_handle:function(e){
1085
+ mousedown_handle: function (e) {
1148
1086
  if (!this.options.default_event_handle['enable_mousedown_handle']) {
1149
1087
  return;
1150
1088
  }
1151
1089
  var element = e.target || event.srcElement;
1152
1090
  var nodeid = this.view.get_binded_nodeid(element);
1153
- if(!!nodeid){
1154
- this.select_node(nodeid);
1155
- }else{
1091
+ if (!!nodeid) {
1092
+ if (element.tagName.toLowerCase() == 'jmnode') {
1093
+ this.select_node(nodeid);
1094
+ }
1095
+ } else {
1156
1096
  this.select_clear();
1157
1097
  }
1158
1098
  },
1159
1099
 
1160
- click_handle:function(e){
1100
+ click_handle: function (e) {
1161
1101
  if (!this.options.default_event_handle['enable_click_handle']) {
1162
1102
  return;
1163
1103
  }
1164
1104
  var element = e.target || event.srcElement;
1165
1105
  var isexpander = this.view.is_expander(element);
1166
- if(isexpander){
1106
+ if (isexpander) {
1167
1107
  var nodeid = this.view.get_binded_nodeid(element);
1168
- if(!!nodeid){
1108
+ if (!!nodeid) {
1169
1109
  this.toggle_node(nodeid);
1170
1110
  }
1171
1111
  }
1172
1112
  },
1173
1113
 
1174
- dblclick_handle:function(e){
1114
+ dblclick_handle: function (e) {
1175
1115
  if (!this.options.default_event_handle['enable_dblclick_handle']) {
1176
1116
  return;
1177
1117
  }
1178
- if(this.get_editable()){
1118
+ if (this.get_editable()) {
1179
1119
  var element = e.target || event.srcElement;
1180
1120
  var nodeid = this.view.get_binded_nodeid(element);
1181
- if(!!nodeid){
1121
+ if (!!nodeid) {
1182
1122
  this.begin_edit(nodeid);
1183
1123
  }
1184
1124
  }
1185
1125
  },
1186
1126
 
1187
- begin_edit:function(node){
1188
- if(!jm.util.is_node(node)){
1127
+ // Use [Ctrl] + Mousewheel, to zoom in/out.
1128
+ mousewheel_handle: function (event) {
1129
+ // Test if mousewheel option is enabled and Ctrl key is pressed.
1130
+ if (!this.options.default_event_handle["enable_mousewheel_handle"] || !window.event.ctrlKey) {
1131
+ return
1132
+ }
1133
+ // Avoid default page scrolling behavior.
1134
+ event.preventDefault()
1135
+
1136
+ var dir = event.deltaY > 0 ? "Up" : "Down"
1137
+ if (dir == "Up") {
1138
+ this.view.zoomIn()
1139
+ } else {
1140
+ this.view.zoomOut()
1141
+ }
1142
+ },
1143
+
1144
+ begin_edit: function (node) {
1145
+ if (!jm.util.is_node(node)) {
1189
1146
  var the_node = this.get_node(node);
1190
- if(!the_node){
1191
- logger.error('the node[id='+node+'] can not be found.');
1147
+ if (!the_node) {
1148
+ logger.error('the node[id=' + node + '] can not be found.');
1192
1149
  return false;
1193
- }else{
1150
+ } else {
1194
1151
  return this.begin_edit(the_node);
1195
1152
  }
1196
1153
  }
1197
- if(this.get_editable()){
1154
+ if (this.get_editable()) {
1198
1155
  this.view.edit_node_begin(node);
1199
- }else{
1156
+ } else {
1200
1157
  logger.error('fail, this mind map is not editable.');
1201
1158
  return;
1202
1159
  }
1203
1160
  },
1204
1161
 
1205
- end_edit:function(){
1162
+ end_edit: function () {
1206
1163
  this.view.edit_node_end();
1207
1164
  },
1208
1165
 
1209
- toggle_node:function(node){
1210
- if(!jm.util.is_node(node)){
1166
+ toggle_node: function (node) {
1167
+ if (!jm.util.is_node(node)) {
1211
1168
  var the_node = this.get_node(node);
1212
- if(!the_node){
1213
- logger.error('the node[id='+node+'] can not be found.');
1169
+ if (!the_node) {
1170
+ logger.error('the node[id=' + node + '] can not be found.');
1214
1171
  return;
1215
- }else{
1172
+ } else {
1216
1173
  return this.toggle_node(the_node);
1217
1174
  }
1218
1175
  }
1219
- if(node.isroot){return;}
1176
+ if (node.isroot) { return; }
1220
1177
  this.view.save_location(node);
1221
1178
  this.layout.toggle_node(node);
1222
1179
  this.view.relayout();
1223
1180
  this.view.restore_location(node);
1224
1181
  },
1225
1182
 
1226
- expand_node:function(node){
1227
- if(!jm.util.is_node(node)){
1183
+ expand_node: function (node) {
1184
+ if (!jm.util.is_node(node)) {
1228
1185
  var the_node = this.get_node(node);
1229
- if(!the_node){
1230
- logger.error('the node[id='+node+'] can not be found.');
1186
+ if (!the_node) {
1187
+ logger.error('the node[id=' + node + '] can not be found.');
1231
1188
  return;
1232
- }else{
1189
+ } else {
1233
1190
  return this.expand_node(the_node);
1234
1191
  }
1235
1192
  }
1236
- if(node.isroot){return;}
1193
+ if (node.isroot) { return; }
1237
1194
  this.view.save_location(node);
1238
1195
  this.layout.expand_node(node);
1239
1196
  this.view.relayout();
1240
1197
  this.view.restore_location(node);
1241
1198
  },
1242
1199
 
1243
- collapse_node:function(node){
1244
- if(!jm.util.is_node(node)){
1200
+ collapse_node: function (node) {
1201
+ if (!jm.util.is_node(node)) {
1245
1202
  var the_node = this.get_node(node);
1246
- if(!the_node){
1247
- logger.error('the node[id='+node+'] can not be found.');
1203
+ if (!the_node) {
1204
+ logger.error('the node[id=' + node + '] can not be found.');
1248
1205
  return;
1249
- }else{
1206
+ } else {
1250
1207
  return this.collapse_node(the_node);
1251
1208
  }
1252
1209
  }
1253
- if(node.isroot){return;}
1210
+ if (node.isroot) { return; }
1254
1211
  this.view.save_location(node);
1255
1212
  this.layout.collapse_node(node);
1256
1213
  this.view.relayout();
1257
1214
  this.view.restore_location(node);
1258
1215
  },
1259
1216
 
1260
- expand_all:function(){
1217
+ expand_all: function () {
1261
1218
  this.layout.expand_all();
1262
1219
  this.view.relayout();
1263
1220
  },
1264
1221
 
1265
- collapse_all:function(){
1222
+ collapse_all: function () {
1266
1223
  this.layout.collapse_all();
1267
1224
  this.view.relayout();
1268
1225
  },
1269
1226
 
1270
- expand_to_depth:function(depth){
1227
+ expand_to_depth: function (depth) {
1271
1228
  this.layout.expand_to_depth(depth);
1272
1229
  this.view.relayout();
1273
1230
  },
1274
1231
 
1275
- _reset:function(){
1232
+ _reset: function () {
1276
1233
  this.view.reset();
1277
1234
  this.layout.reset();
1278
1235
  this.data.reset();
1279
1236
  },
1280
1237
 
1281
- _show:function(mind){
1238
+ _show: function (mind) {
1282
1239
  var m = mind || jm.format.node_array.example;
1283
1240
 
1284
1241
  this.mind = this.data.load(m);
1285
- if(!this.mind){
1242
+ if (!this.mind) {
1286
1243
  logger.error('data.load error');
1287
1244
  return;
1288
- }else{
1245
+ } else {
1289
1246
  logger.debug('data.load ok');
1290
1247
  }
1291
1248
 
@@ -1298,99 +1255,106 @@
1298
1255
  this.view.show(true);
1299
1256
  logger.debug('view.show ok');
1300
1257
 
1301
- this.invoke_event_handle(jm.event_type.show,{data:[mind]});
1258
+ this.invoke_event_handle(jm.event_type.show, { data: [mind] });
1302
1259
  },
1303
1260
 
1304
- show : function(mind){
1261
+ show: function (mind) {
1305
1262
  this._reset();
1306
1263
  this._show(mind);
1307
1264
  },
1308
1265
 
1309
- get_meta: function(){
1266
+ get_meta: function () {
1310
1267
  return {
1311
- name : this.mind.name,
1312
- author : this.mind.author,
1313
- version : this.mind.version
1268
+ name: this.mind.name,
1269
+ author: this.mind.author,
1270
+ version: this.mind.version
1314
1271
  };
1315
1272
  },
1316
1273
 
1317
- get_data: function(data_format){
1274
+ get_data: function (data_format) {
1318
1275
  var df = data_format || 'node_tree';
1319
1276
  return this.data.get_data(df);
1320
1277
  },
1321
1278
 
1322
- get_root:function(){
1279
+ get_root: function () {
1323
1280
  return this.mind.root;
1324
1281
  },
1325
1282
 
1326
- get_node:function(nodeid){
1327
- return this.mind.get_node(nodeid);
1283
+ get_node: function (node) {
1284
+ if (jm.util.is_node(node)) {
1285
+ return node;
1286
+ }
1287
+ return this.mind.get_node(node);
1328
1288
  },
1329
1289
 
1330
- add_node:function(parent_node, nodeid, topic, data){
1331
- if(this.get_editable()){
1332
- var node = this.mind.add_node(parent_node, nodeid, topic, data);
1333
- if(!!node){
1290
+ add_node: function (parent_node, nodeid, topic, data) {
1291
+ if (this.get_editable()) {
1292
+ var the_parent_node = this.get_node(parent_node);
1293
+ var direction = this.layout.calculate_next_child_direction(the_parent_node);
1294
+ var node = this.mind.add_node(the_parent_node, nodeid, topic, data, direction);
1295
+ if (!!node) {
1334
1296
  this.view.add_node(node);
1335
1297
  this.layout.layout();
1336
1298
  this.view.show(false);
1337
1299
  this.view.reset_node_custom_style(node);
1338
- this.expand_node(parent_node);
1339
- this.invoke_event_handle(jm.event_type.edit,{evt:'add_node',data:[parent_node.id,nodeid,topic,data],node:nodeid});
1300
+ this.expand_node(the_parent_node);
1301
+ this.invoke_event_handle(jm.event_type.edit, { evt: 'add_node', data: [the_parent_node.id, nodeid, topic, data], node: nodeid });
1340
1302
  }
1341
1303
  return node;
1342
- }else{
1304
+ } else {
1343
1305
  logger.error('fail, this mind map is not editable');
1344
1306
  return null;
1345
1307
  }
1346
1308
  },
1347
1309
 
1348
- insert_node_before:function(node_before, nodeid, topic, data){
1349
- if(this.get_editable()){
1350
- var beforeid = jm.util.is_node(node_before) ? node_before.id : node_before;
1351
- var node = this.mind.insert_node_before(node_before, nodeid, topic, data);
1352
- if(!!node){
1310
+ insert_node_before: function (node_before, nodeid, topic, data) {
1311
+ if (this.get_editable()) {
1312
+ var the_node_before = this.get_node(node_before);
1313
+ var direction = this.layout.calculate_next_child_direction(the_node_before.parent);
1314
+ var node = this.mind.insert_node_before(the_node_before, nodeid, topic, data, direction);
1315
+ if (!!node) {
1353
1316
  this.view.add_node(node);
1354
1317
  this.layout.layout();
1355
1318
  this.view.show(false);
1356
- this.invoke_event_handle(jm.event_type.edit,{evt:'insert_node_before',data:[beforeid,nodeid,topic,data],node:nodeid});
1319
+ this.invoke_event_handle(jm.event_type.edit, { evt: 'insert_node_before', data: [the_node_before.id, nodeid, topic, data], node: nodeid });
1357
1320
  }
1358
1321
  return node;
1359
- }else{
1322
+ } else {
1360
1323
  logger.error('fail, this mind map is not editable');
1361
1324
  return null;
1362
1325
  }
1363
1326
  },
1364
1327
 
1365
- insert_node_after:function(node_after, nodeid, topic, data){
1366
- if(this.get_editable()){
1367
- var afterid = jm.util.is_node(node_after) ? node_after.id : node_after;
1368
- var node = this.mind.insert_node_after(node_after, nodeid, topic, data);
1369
- if(!!node){
1328
+ insert_node_after: function (node_after, nodeid, topic, data) {
1329
+ if (this.get_editable()) {
1330
+ var the_node_after = this.get_node(node_after);
1331
+ var direction = this.layout.calculate_next_child_direction(the_node_after.parent);
1332
+ var node = this.mind.insert_node_after(the_node_after, nodeid, topic, data, direction);
1333
+ if (!!node) {
1370
1334
  this.view.add_node(node);
1371
1335
  this.layout.layout();
1372
1336
  this.view.show(false);
1373
- this.invoke_event_handle(jm.event_type.edit,{evt:'insert_node_after',data:[afterid,nodeid,topic,data],node:nodeid});
1337
+ this.invoke_event_handle(jm.event_type.edit, { evt: 'insert_node_after', data: [the_node_after.id, nodeid, topic, data], node: nodeid });
1374
1338
  }
1375
1339
  return node;
1376
- }else{
1340
+ } else {
1377
1341
  logger.error('fail, this mind map is not editable');
1378
1342
  return null;
1379
1343
  }
1380
1344
  },
1381
1345
 
1382
- remove_node:function(node){
1383
- if(!jm.util.is_node(node)){
1346
+ remove_node: function (node) {
1347
+ if (!jm.util.is_node(node)) {
1384
1348
  var the_node = this.get_node(node);
1385
- if(!the_node){
1386
- logger.error('the node[id='+node+'] can not be found.');
1349
+ if (!the_node) {
1350
+ logger.error('the node[id=' + node + '] can not be found.');
1387
1351
  return false;
1388
- }else{
1352
+ } else {
1389
1353
  return this.remove_node(the_node);
1390
1354
  }
1391
1355
  }
1392
- if(this.get_editable()){
1393
- if(node.isroot){
1356
+ if (this.get_editable()) {
1357
+ if (node.isroot) {
1394
1358
  logger.error('fail, can not remove root node');
1395
1359
  return false;
1396
1360
  }
@@ -1403,23 +1367,23 @@
1403
1367
  this.layout.layout();
1404
1368
  this.view.show(false);
1405
1369
  this.view.restore_location(parent_node);
1406
- this.invoke_event_handle(jm.event_type.edit,{evt:'remove_node',data:[nodeid],node:parentid});
1370
+ this.invoke_event_handle(jm.event_type.edit, { evt: 'remove_node', data: [nodeid], node: parentid });
1407
1371
  return true;
1408
- }else{
1372
+ } else {
1409
1373
  logger.error('fail, this mind map is not editable');
1410
1374
  return false;
1411
1375
  }
1412
1376
  },
1413
1377
 
1414
- update_node:function(nodeid, topic){
1415
- if(this.get_editable()){
1416
- if(jm.util.text.is_empty(topic)){
1378
+ update_node: function (nodeid, topic) {
1379
+ if (this.get_editable()) {
1380
+ if (jm.util.text.is_empty(topic)) {
1417
1381
  logger.warn('fail, topic can not be empty');
1418
1382
  return;
1419
1383
  }
1420
1384
  var node = this.get_node(nodeid);
1421
- if(!!node){
1422
- if(node.topic === topic){
1385
+ if (!!node) {
1386
+ if (node.topic === topic) {
1423
1387
  logger.info('nothing changed');
1424
1388
  this.view.update_node(node);
1425
1389
  return;
@@ -1428,159 +1392,161 @@
1428
1392
  this.view.update_node(node);
1429
1393
  this.layout.layout();
1430
1394
  this.view.show(false);
1431
- this.invoke_event_handle(jm.event_type.edit,{evt:'update_node',data:[nodeid,topic],node:nodeid});
1395
+ this.invoke_event_handle(jm.event_type.edit, { evt: 'update_node', data: [nodeid, topic], node: nodeid });
1432
1396
  }
1433
- }else{
1397
+ } else {
1434
1398
  logger.error('fail, this mind map is not editable');
1435
1399
  return;
1436
1400
  }
1437
1401
  },
1438
1402
 
1439
- move_node:function(nodeid, beforeid, parentid, direction){
1440
- if(this.get_editable()){
1441
- var node = this.mind.move_node(nodeid,beforeid,parentid,direction);
1442
- if(!!node){
1443
- this.view.update_node(node);
1403
+ move_node: function (nodeid, beforeid, parentid, direction) {
1404
+ if (this.get_editable()) {
1405
+ var node = this.get_node(nodeid);
1406
+ var updated_node = this.mind.move_node(node, beforeid, parentid, direction);
1407
+ if (!!updated_node) {
1408
+ this.view.update_node(updated_node);
1444
1409
  this.layout.layout();
1445
1410
  this.view.show(false);
1446
- this.invoke_event_handle(jm.event_type.edit,{evt:'move_node',data:[nodeid,beforeid,parentid,direction],node:nodeid});
1411
+ this.invoke_event_handle(jm.event_type.edit, { evt: 'move_node', data: [nodeid, beforeid, parentid, direction], node: nodeid });
1447
1412
  }
1448
- }else{
1413
+ } else {
1449
1414
  logger.error('fail, this mind map is not editable');
1450
1415
  return;
1451
1416
  }
1452
1417
  },
1453
1418
 
1454
- select_node:function(node){
1455
- if(!jm.util.is_node(node)){
1419
+ select_node: function (node) {
1420
+ if (!jm.util.is_node(node)) {
1456
1421
  var the_node = this.get_node(node);
1457
- if(!the_node){
1458
- logger.error('the node[id='+node+'] can not be found.');
1422
+ if (!the_node) {
1423
+ logger.error('the node[id=' + node + '] can not be found.');
1459
1424
  return;
1460
- }else{
1425
+ } else {
1461
1426
  return this.select_node(the_node);
1462
1427
  }
1463
1428
  }
1464
- if(!this.layout.is_visible(node)){
1429
+ if (!this.layout.is_visible(node)) {
1465
1430
  return;
1466
1431
  }
1467
1432
  this.mind.selected = node;
1468
1433
  this.view.select_node(node);
1434
+ this.invoke_event_handle(jm.event_type.select, { evt: 'select_node', data: [], node: node.id });
1469
1435
  },
1470
1436
 
1471
- get_selected_node:function(){
1472
- if(!!this.mind){
1437
+ get_selected_node: function () {
1438
+ if (!!this.mind) {
1473
1439
  return this.mind.selected;
1474
- }else{
1440
+ } else {
1475
1441
  return null;
1476
1442
  }
1477
1443
  },
1478
1444
 
1479
- select_clear:function(){
1480
- if(!!this.mind){
1445
+ select_clear: function () {
1446
+ if (!!this.mind) {
1481
1447
  this.mind.selected = null;
1482
1448
  this.view.select_clear();
1483
1449
  }
1484
1450
  },
1485
1451
 
1486
- is_node_visible:function(node){
1452
+ is_node_visible: function (node) {
1487
1453
  return this.layout.is_visible(node);
1488
1454
  },
1489
1455
 
1490
- find_node_before:function(node){
1491
- if(!jm.util.is_node(node)){
1456
+ find_node_before: function (node) {
1457
+ if (!jm.util.is_node(node)) {
1492
1458
  var the_node = this.get_node(node);
1493
- if(!the_node){
1494
- logger.error('the node[id='+node+'] can not be found.');
1459
+ if (!the_node) {
1460
+ logger.error('the node[id=' + node + '] can not be found.');
1495
1461
  return;
1496
- }else{
1462
+ } else {
1497
1463
  return this.find_node_before(the_node);
1498
1464
  }
1499
1465
  }
1500
- if(node.isroot){return null;}
1466
+ if (node.isroot) { return null; }
1501
1467
  var n = null;
1502
- if(node.parent.isroot){
1468
+ if (node.parent.isroot) {
1503
1469
  var c = node.parent.children;
1504
1470
  var prev = null;
1505
1471
  var ni = null;
1506
- for(var i=0;i<c.length;i++){
1472
+ for (var i = 0; i < c.length; i++) {
1507
1473
  ni = c[i];
1508
- if(node.direction === ni.direction){
1509
- if(node.id === ni.id){
1474
+ if (node.direction === ni.direction) {
1475
+ if (node.id === ni.id) {
1510
1476
  n = prev;
1511
1477
  }
1512
1478
  prev = ni;
1513
1479
  }
1514
1480
  }
1515
- }else{
1481
+ } else {
1516
1482
  n = this.mind.get_node_before(node);
1517
1483
  }
1518
1484
  return n;
1519
1485
  },
1520
1486
 
1521
- find_node_after:function(node){
1522
- if(!jm.util.is_node(node)){
1487
+ find_node_after: function (node) {
1488
+ if (!jm.util.is_node(node)) {
1523
1489
  var the_node = this.get_node(node);
1524
- if(!the_node){
1525
- logger.error('the node[id='+node+'] can not be found.');
1490
+ if (!the_node) {
1491
+ logger.error('the node[id=' + node + '] can not be found.');
1526
1492
  return;
1527
- }else{
1493
+ } else {
1528
1494
  return this.find_node_after(the_node);
1529
1495
  }
1530
1496
  }
1531
- if(node.isroot){return null;}
1497
+ if (node.isroot) { return null; }
1532
1498
  var n = null;
1533
- if(node.parent.isroot){
1499
+ if (node.parent.isroot) {
1534
1500
  var c = node.parent.children;
1535
1501
  var getthis = false;
1536
1502
  var ni = null;
1537
- for(var i=0;i<c.length;i++){
1503
+ for (var i = 0; i < c.length; i++) {
1538
1504
  ni = c[i];
1539
- if(node.direction === ni.direction){
1540
- if(getthis){
1505
+ if (node.direction === ni.direction) {
1506
+ if (getthis) {
1541
1507
  n = ni;
1542
1508
  break;
1543
1509
  }
1544
- if(node.id === ni.id){
1510
+ if (node.id === ni.id) {
1545
1511
  getthis = true;
1546
1512
  }
1547
1513
  }
1548
1514
  }
1549
- }else{
1515
+ } else {
1550
1516
  n = this.mind.get_node_after(node);
1551
1517
  }
1552
1518
  return n;
1553
1519
  },
1554
1520
 
1555
- set_node_color:function(nodeid, bgcolor, fgcolor){
1556
- if(this.get_editable()){
1521
+ set_node_color: function (nodeid, bgcolor, fgcolor) {
1522
+ if (this.get_editable()) {
1557
1523
  var node = this.mind.get_node(nodeid);
1558
- if(!!node){
1559
- if(!!bgcolor){
1524
+ if (!!node) {
1525
+ if (!!bgcolor) {
1560
1526
  node.data['background-color'] = bgcolor;
1561
1527
  }
1562
- if(!!fgcolor){
1528
+ if (!!fgcolor) {
1563
1529
  node.data['foreground-color'] = fgcolor;
1564
1530
  }
1565
1531
  this.view.reset_node_custom_style(node);
1566
1532
  }
1567
- }else{
1533
+ } else {
1568
1534
  logger.error('fail, this mind map is not editable');
1569
1535
  return null;
1570
1536
  }
1571
1537
  },
1572
1538
 
1573
- set_node_font_style:function(nodeid, size, weight, style){
1574
- if(this.get_editable()){
1539
+ set_node_font_style: function (nodeid, size, weight, style) {
1540
+ if (this.get_editable()) {
1575
1541
  var node = this.mind.get_node(nodeid);
1576
- if(!!node){
1577
- if(!!size){
1542
+ if (!!node) {
1543
+ if (!!size) {
1578
1544
  node.data['font-size'] = size;
1579
1545
  }
1580
- if(!!weight){
1546
+ if (!!weight) {
1581
1547
  node.data['font-weight'] = weight;
1582
1548
  }
1583
- if(!!style){
1549
+ if (!!style) {
1584
1550
  node.data['font-style'] = style;
1585
1551
  }
1586
1552
  this.view.reset_node_custom_style(node);
@@ -1588,26 +1554,26 @@
1588
1554
  this.layout.layout();
1589
1555
  this.view.show(false);
1590
1556
  }
1591
- }else{
1557
+ } else {
1592
1558
  logger.error('fail, this mind map is not editable');
1593
1559
  return null;
1594
1560
  }
1595
1561
  },
1596
1562
 
1597
- set_node_background_image:function(nodeid, image, width, height, rotation){
1598
- if(this.get_editable()){
1563
+ set_node_background_image: function (nodeid, image, width, height, rotation) {
1564
+ if (this.get_editable()) {
1599
1565
  var node = this.mind.get_node(nodeid);
1600
- if(!!node){
1601
- if(!!image){
1566
+ if (!!node) {
1567
+ if (!!image) {
1602
1568
  node.data['background-image'] = image;
1603
1569
  }
1604
- if(!!width){
1570
+ if (!!width) {
1605
1571
  node.data['width'] = width;
1606
1572
  }
1607
- if(!!height){
1573
+ if (!!height) {
1608
1574
  node.data['height'] = height;
1609
1575
  }
1610
- if(!!rotation){
1576
+ if (!!rotation) {
1611
1577
  node.data['background-rotation'] = rotation;
1612
1578
  }
1613
1579
  this.view.reset_node_custom_style(node);
@@ -1615,17 +1581,17 @@
1615
1581
  this.layout.layout();
1616
1582
  this.view.show(false);
1617
1583
  }
1618
- }else{
1584
+ } else {
1619
1585
  logger.error('fail, this mind map is not editable');
1620
1586
  return null;
1621
1587
  }
1622
1588
  },
1623
1589
 
1624
- set_node_background_rotation:function(nodeid, rotation){
1625
- if(this.get_editable()){
1590
+ set_node_background_rotation: function (nodeid, rotation) {
1591
+ if (this.get_editable()) {
1626
1592
  var node = this.mind.get_node(nodeid);
1627
- if(!!node){
1628
- if(!node.data['background-image']) {
1593
+ if (!!node) {
1594
+ if (!node.data['background-image']) {
1629
1595
  logger.error('fail, only can change rotation angle of node with background image');
1630
1596
  return null;
1631
1597
  }
@@ -1635,89 +1601,93 @@
1635
1601
  this.layout.layout();
1636
1602
  this.view.show(false);
1637
1603
  }
1638
- }else{
1604
+ } else {
1639
1605
  logger.error('fail, this mind map is not editable');
1640
1606
  return null;
1641
1607
  }
1642
1608
  },
1643
1609
 
1644
- resize:function(){
1610
+ resize: function () {
1645
1611
  this.view.resize();
1646
1612
  },
1647
1613
 
1648
1614
  // callback(type ,data)
1649
- add_event_listener:function(callback){
1650
- if(typeof callback === 'function'){
1615
+ add_event_listener: function (callback) {
1616
+ if (typeof callback === 'function') {
1651
1617
  this.event_handles.push(callback);
1652
1618
  }
1653
1619
  },
1654
1620
 
1655
- invoke_event_handle:function(type, data){
1621
+ clear_event_listener: function () {
1622
+ this.event_handles = [];
1623
+ },
1624
+
1625
+ invoke_event_handle: function (type, data) {
1656
1626
  var j = this;
1657
- $w.setTimeout(function(){
1658
- j._invoke_event_handle(type,data);
1659
- },0);
1627
+ $w.setTimeout(function () {
1628
+ j._invoke_event_handle(type, data);
1629
+ }, 0);
1660
1630
  },
1661
1631
 
1662
- _invoke_event_handle:function(type,data){
1632
+ _invoke_event_handle: function (type, data) {
1663
1633
  var l = this.event_handles.length;
1664
- for(var i=0;i<l;i++){
1665
- this.event_handles[i](type,data);
1634
+ for (var i = 0; i < l; i++) {
1635
+ this.event_handles[i](type, data);
1666
1636
  }
1667
- }
1637
+ },
1668
1638
 
1669
1639
  };
1670
1640
 
1671
- // ============= data provider =============================================
1641
+ // ============= data provider =============================================
1672
1642
 
1673
- jm.data_provider = function(jm){
1643
+ jm.data_provider = function (jm) {
1674
1644
  this.jm = jm;
1675
1645
  };
1676
1646
 
1677
- jm.data_provider.prototype={
1678
- init:function(){
1647
+ jm.data_provider.prototype = {
1648
+ init: function () {
1679
1649
  logger.debug('data.init');
1680
1650
  },
1681
1651
 
1682
- reset:function(){
1652
+ reset: function () {
1683
1653
  logger.debug('data.reset');
1684
1654
  },
1685
1655
 
1686
- load:function(mind_data){
1656
+ load: function (mind_data) {
1687
1657
  var df = null;
1688
1658
  var mind = null;
1689
- if(typeof mind_data === 'object'){
1690
- if(!!mind_data.format){
1659
+ if (typeof mind_data === 'object') {
1660
+ if (!!mind_data.format) {
1691
1661
  df = mind_data.format;
1692
- }else{
1662
+ } else {
1693
1663
  df = 'node_tree';
1694
1664
  }
1695
- }else{
1665
+ } else {
1696
1666
  df = 'freemind';
1697
1667
  }
1698
1668
 
1699
- if(df == 'node_array'){
1669
+ if (df == 'node_array') {
1700
1670
  mind = jm.format.node_array.get_mind(mind_data);
1701
- }else if(df == 'node_tree'){
1671
+ } else if (df == 'node_tree') {
1702
1672
  mind = jm.format.node_tree.get_mind(mind_data);
1703
- }else if(df == 'freemind'){
1673
+ } else if (df == 'freemind') {
1704
1674
  mind = jm.format.freemind.get_mind(mind_data);
1705
- }else{
1675
+ } else {
1706
1676
  logger.warn('unsupported format');
1707
1677
  }
1708
1678
  return mind;
1709
1679
  },
1710
1680
 
1711
- get_data:function(data_format){
1681
+ get_data: function (data_format) {
1712
1682
  var data = null;
1713
- if(data_format == 'node_array'){
1683
+ if (data_format == 'node_array') {
1714
1684
  data = jm.format.node_array.get_data(this.jm.mind);
1715
- }else if(data_format == 'node_tree'){
1685
+ } else if (data_format == 'node_tree') {
1716
1686
  data = jm.format.node_tree.get_data(this.jm.mind);
1717
- }else if(data_format == 'freemind'){
1687
+ } else if (data_format == 'freemind') {
1718
1688
  data = jm.format.freemind.get_data(this.jm.mind);
1719
- }else{
1720
- logger.error('unsupported '+data_format+' format');
1689
+ } else {
1690
+ logger.error('unsupported ' + data_format + ' format');
1721
1691
  }
1722
1692
  return data;
1723
1693
  },
@@ -1725,7 +1695,7 @@
1725
1695
 
1726
1696
  // ============= layout provider ===========================================
1727
1697
 
1728
- jm.layout_provider = function(jm, options){
1698
+ jm.layout_provider = function (jm, options) {
1729
1699
  this.opts = options;
1730
1700
  this.jm = jm;
1731
1701
  this.isside = (this.opts.mode == 'side');
@@ -1734,31 +1704,44 @@
1734
1704
  this.cache_valid = false;
1735
1705
  };
1736
1706
 
1737
- jm.layout_provider.prototype={
1738
- init:function(){
1707
+ jm.layout_provider.prototype = {
1708
+ init: function () {
1739
1709
  logger.debug('layout.init');
1740
1710
  },
1741
- reset:function(){
1711
+
1712
+ reset: function () {
1742
1713
  logger.debug('layout.reset');
1743
- this.bounds = {n:0,s:0,w:0,e:0};
1714
+ this.bounds = { n: 0, s: 0, w: 0, e: 0 };
1715
+ },
1716
+
1717
+ calculate_next_child_direction: function (node) {
1718
+ if (this.isside) {
1719
+ return jm.direction.right;
1720
+ }
1721
+ var children = node.children || [];
1722
+ var children_len = children.length;
1723
+ var r = 0;
1724
+ for (var i = 0; i < children_len; i++) { if (children[i].direction === jm.direction.left) { r--; } else { r++; } }
1725
+ return (children_len > 1 && r > 0) ? jm.direction.left : jm.direction.right;
1744
1726
  },
1745
- layout:function(){
1727
+
1728
+ layout: function () {
1746
1729
  logger.debug('layout.layout');
1747
1730
  this.layout_direction();
1748
1731
  this.layout_offset();
1749
1732
  },
1750
1733
 
1751
- layout_direction:function(){
1734
+ layout_direction: function () {
1752
1735
  this._layout_direction_root();
1753
1736
  },
1754
1737
 
1755
- _layout_direction_root:function(){
1738
+ _layout_direction_root: function () {
1756
1739
  var node = this.jm.mind.root;
1757
1740
  // logger.debug(node);
1758
1741
  var layout_data = null;
1759
- if('layout' in node._data){
1742
+ if ('layout' in node._data) {
1760
1743
  layout_data = node._data.layout;
1761
- }else{
1744
+ } else {
1762
1745
  layout_data = {};
1763
1746
  node._data.layout = layout_data;
1764
1747
  }
@@ -1766,20 +1749,20 @@
1766
1749
  var children_count = children.length;
1767
1750
  layout_data.direction = jm.direction.center;
1768
1751
  layout_data.side_index = 0;
1769
- if(this.isside){
1752
+ if (this.isside) {
1770
1753
  var i = children_count;
1771
- while(i--){
1754
+ while (i--) {
1772
1755
  this._layout_direction_side(children[i], jm.direction.right, i);
1773
1756
  }
1774
- }else{
1757
+ } else {
1775
1758
  var i = children_count;
1776
1759
  var subnode = null;
1777
- while(i--){
1760
+ while (i--) {
1778
1761
  subnode = children[i];
1779
- if(subnode.direction == jm.direction.left){
1780
- this._layout_direction_side(subnode,jm.direction.left, i);
1781
- }else{
1782
- this._layout_direction_side(subnode,jm.direction.right, i);
1762
+ if (subnode.direction == jm.direction.left) {
1763
+ this._layout_direction_side(subnode, jm.direction.left, i);
1764
+ } else {
1765
+ this._layout_direction_side(subnode, jm.direction.right, i);
1783
1766
  }
1784
1767
  }
1785
1768
  /*
@@ -1796,11 +1779,11 @@
1796
1779
  }
1797
1780
  },
1798
1781
 
1799
- _layout_direction_side:function(node, direction, side_index){
1782
+ _layout_direction_side: function (node, direction, side_index) {
1800
1783
  var layout_data = null;
1801
- if('layout' in node._data){
1784
+ if ('layout' in node._data) {
1802
1785
  layout_data = node._data.layout;
1803
- }else{
1786
+ } else {
1804
1787
  layout_data = {};
1805
1788
  node._data.layout = layout_data;
1806
1789
  }
@@ -1810,12 +1793,12 @@
1810
1793
  layout_data.direction = direction;
1811
1794
  layout_data.side_index = side_index;
1812
1795
  var i = children_count;
1813
- while(i--){
1796
+ while (i--) {
1814
1797
  this._layout_direction_side(children[i], direction, i);
1815
1798
  }
1816
1799
  },
1817
1800
 
1818
- layout_offset:function(){
1801
+ layout_offset: function () {
1819
1802
  var node = this.jm.mind.root;
1820
1803
  var layout_data = node._data.layout;
1821
1804
  layout_data.offset_x = 0;
@@ -1826,11 +1809,11 @@
1826
1809
  var left_nodes = [];
1827
1810
  var right_nodes = [];
1828
1811
  var subnode = null;
1829
- while(i--){
1812
+ while (i--) {
1830
1813
  subnode = children[i];
1831
- if(subnode._data.layout.direction == jm.direction.right){
1814
+ if (subnode._data.layout.direction == jm.direction.right) {
1832
1815
  right_nodes.unshift(subnode);
1833
- }else{
1816
+ } else {
1834
1817
  left_nodes.unshift(subnode);
1835
1818
  }
1836
1819
  }
@@ -1838,15 +1821,15 @@
1838
1821
  layout_data.right_nodes = right_nodes;
1839
1822
  layout_data.outer_height_left = this._layout_offset_subnodes(left_nodes);
1840
1823
  layout_data.outer_height_right = this._layout_offset_subnodes(right_nodes);
1841
- this.bounds.e=node._data.view.width/2;
1842
- this.bounds.w=0-this.bounds.e;
1824
+ this.bounds.e = node._data.view.width / 2;
1825
+ this.bounds.w = 0 - this.bounds.e;
1843
1826
  //logger.debug(this.bounds.w);
1844
- this.bounds.n=0;
1845
- this.bounds.s = Math.max(layout_data.outer_height_left,layout_data.outer_height_right);
1827
+ this.bounds.n = 0;
1828
+ this.bounds.s = Math.max(layout_data.outer_height_left, layout_data.outer_height_right);
1846
1829
  },
1847
1830
 
1848
1831
  // layout both the x and y axis
1849
- _layout_offset_subnodes:function(nodes){
1832
+ _layout_offset_subnodes: function (nodes) {
1850
1833
  var total_height = 0;
1851
1834
  var nodes_count = nodes.length;
1852
1835
  var i = nodes_count;
@@ -1855,36 +1838,36 @@
1855
1838
  var layout_data = null;
1856
1839
  var base_y = 0;
1857
1840
  var pd = null; // parent._data
1858
- while(i--){
1841
+ while (i--) {
1859
1842
  node = nodes[i];
1860
1843
  layout_data = node._data.layout;
1861
- if(pd == null){
1844
+ if (pd == null) {
1862
1845
  pd = node.parent._data;
1863
1846
  }
1864
1847
 
1865
1848
  node_outer_height = this._layout_offset_subnodes(node.children);
1866
- if(!node.expanded){
1867
- node_outer_height=0;
1868
- this.set_visible(node.children,false);
1849
+ if (!node.expanded) {
1850
+ node_outer_height = 0;
1851
+ this.set_visible(node.children, false);
1869
1852
  }
1870
- node_outer_height = Math.max(node._data.view.height,node_outer_height);
1853
+ node_outer_height = Math.max(node._data.view.height, node_outer_height);
1871
1854
 
1872
1855
  layout_data.outer_height = node_outer_height;
1873
- layout_data.offset_y = base_y - node_outer_height/2;
1874
- layout_data.offset_x = this.opts.hspace * layout_data.direction + pd.view.width * (pd.layout.direction + layout_data.direction)/2;
1875
- if(!node.parent.isroot){
1856
+ layout_data.offset_y = base_y - node_outer_height / 2;
1857
+ layout_data.offset_x = this.opts.hspace * layout_data.direction + pd.view.width * (pd.layout.direction + layout_data.direction) / 2;
1858
+ if (!node.parent.isroot) {
1876
1859
  layout_data.offset_x += this.opts.pspace * layout_data.direction;
1877
1860
  }
1878
1861
 
1879
1862
  base_y = base_y - node_outer_height - this.opts.vspace;
1880
1863
  total_height += node_outer_height;
1881
1864
  }
1882
- if(nodes_count>1){
1883
- total_height += this.opts.vspace * (nodes_count-1);
1865
+ if (nodes_count > 1) {
1866
+ total_height += this.opts.vspace * (nodes_count - 1);
1884
1867
  }
1885
1868
  i = nodes_count;
1886
- var middle_height = total_height/2;
1887
- while(i--){
1869
+ var middle_height = total_height / 2;
1870
+ while (i--) {
1888
1871
  node = nodes[i];
1889
1872
  node._data.layout.offset_y += middle_height;
1890
1873
  }
@@ -1892,7 +1875,7 @@
1892
1875
  },
1893
1876
 
1894
1877
  // layout the y axis only, for collapse/expand a node
1895
- _layout_offset_subnodes_height:function(nodes){
1878
+ _layout_offset_subnodes_height: function (nodes) {
1896
1879
  var total_height = 0;
1897
1880
  var nodes_count = nodes.length;
1898
1881
  var i = nodes_count;
@@ -1901,30 +1884,30 @@
1901
1884
  var layout_data = null;
1902
1885
  var base_y = 0;
1903
1886
  var pd = null; // parent._data
1904
- while(i--){
1887
+ while (i--) {
1905
1888
  node = nodes[i];
1906
1889
  layout_data = node._data.layout;
1907
- if(pd == null){
1890
+ if (pd == null) {
1908
1891
  pd = node.parent._data;
1909
1892
  }
1910
1893
 
1911
1894
  node_outer_height = this._layout_offset_subnodes_height(node.children);
1912
- if(!node.expanded){
1913
- node_outer_height=0;
1895
+ if (!node.expanded) {
1896
+ node_outer_height = 0;
1914
1897
  }
1915
- node_outer_height = Math.max(node._data.view.height,node_outer_height);
1898
+ node_outer_height = Math.max(node._data.view.height, node_outer_height);
1916
1899
 
1917
1900
  layout_data.outer_height = node_outer_height;
1918
- layout_data.offset_y = base_y - node_outer_height/2;
1901
+ layout_data.offset_y = base_y - node_outer_height / 2;
1919
1902
  base_y = base_y - node_outer_height - this.opts.vspace;
1920
1903
  total_height += node_outer_height;
1921
1904
  }
1922
- if(nodes_count>1){
1923
- total_height += this.opts.vspace * (nodes_count-1);
1905
+ if (nodes_count > 1) {
1906
+ total_height += this.opts.vspace * (nodes_count - 1);
1924
1907
  }
1925
1908
  i = nodes_count;
1926
- var middle_height = total_height/2;
1927
- while(i--){
1909
+ var middle_height = total_height / 2;
1910
+ while (i--) {
1928
1911
  node = nodes[i];
1929
1912
  node._data.layout.offset_y += middle_height;
1930
1913
  //logger.debug(node.topic);
@@ -1933,19 +1916,19 @@
1933
1916
  return total_height;
1934
1917
  },
1935
1918
 
1936
- get_node_offset:function(node){
1919
+ get_node_offset: function (node) {
1937
1920
  var layout_data = node._data.layout;
1938
1921
  var offset_cache = null;
1939
- if(('_offset_' in layout_data) && this.cache_valid){
1922
+ if (('_offset_' in layout_data) && this.cache_valid) {
1940
1923
  offset_cache = layout_data._offset_;
1941
- }else{
1942
- offset_cache = {x:-1, y:-1};
1924
+ } else {
1925
+ offset_cache = { x: -1, y: -1 };
1943
1926
  layout_data._offset_ = offset_cache;
1944
1927
  }
1945
- if(offset_cache.x == -1 || offset_cache.y == -1){
1928
+ if (offset_cache.x == -1 || offset_cache.y == -1) {
1946
1929
  var x = layout_data.offset_x;
1947
1930
  var y = layout_data.offset_y;
1948
- if(!node.isroot){
1931
+ if (!node.isroot) {
1949
1932
  var offset_p = this.get_node_offset(node.parent);
1950
1933
  x += offset_p.x;
1951
1934
  y += offset_p.y;
@@ -1956,39 +1939,39 @@
1956
1939
  return offset_cache;
1957
1940
  },
1958
1941
 
1959
- get_node_point:function(node){
1942
+ get_node_point: function (node) {
1960
1943
  var view_data = node._data.view;
1961
1944
  var offset_p = this.get_node_offset(node);
1962
1945
  //logger.debug(offset_p);
1963
1946
  var p = {};
1964
- p.x = offset_p.x + view_data.width*(node._data.layout.direction-1)/2;
1965
- p.y = offset_p.y-view_data.height/2;
1947
+ p.x = offset_p.x + view_data.width * (node._data.layout.direction - 1) / 2;
1948
+ p.y = offset_p.y - view_data.height / 2;
1966
1949
  //logger.debug(p);
1967
1950
  return p;
1968
1951
  },
1969
1952
 
1970
- get_node_point_in:function(node){
1953
+ get_node_point_in: function (node) {
1971
1954
  var p = this.get_node_offset(node);
1972
1955
  return p;
1973
1956
  },
1974
1957
 
1975
- get_node_point_out:function(node){
1958
+ get_node_point_out: function (node) {
1976
1959
  var layout_data = node._data.layout;
1977
1960
  var pout_cache = null;
1978
- if(('_pout_' in layout_data) && this.cache_valid){
1961
+ if (('_pout_' in layout_data) && this.cache_valid) {
1979
1962
  pout_cache = layout_data._pout_;
1980
- }else{
1981
- pout_cache = {x:-1, y:-1};
1963
+ } else {
1964
+ pout_cache = { x: -1, y: -1 };
1982
1965
  layout_data._pout_ = pout_cache;
1983
1966
  }
1984
- if(pout_cache.x == -1 || pout_cache.y == -1){
1985
- if(node.isroot){
1967
+ if (pout_cache.x == -1 || pout_cache.y == -1) {
1968
+ if (node.isroot) {
1986
1969
  pout_cache.x = 0;
1987
1970
  pout_cache.y = 0;
1988
- }else{
1971
+ } else {
1989
1972
  var view_data = node._data.view;
1990
1973
  var offset_p = this.get_node_offset(node);
1991
- pout_cache.x = offset_p.x + (view_data.width+this.opts.pspace)*node._data.layout.direction;
1974
+ pout_cache.x = offset_p.x + (view_data.width + this.opts.pspace) * node._data.layout.direction;
1992
1975
  pout_cache.y = offset_p.y;
1993
1976
  //logger.debug('pout');
1994
1977
  //logger.debug(pout_cache);
@@ -1997,203 +1980,324 @@
1997
1980
  return pout_cache;
1998
1981
  },
1999
1982
 
2000
- get_expander_point:function(node){
1983
+ get_expander_point: function (node) {
2001
1984
  var p = this.get_node_point_out(node);
2002
1985
  var ex_p = {};
2003
- if(node._data.layout.direction == jm.direction.right){
1986
+ if (node._data.layout.direction == jm.direction.right) {
2004
1987
  ex_p.x = p.x - this.opts.pspace;
2005
- }else{
1988
+ } else {
2006
1989
  ex_p.x = p.x;
2007
1990
  }
2008
- ex_p.y = p.y - Math.ceil(this.opts.pspace/2);
1991
+ ex_p.y = p.y - Math.ceil(this.opts.pspace / 2);
2009
1992
  return ex_p;
2010
1993
  },
2011
1994
 
2012
- get_min_size:function(){
1995
+ get_min_size: function () {
2013
1996
  var nodes = this.jm.mind.nodes;
2014
1997
  var node = null;
2015
1998
  var pout = null;
2016
- for(var nodeid in nodes){
1999
+ for (var nodeid in nodes) {
2017
2000
  node = nodes[nodeid];
2018
2001
  pout = this.get_node_point_out(node);
2019
- //logger.debug(pout.x);
2020
- if(pout.x > this.bounds.e){this.bounds.e = pout.x;}
2021
- if(pout.x < this.bounds.w){this.bounds.w = pout.x;}
2002
+ if (pout.x > this.bounds.e) { this.bounds.e = pout.x; }
2003
+ if (pout.x < this.bounds.w) { this.bounds.w = pout.x; }
2022
2004
  }
2023
2005
  return {
2024
- w:this.bounds.e - this.bounds.w,
2025
- h:this.bounds.s - this.bounds.n
2006
+ w: this.bounds.e - this.bounds.w,
2007
+ h: this.bounds.s - this.bounds.n
2026
2008
  }
2027
2009
  },
2028
2010
 
2029
- toggle_node:function(node){
2030
- if(node.isroot){
2011
+ toggle_node: function (node) {
2012
+ if (node.isroot) {
2031
2013
  return;
2032
2014
  }
2033
- if(node.expanded){
2015
+ if (node.expanded) {
2034
2016
  this.collapse_node(node);
2035
- }else{
2017
+ } else {
2036
2018
  this.expand_node(node);
2037
2019
  }
2038
2020
  },
2039
2021
 
2040
- expand_node:function(node){
2022
+ expand_node: function (node) {
2041
2023
  node.expanded = true;
2042
2024
  this.part_layout(node);
2043
- this.set_visible(node.children,true);
2025
+ this.set_visible(node.children, true);
2026
+ this.jm.invoke_event_handle(jm.event_type.show, { evt: 'expand_node', data: [], node: node.id });
2044
2027
  },
2045
2028
 
2046
- collapse_node:function(node){
2029
+ collapse_node: function (node) {
2047
2030
  node.expanded = false;
2048
2031
  this.part_layout(node);
2049
- this.set_visible(node.children,false);
2032
+ this.set_visible(node.children, false);
2033
+ this.jm.invoke_event_handle(jm.event_type.show, { evt: 'collapse_node', data: [], node: node.id });
2050
2034
  },
2051
2035
 
2052
- expand_all:function(){
2036
+ expand_all: function () {
2053
2037
  var nodes = this.jm.mind.nodes;
2054
2038
  var c = 0;
2055
2039
  var node;
2056
- for(var nodeid in nodes){
2040
+ for (var nodeid in nodes) {
2057
2041
  node = nodes[nodeid];
2058
- if(!node.expanded){
2042
+ if (!node.expanded) {
2059
2043
  node.expanded = true;
2060
2044
  c++;
2061
2045
  }
2062
2046
  }
2063
- if(c>0){
2047
+ if (c > 0) {
2064
2048
  var root = this.jm.mind.root;
2065
2049
  this.part_layout(root);
2066
- this.set_visible(root.children,true);
2050
+ this.set_visible(root.children, true);
2067
2051
  }
2068
2052
  },
2069
2053
 
2070
- collapse_all:function(){
2054
+ collapse_all: function () {
2071
2055
  var nodes = this.jm.mind.nodes;
2072
2056
  var c = 0;
2073
2057
  var node;
2074
- for(var nodeid in nodes){
2058
+ for (var nodeid in nodes) {
2075
2059
  node = nodes[nodeid];
2076
- if(node.expanded && !node.isroot){
2077
- node.expanded = false
2060
+ if (node.expanded && !node.isroot) {
2061
+ node.expanded = false;
2078
2062
  c++;
2079
2063
  }
2080
2064
  }
2081
- if(c>0){
2065
+ if (c > 0) {
2082
2066
  var root = this.jm.mind.root;
2083
2067
  this.part_layout(root);
2084
- this.set_visible(root.children,true);
2068
+ this.set_visible(root.children, true);
2085
2069
  }
2086
2070
  },
2087
2071
 
2088
- expand_to_depth:function(target_depth,curr_nodes,curr_depth){
2089
- if(target_depth < 1){return;}
2072
+ expand_to_depth: function (target_depth, curr_nodes, curr_depth) {
2073
+ if (target_depth < 1) { return; }
2090
2074
  var nodes = curr_nodes || this.jm.mind.root.children;
2091
2075
  var depth = curr_depth || 1;
2092
2076
  var i = nodes.length;
2093
2077
  var node = null;
2094
- while(i--){
2078
+ while (i--) {
2095
2079
  node = nodes[i];
2096
- if(depth < target_depth){
2097
- if(!node.expanded){
2080
+ if (depth < target_depth) {
2081
+ if (!node.expanded) {
2098
2082
  this.expand_node(node);
2099
2083
  }
2100
- this.expand_to_depth(target_depth, node.children, depth+1);
2084
+ this.expand_to_depth(target_depth, node.children, depth + 1);
2101
2085
  }
2102
- if(depth == target_depth){
2103
- if(node.expanded){
2086
+ if (depth == target_depth) {
2087
+ if (node.expanded) {
2104
2088
  this.collapse_node(node);
2105
2089
  }
2106
2090
  }
2107
2091
  }
2108
2092
  },
2109
2093
 
2110
- part_layout:function(node){
2094
+ part_layout: function (node) {
2111
2095
  var root = this.jm.mind.root;
2112
- if(!!root){
2096
+ if (!!root) {
2113
2097
  var root_layout_data = root._data.layout;
2114
- if(node.isroot){
2115
- root_layout_data.outer_height_right=this._layout_offset_subnodes_height(root_layout_data.right_nodes);
2116
- root_layout_data.outer_height_left=this._layout_offset_subnodes_height(root_layout_data.left_nodes);
2117
- }else{
2118
- if(node._data.layout.direction == jm.direction.right){
2119
- root_layout_data.outer_height_right=this._layout_offset_subnodes_height(root_layout_data.right_nodes);
2120
- }else{
2121
- root_layout_data.outer_height_left=this._layout_offset_subnodes_height(root_layout_data.left_nodes);
2098
+ if (node.isroot) {
2099
+ root_layout_data.outer_height_right = this._layout_offset_subnodes_height(root_layout_data.right_nodes);
2100
+ root_layout_data.outer_height_left = this._layout_offset_subnodes_height(root_layout_data.left_nodes);
2101
+ } else {
2102
+ if (node._data.layout.direction == jm.direction.right) {
2103
+ root_layout_data.outer_height_right = this._layout_offset_subnodes_height(root_layout_data.right_nodes);
2104
+ } else {
2105
+ root_layout_data.outer_height_left = this._layout_offset_subnodes_height(root_layout_data.left_nodes);
2122
2106
  }
2123
2107
  }
2124
- this.bounds.s = Math.max(root_layout_data.outer_height_left,root_layout_data.outer_height_right);
2108
+ this.bounds.s = Math.max(root_layout_data.outer_height_left, root_layout_data.outer_height_right);
2125
2109
  this.cache_valid = false;
2126
- }else{
2110
+ } else {
2127
2111
  logger.warn('can not found root node');
2128
2112
  }
2129
2113
  },
2130
2114
 
2131
- set_visible:function(nodes,visible){
2115
+ set_visible: function (nodes, visible) {
2132
2116
  var i = nodes.length;
2133
2117
  var node = null;
2134
2118
  var layout_data = null;
2135
- while(i--){
2119
+ while (i--) {
2136
2120
  node = nodes[i];
2137
2121
  layout_data = node._data.layout;
2138
- if(node.expanded){
2139
- this.set_visible(node.children,visible);
2140
- }else{
2141
- this.set_visible(node.children,false);
2122
+ if (node.expanded) {
2123
+ this.set_visible(node.children, visible);
2124
+ } else {
2125
+ this.set_visible(node.children, false);
2142
2126
  }
2143
- if(!node.isroot){
2127
+ if (!node.isroot) {
2144
2128
  node._data.layout.visible = visible;
2145
2129
  }
2146
2130
  }
2147
2131
  },
2148
2132
 
2149
- is_expand:function(node){
2133
+ is_expand: function (node) {
2150
2134
  return node.expanded;
2151
2135
  },
2152
2136
 
2153
- is_visible:function(node){
2137
+ is_visible: function (node) {
2154
2138
  var layout_data = node._data.layout;
2155
- if(('visible' in layout_data) && !layout_data.visible){
2139
+ if (('visible' in layout_data) && !layout_data.visible) {
2156
2140
  return false;
2157
- }else{
2141
+ } else {
2158
2142
  return true;
2159
2143
  }
2144
+ }
2145
+ };
2146
+
2147
+ jm.graph_canvas = function (view) {
2148
+ this.opts = view.opts;
2149
+ this.e_canvas = $c('canvas');
2150
+ this.e_canvas.className = 'jsmind';
2151
+ this.canvas_ctx = this.e_canvas.getContext('2d');
2152
+ this.size = { w: 0, h: 0 };
2153
+ };
2154
+
2155
+ jm.graph_canvas.prototype = {
2156
+ element: function () {
2157
+ return this.e_canvas;
2158
+ },
2159
+
2160
+ set_size: function (w, h) {
2161
+ this.size.w = w;
2162
+ this.size.h = h;
2163
+ this.e_canvas.width = w;
2164
+ this.e_canvas.height = h;
2165
+ },
2166
+
2167
+ clear: function () {
2168
+ this.canvas_ctx.clearRect(0, 0, this.size.w, this.size.h);
2169
+ },
2170
+
2171
+ draw_line: function (pout, pin, offset) {
2172
+ var ctx = this.canvas_ctx;
2173
+ ctx.strokeStyle = this.opts.line_color;
2174
+ ctx.lineWidth = this.opts.line_width;
2175
+ ctx.lineCap = 'round';
2176
+
2177
+ this._bezier_to(ctx,
2178
+ pin.x + offset.x,
2179
+ pin.y + offset.y,
2180
+ pout.x + offset.x,
2181
+ pout.y + offset.y);
2182
+ },
2183
+
2184
+ copy_to: function (dest_canvas_ctx, callback) {
2185
+ dest_canvas_ctx.drawImage(this.e_canvas, 0, 0);
2186
+ !!callback && callback();
2187
+ },
2188
+
2189
+ _bezier_to: function (ctx, x1, y1, x2, y2) {
2190
+ ctx.beginPath();
2191
+ ctx.moveTo(x1, y1);
2192
+ ctx.bezierCurveTo(x1 + (x2 - x1) * 2 / 3, y1, x1, y2, x2, y2);
2193
+ ctx.stroke();
2194
+ },
2195
+
2196
+ _line_to: function (ctx, x1, y1, x2, y2) {
2197
+ ctx.beginPath();
2198
+ ctx.moveTo(x1, y1);
2199
+ ctx.lineTo(x2, y2);
2200
+ ctx.stroke();
2201
+ }
2202
+ };
2203
+
2204
+ jm.graph_svg = function (view) {
2205
+ this.view = view;
2206
+ this.opts = view.opts;
2207
+ this.e_svg = jm.graph_svg.c('svg');
2208
+ this.e_svg.setAttribute('class', 'jsmind');
2209
+ this.size = { w: 0, h: 0 };
2210
+ this.lines = [];
2211
+ };
2212
+
2213
+ jm.graph_svg.c = function (tag) {
2214
+ return $d.createElementNS('http://www.w3.org/2000/svg', tag);
2215
+ };
2216
+
2217
+ jm.graph_svg.prototype = {
2218
+ element: function () {
2219
+ return this.e_svg;
2220
+ },
2221
+
2222
+ set_size: function (w, h) {
2223
+ this.size.w = w;
2224
+ this.size.h = h;
2225
+ this.e_svg.setAttribute('width', w);
2226
+ this.e_svg.setAttribute('height', h);
2227
+ },
2228
+
2229
+ clear: function () {
2230
+ var len = this.lines.length;
2231
+ while (len--) {
2232
+ this.e_svg.removeChild(this.lines[len]);
2233
+ }
2234
+ this.lines.length = 0;
2235
+ },
2236
+
2237
+ draw_line: function (pout, pin, offset) {
2238
+ var line = jm.graph_svg.c('path');
2239
+ line.setAttribute('stroke', this.opts.line_color);
2240
+ line.setAttribute('stroke-width', this.opts.line_width);
2241
+ line.setAttribute('fill', 'transparent');
2242
+ this.lines.push(line);
2243
+ this.e_svg.appendChild(line);
2244
+ this._bezier_to(line, pin.x + offset.x, pin.y + offset.y, pout.x + offset.x, pout.y + offset.y);
2245
+ },
2246
+
2247
+ copy_to: function (dest_canvas_ctx, callback) {
2248
+ var img = new Image();
2249
+ img.onload = function () {
2250
+ dest_canvas_ctx.drawImage(img, 0, 0);
2251
+ !!callback && callback();
2252
+ }
2253
+ img.src = 'data:image/svg+xml;base64,' + btoa(new XMLSerializer().serializeToString(this.e_svg));
2254
+ },
2255
+
2256
+ _bezier_to: function (path, x1, y1, x2, y2) {
2257
+ path.setAttribute('d', 'M' + x1 + ' ' + y1 + ' C ' + (x1 + (x2 - x1) * 2 / 3) + ' ' + y1 + ', ' + x1 + ' ' + y2 + ', ' + x2 + ' ' + y2);
2160
2258
  },
2259
+
2260
+ _line_to: function (path, x1, y1, x2, y2) {
2261
+ path.setAttribute('d', 'M ' + x1 + ' ' + y1 + ' L ' + x2 + ' ' + y2);
2262
+ }
2161
2263
  };
2162
2264
 
2163
2265
  // view provider
2164
- jm.view_provider= function(jm, options){
2266
+ jm.view_provider = function (jm, options) {
2165
2267
  this.opts = options;
2166
2268
  this.jm = jm;
2167
2269
  this.layout = jm.layout;
2168
2270
 
2169
2271
  this.container = null;
2170
2272
  this.e_panel = null;
2171
- this.e_nodes= null;
2172
- this.e_canvas = null;
2273
+ this.e_nodes = null;
2173
2274
 
2174
- this.canvas_ctx = null;
2175
- this.size = {w:0,h:0};
2275
+ this.size = { w: 0, h: 0 };
2176
2276
 
2177
2277
  this.selected_node = null;
2178
2278
  this.editing_node = null;
2279
+
2280
+ this.graph = null;
2179
2281
  };
2180
2282
 
2181
- jm.view_provider.prototype={
2182
- init:function(){
2283
+ jm.view_provider.prototype = {
2284
+ init: function () {
2183
2285
  logger.debug('view.init');
2184
2286
 
2185
2287
  this.container = $i(this.opts.container) ? this.opts.container : $g(this.opts.container);
2186
- if(!this.container){
2288
+ if (!this.container) {
2187
2289
  logger.error('the options.view.container was not be found in dom');
2188
2290
  return;
2189
2291
  }
2190
2292
  this.e_panel = $c('div');
2191
- this.e_canvas = $c('canvas');
2192
2293
  this.e_nodes = $c('jmnodes');
2193
2294
  this.e_editor = $c('input');
2194
2295
 
2296
+ this.graph = this.opts.engine.toLowerCase() === 'svg' ? new jm.graph_svg(this) : new jm.graph_canvas(this);
2297
+
2195
2298
  this.e_panel.className = 'jsmind-inner';
2196
- this.e_panel.appendChild(this.e_canvas);
2299
+ this.e_panel.tabIndex = 1;
2300
+ this.e_panel.appendChild(this.graph.element());
2197
2301
  this.e_panel.appendChild(this.e_nodes);
2198
2302
 
2199
2303
  this.e_editor.className = 'jsmind-editor';
@@ -2205,46 +2309,49 @@
2205
2309
  this.maxZoom = 2;
2206
2310
 
2207
2311
  var v = this;
2208
- jm.util.dom.add_event(this.e_editor,'keydown',function(e){
2312
+ jm.util.dom.add_event(this.e_editor, 'keydown', function (e) {
2209
2313
  var evt = e || event;
2210
- if(evt.keyCode == 13){v.edit_node_end();evt.stopPropagation();}
2314
+ if (evt.keyCode == 13) { v.edit_node_end(); evt.stopPropagation(); }
2211
2315
  });
2212
- jm.util.dom.add_event(this.e_editor,'blur',function(e){
2316
+ jm.util.dom.add_event(this.e_editor, 'blur', function (e) {
2213
2317
  v.edit_node_end();
2214
2318
  });
2215
2319
 
2216
2320
  this.container.appendChild(this.e_panel);
2217
2321
 
2218
- this.init_canvas();
2322
+ // Used to avoid dragging, while editing node.
2323
+ this.dragging_enabled = true
2324
+
2325
+ this.draggable_canvas()
2219
2326
  },
2220
2327
 
2221
- add_event:function(obj,event_name,event_handle){
2222
- jm.util.dom.add_event(this.e_nodes,event_name,function(e){
2328
+ add_event: function (obj, event_name, event_handle) {
2329
+ jm.util.dom.add_event(this.e_nodes, event_name, function (e) {
2223
2330
  var evt = e || event;
2224
- event_handle.call(obj,evt);
2331
+ event_handle.call(obj, evt);
2225
2332
  });
2226
2333
  },
2227
2334
 
2228
- get_binded_nodeid:function(element){
2229
- if(element == null){
2335
+ get_binded_nodeid: function (element) {
2336
+ if (element == null) {
2230
2337
  return null;
2231
2338
  }
2232
2339
  var tagName = element.tagName.toLowerCase();
2233
- if(tagName == 'jmnodes' || tagName == 'body' || tagName == 'html'){
2340
+ if (tagName == 'jmnodes' || tagName == 'body' || tagName == 'html') {
2234
2341
  return null;
2235
2342
  }
2236
- if(tagName == 'jmnode' || tagName == 'jmexpander'){
2343
+ if (tagName == 'jmnode' || tagName == 'jmexpander') {
2237
2344
  return element.getAttribute('nodeid');
2238
- }else{
2345
+ } else {
2239
2346
  return this.get_binded_nodeid(element.parentElement);
2240
2347
  }
2241
2348
  },
2242
2349
 
2243
- is_expander:function(element){
2350
+ is_expander: function (element) {
2244
2351
  return (element.tagName.toLowerCase() == 'jmexpander');
2245
2352
  },
2246
2353
 
2247
- reset:function(){
2354
+ reset: function () {
2248
2355
  logger.debug('view.reset');
2249
2356
  this.selected_node = null;
2250
2357
  this.clear_lines();
@@ -2252,116 +2359,111 @@
2252
2359
  this.reset_theme();
2253
2360
  },
2254
2361
 
2255
- reset_theme:function(){
2362
+ reset_theme: function () {
2256
2363
  var theme_name = this.jm.options.theme;
2257
- if(!!theme_name){
2364
+ if (!!theme_name) {
2258
2365
  this.e_nodes.className = 'theme-' + theme_name;
2259
- }else{
2366
+ } else {
2260
2367
  this.e_nodes.className = '';
2261
2368
  }
2262
2369
  },
2263
2370
 
2264
- reset_custom_style:function(){
2371
+ reset_custom_style: function () {
2265
2372
  var nodes = this.jm.mind.nodes;
2266
- for(var nodeid in nodes){
2373
+ for (var nodeid in nodes) {
2267
2374
  this.reset_node_custom_style(nodes[nodeid]);
2268
2375
  }
2269
2376
  },
2270
2377
 
2271
- load:function(){
2378
+ load: function () {
2272
2379
  logger.debug('view.load');
2273
2380
  this.init_nodes();
2274
2381
  },
2275
2382
 
2276
- expand_size:function(){
2383
+ expand_size: function () {
2277
2384
  var min_size = this.layout.get_min_size();
2278
- var min_width = min_size.w + this.opts.hmargin*2;
2279
- var min_height = min_size.h + this.opts.vmargin*2;
2385
+ var min_width = min_size.w + this.opts.hmargin * 2;
2386
+ var min_height = min_size.h + this.opts.vmargin * 2;
2280
2387
  var client_w = this.e_panel.clientWidth;
2281
2388
  var client_h = this.e_panel.clientHeight;
2282
- if(client_w < min_width){client_w = min_width;}
2283
- if(client_h < min_height){client_h = min_height;}
2389
+ if (client_w < min_width) { client_w = min_width; }
2390
+ if (client_h < min_height) { client_h = min_height; }
2284
2391
  this.size.w = client_w;
2285
2392
  this.size.h = client_h;
2286
2393
  },
2287
2394
 
2288
- init_canvas:function(){
2289
- var ctx = this.e_canvas.getContext('2d');
2290
- this.canvas_ctx = ctx;
2291
- },
2292
-
2293
- init_nodes_size:function(node){
2395
+ init_nodes_size: function (node) {
2294
2396
  var view_data = node._data.view;
2295
2397
  view_data.width = view_data.element.clientWidth;
2296
2398
  view_data.height = view_data.element.clientHeight;
2297
2399
  },
2298
2400
 
2299
- init_nodes:function(){
2401
+ init_nodes: function () {
2300
2402
  var nodes = this.jm.mind.nodes;
2301
2403
  var doc_frag = $d.createDocumentFragment();
2302
- for(var nodeid in nodes){
2303
- this.create_node_element(nodes[nodeid],doc_frag);
2404
+ for (var nodeid in nodes) {
2405
+ this.create_node_element(nodes[nodeid], doc_frag);
2304
2406
  }
2305
2407
  this.e_nodes.appendChild(doc_frag);
2306
- for(var nodeid in nodes){
2408
+ for (var nodeid in nodes) {
2307
2409
  this.init_nodes_size(nodes[nodeid]);
2308
2410
  }
2309
2411
  },
2310
2412
 
2311
- add_node:function(node){
2312
- this.create_node_element(node,this.e_nodes);
2413
+ add_node: function (node) {
2414
+ this.create_node_element(node, this.e_nodes);
2313
2415
  this.init_nodes_size(node);
2314
2416
  },
2315
2417
 
2316
- create_node_element:function(node,parent_node){
2418
+ create_node_element: function (node, parent_node) {
2317
2419
  var view_data = null;
2318
- if('view' in node._data){
2420
+ if ('view' in node._data) {
2319
2421
  view_data = node._data.view;
2320
- }else{
2422
+ } else {
2321
2423
  view_data = {};
2322
2424
  node._data.view = view_data;
2323
2425
  }
2324
2426
 
2325
2427
  var d = $c('jmnode');
2326
- if(node.isroot){
2428
+ if (node.isroot) {
2327
2429
  d.className = 'root';
2328
- }else{
2430
+ } else {
2329
2431
  var d_e = $c('jmexpander');
2330
- $t(d_e,'-');
2331
- d_e.setAttribute('nodeid',node.id);
2432
+ $t(d_e, '-');
2433
+ d_e.setAttribute('nodeid', node.id);
2332
2434
  d_e.style.visibility = 'hidden';
2333
2435
  parent_node.appendChild(d_e);
2334
2436
  view_data.expander = d_e;
2335
2437
  }
2336
2438
  if (!!node.topic) {
2337
- if(this.opts.support_html){
2338
- $h(d,node.topic);
2339
- }else{
2340
- $t(d,node.topic);
2439
+ if (this.opts.support_html) {
2440
+ $h(d, node.topic);
2441
+ } else {
2442
+ $t(d, node.topic);
2341
2443
  }
2342
2444
  }
2343
- d.setAttribute('nodeid',node.id);
2344
- d.style.visibility='hidden';
2445
+ d.setAttribute('nodeid', node.id);
2446
+ d.style.visibility = 'hidden';
2345
2447
  this._reset_node_custom_style(d, node.data);
2346
2448
 
2347
2449
  parent_node.appendChild(d);
2348
2450
  view_data.element = d;
2349
2451
  },
2350
2452
 
2351
- remove_node:function(node){
2352
- if(this.selected_node != null && this.selected_node.id == node.id){
2453
+ remove_node: function (node) {
2454
+ if (this.selected_node != null && this.selected_node.id == node.id) {
2353
2455
  this.selected_node = null;
2354
2456
  }
2355
- if(this.editing_node != null && this.editing_node.id == node.id){
2457
+ if (this.editing_node != null && this.editing_node.id == node.id) {
2356
2458
  node._data.view.element.removeChild(this.e_editor);
2357
2459
  this.editing_node = null;
2358
2460
  }
2359
2461
  var children = node.children;
2360
2462
  var i = children.length;
2361
- while(i--){
2463
+ while (i--) {
2362
2464
  this.remove_node(children[i]);
2363
2465
  }
2364
- if(node._data.view){
2466
+ if (node._data.view) {
2365
2467
  var element = node._data.view.element;
2366
2468
  var expander = node._data.view.expander;
2367
2469
  this.e_nodes.removeChild(element);
@@ -2371,51 +2473,59 @@
2371
2473
  }
2372
2474
  },
2373
2475
 
2374
- update_node:function(node){
2476
+ update_node: function (node) {
2375
2477
  var view_data = node._data.view;
2376
2478
  var element = view_data.element;
2377
2479
  if (!!node.topic) {
2378
- if(this.opts.support_html){
2379
- $h(element,node.topic);
2380
- }else{
2381
- $t(element,node.topic);
2480
+ if (this.opts.support_html) {
2481
+ $h(element, node.topic);
2482
+ } else {
2483
+ $t(element, node.topic);
2382
2484
  }
2383
2485
  }
2384
- view_data.width = element.clientWidth;
2385
- view_data.height = element.clientHeight;
2486
+ if (this.layout.is_visible(node)) {
2487
+ view_data.width = element.clientWidth;
2488
+ view_data.height = element.clientHeight;
2489
+ } else {
2490
+ let origin_style = element.getAttribute('style');
2491
+ element.style = 'visibility: visible; left:0; top:0;';
2492
+ view_data.width = element.clientWidth;
2493
+ view_data.height = element.clientHeight;
2494
+ element.style = origin_style;
2495
+ }
2386
2496
  },
2387
2497
 
2388
- select_node:function(node){
2389
- if(!!this.selected_node){
2498
+ select_node: function (node) {
2499
+ if (!!this.selected_node) {
2390
2500
  this.selected_node._data.view.element.className =
2391
- this.selected_node._data.view.element.className.replace(/\s*selected\b/i,'');
2501
+ this.selected_node._data.view.element.className.replace(/\s*selected\b/i, '');
2392
2502
  this.reset_node_custom_style(this.selected_node);
2393
2503
  }
2394
- if(!!node){
2504
+ if (!!node) {
2395
2505
  this.selected_node = node;
2396
2506
  node._data.view.element.className += ' selected';
2397
2507
  this.clear_node_custom_style(node);
2398
2508
  }
2399
2509
  },
2400
2510
 
2401
- select_clear:function(){
2511
+ select_clear: function () {
2402
2512
  this.select_node(null);
2403
2513
  },
2404
2514
 
2405
- get_editing_node:function(){
2515
+ get_editing_node: function () {
2406
2516
  return this.editing_node;
2407
2517
  },
2408
2518
 
2409
- is_editing:function(){
2519
+ is_editing: function () {
2410
2520
  return (!!this.editing_node);
2411
2521
  },
2412
2522
 
2413
- edit_node_begin:function(node){
2414
- if(!node.topic) {
2523
+ edit_node_begin: function (node) {
2524
+ if (!node.topic) {
2415
2525
  logger.warn("don't edit image nodes");
2416
2526
  return;
2417
2527
  }
2418
- if(this.editing_node != null){
2528
+ if (this.editing_node != null) {
2419
2529
  this.edit_node_end();
2420
2530
  }
2421
2531
  this.editing_node = node;
@@ -2424,7 +2534,7 @@
2424
2534
  var topic = node.topic;
2425
2535
  var ncs = getComputedStyle(element);
2426
2536
  this.e_editor.value = topic;
2427
- this.e_editor.style.width = (element.clientWidth-parseInt(ncs.getPropertyValue('padding-left'))-parseInt(ncs.getPropertyValue('padding-right')))+'px';
2537
+ this.e_editor.style.width = (element.clientWidth - parseInt(ncs.getPropertyValue('padding-left')) - parseInt(ncs.getPropertyValue('padding-right'))) + 'px';
2428
2538
  element.innerHTML = '';
2429
2539
  element.appendChild(this.e_editor);
2430
2540
  element.style.zIndex = 5;
@@ -2432,8 +2542,8 @@
2432
2542
  this.e_editor.select();
2433
2543
  },
2434
2544
 
2435
- edit_node_end:function(){
2436
- if(this.editing_node != null){
2545
+ edit_node_end: function () {
2546
+ if (this.editing_node != null) {
2437
2547
  var node = this.editing_node;
2438
2548
  this.editing_node = null;
2439
2549
  var view_data = node._data.view;
@@ -2441,28 +2551,28 @@
2441
2551
  var topic = this.e_editor.value;
2442
2552
  element.style.zIndex = 'auto';
2443
2553
  element.removeChild(this.e_editor);
2444
- if(jm.util.text.is_empty(topic) || node.topic === topic){
2445
- if(this.opts.support_html){
2446
- $h(element,node.topic);
2447
- }else{
2448
- $t(element,node.topic);
2554
+ if (jm.util.text.is_empty(topic) || node.topic === topic) {
2555
+ if (this.opts.support_html) {
2556
+ $h(element, node.topic);
2557
+ } else {
2558
+ $t(element, node.topic);
2449
2559
  }
2450
- }else{
2451
- this.jm.update_node(node.id,topic);
2560
+ } else {
2561
+ this.jm.update_node(node.id, topic);
2452
2562
  }
2453
2563
  }
2564
+ this.e_panel.focus();
2454
2565
  },
2455
2566
 
2456
- get_view_offset:function(){
2567
+ get_view_offset: function () {
2457
2568
  var bounds = this.layout.bounds;
2458
- var _x = (this.size.w - bounds.e - bounds.w)/2;
2569
+ var _x = (this.size.w - bounds.e - bounds.w) / 2;
2459
2570
  var _y = this.size.h / 2;
2460
- return{x:_x, y:_y};
2571
+ return { x: _x, y: _y };
2461
2572
  },
2462
2573
 
2463
- resize:function(){
2464
- this.e_canvas.width = 1;
2465
- this.e_canvas.height = 1;
2574
+ resize: function () {
2575
+ this.graph.set_size(1, 1);
2466
2576
  this.e_nodes.style.width = '1px';
2467
2577
  this.e_nodes.style.height = '1px';
2468
2578
 
@@ -2470,31 +2580,30 @@
2470
2580
  this._show();
2471
2581
  },
2472
2582
 
2473
- _show:function(){
2474
- this.e_canvas.width = this.size.w;
2475
- this.e_canvas.height = this.size.h;
2476
- this.e_nodes.style.width = this.size.w+'px';
2477
- this.e_nodes.style.height = this.size.h+'px';
2583
+ _show: function () {
2584
+ this.graph.set_size(this.size.w, this.size.h);
2585
+ this.e_nodes.style.width = this.size.w + 'px';
2586
+ this.e_nodes.style.height = this.size.h + 'px';
2478
2587
  this.show_nodes();
2479
2588
  this.show_lines();
2480
2589
  //this.layout.cache_valid = true;
2481
- this.jm.invoke_event_handle(jm.event_type.resize,{data:[]});
2590
+ this.jm.invoke_event_handle(jm.event_type.resize, { data: [] });
2482
2591
  },
2483
2592
 
2484
- zoomIn: function() {
2593
+ zoomIn: function () {
2485
2594
  return this.setZoom(this.actualZoom + this.zoomStep);
2486
2595
  },
2487
2596
 
2488
- zoomOut: function() {
2597
+ zoomOut: function () {
2489
2598
  return this.setZoom(this.actualZoom - this.zoomStep);
2490
2599
  },
2491
2600
 
2492
- setZoom: function(zoom) {
2601
+ setZoom: function (zoom) {
2493
2602
  if ((zoom < this.minZoom) || (zoom > this.maxZoom)) {
2494
2603
  return false;
2495
2604
  }
2496
2605
  this.actualZoom = zoom;
2497
- for (var i=0; i < this.e_panel.children.length; i++) {
2606
+ for (var i = 0; i < this.e_panel.children.length; i++) {
2498
2607
  this.e_panel.children[i].style.transform = 'scale(' + zoom + ')';
2499
2608
  };
2500
2609
  this.show(true);
@@ -2502,55 +2611,55 @@
2502
2611
 
2503
2612
  },
2504
2613
 
2505
- _center_root:function(){
2614
+ _center_root: function () {
2506
2615
  // center root node
2507
2616
  var outer_w = this.e_panel.clientWidth;
2508
2617
  var outer_h = this.e_panel.clientHeight;
2509
- if(this.size.w > outer_w){
2618
+ if (this.size.w > outer_w) {
2510
2619
  var _offset = this.get_view_offset();
2511
- this.e_panel.scrollLeft = _offset.x - outer_w/2;
2620
+ this.e_panel.scrollLeft = _offset.x - outer_w / 2;
2512
2621
  }
2513
- if(this.size.h > outer_h){
2514
- this.e_panel.scrollTop = (this.size.h - outer_h)/2;
2622
+ if (this.size.h > outer_h) {
2623
+ this.e_panel.scrollTop = (this.size.h - outer_h) / 2;
2515
2624
  }
2516
2625
  },
2517
2626
 
2518
- show:function(keep_center){
2627
+ show: function (keep_center) {
2519
2628
  logger.debug('view.show');
2520
2629
  this.expand_size();
2521
2630
  this._show();
2522
- if(!!keep_center){
2631
+ if (!!keep_center) {
2523
2632
  this._center_root();
2524
2633
  }
2525
2634
  },
2526
2635
 
2527
- relayout:function(){
2636
+ relayout: function () {
2528
2637
  this.expand_size();
2529
2638
  this._show();
2530
2639
  },
2531
2640
 
2532
- save_location:function(node){
2641
+ save_location: function (node) {
2533
2642
  var vd = node._data.view;
2534
- vd._saved_location={
2535
- x:parseInt(vd.element.style.left)-this.e_panel.scrollLeft,
2536
- y:parseInt(vd.element.style.top)-this.e_panel.scrollTop,
2643
+ vd._saved_location = {
2644
+ x: parseInt(vd.element.style.left) - this.e_panel.scrollLeft,
2645
+ y: parseInt(vd.element.style.top) - this.e_panel.scrollTop,
2537
2646
  };
2538
2647
  },
2539
2648
 
2540
- restore_location:function(node){
2649
+ restore_location: function (node) {
2541
2650
  var vd = node._data.view;
2542
- this.e_panel.scrollLeft = parseInt(vd.element.style.left)-vd._saved_location.x;
2543
- this.e_panel.scrollTop = parseInt(vd.element.style.top)-vd._saved_location.y;
2651
+ this.e_panel.scrollLeft = parseInt(vd.element.style.left) - vd._saved_location.x;
2652
+ this.e_panel.scrollTop = parseInt(vd.element.style.top) - vd._saved_location.y;
2544
2653
  },
2545
2654
 
2546
- clear_nodes:function(){
2655
+ clear_nodes: function () {
2547
2656
  var mind = this.jm.mind;
2548
- if(mind == null){
2657
+ if (mind == null) {
2549
2658
  return;
2550
2659
  }
2551
2660
  var nodes = mind.nodes;
2552
2661
  var node = null;
2553
- for(var nodeid in nodes){
2662
+ for (var nodeid in nodes) {
2554
2663
  node = nodes[nodeid];
2555
2664
  node._data.view.element = null;
2556
2665
  node._data.view.expander = null;
@@ -2558,22 +2667,22 @@
2558
2667
  this.e_nodes.innerHTML = '';
2559
2668
  },
2560
2669
 
2561
- show_nodes:function(){
2670
+ show_nodes: function () {
2562
2671
  var nodes = this.jm.mind.nodes;
2563
2672
  var node = null;
2564
2673
  var node_element = null;
2565
2674
  var expander = null;
2566
2675
  var p = null;
2567
- var p_expander= null;
2676
+ var p_expander = null;
2568
2677
  var expander_text = '-';
2569
2678
  var view_data = null;
2570
2679
  var _offset = this.get_view_offset();
2571
- for(var nodeid in nodes){
2680
+ for (var nodeid in nodes) {
2572
2681
  node = nodes[nodeid];
2573
2682
  view_data = node._data.view;
2574
2683
  node_element = view_data.element;
2575
2684
  expander = view_data.expander;
2576
- if(!this.layout.is_visible(node)){
2685
+ if (!this.layout.is_visible(node)) {
2577
2686
  node_element.style.display = 'none';
2578
2687
  expander.style.display = 'none';
2579
2688
  continue;
@@ -2582,139 +2691,171 @@
2582
2691
  p = this.layout.get_node_point(node);
2583
2692
  view_data.abs_x = _offset.x + p.x;
2584
2693
  view_data.abs_y = _offset.y + p.y;
2585
- node_element.style.left = (_offset.x+p.x) + 'px';
2586
- node_element.style.top = (_offset.y+p.y) + 'px';
2694
+ node_element.style.left = (_offset.x + p.x) + 'px';
2695
+ node_element.style.top = (_offset.y + p.y) + 'px';
2587
2696
  node_element.style.display = '';
2588
2697
  node_element.style.visibility = 'visible';
2589
- if(!node.isroot && node.children.length>0){
2590
- expander_text = node.expanded?'-':'+';
2591
- p_expander= this.layout.get_expander_point(node);
2698
+ if (!node.isroot && node.children.length > 0) {
2699
+ expander_text = node.expanded ? '-' : '+';
2700
+ p_expander = this.layout.get_expander_point(node);
2592
2701
  expander.style.left = (_offset.x + p_expander.x) + 'px';
2593
2702
  expander.style.top = (_offset.y + p_expander.y) + 'px';
2594
2703
  expander.style.display = '';
2595
2704
  expander.style.visibility = 'visible';
2596
- $t(expander,expander_text);
2705
+ $t(expander, expander_text);
2597
2706
  }
2598
2707
  // hide expander while all children have been removed
2599
- if(!node.isroot && node.children.length==0){
2708
+ if (!node.isroot && node.children.length == 0) {
2600
2709
  expander.style.display = 'none';
2601
2710
  expander.style.visibility = 'hidden';
2602
2711
  }
2603
2712
  }
2604
2713
  },
2605
2714
 
2606
- reset_node_custom_style:function(node){
2715
+ reset_node_custom_style: function (node) {
2607
2716
  this._reset_node_custom_style(node._data.view.element, node.data);
2608
2717
  },
2609
2718
 
2610
- _reset_node_custom_style:function(node_element, node_data){
2611
- if('background-color' in node_data){
2719
+ _reset_node_custom_style: function (node_element, node_data) {
2720
+ if ('background-color' in node_data) {
2612
2721
  node_element.style.backgroundColor = node_data['background-color'];
2613
2722
  }
2614
- if('foreground-color' in node_data){
2723
+ if ('foreground-color' in node_data) {
2615
2724
  node_element.style.color = node_data['foreground-color'];
2616
2725
  }
2617
- if('width' in node_data){
2618
- node_element.style.width = node_data['width']+'px';
2726
+ if ('width' in node_data) {
2727
+ node_element.style.width = node_data['width'] + 'px';
2619
2728
  }
2620
- if('height' in node_data){
2621
- node_element.style.height = node_data['height']+'px';
2729
+ if ('height' in node_data) {
2730
+ node_element.style.height = node_data['height'] + 'px';
2622
2731
  }
2623
- if('font-size' in node_data){
2624
- node_element.style.fontSize = node_data['font-size']+'px';
2732
+ if ('font-size' in node_data) {
2733
+ node_element.style.fontSize = node_data['font-size'] + 'px';
2625
2734
  }
2626
- if('font-weight' in node_data){
2735
+ if ('font-weight' in node_data) {
2627
2736
  node_element.style.fontWeight = node_data['font-weight'];
2628
2737
  }
2629
- if('font-style' in node_data){
2738
+ if ('font-style' in node_data) {
2630
2739
  node_element.style.fontStyle = node_data['font-style'];
2631
2740
  }
2632
- if('background-image' in node_data) {
2741
+ if ('background-image' in node_data) {
2633
2742
  var backgroundImage = node_data['background-image'];
2634
2743
  if (backgroundImage.startsWith('data') && node_data['width'] && node_data['height']) {
2635
2744
  var img = new Image();
2636
2745
 
2637
- img.onload = function() {
2746
+ img.onload = function () {
2638
2747
  var c = $c('canvas');
2639
2748
  c.width = node_element.clientWidth;
2640
2749
  c.height = node_element.clientHeight;
2641
2750
  var img = this;
2642
- if(c.getContext) {
2751
+ if (c.getContext) {
2643
2752
  var ctx = c.getContext('2d');
2644
2753
  ctx.drawImage(img, 2, 2, node_element.clientWidth, node_element.clientHeight);
2645
2754
  var scaledImageData = c.toDataURL();
2646
- node_element.style.backgroundImage='url('+scaledImageData+')';
2755
+ node_element.style.backgroundImage = 'url(' + scaledImageData + ')';
2647
2756
  }
2648
2757
  };
2649
2758
  img.src = backgroundImage;
2650
2759
 
2651
2760
  } else {
2652
- node_element.style.backgroundImage='url('+backgroundImage+')';
2761
+ node_element.style.backgroundImage = 'url(' + backgroundImage + ')';
2653
2762
  }
2654
- node_element.style.backgroundSize='99%';
2763
+ node_element.style.backgroundSize = '99%';
2655
2764
 
2656
- if('background-rotation' in node_data){
2765
+ if ('background-rotation' in node_data) {
2657
2766
  node_element.style.transform = 'rotate(' + node_data['background-rotation'] + 'deg)';
2658
2767
  }
2659
-
2660
2768
  }
2661
2769
  },
2662
2770
 
2663
- clear_node_custom_style:function(node){
2771
+ clear_node_custom_style: function (node) {
2664
2772
  var node_element = node._data.view.element;
2665
2773
  node_element.style.backgroundColor = "";
2666
2774
  node_element.style.color = "";
2667
2775
  },
2668
2776
 
2669
- clear_lines:function(canvas_ctx){
2670
- var ctx = canvas_ctx || this.canvas_ctx;
2671
- jm.util.canvas.clear(ctx,0,0,this.size.w,this.size.h);
2777
+ clear_lines: function () {
2778
+ this.graph.clear();
2672
2779
  },
2673
2780
 
2674
- show_lines:function(canvas_ctx){
2675
- this.clear_lines(canvas_ctx);
2781
+ show_lines: function () {
2782
+ this.clear_lines();
2676
2783
  var nodes = this.jm.mind.nodes;
2677
2784
  var node = null;
2678
2785
  var pin = null;
2679
2786
  var pout = null;
2680
2787
  var _offset = this.get_view_offset();
2681
- for(var nodeid in nodes){
2788
+ for (var nodeid in nodes) {
2682
2789
  node = nodes[nodeid];
2683
- if(!!node.isroot){continue;}
2684
- if(('visible' in node._data.layout) && !node._data.layout.visible){continue;}
2790
+ if (!!node.isroot) { continue; }
2791
+ if (('visible' in node._data.layout) && !node._data.layout.visible) { continue; }
2685
2792
  pin = this.layout.get_node_point_in(node);
2686
2793
  pout = this.layout.get_node_point_out(node.parent);
2687
- this.draw_line(pout,pin,_offset,canvas_ctx);
2794
+ this.graph.draw_line(pout, pin, _offset);
2795
+ }
2796
+ },
2797
+
2798
+ // Drag the whole mind map with your mouse (usefull when it's larger that the container).
2799
+ draggable_canvas: function () {
2800
+ // If draggable option is true.
2801
+ if (this.opts.draggable) {
2802
+ // Dragging disabled by default.
2803
+ let dragging = false
2804
+ let x, y
2805
+ if (this.opts.hide_scrollbars_when_draggable) {
2806
+ // Avoid scrollbars when mind map is larger than the container (e_panel = id jsmind-inner)
2807
+ this.e_panel.style = 'overflow: hidden'
2808
+ }
2809
+ // Move the whole mind map with mouse moves, while button is down.
2810
+ jm.util.dom.add_event(this.container, 'mousedown', (eventDown) => {
2811
+ dragging = true
2812
+ // Record current mouse position.
2813
+ x = eventDown.clientX
2814
+ y = eventDown.clientY
2815
+ })
2816
+ // Stop moving mind map once mouse button is released.
2817
+ jm.util.dom.add_event(this.container, 'mouseup', () => {
2818
+ dragging = false
2819
+ })
2820
+ // Follow current mouse position and move mind map accordingly.
2821
+ jm.util.dom.add_event(this.container, 'mousemove', (eventMove) => {
2822
+ if (this.dragging_enabled && dragging) {
2823
+ this.e_panel.scrollBy(x - eventMove.clientX, y - eventMove.clientY)
2824
+ // Record new current position.
2825
+ x = eventMove.clientX
2826
+ y = eventMove.clientY
2827
+ }
2828
+ })
2688
2829
  }
2689
2830
  },
2690
2831
 
2691
- draw_line:function(pin,pout,offset,canvas_ctx){
2692
- var ctx = canvas_ctx || this.canvas_ctx;
2693
- ctx.strokeStyle = this.opts.line_color;
2694
- ctx.lineWidth = this.opts.line_width;
2695
- ctx.lineCap = 'round';
2832
+ get_draggable_canvas: function () {
2833
+ return this.opts.draggable
2834
+ },
2696
2835
 
2697
- jm.util.canvas.bezierto(
2698
- ctx,
2699
- pin.x + offset.x,
2700
- pin.y + offset.y,
2701
- pout.x + offset.x,
2702
- pout.y + offset.y);
2836
+ enable_draggable_canvas: function () {
2837
+ this.dragging_enabled = true
2838
+ },
2839
+
2840
+ disable_draggable_canvas: function () {
2841
+ this.dragging_enabled = false
2703
2842
  },
2843
+
2704
2844
  };
2705
2845
 
2706
2846
  // shortcut provider
2707
- jm.shortcut_provider= function(jm, options){
2847
+ jm.shortcut_provider = function (jm, options) {
2708
2848
  this.jm = jm;
2709
2849
  this.opts = options;
2710
2850
  this.mapping = options.mapping;
2711
2851
  this.handles = options.handles;
2852
+ this._newid = null;
2712
2853
  this._mapping = {};
2713
2854
  };
2714
2855
 
2715
2856
  jm.shortcut_provider.prototype = {
2716
- init : function(){
2717
- jm.util.dom.add_event($d,'keydown',this.handler.bind(this));
2857
+ init: function () {
2858
+ jm.util.dom.add_event(this.jm.view.e_panel, 'keydown', this.handler.bind(this));
2718
2859
 
2719
2860
  this.handles['addchild'] = this.handle_addchild;
2720
2861
  this.handles['addbrother'] = this.handle_addbrother;
@@ -2726,87 +2867,94 @@
2726
2867
  this.handles['left'] = this.handle_left;
2727
2868
  this.handles['right'] = this.handle_right;
2728
2869
 
2729
- for(var handle in this.mapping){
2730
- if(!!this.mapping[handle] && (handle in this.handles)){
2870
+ for (var handle in this.mapping) {
2871
+ if (!!this.mapping[handle] && (handle in this.handles)) {
2731
2872
  this._mapping[this.mapping[handle]] = this.handles[handle];
2732
2873
  }
2733
2874
  }
2875
+
2876
+ if (typeof this.opts.id_generator === 'function') {
2877
+ this._newid = this.opts.id_generator;
2878
+ } else {
2879
+ this._newid = jm.util.uuid.newid;
2880
+ }
2734
2881
  },
2735
2882
 
2736
- enable_shortcut : function(){
2883
+ enable_shortcut: function () {
2737
2884
  this.opts.enable = true;
2738
2885
  },
2739
2886
 
2740
- disable_shortcut : function(){
2887
+ disable_shortcut: function () {
2741
2888
  this.opts.enable = false;
2742
2889
  },
2743
2890
 
2744
- handler : function(e){
2745
- if(this.jm.view.is_editing()){return;}
2891
+ handler: function (e) {
2892
+ if (e.which == 9) { e.preventDefault(); } //prevent tab to change focus in browser
2893
+ if (this.jm.view.is_editing()) { return; }
2746
2894
  var evt = e || event;
2747
- if(!this.opts.enable){return true;}
2748
- var kc = evt.keyCode;
2749
- if(kc in this._mapping){
2750
- this._mapping[kc].call(this,this.jm,e);
2895
+ if (!this.opts.enable) { return true; }
2896
+ var kc = evt.keyCode + (evt.metaKey << 13) + (evt.ctrlKey << 12) + (evt.altKey << 11) + (evt.shiftKey << 10);
2897
+ if (kc in this._mapping) {
2898
+ this._mapping[kc].call(this, this.jm, e);
2751
2899
  }
2752
2900
  },
2753
2901
 
2754
- handle_addchild: function(_jm,e){
2902
+ handle_addchild: function (_jm, e) {
2755
2903
  var selected_node = _jm.get_selected_node();
2756
- if(!!selected_node){
2757
- var nodeid = jm.util.uuid.newid();
2904
+ if (!!selected_node) {
2905
+ var nodeid = this._newid();
2758
2906
  var node = _jm.add_node(selected_node, nodeid, 'New Node');
2759
- if(!!node){
2907
+ if (!!node) {
2760
2908
  _jm.select_node(nodeid);
2761
2909
  _jm.begin_edit(nodeid);
2762
2910
  }
2763
2911
  }
2764
2912
  },
2765
- handle_addbrother:function(_jm,e){
2913
+ handle_addbrother: function (_jm, e) {
2766
2914
  var selected_node = _jm.get_selected_node();
2767
- if(!!selected_node && !selected_node.isroot){
2768
- var nodeid = jm.util.uuid.newid();
2915
+ if (!!selected_node && !selected_node.isroot) {
2916
+ var nodeid = this._newid();
2769
2917
  var node = _jm.insert_node_after(selected_node, nodeid, 'New Node');
2770
- if(!!node){
2918
+ if (!!node) {
2771
2919
  _jm.select_node(nodeid);
2772
2920
  _jm.begin_edit(nodeid);
2773
2921
  }
2774
2922
  }
2775
2923
  },
2776
- handle_editnode:function(_jm,e){
2924
+ handle_editnode: function (_jm, e) {
2777
2925
  var selected_node = _jm.get_selected_node();
2778
- if(!!selected_node){
2926
+ if (!!selected_node) {
2779
2927
  _jm.begin_edit(selected_node);
2780
2928
  }
2781
2929
  },
2782
- handle_delnode:function(_jm,e){
2930
+ handle_delnode: function (_jm, e) {
2783
2931
  var selected_node = _jm.get_selected_node();
2784
- if(!!selected_node && !selected_node.isroot){
2932
+ if (!!selected_node && !selected_node.isroot) {
2785
2933
  _jm.select_node(selected_node.parent);
2786
2934
  _jm.remove_node(selected_node);
2787
2935
  }
2788
2936
  },
2789
- handle_toggle:function(_jm,e){
2937
+ handle_toggle: function (_jm, e) {
2790
2938
  var evt = e || event;
2791
2939
  var selected_node = _jm.get_selected_node();
2792
- if(!!selected_node){
2940
+ if (!!selected_node) {
2793
2941
  _jm.toggle_node(selected_node.id);
2794
2942
  evt.stopPropagation();
2795
2943
  evt.preventDefault();
2796
2944
  }
2797
2945
  },
2798
- handle_up:function(_jm,e){
2946
+ handle_up: function (_jm, e) {
2799
2947
  var evt = e || event;
2800
2948
  var selected_node = _jm.get_selected_node();
2801
- if(!!selected_node){
2949
+ if (!!selected_node) {
2802
2950
  var up_node = _jm.find_node_before(selected_node);
2803
- if(!up_node){
2951
+ if (!up_node) {
2804
2952
  var np = _jm.find_node_before(selected_node.parent);
2805
- if(!!np && np.children.length > 0){
2806
- up_node = np.children[np.children.length-1];
2953
+ if (!!np && np.children.length > 0) {
2954
+ up_node = np.children[np.children.length - 1];
2807
2955
  }
2808
2956
  }
2809
- if(!!up_node){
2957
+ if (!!up_node) {
2810
2958
  _jm.select_node(up_node);
2811
2959
  }
2812
2960
  evt.stopPropagation();
@@ -2814,18 +2962,18 @@
2814
2962
  }
2815
2963
  },
2816
2964
 
2817
- handle_down:function(_jm,e){
2965
+ handle_down: function (_jm, e) {
2818
2966
  var evt = e || event;
2819
2967
  var selected_node = _jm.get_selected_node();
2820
- if(!!selected_node){
2968
+ if (!!selected_node) {
2821
2969
  var down_node = _jm.find_node_after(selected_node);
2822
- if(!down_node){
2970
+ if (!down_node) {
2823
2971
  var np = _jm.find_node_after(selected_node.parent);
2824
- if(!!np && np.children.length > 0){
2972
+ if (!!np && np.children.length > 0) {
2825
2973
  down_node = np.children[0];
2826
2974
  }
2827
2975
  }
2828
- if(!!down_node){
2976
+ if (!!down_node) {
2829
2977
  _jm.select_node(down_node);
2830
2978
  }
2831
2979
  evt.stopPropagation();
@@ -2833,37 +2981,37 @@
2833
2981
  }
2834
2982
  },
2835
2983
 
2836
- handle_left:function(_jm,e){
2837
- this._handle_direction(_jm,e,jm.direction.left);
2984
+ handle_left: function (_jm, e) {
2985
+ this._handle_direction(_jm, e, jm.direction.left);
2838
2986
  },
2839
- handle_right:function(_jm,e){
2840
- this._handle_direction(_jm,e,jm.direction.right);
2987
+ handle_right: function (_jm, e) {
2988
+ this._handle_direction(_jm, e, jm.direction.right);
2841
2989
  },
2842
- _handle_direction:function(_jm,e,d){
2990
+ _handle_direction: function (_jm, e, d) {
2843
2991
  var evt = e || event;
2844
2992
  var selected_node = _jm.get_selected_node();
2845
2993
  var node = null;
2846
- if(!!selected_node){
2847
- if(selected_node.isroot){
2994
+ if (!!selected_node) {
2995
+ if (selected_node.isroot) {
2848
2996
  var c = selected_node.children;
2849
2997
  var children = [];
2850
- for(var i=0;i<c.length;i++){
2851
- if(c[i].direction === d){
2852
- children.push(i)
2998
+ for (var i = 0; i < c.length; i++) {
2999
+ if (c[i].direction === d) {
3000
+ children.push(i);
2853
3001
  }
2854
3002
  }
2855
- node = c[children[Math.floor((children.length-1)/2)]];
3003
+ node = c[children[Math.floor((children.length - 1) / 2)]];
2856
3004
  }
2857
- else if(selected_node.direction === d){
3005
+ else if (selected_node.direction === d) {
2858
3006
  var children = selected_node.children;
2859
3007
  var childrencount = children.length;
2860
- if(childrencount > 0){
2861
- node = children[Math.floor((childrencount-1)/2)]
3008
+ if (childrencount > 0) {
3009
+ node = children[Math.floor((childrencount - 1) / 2)];
2862
3010
  }
2863
- }else{
3011
+ } else {
2864
3012
  node = selected_node.parent;
2865
3013
  }
2866
- if(!!node){
3014
+ if (!!node) {
2867
3015
  _jm.select_node(node);
2868
3016
  }
2869
3017
  evt.stopPropagation();
@@ -2874,38 +3022,38 @@
2874
3022
 
2875
3023
 
2876
3024
  // plugin
2877
- jm.plugin = function(name,init){
3025
+ jm.plugin = function (name, init) {
2878
3026
  this.name = name;
2879
3027
  this.init = init;
2880
3028
  };
2881
3029
 
2882
3030
  jm.plugins = [];
2883
3031
 
2884
- jm.register_plugin = function(plugin){
2885
- if(plugin instanceof jm.plugin){
3032
+ jm.register_plugin = function (plugin) {
3033
+ if (plugin instanceof jm.plugin) {
2886
3034
  jm.plugins.push(plugin);
2887
3035
  }
2888
3036
  };
2889
3037
 
2890
- jm.init_plugins = function(sender){
2891
- $w.setTimeout(function(){
3038
+ jm.init_plugins = function (sender) {
3039
+ $w.setTimeout(function () {
2892
3040
  jm._init_plugins(sender);
2893
- },0);
3041
+ }, 0);
2894
3042
  };
2895
3043
 
2896
- jm._init_plugins = function(sender){
3044
+ jm._init_plugins = function (sender) {
2897
3045
  var l = jm.plugins.length;
2898
3046
  var fn_init = null;
2899
- for(var i=0;i<l;i++){
3047
+ for (var i = 0; i < l; i++) {
2900
3048
  fn_init = jm.plugins[i].init;
2901
- if(typeof fn_init === 'function'){
3049
+ if (typeof fn_init === 'function') {
2902
3050
  fn_init(sender);
2903
3051
  }
2904
3052
  }
2905
3053
  };
2906
3054
 
2907
3055
  // quick way
2908
- jm.show = function(options,mind){
3056
+ jm.show = function (options, mind) {
2909
3057
  var _jm = new jm(options);
2910
3058
  _jm.show(mind);
2911
3059
  return _jm;
@@ -2915,9 +3063,8 @@
2915
3063
  if (typeof module !== 'undefined' && typeof exports === 'object') {
2916
3064
  module.exports = jm;
2917
3065
  } else if (typeof define === 'function' && (define.amd || define.cmd)) {
2918
- define(function() { return jm; });
3066
+ define(function () { return jm; });
2919
3067
  } else {
2920
3068
  $w[__name__] = jm;
2921
3069
  }
2922
3070
  })(typeof window !== 'undefined' ? window : global);
2923
-