add tabs for maps
[myslice.git] / plugins / senslabmap / static / js / map.js
index f37a14e..6918e7c 100644 (file)
@@ -1,7 +1,7 @@
 var Senslab = {
   normalize: function(node) {
     var info;
-
+    
     if (node.component_name) { // wsn430-11.devlille.iot-lab.info
       info = node.component_name.split(".");
     } /*else if (node.hrn) { // iotlab.a8-11\.devgrenoble\.iot-lab\.info
@@ -11,14 +11,28 @@ var Senslab = {
 
     if (info && info[2] == "iot-lab" && info[3] == "info") {
       node.arch = info[0].split("-")[0];
-      node.id = info[0].split("-")[1];
+      node.id = parseInt(info[0].split("-")[1]);
       node.site = info[1];
-      return true;
-    } else {
-      console.warn("could not normalize node :");
-      console.warn(node);
-      return false;
+      node.normalized = true;
     }
+  },
+  notify: function(node) {
+    console.log("[Notify] node " + node.id + " is " + node.boot_state);
+  },
+  createMaps: function($container, sites, nodes) {
+    var maps = {};
+    var $menu = $("<ul id='sites-tabs' class='nav nav-tabs' data-tabs='sites-tabs'/>").appendTo($container);
+    var $maps = $("<div id='maps' class='tab-content' />").appendTo($container);
+    
+    $.each(sites, function(i, site) {
+      var entry = $("<li><a href='#" + site + "' data-toggle='tab'>" + site + "</a></li>").appendTo($menu);
+      var $tab = $("<div class='tab-pane' id='" + site + "' />").appendTo($maps);
+      maps[site] = new Senslab.Map($tab);
+      maps[site].addNodes(nodes[site]);
+    });
+    
+    $menu.find("li").eq(0).addClass("active");
+    $maps.find("div").eq(0).addClass("active");
   }
 };
 
@@ -30,10 +44,6 @@ Senslab.Map = function() {
     "Selected": 0x0099CC
   };
 
-  function setColor(node) {
-    node.material.color.setHex(colors[node.boot_state] || colors["Selected"]);
-  }
-  
   var archs = [
     "wsn430",
     "m3",
@@ -48,10 +58,6 @@ Senslab.Map = function() {
     this.phi = -100;
     this.theta = 0;
     this.onRot = false;
-
-    this._notify = function(node) {
-      console.log("[Notify] node " + node.id + " is " + node.boot_state);
-    };
     
     this.pointerDetectRay = new THREE.Raycaster();
     this.pointerDetectRay.ray.direction.set(0, -1, 0);
@@ -76,7 +82,8 @@ Senslab.Map = function() {
       self.$nodeInputs[arch] = $("<input type='text' placeholder='" + arch + "'>")
       .appendTo($container)
       .change(function() {
-        self.updateColor(arch, expand($(this).val()));
+        self.updateSelected(arch, expand($(this).val()));
+        self.update();
       });
     });
     
@@ -106,13 +113,11 @@ Senslab.Map = function() {
           self.pointerDetectRay = self.projector.pickingRay(self.mouse2D.clone(), self.camera);
           var intersects = self.pointerDetectRay.intersectObjects(self.scene.children);
           if (intersects.length > 0) {
-            var node = intersects[0].object;
-            if (node.boot_state != "Suspected") {
-              node.boot_state = node.boot_state == "Alive" ? "Selected" : "Alive";
-              setColor(node);
-              self._notify(node);
-              var $nodeInput = self.$nodeInputs[node.arch];
-              $nodeInput.val(factorize(self.getNodesId(node.arch)));
+            var particle = intersects[0].object;
+            if (particle.data.boot_state != "Suspected") {
+              setState(particle, particle.data.boot_state == "Alive" ? "Selected" : "Alive");
+              var $nodeInput = self.$nodeInputs[particle.data.arch];
+              $nodeInput.val(factorize(self.getNodesId(particle.data.arch)));
               self.update();
             }
           }
@@ -141,13 +146,13 @@ Senslab.Map = function() {
     
     $container.append($canvas);
   }
-
+  
   Map.prototype.getNodesId = function(arch) {
-    var allNodes = this.scene.children;
+    var particles = this.scene.children;
     var nodes = [];
-    for (var i = 0; i < allNodes.length; ++i) {
-      if (allNodes[i].arch == arch && allNodes[i].boot_state == "Selected") {
-        nodes.push(parseInt(allNodes[i].id));
+    for (var i = 0; i < particles.length; ++i) {
+      if (particles[i].data && particles[i].data.arch == arch && particles[i].data.boot_state == "Selected") {
+        nodes.push(particles[i].id);
       }
     }
     return nodes;
@@ -163,9 +168,8 @@ Senslab.Map = function() {
     for(var i = 0; i < nodes.length; ++i) {
       var material = new THREE.ParticleCanvasMaterial({program: circle});
       var particle = new THREE.Particle(material);
-      particle.id = nodes[i].id;
-      particle.arch = nodes[i].arch;
-      particle.boot_state = nodes[i].boot_state;
+      particle.data = nodes[i];
+      particle.id = parseInt(nodes[i].id);
       particle.position.x = (nodes[i].x - center.x) * 10;
       particle.position.y = (nodes[i].y - center.y) * 10;
       particle.position.z = (nodes[i].z - center.z) * 10;
@@ -176,27 +180,27 @@ Senslab.Map = function() {
     this.update();
   };
   
-  Map.prototype.updateColor = function(arch, selected) {
-    var nodes = this.scene.children;
-    for (var i = 0; i < nodes.length; ++i) {
-      if (nodes[i].arch == arch && nodes[i].boot_state != "Suspected") {
-        var node = nodes[i];
-        var id = parseInt(node.id);
-        var state = $.inArray(id, selected) == -1 ? "Alive" : "Selected";
-        if (node.boot_state != state) {
-          node.boot_state = state;
-          this._notify(node);
+  Map.prototype.updateSelected = function(arch, selected) {
+    var particles = this.scene.children;
+    for (var i = 0; i < particles.length; ++i) {
+      if (particles[i].data && particles[i].data.arch == arch && particles[i].data.boot_state != "Suspected") {
+        var particle = particles[i];
+        var state = $.inArray(particle.id, selected) == -1 ? "Alive" : "Selected";
+        if (particle.data.boot_state != state) {
+          setState(particle, state);
         }
-        setColor(node);
       }
     }
-    this.update();
   }
   
   Map.prototype.updatePosition = function() {
-    this.camera.position.x = this.distance * Math.sin(this.theta * Math.PI / 360) * Math.cos(this.phi * Math.PI / 360);
+    this.camera.position.x = this.distance
+      * Math.sin(this.theta * Math.PI / 360)
+      * Math.cos(this.phi * Math.PI / 360);
     this.camera.position.y = this.distance * Math.sin(this.phi * Math.PI / 360);
-    this.camera.position.z = this.distance * Math.cos(this.theta * Math.PI / 360) * Math.cos(this.phi * Math.PI / 360);
+    this.camera.position.z = this.distance
+      * Math.cos(this.theta * Math.PI / 360)
+      * Math.cos(this.phi * Math.PI / 360);
     this.camera.lookAt(this.scene.position);
     this.camera.updateMatrix();
   };
@@ -205,6 +209,17 @@ Senslab.Map = function() {
     this.renderer.render(this.scene, this.camera);
   };
 
+  function setState(particle, state) {
+    particle.data.boot_state = state;
+    setColor(particle);
+    Senslab.notify(particle.data);
+  }
+
+  function setColor(particle) {
+    var color = colors[particle.data.boot_state] || colors["Selected"];
+    particle.material.color.setHex(color);
+  }
+
   function getCenter(nodes) {
     var xmin = 0, ymin = 0, zmin = 0;
     var xmax = 0, ymax = 0, zmax = 0;