fixed styles and js
[myslice.git] / portal / static / js / myslice.js
index 0807d49..40cf8a2 100644 (file)
@@ -1,6 +1,10 @@
 /*
  * MySlice Class
  */
+function isFunction(functionToCheck) {
+ var getType = {};
+ return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
+}
 
 function list() {
        this.elements = [];
@@ -106,16 +110,108 @@ var myslice = {
        user: function() {
                if ($.isEmptyObject(this.user)) {
                        //this.login(function() { return this.user; });
-                       this.user = localStorage.getItem('user');
+            if(localStorage.getItem('user')!='undefined'){
+                           this.user = JSON.parse(localStorage.getItem('user'));
+            }else{
+                return false;
+            }
                }
                return this.user;
        },
+       projects: {},
+       
+       projects: function() {
+               if ($.isEmptyObject(this.projects)) {
+                       //this.login(function() { return this.user; });
+            if(localStorage.getItem('projects')!='undefined'){
+                           this.projects = JSON.parse(localStorage.getItem('projects'));
+            }else{
+                return false;
+            }
+               }
+               return this.projects;
+       },
+
+    loadSlices: function(slices) {
+        if (typeof(slices) == "undefined"){
+
+            if(myslice.user != null && typeof(myslice.user.slices) != "undefined" && myslice.user.slices.length>0){
+                slices = myslice.user.slices
+            }
+        }
+        // myslice.user is in LocalStorage
+        if(typeof(slices) != "undefined"){
+            /*
+                This allows progressive loading per AM platform
+                Launch queries to get the resources and leases in Manifold Cache
+                XXX platform:object
+                TODO support cache for prefixed objects
+                XXX Disabled until it's supported on Manifold side
+            */
+            /*
+            $.post("/rest/platform/", function( data ) {
+                $.each(data, function(index, p) {
+                    $.post("/rest/"+p.platform+":resource/", function( data ) {
+                    });
+                    $.post("/rest/"+p.platform+":lease/", function( data ) {
+                    });
+                    $.each( slices, function(i, val) {
+                        // Launch a Query for each slice to get it in Manifold Cache
+                        $.post("/rest/"+p.platform+":slice/", { 'filters': { 'slice_hrn' : val } }, function(data) {
+                        });
+                    });
 
+                });
+            });
+            */
+        }
+
+    },
        login: function(fn) {
-               $.post("/rest/user/",{'filters':{'user_hrn':'$user_hrn'}}, function( data ) {
-                       myslice.user = new user(data[0]);
-                       localStorage.setItem('user', JSON.stringify(myslice.user));
-               });
+        user = localStorage.getItem('user');
+        if($.isEmptyObject(user)){
+            // REGISTRY ONLY TO BE REMOVED WITH MANIFOLD-V2
+                   $.post("/rest/myslice:user/",{'filters':{'user_hrn':'$user_hrn'}}, function( data ) {
+                       if (data.length > 0) {
+                           localStorage.setItem('user', JSON.stringify(data[0]));
+                    myslice.loadSlices(data[0].slices);
+                    if(isFunction(fn)){
+                        fn();
+                    }
+                }
+                   });
+        }else{
+            if(isFunction(fn)){
+                fn();
+            }
+        }
+
+       },
+       loadProjects: function(fn) {
+           var u = localStorage.getItem('user');
+           if (u !== 'undefined') {
+            user = JSON.parse(u);
+            projects = localStorage.getItem('projects');
+            if($.isEmptyObject(projects)){
+                if($.isEmptyObject(user) || $.isEmptyObject(user.parent_authority)){
+                       $.post("/rest/myslice:user/",{'filters':{'user_hrn':'$user_hrn'},'fields':['parent_authority']}, function( data ) {
+                        parent_authority = data[0].parent_authority;
+    
+                    });
+                }else{
+                    parent_authority = user.parent_authority;
+                }
+                // REGISTRY ONLY TO BE REMOVED WITH MANIFOLD-V2
+                $.post("/rest/myslice:authority/",{'fields':['authority_hrn'],'filters':{'authority_hrn':'CONTAINS'+parent_authority}}, function( data ) {
+                    localStorage.setItem('projects', JSON.stringify(data));
+                });
+            }else{
+                if(isFunction(fn)){
+                    fn();
+                }
+            }
+        }
+
        },
 
     getSlices: function(name) {