geometry_utils.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*******************************************************************************
  2. AToMPM - A Tool for Multi-Paradigm Modelling
  3. Copyright (c) 2011 Raphael Mannadiar (raphael.mannadiar@mail.mcgill.ca)
  4. Modified by Conner Hansen (chansen@crimson.ua.edu)
  5. This file is part of AToMPM.
  6. AToMPM is free software: you can redistribute it and/or modify it under the
  7. terms of the GNU Lesser General Public License as published by the Free Software
  8. Foundation, either version 3 of the License, or (at your option) any later
  9. version.
  10. AToMPM is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  12. PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with AToMPM. If not, see <http://www.gnu.org/licenses/>.
  15. *******************************************************************************/
  16. GeometryUtils = function(){
  17. var geometryControlsOverlay = undefined;
  18. var transformationPreviewOverlay = undefined;
  19. /**
  20. * Determines whether or not geometric transformations are allowed. This only
  21. * applies if:
  22. * 1. Geometry controls are hidden
  23. * 2. If an edge is selected, its start and end icons are also selected
  24. */
  25. this.areTransformationsAllowed = function(){
  26. var seen = {};
  27. return (geometryControlsOverlay == undefined ||
  28. geometryControlsOverlay.css("display") == 'none') &&
  29. __selection['items'].every(
  30. function(it)
  31. {
  32. if( it in __edges )
  33. {
  34. var start = __edges[it]['start'],
  35. end = __edges[it]['end'];
  36. if( ! (start in seen) &&
  37. ! utils.contains(__selection['items'],start) )
  38. return false;
  39. if( ! (end in seen) &&
  40. ! utils.contains(__selection['items'],end) )
  41. return false;
  42. seen[start] = seen[end] = 1;
  43. }
  44. return true;
  45. });
  46. };
  47. /**
  48. * Hides the geometry controls overlay
  49. */
  50. this.hideGeometryControlsOverlay = function() {
  51. if( geometryControlsOverlay != undefined )
  52. geometryControlsOverlay.css("display", "none");
  53. __setCanvasScrolling(true);
  54. };
  55. /**
  56. * Hides the transformation preview overlay
  57. */
  58. this.hideTransformationPreviewOverlay = function() {
  59. if( transformationPreviewOverlay != undefined )
  60. {
  61. transformationPreviewOverlay.remove();
  62. transformationPreviewOverlay = undefined;
  63. }
  64. };
  65. /*
  66. NOTE:: _x and _y are used to remember the last 'confirmed' position which we
  67. to compute the relative parameters of calls to translate(..)
  68. NOTE:: the call to toBack() causes whatever is beneath the transformation
  69. preview overlay to become above it, thus becoming detectable by
  70. document.elementFromPoint()... this is used to distinguish between
  71. dropping selections on the canvas and on icons, with the latter
  72. possibly causing insertion */
  73. /**
  74. * Initializes a Raphael rectangle matching the selection bounding box.
  75. */
  76. this.initSelectionTransformationPreviewOverlay = function(x,y)
  77. {
  78. if( transformationPreviewOverlay != undefined )
  79. return;
  80. var bbox = __selection['bbox'];
  81. transformationPreviewOverlay = __bbox2rect(bbox,'transformation_preview');
  82. transformationPreviewOverlay.node.setAttribute('_x0',x);
  83. transformationPreviewOverlay.node.setAttribute('_y0',y);
  84. transformationPreviewOverlay.node.setAttribute('_x',x);
  85. transformationPreviewOverlay.node.setAttribute('_y',y);
  86. transformationPreviewOverlay.node.onmouseup =
  87. function(event)
  88. {
  89. if( event.button == 0 )
  90. transformationPreviewOverlay.toBack();
  91. var beneathTPO = document.elementFromPoint(event.clientX,event.clientY),
  92. _event;
  93. if ( transformationPreviewOverlay.node != beneathTPO &&
  94. beneathTPO != __selection['rect'].node )
  95. {
  96. _event = document.createEvent('MouseEvents');
  97. _event.initMouseEvent(
  98. event.type, event.canBubble, event.cancelable, event.view,
  99. event.detail, event.screenX, event.screenY, event.clientX,
  100. event.clientY, event.ctrlKey, event.altKey, event.shiftKey,
  101. event.metaKey, event.button, event.relatedTarget );
  102. beneathTPO.parentNode.dispatchEvent(_event);
  103. } else {
  104. BehaviorManager.handleUserEvent(__EVENT_LEFT_RELEASE_CANVAS,event);
  105. }
  106. };
  107. };
  108. /**
  109. * Applies the effects of the specified transformation to the preview overlay
  110. */
  111. this.previewSelectionTransformation = function(op,dir) {
  112. if (transformationPreviewOverlay == undefined)
  113. return
  114. var bbox = __selection['bbox'],
  115. scale = (dir > 0 ? 1.05 : 0.95),
  116. angle = (dir > 0 ? 3 : -3);
  117. if( op == 'resize' )
  118. transformationPreviewOverlay.scale(scale,scale,bbox.x,bbox.y);
  119. else if( op == 'resizeH' )
  120. transformationPreviewOverlay.scale(1,scale,bbox.x,bbox.y);
  121. else if( op == 'resizeW' )
  122. transformationPreviewOverlay.scale(scale,1,bbox.x,bbox.y);
  123. else if( op == 'rotate' )
  124. transformationPreviewOverlay.rotate(angle,bbox.x,bbox.y);
  125. };
  126. /**
  127. * Moves the transformation preview overlay to the specified coordinates
  128. */
  129. this.previewSelectionTranslation = function(x,y) {
  130. if (transformationPreviewOverlay == undefined)
  131. return
  132. var _x = parseInt(transformationPreviewOverlay.node.getAttribute('_x')),
  133. _y = parseInt(transformationPreviewOverlay.node.getAttribute('_y'));
  134. transformationPreviewOverlay.translate(x-_x,y-_y);
  135. transformationPreviewOverlay.node.setAttribute('_x',x);
  136. transformationPreviewOverlay.node.setAttribute('_y',y);
  137. };
  138. /*
  139. 0. exit on empty icon list
  140. 1. foreach non-link icon,
  141. a. loop back to step 1 if it has no container
  142. b. determine if it's bbox is fully inside, fully outside or intersects
  143. with its container's
  144. i. when fully inside, loop to step 1
  145. ii. when fully outside AND was actually contained (as opposed to
  146. to-be-inserted) AND dragouts are enabled, produce deletion
  147. request for containment link
  148. iii. otherwise, store needed changes to container position and size
  149. to fit icon... we do this (as opposed to producing a request)
  150. to lump together all changes to a given container (each which
  151. may originiate from different icons)
  152. 2. exit on empty request and container changes lists
  153. 3. convert container changes to CS update requests and append to existing
  154. deletion requests, if any
  155. 4. recurse with 'icons' set to any modified containers and 'context' set
  156. to their pending changes (computed in step 1biii) and append returned
  157. requests... the purpose of this step is for container resizing to have
  158. a cascading effect (i.e., a resized container triggers its parent's
  159. resizing if need be)
  160. 5. send batchEdit or return requests
  161. NOTE:: the 'context' parameter contains a list of pending changes computed by
  162. GeometryUtils.transformSelection() but not yet persisted onto the canvas, as well
  163. as a map of pending insertions, if any... this seemingly odd passing
  164. around of pending information is necessary to enable atomicity of icon
  165. transformations, insertions and container resizings */
  166. /**
  167. * Resizes the containers of icons (specified as uri array) that have moved within
  168. * them as required and uninsert dragged-out icons.
  169. */
  170. this.resizeContainers = function(icons,context,dryRun,disabledDragouts,reqs) {
  171. if( icons.length == 0 )
  172. return (dryRun ? [] : undefined);
  173. if( reqs == undefined )
  174. reqs = [];
  175. var requests = [],
  176. containers2changes = {},
  177. resizeContainer =
  178. function(c,clink,it)
  179. {
  180. var cbbox = __getBBox(
  181. c,utils.mergeDicts([context,containers2changes]) ),
  182. itbbox = __getBBox(it,context);
  183. if( __isBBoxInside(itbbox, cbbox) )
  184. return;
  185. else if( __isBBoxDisjoint(itbbox, cbbox) &&
  186. clink &&
  187. ! disabledDragouts )
  188. requests.push(
  189. {'method':'DELETE',
  190. 'uri':HttpUtils.url(clink,__NO_USERNAME+__NO_WID)});
  191. else
  192. {
  193. containers2changes[c] =
  194. containers2changes[c] ||
  195. utils.mergeDicts(
  196. [{'position':
  197. [parseFloat(__getIcon(c).getAttr('__x')),
  198. parseFloat(__getIcon(c).getAttr('__y'))],
  199. 'scale':
  200. [parseFloat(__getIcon(c).getAttr('__sx')),
  201. parseFloat(__getIcon(c).getAttr('__sy'))]},
  202. context[c]]);
  203. var padding = 20,
  204. overflow =
  205. {'right': (itbbox.x + itbbox.width) -
  206. (cbbox.x + cbbox.width) + padding,
  207. 'left': cbbox.x - itbbox.x + padding,
  208. 'top': cbbox.y - itbbox.y + padding,
  209. 'bottom': (itbbox.y + itbbox.height) -
  210. (cbbox.y + cbbox.height) + padding};
  211. if( overflow.left > 0 )
  212. {
  213. containers2changes[c]['position'][0] -= overflow.left;
  214. containers2changes[c]['scale'][0] *=
  215. (cbbox.width+overflow.left)/cbbox.width;
  216. cbbox.width *= containers2changes[c]['scale'][0];
  217. }
  218. if( overflow.right > 0 )
  219. containers2changes[c]['scale'][0] *=
  220. (cbbox.width+overflow.right)/cbbox.width;
  221. if( overflow.top > 0 )
  222. {
  223. containers2changes[c]['position'][1] -= overflow.top;
  224. containers2changes[c]['scale'][1] *=
  225. (cbbox.height+overflow.top)/cbbox.height;
  226. cbbox.height *= containers2changes[c]['scale'][1];
  227. }
  228. if( overflow.bottom > 0 )
  229. containers2changes[c]['scale'][1] *=
  230. (cbbox.height+overflow.bottom)/cbbox.height;
  231. }
  232. };
  233. icons.forEach(
  234. function(it)
  235. {
  236. if( !(it in __icons) || __isConnectionType(it) )
  237. return;
  238. __icons[it]['edgesIn'].forEach(
  239. function(edgeId)
  240. {
  241. var linkIn = __edgeId2ends(edgeId)[0];
  242. if( __isContainmentConnectionType(linkIn) ) {
  243. if ( reqs.map(function(_node) {return _node['uri'];}).indexOf(__edgeId2ends(__icons[linkIn]['edgesIn'][0])[0] + '.cs') < 0 ) {
  244. resizeContainer(
  245. __edgeId2ends(__icons[linkIn]['edgesIn'][0])[0],
  246. linkIn,
  247. it);
  248. }
  249. }
  250. });
  251. if( context.toBeInserted && it in context.toBeInserted )
  252. resizeContainer(context.toBeInserted[it],undefined,it);
  253. });
  254. if( utils.keys(containers2changes).length == 0 && requests.length == 0 )
  255. return (dryRun ? [] : undefined);
  256. for( var uri in containers2changes )
  257. requests.push(
  258. {'method':'PUT',
  259. 'uri':HttpUtils.url(uri+'.cs',__NO_USERNAME+__NO_WID),
  260. 'reqData':{'changes':containers2changes[uri]}});
  261. for (var req_id in requests) {
  262. var to_concat = utils.flatten(GeometryUtils.resizeContainers(
  263. utils.keys(containers2changes),
  264. containers2changes,
  265. true,
  266. false,
  267. requests)
  268. )
  269. requests = requests.concat(to_concat);
  270. }
  271. if( dryRun )
  272. return requests;
  273. else
  274. HttpUtils.httpReq(
  275. 'POST',
  276. HttpUtils.url('/batchEdit',__NO_USERNAME),
  277. requests);
  278. };
  279. /**
  280. * Shows the geometry controls overlay (positioning is based on the bounding box
  281. * of the current selection) and initializes the transformation preview overlay
  282. */
  283. this.showGeometryControlsOverlay = function() {
  284. var bbox = __selection['bbox'];
  285. if( geometryControlsOverlay == undefined )
  286. {
  287. geometryControlsOverlay = $('#div_geom_ctrls');
  288. ['resize','resizeH','resizeW','rotate'].forEach(
  289. function(x)
  290. {
  291. var img = $('<img>');
  292. img.attr('class', 'geometry_ctrl');
  293. img.attr('src', 'client/media/'+x+'.png');
  294. img.get(0).onmousewheel =
  295. function(event)
  296. {
  297. var dir = event.wheelDelta;
  298. GeometryUtils.previewSelectionTransformation(x,dir);
  299. return false;
  300. };
  301. geometryControlsOverlay.append(img);
  302. });
  303. var img = $('<img>');
  304. img.attr('class', 'geometry_ctrl');
  305. img.attr('src', 'client/media/ok.png');
  306. img.click(function(event) {GeometryUtils.transformSelection(__GEOM_TRANSF);});
  307. geometryControlsOverlay.append(img);
  308. }
  309. geometryControlsOverlay.css("top",
  310. bbox.y + bbox.height - $("#div_container").scrollTop() + "px"),
  311. geometryControlsOverlay.css("left",
  312. bbox.x + bbox.width/2 - __GEOM_CTRLS_WIDTH/2.0 - $("#div_container").scrollLeft() + "px");
  313. geometryControlsOverlay.css("display", "inline");
  314. GeometryUtils.initSelectionTransformationPreviewOverlay();
  315. __setCanvasScrolling(false);
  316. };
  317. /**
  318. * Snaps the top-left corner of the selection bounding box to the nearest
  319. * grid point
  320. */
  321. this.snapSelectionToGrid = function() {
  322. var bbox = __selection['bbox'],
  323. dx = bbox.x % __GRID_CELL_SIZE,
  324. dy = bbox.y % __GRID_CELL_SIZE;
  325. if( dx == 0 && dy == 0 )
  326. return;
  327. GeometryUtils.initSelectionTransformationPreviewOverlay(bbox.x,bbox.y);
  328. GeometryUtils.previewSelectionTranslation(
  329. bbox.x + (dx < __GRID_CELL_SIZE/2 ? -dx : __GRID_CELL_SIZE-dx),
  330. bbox.y + (dy < __GRID_CELL_SIZE/2 ? -dy : __GRID_CELL_SIZE-dy));
  331. GeometryUtils.transformSelection(__GEOM_TRANSF);
  332. };
  333. /* applies the transformation currently applied to the preview overlay to the
  334. selected icon(s)/edge(s) and removes the geometry controls and transformation
  335. preview overlays... if 'insertInfo' is specified, also inserts selection into
  336. it (see NOTE about why this is done from here)... this function doesn't
  337. actually transform the icons, it merely requests the update of the icon(s)'s
  338. 'transformation' and/or the link(s)'s $segments attributes on the csworker
  339. (i.e., a changelog triggers the actual transformation)
  340. 1. extract transformation and build up changes in 'uri2changes'
  341. 2. add $segments changes to 'uris2changes'
  342. 3. retrieve and compute all necessary requests
  343. a. retrieve insertion requests (+ provide DataUtils.insert() with data needed
  344. to compute bboxes of to-be-transformed icons, i.e., 'uris2changes')
  345. b. convert 'uri2changes' to icon transformation requests
  346. c. retrieve container resizing requests (+ provide GeometryUtils.resizeContainers()
  347. with 'uris2changes', a list of pending insertions from step 3a, and
  348. possibly a dragout prohibition)
  349. 4. send batchEdit with requests from step 3... note that requests from
  350. step 3a. are inserted last s.t. the event-flow is 1-something moved
  351. followed by 2-something inserted... this ordering is needed to ensure
  352. mappers and parsers are evaluated in a sensible order
  353. the following describes the algorithm for getting edge ends to follow their
  354. icons when these are transformed:
  355. 1. for each outgoing edge,
  356. z) do nothing if the edge's Link is in __selection
  357. a) fetch the edge's source xy
  358. b) apply transformation T on it to produce xy'
  359. c) 'move' the edge source and possibly its first control point (when
  360. they are colocated) to xy'... in reality, save the desired motion in
  361. connectedEdgesChanges
  362. 2. for each outgoing edge, apply similar logic but to edge's end and last
  363. control point
  364. NOTE:: to avoid race conditions between updates to different edges within a
  365. single Link's $segments, relevant changes are accumulated in
  366. connectedEdgesChanges s.t. those pertaining to the same Link end up
  367. bundled in a single update request
  368. NOTE:: to avoid race conditions between updates to $segments resulting from
  369. edge ends following connected icon and updates resulting from edges
  370. themselves being transformed (i.e., when they are within __selection),
  371. the former are ignored when we know the latter will be carried out
  372. NOTE:: because SVG transformations are always relative to the global (0,0),
  373. non-translate transformations still technically translate things...
  374. Raphael allows specifiying different origins for transformations...
  375. default SVG scale x2 :
  376. Rect(10,10,200,100) > Rect(20,20,400,200)
  377. Raphael scale with scale origin set to (10,10)
  378. Rect(10,10,200,100) > Rect(10,10,200,100)
  379. in the above example, Raphael's transformation matrix will report the
  380. translation from (20,20) back to (10,10) even though from my
  381. perspective, the figure hasn't moved and has only been scaled... to
  382. account for this, when decomposing the said matrix, we ignore tx,ty
  383. when r|sx|sy aren't 0|1|1 and vice-versa... this doesn't cause any
  384. problems because the client interface doesn't support scaling/rotating
  385. *and* translating without an intermediate call to this function...
  386. NOTE:: essentially, the above-explained ignored rotation/scaling translation
  387. components apply to the top-left corner of the selection bbox (i.e.,
  388. it's Raphael ensuring that the said corner does not move as a result
  389. of rotations/scalings 'centered' on it)... however, similar rotation/
  390. scaling translation components apply to contents of the selection...
  391. this is because the said contents are changing wrt. the top-left
  392. corner of the selection, not wrt. their own (x,y)... ignoring these
  393. 'internal' translation components would cause altering a selection to
  394. act like altering each selected item individually... long story short,
  395. we can not and do not ignore them... below is the algorithm we use to
  396. compute the internal translation components:
  397. 1. foreach selected icon
  398. [do nothing if no rotation or and no scaling]
  399. a) compute offset between icon's x,y and selection's top-left corner
  400. b) apply extracted (from transformation matrix) rotation and scale
  401. to a point whose coordinates are the x and y offsets from step a)
  402. c) determine translation from point from step a) to transformed
  403. point from step b)
  404. d) the icon's transformation is now the extracted rotation and
  405. scaling *and* the translation from step c)
  406. NOTE:: since the selection transformation should be an atomic operation,
  407. changes are accumulated in 'uris2changes' and are only actually sent
  408. to the csworker at the very end of this function... also, since
  409. insertions and container resizings and the selection transformations
  410. that triggered them should be atomic too, requests pertaining to the 2
  411. former tasks are computed and bundled with those that effect the
  412. latter... the results of this form the emitted batchEdit */
  413. this.transformSelection = function(callingContext,insertInfo) {
  414. if (transformationPreviewOverlay == undefined)
  415. return
  416. var T = transformationPreviewOverlay.node.getAttribute('transform');
  417. if( T == null || T == 'matrix(1,0,0,1,0,0)' )
  418. {
  419. GeometryUtils.hideGeometryControlsOverlay();
  420. GeometryUtils.hideTransformationPreviewOverlay();
  421. return;
  422. }
  423. /** 1 **/
  424. var _T = __decomposeTransformationMatrix(T),
  425. connectedEdgesChanges = {},
  426. uris2changes = {};
  427. __selection['items'].forEach(
  428. function(it)
  429. {
  430. if( it in __icons )
  431. {
  432. var icon = __icons[it]['icon'],
  433. changes = {};
  434. if( _T.r == 0 &&
  435. Math.abs(1-_T.sx) <= 0.001 &&
  436. Math.abs(1-_T.sy) <= 0.001 )
  437. {
  438. /* translation only */
  439. if( _T.tx != 0 || _T.ty != 0 )
  440. changes['position'] =
  441. [_T.tx + parseFloat(icon.getAttr('__x')),
  442. _T.ty + parseFloat(icon.getAttr('__y'))];
  443. }
  444. else
  445. {
  446. /* rotation/scale only */
  447. var offset = [icon.getAttr('__x') - __selection['bbox'].x,
  448. icon.getAttr('__y') - __selection['bbox'].y],
  449. rsOffset = GeometryUtils.transformPoint(
  450. offset[0],
  451. offset[1],
  452. 'rotate('+_T.r+') scale('+_T.sx+','+_T.sy+')'),
  453. offsetTx = rsOffset[0] - offset[0],
  454. offsetTy = rsOffset[1] - offset[1];
  455. if( _T.r != 0 )
  456. changes['orientation'] =
  457. (parseFloat(icon.getAttr('__r')) + _T.r) % 360;
  458. if( Math.abs(1-_T.sx) > 0.001 || Math.abs(1-_T.sy) > 0.001 )
  459. changes['scale'] =
  460. [_T.sx * parseFloat(icon.getAttr('__sx')),
  461. _T.sy * parseFloat(icon.getAttr('__sy'))];
  462. if( offsetTx != 0 || offsetTy != 0 )
  463. changes['position'] =
  464. [offsetTx + parseFloat(icon.getAttr('__x')),
  465. offsetTy + parseFloat(icon.getAttr('__y'))];
  466. }
  467. uris2changes[it] = changes;
  468. if( ! __isConnectionType(it) )
  469. {
  470. /* have edge ends out follow */
  471. __icons[it]['edgesOut'].forEach(
  472. function(edgeId)
  473. {
  474. var linkuri = __edgeId2linkuri(edgeId);
  475. if( __isSelected(linkuri) )
  476. return;
  477. var segments = __edges[edgeId]['segments'],
  478. points = segments.match(/([\d\.]*,[\d\.]*)/g),
  479. xy = utils.head(points).split(','),
  480. newXY = GeometryUtils.transformPoint(xy[0],xy[1],T);
  481. connectedEdgesChanges[linkuri] =
  482. (connectedEdgesChanges[linkuri] || {});
  483. points.splice(0,1,newXY.join(','));
  484. connectedEdgesChanges[linkuri][edgeId] =
  485. 'M'+points.join('L');
  486. });
  487. /* have edge ends in follow */
  488. __icons[it]['edgesIn'].forEach(
  489. function(edgeId)
  490. {
  491. var linkuri = __edgeId2linkuri(edgeId);
  492. if( __isSelected(linkuri) )
  493. return;
  494. var segments = __edges[edgeId]['segments'],
  495. points = segments.match(/([\d\.]*,[\d\.]*)/g),
  496. xy = utils.tail(points).split(','),
  497. newXY = GeometryUtils.transformPoint(xy[0],xy[1],T);
  498. connectedEdgesChanges[linkuri] =
  499. (connectedEdgesChanges[linkuri] || {});
  500. points.splice(points.length-1,1,newXY.join(','));
  501. connectedEdgesChanges[linkuri][edgeId] =
  502. 'M'+points.join('L');
  503. });
  504. }
  505. else
  506. {
  507. /* transform entire edges */
  508. var __segments = __linkuri2segments(it),
  509. changes = {};
  510. for( var edgeId in __segments )
  511. {
  512. var segments = __segments[edgeId],
  513. points = segments.match(/([\d\.]*,[\d\.]*)/g),
  514. newPoints = points.map(
  515. function(p)
  516. {
  517. p = p.split(',');
  518. return GeometryUtils.transformPoint(p[0],p[1],T);
  519. });
  520. changes[edgeId] = 'M'+newPoints.join('L');
  521. }
  522. uris2changes[it]['$segments'] = changes;
  523. }
  524. }
  525. });
  526. /** 2 **/
  527. if( utils.keys(connectedEdgesChanges).length > 0 )
  528. for( var linkuri in connectedEdgesChanges )
  529. {
  530. if( !(linkuri in uris2changes) )
  531. uris2changes[linkuri] = {};
  532. if( !('$segments' in uris2changes[linkuri]) )
  533. uris2changes[linkuri]['$segments'] = __linkuri2segments(linkuri);
  534. uris2changes[linkuri]['$segments'] =
  535. utils.mergeDicts([
  536. uris2changes[linkuri]['$segments'],
  537. connectedEdgesChanges[linkuri]]);
  538. }
  539. /** 3-4 **/
  540. if( utils.keys(uris2changes).length > 0 )
  541. {
  542. var csRequests = [],
  543. insertRequests = [];
  544. if( insertInfo )
  545. {
  546. insertRequests = DataUtils.insert(
  547. insertInfo['dropTarget'].getAttribute('__csuri'),
  548. __selection['items'],
  549. insertInfo['connectionType'],
  550. uris2changes,
  551. true);
  552. var toBeInserted = {};
  553. insertRequests.forEach(
  554. function(r)
  555. {
  556. if ('reqData' in r)
  557. toBeInserted[r['reqData']['dest']] = r['reqData']['src'];
  558. });
  559. }
  560. for( var uri in uris2changes )
  561. if( utils.keys(uris2changes[uri]).length > 0 )
  562. csRequests.push(
  563. {'method':'PUT',
  564. 'uri':HttpUtils.url(uri+'.cs',__NO_USERNAME+__NO_WID),
  565. 'reqData':{'changes':uris2changes[uri]}});
  566. HttpUtils.httpReq(
  567. 'POST',
  568. HttpUtils.url('/batchEdit',__NO_USERNAME),
  569. csRequests.concat(
  570. GeometryUtils.resizeContainers(
  571. __selection['items'],
  572. utils.mergeDicts(
  573. [uris2changes, {'toBeInserted':toBeInserted}]),
  574. true,
  575. (callingContext == __GEOM_TRANSF)),
  576. insertRequests));
  577. }
  578. };
  579. /**
  580. * Apply the specified transformation to the given point and return
  581. * the resulting point
  582. */
  583. this.transformPoint = function(x,y,T) {
  584. var pt = __canvas.group();
  585. pt.push( __canvas.point(x,y) );
  586. pt.node.setAttribute('transform',T);
  587. var bbox = pt.getBBox();
  588. pt.remove();
  589. return [bbox.x+bbox.width/2,bbox.y+bbox.height/2];
  590. };
  591. return this;
  592. }();