API Docs for:
Show:

File: view\NodePositionView.js

  1. /*
  2. * BGPlay.js #9660
  3. * A web-based service for the visualization of the Internet routing
  4. *
  5. * Copyright (c) 2012 Roma Tre University and RIPE NCC
  6. *
  7. * See the file LICENSE.txt for copying permission.
  8. */
  9.  
  10. /**
  11. * NodePositionView provides three functionalities:
  12. * - allows the user to obtain the json of the graph;
  13. * - allows the user to edit and apply a json to the graph;
  14. * - applies, if present, a json from widget's parameters.
  15. * @class NodePositionView
  16. * @module modules
  17. */
  18. var NodePositionView=Backbone.View.extend({
  19.  
  20. /**
  21. * The initialization method of this object.
  22. * @method initialize
  23. * @param {Map} A map of parameters
  24. */
  25. initialize:function(){
  26. this.environment=this.options.environment;
  27. this.bgplay=this.environment.bgplay;
  28. this.fileRoot=this.environment.fileRoot;
  29. this.eventAggregator=this.environment.eventAggregator;
  30.  
  31. this.eventAggregator.trigger("moduleLoading", true);
  32.  
  33. this.environment.optionalParams.push('nodesPosition');
  34. this.positions="";
  35. this.eventAggregator.on("destroyAll", function(){
  36. this.destroyMe();
  37. },this);
  38.  
  39. this.render();
  40. this.eventManager();
  41.  
  42. this.eventAggregator.on("nodeMoved", function(){
  43. if (this.popup.is(':visible')){
  44. this.getNodesPositions();
  45. }
  46. },this);
  47.  
  48. if (this.environment.params.nodesPosition!=null){
  49. this.environment.config.graph.computeNodesPosition = false;
  50. this.environment.params.preventNewQueries = true;
  51. this.textArea.val(this.environment.params.nodesPosition);
  52. this.apply();
  53. }
  54. this.eventAggregator.trigger("moduleLoading", false);
  55. },
  56.  
  57. /**
  58. * This method draws this module (eg. inject the DOM and elements).
  59. * @method render
  60. */
  61. render:function(){
  62. this.footerDiv = this.environment.thisWidget.dom.closest('.stat-widget').find('.box-buttons');
  63. this.button = $('<li><a class="button" href="javascript:void(0);">json nodes</a></li>');
  64. this.popup = $('<div class="json_nodes popup"><h3>Json of the positions of the nodes</h3></div>');
  65. this.popup.hide();
  66. parseTemplate('nodePosition.html',this,this.popup,"append");
  67. this.textArea = this.popup.find('textarea');
  68. this.applyButton = this.popup.find('input[value=Apply]');
  69.  
  70. this.footerDiv.find('.left').append(this.button);
  71. this.footerDiv.append(this.popup);
  72.  
  73. return this;
  74. },
  75.  
  76. /**
  77. * This method manages the events of the built DOM.
  78. * @method eventManager
  79. */
  80. eventManager:function(){
  81. var $this=this;
  82. this.button.click(function(){
  83. if ($this.popup.is(':visible')){
  84. $this.popup.hide();
  85. }else{
  86. $this.footerDiv.find('.popup').hide();
  87. $this.getNodesPositions();
  88. $this.popup.show();
  89. }
  90. });
  91.  
  92. this.applyButton.click(function(){
  93. $this.apply();
  94. });
  95. },
  96.  
  97. /**
  98. * This methods builds a string describing the graph.
  99. * @method getNodesPositions
  100. */
  101. getNodesPositions:function(){
  102. var positions={};
  103. this.bgplay.get("nodes").forEach(function(node){
  104. positions[node.id]={x:node.view.x, y:node.view.y};
  105. });
  106.  
  107. this.positions=JSON.stringify(positions);
  108. this.textArea.val(this.positions);
  109. },
  110.  
  111. /**
  112. * This methods applies to each node the coordinates obtained with the getNodesPositions method.
  113. * @method apply
  114. */
  115. apply:function(){
  116. var newPositions, position, $this;
  117. $this=this;
  118. newPositions = JSON.parse(this.textArea.val());
  119. this.bgplay.get("nodes").forEach(function(node){
  120. position = newPositions[node.id];
  121. if (position!=null){
  122. var x = (position.x - node.view.x);
  123. var y = (position.y - node.view.y);
  124. node.view.view.translate(x, y);
  125. node.oldX=node.view.x;
  126. node.oldY=node.view.y;
  127. node.view.x += x;
  128. node.view.y += y;
  129. $this.eventAggregator.trigger("nodeMoved",node);
  130. }
  131. });
  132. }
  133. });