geometry_utils.js 24 KB

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