Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.42 KB | None | 0 0
  1. import {CanvasHelper} from'../CanvasHelper';
  2. import {Graph} from '../model/Graph';
  3. import {Position} from '../model/Position';
  4.  
  5. export class GraphCanvasView {
  6. /**
  7. * @param {Graph} graph
  8. * @param {HTMLCanvasElement} canvas
  9. */
  10. //static vertex: Vertex;
  11.  
  12. dragVertex = null;
  13. graph = null;
  14. canvasHelper=null;
  15. selectedVertexColor=null;
  16. vertexRadius;
  17. selectedVertex;
  18. uncompletedEdge;
  19.  
  20. constructor(graph, private readonly canvas: HTMLCanvasElement) {
  21. this.graph = graph;
  22. this.canvas = canvas;
  23. this.canvasHelper = new CanvasHelper(canvas);
  24. this.dragVertex = null;
  25. this.uncompletedEdge = null;
  26. this.selectedVertex = null;
  27. this.canvas.addEventListener('click', this.onClickListener.bind(this)); //get the variable value which is inside the function closure
  28. this.canvas.addEventListener('contextmenu', this.onContextMenuListener.bind(this));
  29. this.canvas.addEventListener('mousemove', this.onMousemoveListener.bind(this));
  30. this.canvas.addEventListener('mouseup', this.onMouseupListener.bind(this));
  31. this.canvas.addEventListener('mousedown', this.onMousedownListener.bind(this));
  32. this.graph.on(Graph.EVENT_VERTEX_CREATED, this.drawVertex.bind(this));
  33. this.selectedVertexColor = '#0f0';
  34. this.vertexRadius = 20;
  35.  
  36. }
  37. //methodTest ( ) { return console.log("test message")}
  38.  
  39. /**
  40. * @param {Vertex} vertex
  41. */
  42. setVertexAsSelected(vertex) {
  43. if (!this.graph.containsVertex(vertex)) {
  44. throw new Error('Attempt to select not added vertex');
  45. }
  46.  
  47. this.selectedVertex = vertex;
  48. this.redraw();
  49. // console.log("selectedVertex = ",vertex);
  50. //console.log("fromselectedVertex>vertex.getId", vertex.getId());
  51. };
  52.  
  53. /**
  54. * @return {Vertex|null}
  55. **/
  56. getSelectedVertex() {
  57. return this.selectedVertex;
  58. }
  59.  
  60. discardSelectedVertex() {
  61. this.selectedVertex = null;
  62. }
  63.  
  64. /**
  65. * @param {Position} position
  66. * @return {Vertex|null}
  67. * @private
  68. */
  69. getVertexByPosition(position) {
  70. return this.graph.getVerticesList().find((vertex) => {
  71. return checkPositionIsInCircle(position, vertex.getPosition(), this.vertexRadius);
  72. });
  73. }
  74.  
  75. redraw() {
  76. console.log("redrawMethod");
  77. this.canvasHelper.clearCanvas();
  78.  
  79. const groupedEdges = groupEdgesByVertices(this.graph.getEdgesList());
  80.  
  81. //console.log("groupedEdges", groupedEdges);
  82.  
  83. groupedEdges.forEach((edges) => this.canvasHelper.drawEdges(edges));
  84.  
  85. this.graph.getVerticesList().forEach((vertex) => {
  86. const color = (vertex === this.selectedVertex) ? this.selectedVertexColor : null;
  87. this.canvasHelper.drawCircle(vertex.getPosition(), this.vertexRadius, vertex.getId(), color);
  88.  
  89. });
  90. }
  91.  
  92. /**
  93. * @return {number}
  94. */
  95. getVertexRadius() {
  96. return this.vertexRadius;
  97. }
  98.  
  99. /**
  100. * @param {Event} event
  101. * @private
  102. */
  103.  
  104. // start
  105. onClickListener(event) {
  106. console.log("onClickListener(event)",event);
  107. const clickPosition = this.getEventPosition(event);
  108. const vertex = this.getVertexByPosition(clickPosition);
  109.  
  110. //console.log("onClickListener: vertexc",vertex," lickPosition", clickPosition);
  111. //checkposition for drow vertex
  112. if (!this.dragVertex && !vertex) {
  113. this.graph.createVertexWithPosition(clickPosition);
  114. } else if (vertex && ctrlKeyIsPressed(event)) {
  115. this.setVertexAsSelected(vertex);
  116. }
  117. }
  118.  
  119. /**
  120. * @param {Event} event
  121. * @private
  122. */
  123.  
  124. //right-click event //event.preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur.
  125. onContextMenuListener(event) {
  126.  
  127. //check
  128. // console.log("----------------", (<HTMLInputElement> document.getElementById('directed-edge')).value)
  129. const flag = document.getElementById('directed-edge') as HTMLInputElement;
  130.  
  131. /* TEST FLAG check */
  132. if (flag.checked) { console.log("checkkk____",document.getElementById('directed-edge'))
  133. } else {console.log("____UNdirected-edge")}
  134.  
  135. event.preventDefault();
  136. const clickPosition = this.getEventPosition(event);
  137. const vertex = this.getVertexByPosition(clickPosition);
  138.  
  139. if (ctrlKeyIsPressed(event) && vertex) {
  140. this.graph.deleteVertex(vertex);
  141. } else if (vertex) {
  142. if (this.uncompletedEdge) {
  143. const vertexFrom = this.getVertexByPosition(this.uncompletedEdge);
  144. console.log("const vertexFrom-----", vertexFrom)
  145.  
  146. if (flag.checked ) { //document.getElementById('directed-edge').checked
  147. console.log("create___DDirectedEdgeTo")
  148.  
  149. this.graph.addEdge(vertexFrom.createDirectedEdgeTo(vertex));
  150. } else {
  151. console.log("create___UndirectedEdgeTo")
  152. this.graph.addEdge(vertexFrom.createUndirectedEdgeTo(vertex));
  153. }
  154. this.uncompletedEdge = null;
  155. this.redraw();
  156. } else if (vertex) {
  157. this.uncompletedEdge = vertex.getPosition();
  158. }
  159. } else if (this.uncompletedEdge && !vertex) {
  160. this.uncompletedEdge = null;
  161. this.redraw();
  162. }
  163. }
  164.  
  165. /**
  166. * @param {Event} event
  167. * @private
  168. **/
  169.  
  170. //drawDirectedLine
  171. onMousemoveListener(event) {
  172. const mousePosition = this.getEventPosition(event);
  173. //console.log("mousePosition--->>", mousePosition)
  174. if (this.uncompletedEdge) {
  175. this.redraw();
  176.  
  177. const isEdgeDirected = document.getElementById('directed-edge') as HTMLInputElement; //.checked
  178.  
  179. if (isEdgeDirected.checked) { //console.log("isEdgeDirected",isEdgeDirected.checked)
  180.  
  181. this.canvasHelper.drawDirectedLine(this.uncompletedEdge, mousePosition);
  182. } else {
  183. this.canvasHelper.drawUndirectedLine(this.uncompletedEdge, mousePosition);
  184. }
  185. } else if (this.dragVertex) {
  186. this.dragVertex.setPosition(mousePosition);
  187. this.redraw();
  188. }
  189. }
  190.  
  191. onMouseupListener() {
  192. if (this.dragVertex) {
  193. this.dragVertex = null;
  194. this.redraw();
  195. }
  196. }
  197.  
  198. /**
  199. * @param {Event} event
  200. * @private
  201. */
  202. onMousedownListener(event) {
  203. const vertex = this.getVertexByPosition(this.getEventPosition(event));
  204.  
  205. if (!vertex) {
  206. return false;
  207. }
  208.  
  209. this.dragVertex = vertex;
  210. }
  211.  
  212. /**
  213. * @param {Event} event
  214. * @return {Position}
  215. * @private
  216. **/
  217. getEventPosition(event) {
  218. const boundingClientRect = this.canvas.getBoundingClientRect(); //method returns the size of an element and its position relative to the viewport
  219.  
  220. //console.log(Math.floor(event.clientX - boundingClientRect.left));
  221. //console.log((event.clientX - boundingClientRect.left));
  222. return new Position(
  223. Math.floor(event.clientX - boundingClientRect.left), //Math.floo: correct results
  224. Math.floor(event.clientY - boundingClientRect.top)
  225. );
  226. }
  227.  
  228. /**
  229. * @param {Vertex} vertex
  230. * @private
  231. */
  232. drawVertex(vertex) {
  233. console.log("drawVertexMethod = ",vertex.getId());
  234. //let v=(vertex) => vertex.getPosition();
  235. //console.log("(vertex) =>", v(vertex))
  236. this.canvasHelper.drawCircle(vertex.getPosition(), this.vertexRadius, vertex.getId());
  237. }
  238. }
  239.  
  240. /**
  241. * @param {Position} position
  242. * @param {Position} circlePosition
  243. * @param {number} circleRadius
  244. * @return {boolean}
  245. */
  246.  
  247. function checkPositionIsInCircle(position, circlePosition, circleRadius) {
  248. return Math.pow(position.getX() - circlePosition.getX(), 2)
  249. + Math.pow(position.getY() - circlePosition.getY(), 2)
  250. <= Math.pow(circleRadius, 2);
  251. }
  252.  
  253. /**
  254. * @param {UndirectedEdge[]} edges array
  255. * @return {Array}
  256. */
  257.  
  258. function groupEdgesByVertices(edges) {
  259.  
  260. const hashMap = [];
  261. console.log("edge input", edges);
  262. edges.forEach(function (edge) {
  263.  
  264. const getId = (vertex) => vertex.getId();
  265. console.log("const getId =", getId);
  266. //const getId = function (vertex){ (vertex) => vertex.getId()}
  267. //const getId =function (vertex) { (vertex) => vertex.getId()};
  268.  
  269. //setTimeout( ()=>console.log("setTimeout called!"),2000);
  270. //TEST arrow function anonim
  271. //const sayHi = () => { console.log('Hi');}
  272. //sayHi();
  273.  
  274. console.trace("test")
  275. //error
  276. //const hash = edge.getVertices().map(function (vertex) { (vertex) => vertex.getId()}).sort().join(''); //da conctrollare
  277. const hash = edge.getVertices().map(getId).sort().join(''); //da conctrollare
  278.  
  279. if (hashMap[hash]) {
  280. hashMap[hash].push(edge);
  281. } else {
  282. hashMap[hash] = [edge];
  283. }
  284. });
  285.  
  286. const withoutHash = [];
  287.  
  288. for (let i in hashMap) {
  289. withoutHash.push(hashMap[i]);
  290. }
  291.  
  292. return withoutHash;
  293. }
  294.  
  295.  
  296. /**
  297. * @param {Event} event
  298. * @return {boolean}
  299. **/
  300.  
  301.  
  302. function ctrlKeyIsPressed(event) {
  303. return event.ctrlKey || event.metaKey;
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement