You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

351 lines
13 KiB

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