connection_utils.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /* This file is part of AToMPM - A Tool for Multi-Paradigm Modelling
  2. * Copyright 2011 by the AToMPM team and licensed under the LGPL
  3. * See COPYING.lesser and README.md in the root of this project for full details
  4. */
  5. ConnectionUtils = function(){
  6. var connectionPathEditingOverlay = {};
  7. var currentControlPoint = undefined;
  8. var connectionSource = undefined;
  9. var connectionPath = undefined;
  10. this.getConnectionSource = function(){
  11. return connectionSource;
  12. };
  13. this.getConnectionPath = function(){
  14. return connectionPath;
  15. };
  16. /**
  17. * "Confirm"s the entire current connection path.
  18. * TODO: update this documentation
  19. */
  20. this.addConnectionSegment = function(){
  21. connectionPath.node.setAttribute('_d',connectionPath.attr('path'));
  22. };
  23. /**
  24. * Adds a new control point to the current path
  25. * @param x the x-coordinate
  26. * @param y the y-coordinate
  27. * @param overlay
  28. */
  29. this.addControlPoint = function(x,y,overlay) {
  30. ConnectionUtils.addOrDeleteControlPoint('+',overlay,x,y);
  31. };
  32. /* Explanation of the Add/Delete algorithm:
  33. addition:
  34. . clicked overlay corresponds to Lx2,y2
  35. Mx0,y0 Lx1,y1 Lx2,y2 Lx3,y3
  36. becomes
  37. Mx0,y0 Lx1,y1 Lx2,y2 Lx2,y2 Lx3,y3
  38. deletion:
  39. . clicked overlay corresponds to Lx2,y2
  40. Mx0,y0 Lx1,y1 Lx2,y2 Lx3,y3
  41. becomes
  42. Mx0,y0 Lx1,y1 Lx3,y3
  43. after making the described modifications to the corresponding edge's segments
  44. property,
  45. 1 the edge is redrawn
  46. 2 a request is sent to the csworker to update the edge's Link's $segments
  47. property
  48. 3 the connection path editing overlay is refreshed (this will cause newly
  49. added control points to appear, and deleted ones to disappear)
  50. NOTE:: the first and last control points can never be deleted */
  51. /**
  52. * Adds or deletes the control point associated with the given overlay.
  53. */
  54. this.addOrDeleteControlPoint = function(op,overlay,x,y){
  55. if( ! overlay.hasAttribute('__edgeId') )
  56. return;
  57. var edgeId = overlay.getAttribute('__edgeId'),
  58. num = parseInt( overlay.getAttribute('__num') ),
  59. offset = parseInt( overlay.getAttribute('__offset') ),
  60. segments = __edges[edgeId]['segments'],
  61. points = segments.match(/([\d\.]*,[\d\.]*)/g);
  62. if( op == '-' )
  63. /* delete a control point */
  64. {
  65. if( num+offset == 0 || num+offset == points.length-1 )
  66. return;
  67. points.splice(num+offset,1);
  68. }
  69. else
  70. /* add a control point */
  71. points.splice(num+offset,0,x+','+y);
  72. var newpath = 'M'+points.join('L'),
  73. edgeIds = utils.keys(connectionPathEditingOverlay),
  74. linkuri = __edgeId2linkuri(edgeId),
  75. changes = {};
  76. changes[edgeId] = newpath;
  77. __redrawEdge(edgeId,newpath);
  78. DataUtils.updatecs(
  79. linkuri,
  80. {'$segments':utils.mergeDicts([__linkuri2segments(linkuri),changes])});
  81. ConnectionUtils.hideConnectionPathEditingOverlay();
  82. ConnectionUtils.showConnectionPathEditingOverlay(edgeIds);
  83. };
  84. /**
  85. * Removes the current control point
  86. * @param overlay - the overlay to be used to identify the control point
  87. */
  88. this.deleteControlPoint = function(overlay) {
  89. ConnectionUtils.addOrDeleteControlPoint('-',overlay);
  90. };
  91. /**
  92. * "Unconfirm" the last segment of the connection path (ie
  93. * remove it). Do nothing if all segments have been "confirmed"
  94. */
  95. this.deleteConnectionSegment = function() {
  96. var d = String(connectionPath.attr('path')),
  97. matches = d.match(/(M.*,.*)L(.*),(.*)/),
  98. _d = connectionPath.node.getAttribute('_d'),
  99. _matches = _d.match(/(M.*,.*)L.*,.*/);
  100. if( ! _matches )
  101. ; // do nothing
  102. else if( matches )
  103. {
  104. var x = matches[2], y = matches[3];
  105. connectionPath.node.setAttribute('_d',_matches[1]);
  106. ConnectionUtils.updateConnectionSegment(x,y);
  107. }
  108. };
  109. /*
  110. NOTE:: connectionSource is used to remember the uri of the icon at the
  111. start of the path
  112. NOTE:: _d is used ro remember the 'confirmed' portions of the path
  113. NOTE:: the call to connectionPath.toBack() causes mouse events that occur
  114. on top of icons to be captured by those (as opposed to by the in-
  115. progress connection path which would capture them otherwise)
  116. */
  117. /**
  118. * Initializes a Raphael Path starting at (x, y) and that reports the
  119. * mouseup event as if it were the canvas
  120. */
  121. this.initConnectionPath = function(x,y,target){
  122. if( connectionPath != undefined )
  123. return;
  124. connectionSource = __vobj2uri(target);
  125. connectionPath = __canvas.path('M'+x+','+y);
  126. connectionPath.node.setAttribute('_d','M'+x+','+y);
  127. connectionPath.toBack();
  128. connectionPath.node.onmouseup = function(event) {
  129. if( event.button == 0 )
  130. BehaviorManager.handleUserEvent(__EVENT_LEFT_RELEASE_CANVAS,event);
  131. else if( event.button == 1 )
  132. BehaviorManager.handleUserEvent(__EVENT_MIDDLE_RELEASE_CANVAS,event);
  133. else if( event.button == 2 )
  134. BehaviorManager.handleUserEvent(__EVENT_RIGHT_RELEASE_CANVAS,event);
  135. };
  136. };
  137. /**
  138. * Saves the Raphael element associated with the specific overlay as the current
  139. * control point.
  140. *
  141. * This provides a more robust defense against moving the mouse so quickly that it
  142. * exits the overlay we're dragging.
  143. */
  144. this.initControlPointTranslation = function(overlay){
  145. if( overlay.hasAttribute('__edgeId') )
  146. /* set currentControlPoint to normal overlay */
  147. {
  148. var edgeId = overlay.getAttribute('__edgeId'),
  149. num = overlay.getAttribute('__num');
  150. currentControlPoint = connectionPathEditingOverlay[edgeId][num];
  151. }
  152. else
  153. /* set currentControlPoint to central overlay */
  154. {
  155. var linkuri = overlay.getAttribute('__linkuri');
  156. currentControlPoint = connectionPathEditingOverlay[linkuri][0];
  157. }
  158. };
  159. /**
  160. * Hide and delete the connection path
  161. */
  162. this.hideConnectionPath = function(){
  163. connectionPath.remove();
  164. connectionPath = undefined;
  165. connectionSource = undefined;
  166. };
  167. /**
  168. * Hides the current connection path overlay
  169. */
  170. this.hideConnectionPathEditingOverlay = function(){
  171. for( var _ in connectionPathEditingOverlay )
  172. connectionPathEditingOverlay[_].forEach(
  173. function(overlay)
  174. {
  175. overlay.remove();
  176. });
  177. connectionPathEditingOverlay = {};
  178. currentControlPoint = undefined;
  179. };
  180. /**
  181. * Moves the control point and its overlay to the specified coordinates
  182. */
  183. this.previewControlPointTranslation = function(x,y){
  184. var _x = parseInt( currentControlPoint.node.getAttribute('_x') ),
  185. _y = parseInt( currentControlPoint.node.getAttribute('_y') );
  186. currentControlPoint.translate(x-_x,y-_y);
  187. currentControlPoint.node.setAttribute('_x',x);
  188. currentControlPoint.node.setAttribute('_y',y);
  189. ConnectionUtils.updateConnectionPath(true);
  190. };
  191. /**
  192. * Show the connection path editing overlay. This shows draggable circles
  193. * above every control point along the selected edges.
  194. */
  195. this.showConnectionPathEditingOverlay = function(_edgeIds){
  196. var edgeIds =
  197. (_edgeIds ? _edgeIds : __selection['items']).
  198. filter( function(it) {return it in __edges;} ),
  199. onmousedown =
  200. function(event)
  201. {
  202. if( event.button == 0 )
  203. BehaviorManager.handleUserEvent(__EVENT_LEFT_PRESS_CTRL_POINT,event);
  204. },
  205. onmouseup =
  206. function(event)
  207. {
  208. if( event.button == 0 )
  209. BehaviorManager.handleUserEvent(__EVENT_LEFT_RELEASE_CTRL_POINT,event);
  210. else if( event.button == 1 )
  211. BehaviorManager.handleUserEvent(__EVENT_MIDDLE_RELEASE_CTRL_POINT,event);
  212. else if( event.button == 2 )
  213. BehaviorManager.handleUserEvent(__EVENT_RIGHT_RELEASE_CTRL_POINT,event);
  214. };
  215. edgeIds.forEach(
  216. function(edgeId)
  217. {
  218. var points = __edges[edgeId]['segments'].match(/([\d\.]*,[\d\.]*)/g),
  219. linkuri = __edgeId2linkuri(edgeId),
  220. edgeToLink = edgeId.match(linkuri+'$');
  221. /* setup normal overlay */
  222. connectionPathEditingOverlay[edgeId] = [];
  223. (edgeToLink ?
  224. points.slice(0,points.length-1) :
  225. points.slice(1)).forEach(
  226. function(p)
  227. {
  228. var xy = p.split(','),
  229. x = xy[0],
  230. y = xy[1],
  231. overlay = __canvas.circle(x,y,5);
  232. overlay.node.setAttribute('class','ctrl_point_overlay');
  233. overlay.node.setAttribute('__edgeId',edgeId);
  234. overlay.node.setAttribute('__offset',(edgeToLink ? 0 : 1));
  235. overlay.node.setAttribute('__num',
  236. connectionPathEditingOverlay[edgeId].length);
  237. overlay.node.setAttribute('_x',x);
  238. overlay.node.setAttribute('_y',y);
  239. overlay.node.onmouseup = onmouseup;
  240. overlay.node.onmousedown = onmousedown;
  241. connectionPathEditingOverlay[edgeId].push(overlay);
  242. });
  243. /* enhance start/end */
  244. if( edgeToLink )
  245. utils.head(connectionPathEditingOverlay[edgeId]).node.
  246. setAttribute('__start', __edges[edgeId]['start']);
  247. else
  248. utils.tail(connectionPathEditingOverlay[edgeId]).node.
  249. setAttribute('__end', __edges[edgeId]['end']);
  250. /* setup central overlay */
  251. var edgeListAttr = (edgeToLink ? '__edgesTo' : '__edgesFrom');
  252. if( ! (linkuri in connectionPathEditingOverlay) )
  253. {
  254. var xy = (edgeToLink ?
  255. __edges[edgeId]['segments'].match(/.*L(.*)/) :
  256. __edges[edgeId]['segments'].match(/M([\d\.]*,[\d\.]*)/))[1].split(','),
  257. x = xy[0],
  258. y = xy[1],
  259. overlay = __canvas.circle(x,y,8);
  260. overlay.node.setAttribute('class','ctrl_point_center_overlay');
  261. overlay.node.setAttribute('_x',x);
  262. overlay.node.setAttribute('_y',y);
  263. overlay.node.setAttribute('_x0',x);
  264. overlay.node.setAttribute('_y0',y);
  265. overlay.node.setAttribute('__linkuri',linkuri);
  266. overlay.node.setAttribute('__edgesTo',utils.jsons([]));
  267. overlay.node.setAttribute('__edgesFrom',utils.jsons([]));
  268. overlay.node.onmouseup = onmouseup;
  269. overlay.node.onmousedown = onmousedown;
  270. connectionPathEditingOverlay[linkuri] = [overlay];
  271. }
  272. var centerOverlay = connectionPathEditingOverlay[linkuri][0],
  273. edgeList = utils.jsonp(centerOverlay.node.getAttribute(edgeListAttr));
  274. edgeList.push(edgeId);
  275. centerOverlay.node.setAttribute(edgeListAttr,utils.jsons(edgeList));
  276. });
  277. };
  278. /**
  279. * Snaps the current segment to the x or y axis depending on its proximity
  280. * to both axes
  281. */
  282. this.snapConnectionSegment = function(x,y){
  283. var _d = connectionPath.node.getAttribute('_d'),
  284. _matches = _d.match(/.*[L|M](.*),(.*)/),
  285. _x = parseInt( _matches[1] ),
  286. _y = parseInt( _matches[2] ),
  287. d = String(connectionPath.attr('path')),
  288. matches = d.match(/.*[L|M](.*),(.*)/),
  289. x = parseInt( matches[1] ),
  290. y = parseInt( matches[2] );
  291. if( Math.abs(x-_x) > Math.abs(y-_y) )
  292. y = _y;
  293. else
  294. x = _x;
  295. ConnectionUtils.updateConnectionSegment(x,y);
  296. };
  297. /**
  298. * Snap the current control point, if any
  299. */
  300. this.snapControlPoint = function(){
  301. if( currentControlPoint == undefined )
  302. return;
  303. var cpn = currentControlPoint.node,
  304. _x = cpn.getAttribute('_x'),
  305. _y = cpn.getAttribute('_y');
  306. if( cpn.hasAttribute('__edgeId') )
  307. /* snapping normal overlay */
  308. {
  309. var edgeId = cpn.getAttribute('__edgeId'),
  310. num = parseInt(cpn.getAttribute('__num')),
  311. offset = parseInt(cpn.getAttribute('__offset')),
  312. points = __edges[edgeId]['segments'].match(/([\d\.]*,[\d\.]*)/g),
  313. prevXY = points[num+offset-1];
  314. if( num+offset == 0 || num+offset == points.length-1 )
  315. /* don't snap end points */
  316. return;
  317. }
  318. else
  319. /* snapping central overlay */
  320. var edgeId = utils.jsonp(cpn.getAttribute('__edgesTo'))[0],
  321. points = __edges[edgeId]['segments'].match(/([\d\.]*,[\d\.]*)/g),
  322. prevXY = points[points.length-2];
  323. prevXY = prevXY.split(',');
  324. if( Math.abs(prevXY[0]-_x) > Math.abs(prevXY[1]-_y) )
  325. _y = prevXY[1];
  326. else
  327. _x = prevXY[0];
  328. ConnectionUtils.previewControlPointTranslation(_x,_y);
  329. ConnectionUtils.updateConnectionPath();
  330. };
  331. /* NOTE:: when 'local' is false/omitted, edge and center-piece alterations are
  332. not merely displayed, but also persisted to the csworker
  333. */
  334. /**
  335. * Alters edges and/or center-pieces to ensure they follow the changes
  336. * effected to their overlays by ConnectionUtils.previewControlPointTranslation()
  337. * and ConnectionUtils.snapConnectionSegment(). This function redraws edges and/or
  338. * moves center pieces
  339. */
  340. this.updateConnectionPath = function(local){
  341. var cpn = currentControlPoint.node,
  342. _x = cpn.getAttribute('_x'),
  343. _y = cpn.getAttribute('_y');
  344. function updatedCenterPiecePosition()
  345. {
  346. var linkuri = cpn.getAttribute('__linkuri'),
  347. x0 = parseInt( cpn.getAttribute('_x0') ),
  348. y0 = parseInt( cpn.getAttribute('_y0') ),
  349. icon = __icons[linkuri]['icon'];
  350. cpn.setAttribute('_x0',_x);
  351. cpn.setAttribute('_y0',_y);
  352. return [(_x-x0) + parseFloat(icon.getAttr('__x')),
  353. (_y-y0) + parseFloat(icon.getAttr('__y'))];
  354. }
  355. function updateEdgeExtremity(edgeId,start)
  356. {
  357. var matches = __edges[edgeId]['segments'].
  358. match(/(M[\d\.]*,[\d\.]*)(.*)(L.*)/),
  359. newpath = (start ?
  360. 'M'+_x+','+_y+matches[2]+matches[3] :
  361. matches[1]+matches[2]+'L'+_x+','+_y);
  362. __redrawEdge(edgeId,newpath);
  363. return newpath;
  364. }
  365. function updateInnerEdge(edgeId,idx)
  366. {
  367. var points = __edges[edgeId]['segments'].match(/([\d\.]*,[\d\.]*)/g);
  368. points.splice(idx,1,_x+','+_y);
  369. var newpath = 'M'+points.join('L');
  370. __redrawEdge(edgeId,newpath);
  371. return newpath;
  372. }
  373. if( cpn.hasAttribute('__edgeId') )
  374. /* dragging normal overlay */
  375. {
  376. var edgeId = cpn.getAttribute('__edgeId'),
  377. num = cpn.getAttribute('__num'),
  378. offset = cpn.getAttribute('__offset'),
  379. linkuri = __edgeId2linkuri(edgeId),
  380. changes = {};
  381. changes[edgeId] = updateInnerEdge(edgeId,parseInt(num)+parseInt(offset));
  382. }
  383. else
  384. /* dragging central overlay */
  385. {
  386. var linkuri = cpn.getAttribute('__linkuri'),
  387. changes = {};
  388. utils.jsonp( cpn.getAttribute('__edgesTo') ).forEach(
  389. function(edgeId)
  390. {
  391. changes[edgeId] = updateEdgeExtremity(edgeId,false);
  392. });
  393. utils.jsonp( cpn.getAttribute('__edgesFrom') ).forEach(
  394. function(edgeId)
  395. {
  396. changes[edgeId] = updateEdgeExtremity(edgeId,true);
  397. });
  398. }
  399. if( ! local )
  400. DataUtils.updatecs(
  401. linkuri,
  402. utils.mergeDicts([
  403. {'$segments':utils.mergeDicts(
  404. [__linkuri2segments(linkuri),changes])},
  405. (cpn.hasAttribute('__linkuri') ?
  406. {'position' :updatedCenterPiecePosition()} : {})]));
  407. };
  408. /**
  409. * Redraws the current segment such that its end is at (x, y)
  410. */
  411. this.updateConnectionSegment = function(x,y){
  412. connectionPath.attr(
  413. 'path',
  414. connectionPath.node.getAttribute('_d')+'L'+x+','+y);
  415. };
  416. return this;
  417. }();