File: view\NodePositionView.js
- /*
- * BGPlay.js #9660
- * A web-based service for the visualization of the Internet routing
- *
- * Copyright (c) 2012 Roma Tre University and RIPE NCC
- *
- * See the file LICENSE.txt for copying permission.
- */
-
- /**
- * NodePositionView provides three functionalities:
- * - allows the user to obtain the json of the graph;
- * - allows the user to edit and apply a json to the graph;
- * - applies, if present, a json from widget's parameters.
- * @class NodePositionView
- * @module modules
- */
- var NodePositionView=Backbone.View.extend({
-
- /**
- * The initialization method of this object.
- * @method initialize
- * @param {Map} A map of parameters
- */
- initialize:function(){
- this.environment=this.options.environment;
- this.bgplay=this.environment.bgplay;
- this.fileRoot=this.environment.fileRoot;
- this.eventAggregator=this.environment.eventAggregator;
-
- this.eventAggregator.trigger("moduleLoading", true);
-
- this.environment.optionalParams.push('nodesPosition');
- this.positions="";
- this.eventAggregator.on("destroyAll", function(){
- this.destroyMe();
- },this);
-
- this.render();
- this.eventManager();
-
- this.eventAggregator.on("nodeMoved", function(){
- if (this.popup.is(':visible')){
- this.getNodesPositions();
- }
- },this);
-
- if (this.environment.params.nodesPosition!=null){
- this.environment.config.graph.computeNodesPosition = false;
- this.environment.params.preventNewQueries = true;
- this.textArea.val(this.environment.params.nodesPosition);
- this.apply();
- }
- this.eventAggregator.trigger("moduleLoading", false);
- },
-
- /**
- * This method draws this module (eg. inject the DOM and elements).
- * @method render
- */
- render:function(){
- this.footerDiv = this.environment.thisWidget.dom.closest('.stat-widget').find('.box-buttons');
- this.button = $('<li><a class="button" href="javascript:void(0);">json nodes</a></li>');
- this.popup = $('<div class="json_nodes popup"><h3>Json of the positions of the nodes</h3></div>');
- this.popup.hide();
- parseTemplate('nodePosition.html',this,this.popup,"append");
- this.textArea = this.popup.find('textarea');
- this.applyButton = this.popup.find('input[value=Apply]');
-
- this.footerDiv.find('.left').append(this.button);
- this.footerDiv.append(this.popup);
-
- return this;
- },
-
- /**
- * This method manages the events of the built DOM.
- * @method eventManager
- */
- eventManager:function(){
- var $this=this;
- this.button.click(function(){
- if ($this.popup.is(':visible')){
- $this.popup.hide();
- }else{
- $this.footerDiv.find('.popup').hide();
- $this.getNodesPositions();
- $this.popup.show();
- }
- });
-
- this.applyButton.click(function(){
- $this.apply();
- });
- },
-
- /**
- * This methods builds a string describing the graph.
- * @method getNodesPositions
- */
- getNodesPositions:function(){
- var positions={};
- this.bgplay.get("nodes").forEach(function(node){
- positions[node.id]={x:node.view.x, y:node.view.y};
- });
-
- this.positions=JSON.stringify(positions);
- this.textArea.val(this.positions);
- },
-
- /**
- * This methods applies to each node the coordinates obtained with the getNodesPositions method.
- * @method apply
- */
- apply:function(){
- var newPositions, position, $this;
- $this=this;
- newPositions = JSON.parse(this.textArea.val());
- this.bgplay.get("nodes").forEach(function(node){
- position = newPositions[node.id];
- if (position!=null){
- var x = (position.x - node.view.x);
- var y = (position.y - node.view.y);
- node.view.view.translate(x, y);
- node.oldX=node.view.x;
- node.oldY=node.view.y;
- node.view.x += x;
- node.view.y += y;
- $this.eventAggregator.trigger("nodeMoved",node);
- }
- });
- }
- });
-