jsmind 0.4.6 → 0.4.8

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.
@@ -1,35 +1,33 @@
1
1
  /*
2
2
  * Released under BSD License
3
- * Copyright (c) 2014-2015 hizzgdev@163.com
3
+ * Copyright (c) 2014-2021 hizzgdev@163.com
4
4
  *
5
5
  * Project Home:
6
6
  * https://github.com/hizzgdev/jsmind/
7
7
  */
8
8
 
9
- (function($w){
9
+ (function ($w) {
10
10
  'use strict';
11
11
  var $d = $w.document;
12
12
  var __name__ = 'jsMind';
13
13
  var jsMind = $w[__name__];
14
- if(!jsMind){return;}
15
- if(typeof jsMind.draggable != 'undefined'){return;}
14
+ if (!jsMind) { return; }
15
+ if (typeof jsMind.draggable != 'undefined') { return; }
16
16
 
17
17
  var jdom = jsMind.util.dom;
18
- var jcanvas = jsMind.util.canvas;
19
-
20
- var clear_selection = 'getSelection' in $w ? function(){
21
- $w.getSelection().removeAllRanges();
22
- } : function(){
23
- $d.selection.empty();
18
+ var clear_selection = 'getSelection' in $w ? function () {
19
+ $w.getSelection().removeAllRanges();
20
+ } : function () {
21
+ $d.selection.empty();
24
22
  };
25
23
 
26
24
  var options = {
27
- line_width : 5,
28
- lookup_delay : 500,
29
- lookup_interval : 80
25
+ line_width: 5,
26
+ lookup_delay: 500,
27
+ lookup_interval: 80
30
28
  };
31
29
 
32
- jsMind.draggable = function(jm){
30
+ jsMind.draggable = function (jm) {
33
31
  this.jm = jm;
34
32
  this.e_canvas = null;
35
33
  this.canvas_ctx = null;
@@ -50,19 +48,19 @@
50
48
  };
51
49
 
52
50
  jsMind.draggable.prototype = {
53
- init:function(){
51
+ init: function () {
54
52
  this._create_canvas();
55
53
  this._create_shadow();
56
54
  this._event_bind();
57
55
  },
58
56
 
59
- resize:function(){
57
+ resize: function () {
60
58
  this.jm.view.e_nodes.appendChild(this.shadow);
61
- this.e_canvas.width=this.jm.view.size.w;
62
- this.e_canvas.height=this.jm.view.size.h;
59
+ this.e_canvas.width = this.jm.view.size.w;
60
+ this.e_canvas.height = this.jm.view.size.h;
63
61
  },
64
62
 
65
- _create_canvas:function(){
63
+ _create_canvas: function () {
66
64
  var c = $d.createElement('canvas');
67
65
  this.jm.view.e_panel.appendChild(c);
68
66
  var ctx = c.getContext('2d');
@@ -70,16 +68,16 @@
70
68
  this.canvas_ctx = ctx;
71
69
  },
72
70
 
73
- _create_shadow:function(){
71
+ _create_shadow: function () {
74
72
  var s = $d.createElement('jmnode');
75
73
  s.style.visibility = 'hidden';
76
74
  s.style.zIndex = '3';
77
75
  s.style.cursor = 'move';
78
- s.style.opacity= '0.7';
76
+ s.style.opacity = '0.7';
79
77
  this.shadow = s;
80
78
  },
81
79
 
82
- reset_shadow:function(el){
80
+ reset_shadow: function (el) {
83
81
  var s = this.shadow.style;
84
82
  this.shadow.innerHTML = el.innerHTML;
85
83
  s.left = el.style.left;
@@ -94,77 +92,84 @@
94
92
 
95
93
  },
96
94
 
97
- show_shadow:function(){
98
- if(!this.moved){
95
+ show_shadow: function () {
96
+ if (!this.moved) {
99
97
  this.shadow.style.visibility = 'visible';
100
98
  }
101
99
  },
102
100
 
103
- hide_shadow:function(){
101
+ hide_shadow: function () {
104
102
  this.shadow.style.visibility = 'hidden';
105
103
  },
106
104
 
107
- clear_lines:function(){
108
- jcanvas.clear(this.canvas_ctx, 0, 0, this.jm.view.size.w, this.jm.view.size.h);
109
- },
110
-
111
- _magnet_shadow:function(node){
112
- if(!!node){
105
+ _magnet_shadow: function (node) {
106
+ if (!!node) {
113
107
  this.canvas_ctx.lineWidth = options.line_width;
114
108
  this.canvas_ctx.strokeStyle = 'rgba(0,0,0,0.3)';
115
109
  this.canvas_ctx.lineCap = 'round';
116
- this.clear_lines();
117
- jcanvas.lineto(this.canvas_ctx,
118
- node.sp.x,
119
- node.sp.y,
120
- node.np.x,
121
- node.np.y);
110
+ this._clear_lines();
111
+ this._canvas_lineto(node.sp.x, node.sp.y, node.np.x, node.np.y);
122
112
  }
123
113
  },
124
114
 
125
- _lookup_close_node:function(){
115
+ _clear_lines: function () {
116
+ this.canvas_ctx.clearRect(0, 0, this.jm.view.size.w, this.jm.view.size.h);
117
+ },
118
+
119
+ _canvas_lineto: function (x1, y1, x2, y2) {
120
+ this.canvas_ctx.beginPath();
121
+ this.canvas_ctx.moveTo(x1, y1);
122
+ this.canvas_ctx.lineTo(x2, y2);
123
+ this.canvas_ctx.stroke();
124
+ },
125
+
126
+ _lookup_close_node: function () {
126
127
  var root = this.jm.get_root();
127
128
  var root_location = root.get_location();
128
129
  var root_size = root.get_size();
129
- var root_x = root_location.x + root_size.w/2;
130
+ var root_x = root_location.x + root_size.w / 2;
130
131
 
131
132
  var sw = this.shadow_w;
132
133
  var sh = this.shadow_h;
133
134
  var sx = this.shadow.offsetLeft;
134
135
  var sy = this.shadow.offsetTop;
135
136
 
136
- var ns,nl;
137
+ var ns, nl;
137
138
 
138
- var direct = (sx + sw/2)>=root_x ?
139
- jsMind.direction.right : jsMind.direction.left;
139
+ var direct = (sx + sw / 2) >= root_x ?
140
+ jsMind.direction.right : jsMind.direction.left;
140
141
  var nodes = this.jm.mind.nodes;
141
142
  var node = null;
143
+ var layout = this.jm.layout;
142
144
  var min_distance = Number.MAX_VALUE;
143
145
  var distance = 0;
144
146
  var closest_node = null;
145
147
  var closest_p = null;
146
148
  var shadow_p = null;
147
- for(var nodeid in nodes){
148
- var np,sp;
149
+ for (var nodeid in nodes) {
150
+ var np, sp;
149
151
  node = nodes[nodeid];
150
- if(node.isroot || node.direction == direct){
151
- if(node.id == this.active_node.id){
152
+ if (node.isroot || node.direction == direct) {
153
+ if (node.id == this.active_node.id) {
154
+ continue;
155
+ }
156
+ if (!layout.is_visible(node)) {
152
157
  continue;
153
158
  }
154
159
  ns = node.get_size();
155
160
  nl = node.get_location();
156
- if(direct == jsMind.direction.right){
157
- if(sx-nl.x-ns.w<=0){continue;}
158
- distance = Math.abs(sx-nl.x-ns.w) + Math.abs(sy+sh/2-nl.y-ns.h/2);
159
- np = {x:nl.x+ns.w-options.line_width,y:nl.y+ns.h/2};
160
- sp = {x:sx+options.line_width,y:sy+sh/2};
161
- }else{
162
- if(nl.x-sx-sw<=0){continue;}
163
- distance = Math.abs(sx+sw-nl.x) + Math.abs(sy+sh/2-nl.y-ns.h/2);
164
- np = {x:nl.x+options.line_width,y:nl.y+ns.h/2};
165
- sp = {x:sx+sw-options.line_width,y:sy+sh/2};
161
+ if (direct == jsMind.direction.right) {
162
+ if (sx - nl.x - ns.w <= 0) { continue; }
163
+ distance = Math.abs(sx - nl.x - ns.w) + Math.abs(sy + sh / 2 - nl.y - ns.h / 2);
164
+ np = { x: nl.x + ns.w - options.line_width, y: nl.y + ns.h / 2 };
165
+ sp = { x: sx + options.line_width, y: sy + sh / 2 };
166
+ } else {
167
+ if (nl.x - sx - sw <= 0) { continue; }
168
+ distance = Math.abs(sx + sw - nl.x) + Math.abs(sy + sh / 2 - nl.y - ns.h / 2);
169
+ np = { x: nl.x + options.line_width, y: nl.y + ns.h / 2 };
170
+ sp = { x: sx + sw - options.line_width, y: sy + sh / 2 };
166
171
  }
167
- if(distance < min_distance){
172
+ if (distance < min_distance) {
168
173
  closest_node = node;
169
174
  closest_p = np;
170
175
  shadow_p = sp;
@@ -173,126 +178,125 @@
173
178
  }
174
179
  }
175
180
  var result_node = null;
176
- if(!!closest_node){
181
+ if (!!closest_node) {
177
182
  result_node = {
178
- node:closest_node,
179
- direction:direct,
180
- sp:shadow_p,
181
- np:closest_p
183
+ node: closest_node,
184
+ direction: direct,
185
+ sp: shadow_p,
186
+ np: closest_p
182
187
  };
183
188
  }
184
189
  return result_node;
185
190
  },
186
191
 
187
- lookup_close_node:function(){
192
+ lookup_close_node: function () {
188
193
  var node_data = this._lookup_close_node();
189
- if(!!node_data){
194
+ if (!!node_data) {
190
195
  this._magnet_shadow(node_data);
191
196
  this.target_node = node_data.node;
192
197
  this.target_direct = node_data.direction;
193
198
  }
194
199
  },
195
200
 
196
- _event_bind:function(){
201
+ _event_bind: function () {
197
202
  var jd = this;
198
203
  var container = this.jm.view.container;
199
- jdom.add_event(container,'mousedown',function(e){
204
+ jdom.add_event(container, 'mousedown', function (e) {
200
205
  var evt = e || event;
201
- jd.dragstart.call(jd,evt);
206
+ jd.dragstart.call(jd, evt);
202
207
  });
203
- jdom.add_event(container,'mousemove',function(e){
208
+ jdom.add_event(container, 'mousemove', function (e) {
204
209
  var evt = e || event;
205
- jd.drag.call(jd,evt);
210
+ jd.drag.call(jd, evt);
206
211
  });
207
- jdom.add_event(container,'mouseup',function(e){
212
+ jdom.add_event(container, 'mouseup', function (e) {
208
213
  var evt = e || event;
209
- jd.dragend.call(jd,evt);
214
+ jd.dragend.call(jd, evt);
210
215
  });
211
- jdom.add_event(container,'touchstart',function(e){
216
+ jdom.add_event(container, 'touchstart', function (e) {
212
217
  var evt = e || event;
213
- jd.dragstart.call(jd,evt);
218
+ jd.dragstart.call(jd, evt);
214
219
  });
215
- jdom.add_event(container,'touchmove',function(e){
220
+ jdom.add_event(container, 'touchmove', function (e) {
216
221
  var evt = e || event;
217
- jd.drag.call(jd,evt);
222
+ jd.drag.call(jd, evt);
218
223
  });
219
- jdom.add_event(container,'touchend',function(e){
224
+ jdom.add_event(container, 'touchend', function (e) {
220
225
  var evt = e || event;
221
- jd.dragend.call(jd,evt);
226
+ jd.dragend.call(jd, evt);
222
227
  });
223
228
  },
224
229
 
225
- dragstart:function(e){
226
- if(!this.jm.get_editable()){return;}
227
- if(this.capture){return;}
230
+ dragstart: function (e) {
231
+ if (!this.jm.get_editable()) { return; }
232
+ if (this.capture) { return; }
228
233
  this.active_node = null;
229
234
 
230
235
  var jview = this.jm.view;
231
236
  var el = e.target || event.srcElement;
232
- if(el.tagName.toLowerCase() != 'jmnode'){return;}
237
+ if (el.tagName.toLowerCase() != 'jmnode') { return; }
233
238
  var nodeid = jview.get_binded_nodeid(el);
234
- if(!!nodeid){
239
+ if (!!nodeid) {
235
240
  var node = this.jm.get_node(nodeid);
236
- if(!node.isroot){
241
+ if (!node.isroot) {
237
242
  this.reset_shadow(el);
238
243
  this.active_node = node;
239
- this.offset_x = (e.clientX || e.touches[0].clientX) - el.offsetLeft;
240
- this.offset_y = (e.clientY || e.touches[0].clientY) - el.offsetTop;
241
- this.client_hw = Math.floor(el.clientWidth/2);
242
- this.client_hh = Math.floor(el.clientHeight/2);
243
- if(this.hlookup_delay != 0){
244
+ this.offset_x = (e.clientX || e.touches[0].clientX) / jview.actualZoom - el.offsetLeft;
245
+ this.offset_y = (e.clientY || e.touches[0].clientY) / jview.actualZoom - el.offsetTop;
246
+ this.client_hw = Math.floor(el.clientWidth / 2);
247
+ this.client_hh = Math.floor(el.clientHeight / 2);
248
+ if (this.hlookup_delay != 0) {
244
249
  $w.clearTimeout(this.hlookup_delay);
245
250
  }
246
- if(this.hlookup_timer != 0){
251
+ if (this.hlookup_timer != 0) {
247
252
  $w.clearInterval(this.hlookup_timer);
248
253
  }
249
254
  var jd = this;
250
- this.hlookup_delay = $w.setTimeout(function(){
255
+ this.hlookup_delay = $w.setTimeout(function () {
251
256
  jd.hlookup_delay = 0;
252
- jd.hlookup_timer = $w.setInterval(function(){
257
+ jd.hlookup_timer = $w.setInterval(function () {
253
258
  jd.lookup_close_node.call(jd);
254
- },options.lookup_interval);
255
- },options.lookup_delay);
259
+ }, options.lookup_interval);
260
+ }, options.lookup_delay);
256
261
  this.capture = true;
257
262
  }
258
263
  }
259
264
  },
260
265
 
261
- drag:function(e){
262
- if(!this.jm.get_editable()){return;}
263
- if(this.capture){
266
+ drag: function (e) {
267
+ if (!this.jm.get_editable()) { return; }
268
+ if (this.capture) {
264
269
  e.preventDefault();
265
270
  this.show_shadow();
266
271
  this.moved = true;
267
272
  clear_selection();
268
- var px = (e.clientX || e.touches[0].clientX) - this.offset_x;
269
- var py = (e.clientY || e.touches[0].clientY) - this.offset_y;
270
- var cx = px + this.client_hw;
271
- var cy = py + this.client_hh;
273
+ var jview = this.jm.view;
274
+ var px = (e.clientX || e.touches[0].clientX) / jview.actualZoom - this.offset_x;
275
+ var py = (e.clientY || e.touches[0].clientY) / jview.actualZoom - this.offset_y;
272
276
  this.shadow.style.left = px + 'px';
273
277
  this.shadow.style.top = py + 'px';
274
278
  clear_selection();
275
279
  }
276
280
  },
277
281
 
278
- dragend:function(e){
279
- if(!this.jm.get_editable()){return;}
280
- if(this.capture){
281
- if(this.hlookup_delay != 0){
282
+ dragend: function (e) {
283
+ if (!this.jm.get_editable()) { return; }
284
+ if (this.capture) {
285
+ if (this.hlookup_delay != 0) {
282
286
  $w.clearTimeout(this.hlookup_delay);
283
287
  this.hlookup_delay = 0;
284
- this.clear_lines();
288
+ this._clear_lines();
285
289
  }
286
- if(this.hlookup_timer != 0){
290
+ if (this.hlookup_timer != 0) {
287
291
  $w.clearInterval(this.hlookup_timer);
288
292
  this.hlookup_timer = 0;
289
- this.clear_lines();
293
+ this._clear_lines();
290
294
  }
291
- if(this.moved){
295
+ if (this.moved) {
292
296
  var src_node = this.active_node;
293
297
  var target_node = this.target_node;
294
298
  var target_direct = this.target_direct;
295
- this.move_node(src_node,target_node,target_direct);
299
+ this.move_node(src_node, target_node, target_direct);
296
300
  }
297
301
  this.hide_shadow();
298
302
  }
@@ -300,9 +304,9 @@
300
304
  this.capture = false;
301
305
  },
302
306
 
303
- move_node:function(src_node,target_node,target_direct){
307
+ move_node: function (src_node, target_node, target_direct) {
304
308
  var shadow_h = this.shadow.offsetTop;
305
- if(!!target_node && !!src_node && !jsMind.node.inherited(src_node, target_node)){
309
+ if (!!target_node && !!src_node && !jsMind.node.inherited(src_node, target_node)) {
306
310
  // lookup before_node
307
311
  var sibling_nodes = target_node.children;
308
312
  var sc = sibling_nodes.length;
@@ -310,18 +314,18 @@
310
314
  var delta_y = Number.MAX_VALUE;
311
315
  var node_before = null;
312
316
  var beforeid = '_last_';
313
- while(sc--){
317
+ while (sc--) {
314
318
  node = sibling_nodes[sc];
315
- if(node.direction == target_direct && node.id != src_node.id){
319
+ if (node.direction == target_direct && node.id != src_node.id) {
316
320
  var dy = node.get_location().y - shadow_h;
317
- if(dy > 0 && dy < delta_y){
321
+ if (dy > 0 && dy < delta_y) {
318
322
  delta_y = dy;
319
323
  node_before = node;
320
324
  beforeid = '_first_';
321
325
  }
322
326
  }
323
327
  }
324
- if(!!node_before){beforeid = node_before.id;}
328
+ if (!!node_before) { beforeid = node_before.id; }
325
329
  this.jm.move_node(src_node.id, beforeid, target_node.id, target_direct);
326
330
  }
327
331
  this.active_node = null;
@@ -329,18 +333,18 @@
329
333
  this.target_direct = null;
330
334
  },
331
335
 
332
- jm_event_handle:function(type,data){
333
- if(type === jsMind.event_type.resize){
336
+ jm_event_handle: function (type, data) {
337
+ if (type === jsMind.event_type.resize) {
334
338
  this.resize();
335
339
  }
336
340
  }
337
341
  };
338
342
 
339
- var draggable_plugin = new jsMind.plugin('draggable',function(jm){
343
+ var draggable_plugin = new jsMind.plugin('draggable', function (jm) {
340
344
  var jd = new jsMind.draggable(jm);
341
345
  jd.init();
342
- jm.add_event_listener(function(type,data){
343
- jd.jm_event_handle.call(jd,type,data);
346
+ jm.add_event_listener(function (type, data) {
347
+ jd.jm_event_handle.call(jd, type, data);
344
348
  });
345
349
  });
346
350