Dashboard: hide No Creds msg while fetching projects & slices, display only if it...
[myslice.git] / portal / static / js / myslice.js
index 34a3516..d97d6b0 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 = [];
@@ -103,7 +107,7 @@ user.prototype.list = function() {
 var myslice = {
        user: {},
        
-       user: function() {
+       get_user: function() {
                if ($.isEmptyObject(this.user)) {
                        //this.login(function() { return this.user; });
             if(localStorage.getItem('user')!='undefined'){
@@ -114,6 +118,20 @@ var myslice = {
                }
                return this.user;
        },
+       projects: {},
+       
+       get_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"){
 
@@ -124,20 +142,28 @@ var myslice = {
         // 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/resource/", function( data ) {
-            });
-            $.post("/rest/lease/", function( data ) {
-            });
+            /*
+            $.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) {
+                        });
+                    });
 
-            $.each( slices, function(i, val) {
-                /*
-                Launch a Query for each slice to get resources and leases in Manifold Cache
-                */
-                $.post("/rest/slice/", { 'filters': { 'slice_hrn' : val } }, function(data) {
                 });
             });
+            */
         }
 
     },
@@ -146,12 +172,47 @@ var myslice = {
         if($.isEmptyObject(user)){
             // REGISTRY ONLY TO BE REMOVED WITH MANIFOLD-V2
                    $.post("/rest/myslice:user/",{'filters':{'user_hrn':'$user_hrn'}}, function( data ) {
-                           //myslice.user = new user(data[0]);
-                           localStorage.setItem('user', JSON.stringify(data[0]));
-                myslice.loadSlices(data[0].slices);
+                       if (data.length > 0) {
+                           localStorage.setItem('user', JSON.stringify(data[0]));
+                    projects = [];
+                           $.each(data[0].pi_authorities, function(idx, auth) {
+                        // PI on projects
+                        if(auth.split('.').length>2){
+                            if($.inArray(auth,projects) == -1){
+                                projects.push(auth);
+                            }
+                        }else if (auth.split('.').length>1){
+                        // PI on authorities
+                            // What are the projects under this authority?
+                            $.post("/rest/myslice:authority/",{'fields':['authority_hrn'],'filters':{'authority_hrn':'CONTAINS'+auth}}, function( data ) {
+                                       $.each(data, function(idx, project) {
+                                    if($.inArray(project.authority_hrn,projects) == -1){
+                                        projects.push(project.authority_hrn);
+                                    }
+                                });
+                            });
+                        }else{
+                            console.log("admin account - we don't list all from root");
+                        }
+                    });
+                    localStorage.setItem('projects', JSON.stringify(projects));
+                    myslice.loadSlices(data[0].slices);
+                    if(isFunction(fn)){
+                        fn();
+                    }
+                }else{
+                    $('#credentials_msg').css('display','block');
+                }
                    });
+        }else{
+            $('#credentials_msg').css('display','block');
+            if(isFunction(fn)){
+                fn();
+            }
         }
        },
+
+
     getSlices: function(name) {
        
     },