Bug fix: if no local storage, this.user = false
[unfold.git] / portal / static / js / myslice.js
index 2caa4b8..d9593af 100644 (file)
@@ -6,24 +6,6 @@ function list() {
        this.elements = [];
 }
 
-list.prototype.save = function() {
-       for (var prop in this) {
-      if (typeof this[prop] != 'function') {
-        console.log("prop: " + prop);
-      } else {
-       console.log("func: " + prop);
-      }
-    }
-       //localStorage.setItem(name, JSON.stringify(value));
-};
-
-list.prototype.load = function(name) {
-       this.pending = JSON.parse(localStorage.getItem(name));
-       if (!this.pending) {
-               this.pending = [];
-       }
-};
-
 list.prototype.add = function(element) {
        if (!this.has(element)) {
                this.elements.push(element);
@@ -60,14 +42,14 @@ function resources() {
        };
 };
 
-function users() {
+function leases() {
        this.pending = {
                toremove: new list(),
                toadd: new list(),
        };
 };
 
-function leases() {
+function users() {
        this.pending = {
                toremove: new list(),
                toadd: new list(),
@@ -83,27 +65,19 @@ function slice(name) {
        this.users = new users();
        this.leases = new leases();
        
-};
-slice.prototype.pending = function() {
-       
-};
-slice.prototype.reserve = function() {
-       
-};
-slice.prototype.unreserve = function() {
-       
 };
 
+
 /*
  * User
  */
-function user(u) {
-       this.u = u;
-       this.testbeds = {};
-       this.slices = {};
+function user(user) {
+       this.user = user;
+       this.testbeds = new list();
+       this.slices = new list();
        
-       for (i = 0; i < this.u.slices.length; i++) {
-               this.slices[this.u.slices[i]] = new slice(this.u.slices[i]);
+       for (i = 0; i < this.user.slices.length; i++) {
+               this.slices[this.user.slices[i]] = new slice(this.user.slices[i]);
        }
 };
 
@@ -113,7 +87,7 @@ user.prototype.slice = function(name) {
 
 user.prototype.list = function() {
     for (s in this.slices) {
-       for (o in s) {
+    for (o in s) {
       if (typeof o != 'function') {
         console.log(o);
       } else {
@@ -128,7 +102,29 @@ user.prototype.list = function() {
  */
 var myslice = {
        user: {},
-    
+       
+       user: function() {
+               if ($.isEmptyObject(this.user)) {
+                       //this.login(function() { return this.user; });
+            if(localStorage.getItem('user')!='undefined'){
+                           this.user = JSON.parse(localStorage.getItem('user'));
+            }else{
+                return false;
+            }
+               }
+               return this.user;
+       },
+
+       login: function(fn) {
+        user = localStorage.getItem('user');
+        if($.isEmptyObject(user)){
+                   $.post("/rest/user/",{'filters':{'user_hrn':'$user_hrn'}}, function( data ) {
+                           //myslice.user = new user(data[0]);
+                           localStorage.setItem('user', JSON.stringify(data[0]));
+                   });
+        }
+       },
+
     getSlices: function(name) {
        
     },
@@ -138,38 +134,39 @@ var myslice = {
     },
     
     apply: function() {
-       $('div#loading').show();
-       this.pending = [];
-       this.save();
-       // setTimeout(function(){
-               // $('div#loading').hide();
-               // window.location.href = '/resources/' + this.slice + '?message=true';
-               // },6000);
+
+       //$('div#loading').show();
+       //this.pending = [];
+       //this.save();
+       //setTimeout(function(){
+               //$('div#loading').hide();
+               //window.location.href = '/resources/' + this.slice + '?message=true';
+               //},6000);
        
         
 
-       $.post("/rest/slice/", { 'fields': ['resource','slice_hrn'], 'filters': { 'slice_hrn' : this.slice  } }, function(data) {
-               console.log(data);
-               resources = [];
-               reserved = [];
-               update = [];
-               if ('resource' in data[0]) {
-                       $.each(data[0].resource, function(idx, r) {
-                               resources.push(r.urn);
-                       });
-               }
-               //myslice.pending
-               console.log(myslice.pending);
-               console.log(resources);
-               $.each(resources.concat(myslice.pending), function(idx, v) {
-                       update.push({ 'resource': v });
-               });
-               console.log(update);
-               $.post("/update/slice/", { 'filters': { 'slice_hrn' : 'ple.upmc.myslicedemo'  }, 'params' : update }, function(data) {
-                       console.log(data);
-               });
-       });
-       //console.log(this.slice);
+        $.post("/rest/slice/", { 'fields': ['resource','slice_hrn'], 'filters': { 'slice_hrn' : this.slice  } }, function(data) {
+                console.log(data);
+                resources = [];
+                reserved = [];
+                update = [];
+                if ('resource' in data[0]) {
+                        $.each(data[0].resource, function(idx, r) {
+                                resources.push(r.urn);
+                        });
+                }
+                //myslice.pending
+                console.log(myslice.pending);
+                console.log(resources);
+                $.each(resources.concat(myslice.pending), function(idx, v) {
+                        update.push( v );
+                });
+                console.log(update);
+                $.post("/update/slice/", { 'filters': { 'slice_hrn' : myslice.slice  }, 'params' : update }, function(data) {
+                        console.log(data);
+                });
+        });
+       console.log(this.slice);
     }
     
 };
@@ -196,6 +193,8 @@ var myslice = {
 
 
 $(document).ready(function() {
+       //console.log(myslice.user().slices);
+       
        // $.post("/rest/user/",{'filters':{'user_hrn':'$user_hrn'}}, function(data) {
                // myslice.user = new user(data[0]);
                // console.log(myslice.user.slices);
@@ -215,5 +214,5 @@ $(document).ready(function() {
 /* EXEMPLES */
 // add a resource to pending
 
-myslice.user.slice('ple.upmc.myslicedemo').resources.pending.add(resource);
+//myslice.user.slice('ple.upmc.myslicedemo').resources.pending.add(resource);