X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=portal%2Fstatic%2Fjs%2Fmyslice.js;h=548d5dabe89b34cc1fc2a401c0be0d5b7ed3a106;hb=111d0fcb9681bf94ff184fe43bf636b6d672cbff;hp=2caa4b8ffdc620af20251629b1f2a583186212fa;hpb=a0149650c8519c69236da8b58720eb94393c8307;p=unfold.git diff --git a/portal/static/js/myslice.js b/portal/static/js/myslice.js index 2caa4b8f..548d5dab 100644 --- a/portal/static/js/myslice.js +++ b/portal/static/js/myslice.js @@ -1,29 +1,15 @@ /* * MySlice Class */ +function isFunction(functionToCheck) { + var getType = {}; + return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]'; +} 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 +46,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 +69,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 +91,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 +106,112 @@ 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; + }, + 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) { + 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])); + 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) { + console.log(project.authority_hrn); + 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{ + if(isFunction(fn)){ + fn(); + } + } + + }, + + getSlices: function(name) { }, @@ -138,38 +221,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 +280,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 +301,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);