selection_utils.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. var __selectionOverlay;
  6. var __highlighted = [];
  7. var __selection;
  8. function __isSelected(it)
  9. {
  10. return __selection != undefined && utils.contains(__selection['items'],it);
  11. }
  12. /* draw a selection overlay rectangle around the specified (via uri) icons
  13. 1. clear any past __selection
  14. 2. return undefined on undefined argument
  15. 2. extract uri from non-array argument (when select called with event.target)
  16. ... event.target may be:
  17. a) normal icon: selection becomes that icon's uri
  18. b) center-piece: selection becomes center-piece and all of its connected
  19. edges
  20. c) edge: selection becomes connected center-piece and all of its
  21. connected edges
  22. 2. remove selected items <edgeTo,centerPiece,edgeFrom> if any of the 3 are
  23. missing (e.g., when the canvas selection overlay contains only edgeTo or
  24. centerPiece)
  25. 3. foreach icon in the updated selection, add its contents if any/applicable
  26. except when the 'ignoreContents' is set to true
  27. 4. return undefined on empty updated selection
  28. 5. compute a bbox that contains all specified icons/edges
  29. 6. draw a selection overlay rectangle matching computed bbox and style it
  30. appropriately
  31. 7. remember the drawn rectangle, the computed bbox and the selection in
  32. __selection
  33. 8. give rectangle listener to report __EVENT_LEFT_PRESS_SELECTION
  34. 9. return true (to indicate successfully selecting something) */
  35. function __select(selection,ignoreContents)
  36. {
  37. if( __selection != undefined )
  38. {
  39. __selection['rect'].remove();
  40. __selection = undefined;
  41. GeometryUtils.hideGeometryControlsOverlay();
  42. GeometryUtils.hideTransformationPreviewOverlay();
  43. }
  44. if( selection == undefined )
  45. return;
  46. else if( ! utils.isArray(selection) )
  47. /* selection is event.target */
  48. {
  49. if( (uri = __vobj2uri(selection)) )
  50. selection = [uri].concat(
  51. (__isConnectionType(uri) ? __getConnectedEdges(uri) : []));
  52. else
  53. selection = __getConnectionParticipants( __edge2edgeId(selection) );
  54. }
  55. else
  56. /* filter canvas selection for incomplete connection tuples */
  57. {
  58. var missingFromSelection =
  59. function(required)
  60. {
  61. return required.some(
  62. function(edgeId)
  63. {return ! utils.contains(selection,edgeId);});
  64. };
  65. while(
  66. selection.length > 0 &&
  67. selection.some(
  68. function(it)
  69. {
  70. if( it in __edges )
  71. var items = __getConnectionParticipants(it);
  72. else if( __isConnectionType(it) )
  73. var items = [it].concat(__getConnectedEdges(it));
  74. if( items != undefined && missingFromSelection(items) )
  75. {
  76. utils.filter(selection,items);
  77. return true;
  78. }
  79. }) ) ;
  80. }
  81. /* add icon contents */
  82. if( !ignoreContents )
  83. {
  84. var containedIcons = [];
  85. selection.forEach(
  86. function(it)
  87. {
  88. if( it in __icons )
  89. containedIcons =
  90. containedIcons.concat( __getIconsInContainer(it) );
  91. });
  92. selection = selection.concat(
  93. containedIcons.filter(
  94. function(ci)
  95. {
  96. return ! utils.contains(selection,ci);
  97. }));
  98. }
  99. /* compute selection bbox */
  100. if( selection.length == 0 )
  101. return;
  102. else if( selection.length == 1 )
  103. var bbox = __icons[selection[0]]['icon'].getBBox();
  104. else
  105. var bbox = __getGlobalBBox(selection);
  106. __selection =
  107. {'items':selection,
  108. 'bbox':bbox,
  109. 'rect':__bbox2rect(bbox,'canvas_selection')};
  110. __selection['rect'].node.onmousedown =
  111. function(event)
  112. {
  113. if( event.button == 0 )
  114. BehaviorManager.handleUserEvent(__EVENT_LEFT_PRESS_SELECTION,event);
  115. };
  116. __selection['rect'].node.onmouseup =
  117. function(event)
  118. {
  119. if( event.button == 0 )
  120. BehaviorManager.handleUserEvent(__EVENT_LEFT_RELEASE_SELECTION,event);
  121. };
  122. return true;
  123. }
  124. /* returns true if __selection contains an 'instance' of the given type */
  125. function __selectionContainsType(type)
  126. {
  127. if( __selection == undefined )
  128. return false;
  129. return __selection['items'].some(
  130. function(it)
  131. {
  132. return (type == __EDGETYPE && it in __edges) ||
  133. (type == __NODETYPE && it in __icons);
  134. });
  135. }
  136. /* temporarily highlight specified icon (e.g., to draw attention on it) without
  137. disrupting other highlighted elements */
  138. function __flash(uri,color,timeout)
  139. {
  140. __icons[uri]['icon'].highlight({'color':color || 'MediumVioletRed','fill':true});
  141. function turnOff()
  142. {
  143. try {__icons[uri]['icon'].unhighlight();}
  144. catch(err) {
  145. console.log(err);
  146. }
  147. }
  148. window.setTimeout(turnOff,timeout || 500);
  149. }
  150. /* if 'uri' isn't already highlighted, unhighlights whatever is (if applicable)
  151. and highlights 'uri'... highlighting implies setting
  152. 1. highlighting 'uri'
  153. 2. possibly highlighting 'uri''s cross-formalism neighbors
  154. 3. possibly setting a timeout to remove the highlight
  155. 4. setting up the __highlighted object like so
  156. 'uri' :: 'uri'
  157. 'turnOff' :: a function that unhighlights 'uri' and nodes from step 2. if
  158. any... the try/catch blocks ensure safety against deletion of
  159. highlighted icons */
  160. function __highlight(uri,followCrossFormalismLinks,timeout,color)
  161. {
  162. if( ! isHighlighted(uri) )
  163. {
  164. __unhighlight(uri);
  165. __icons[uri]['icon'].highlight({'color':color || 'DarkTurquoise','fill':true});
  166. if( followCrossFormalismLinks != undefined )
  167. {
  168. var neighbors =
  169. __getCrossFormalismNeighbors(uri,followCrossFormalismLinks);
  170. neighbors.nodes.forEach(
  171. function(n)
  172. {
  173. if( n != uri )
  174. __icons[n]['icon'].highlight({'color':'Gold','fill':true});
  175. });
  176. }
  177. if( timeout != undefined )
  178. var tid = window.setTimeout(__unhighlight,timeout);
  179. __highlighted.push(
  180. {'uri':uri,
  181. 'turnOff':
  182. function()
  183. {
  184. try {__icons[uri]['icon'].unhighlight();}
  185. catch(err) {
  186. console.log(err);
  187. }
  188. if( followCrossFormalismLinks != undefined )
  189. neighbors.nodes.forEach(
  190. function(n) {
  191. try {__icons[n]['icon'].unhighlight();}
  192. catch(err) {
  193. console.log(err);
  194. }
  195. } );
  196. if( timeout != undefined )
  197. window.clearTimeout(tid);
  198. }});
  199. }
  200. }
  201. function isHighlighted(uri)
  202. {
  203. return __highlighted.length > 0 && __highlighted.filter(function(hl) {return uri == hl['uri'];}).length == 1;
  204. }
  205. function __unhighlight(uri)
  206. {
  207. if( __highlighted.length > 0 )
  208. {
  209. __highlighted.filter(function(hl) {return !uri || uri == hl['uri'];}).forEach(function(hl) {hl.turnOff();});
  210. if (!uri) {
  211. __highlighted = [];
  212. } else {
  213. __highlighted = __highlighted.filter(function(hl) {return uri != hl['uri'];});
  214. }
  215. }
  216. }
  217. /* return the contents of the selection orverlay rectangle... the max parameter
  218. is used to stop searching after the specified number of contained icons/edges
  219. are found */
  220. function __getCanvasSelectionOverlayContents(max)
  221. {
  222. if( __selectionOverlay == undefined )
  223. return [];
  224. var sobbox = {'x' : __selectionOverlay.attr('x'),
  225. 'y' : __selectionOverlay.attr('y'),
  226. 'width' : __selectionOverlay.attr('width'),
  227. 'height': __selectionOverlay.attr('height')};
  228. contents = [];
  229. [__icons,__edges].some(
  230. function(A)
  231. {
  232. for( var a in A )
  233. {
  234. if( A[a]['icon'].isVisible() &&
  235. __isBBoxInside(A[a]['icon'].getBBox(),sobbox) )
  236. {
  237. contents.push(a);
  238. if( contents.length == max )
  239. return true;
  240. }
  241. }
  242. });
  243. return contents;
  244. }
  245. /* initialize a Raphael.rect originating at (x,y), appropriately styled, and
  246. that reports events (specifically, mouseup) as if it were the canvas
  247. NOTE:: _x0 and _y0 are use to remember the point from where the selection
  248. began */
  249. function __initCanvasSelectionOverlay(x,y)
  250. {
  251. if( __selectionOverlay != undefined )
  252. return;
  253. __selectionOverlay = __canvas.rect(x,y,0,0);
  254. __selectionOverlay.node.setAttribute('class','canvas_selection_overlay');
  255. __selectionOverlay.node.setAttribute('_x0',x);
  256. __selectionOverlay.node.setAttribute('_y0',y);
  257. __selectionOverlay.node.onmouseup =
  258. function(event)
  259. {
  260. if( event.button == 0 )
  261. BehaviorManager.handleUserEvent(__EVENT_LEFT_RELEASE_CANVAS,event);
  262. };
  263. }
  264. /* returns true if there is at least one icon fully encompassed within the
  265. selection overlay rectangle */
  266. function __isCanvasSelectionOverlayEmpty()
  267. {
  268. return __getCanvasSelectionOverlayContents(1).length == 0;
  269. }
  270. /* selects contents of and hides canvas selection overlay */
  271. function __selectSelection()
  272. {
  273. var selectedSomething = __select( __getCanvasSelectionOverlayContents() );
  274. __selectionOverlay.remove();
  275. __selectionOverlay = undefined;
  276. return selectedSomething;
  277. }
  278. /* resizes the selection overlay rectangle following a mouse motion... resizing
  279. may involve changing x and y attributes because widths and heights can not be
  280. negative */
  281. function __updateCanvasSelectionOverlay(x,y)
  282. {
  283. var w = x - parseInt(__selectionOverlay.node.getAttribute('_x0')),
  284. h = y - parseInt(__selectionOverlay.node.getAttribute('_y0'));
  285. if( w < 0 )
  286. {
  287. __selectionOverlay.attr('x', x);
  288. __selectionOverlay.attr('width', -w);
  289. }
  290. else
  291. __selectionOverlay.attr('width', w);
  292. if( h < 0 )
  293. {
  294. __selectionOverlay.attr('y', y);
  295. __selectionOverlay.attr('height', -h);
  296. }
  297. else
  298. __selectionOverlay.attr('height', h);
  299. }