geometry_utils.js 23 KB

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