ba3512c21f411e1de82e74419be4371868da8679
[myslice.git] / portal / static / js / myslice.js
1 /*
2  * MySlice Class
3  */
4 function isFunction(functionToCheck) {
5  var getType = {};
6  return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
7 }
8
9 function list() {
10         this.elements = [];
11 }
12
13 list.prototype.add = function(element) {
14         if (!this.has(element)) {
15                 this.elements.push(element);
16         }
17 };
18     
19 list.prototype.del = function(element) {
20         if (this.has(element)) {
21                 this.elements.splice(index, 1);
22         }
23 };
24     
25 list.prototype.has = function(element) {
26         index = $.inArray( element, this.elements );
27         if (index != -1) {
28                 return true;
29         }
30         return false;
31 };
32     
33 list.prototype.count = function() {
34     return this.elements.length;
35 };
36
37
38 /*
39  * resources, users, leases
40  */
41
42 function resources() {
43         this.pending = {
44                 toremove: new list(),
45                 toadd: new list(),
46         };
47 };
48
49 function leases() {
50         this.pending = {
51                 toremove: new list(),
52                 toadd: new list(),
53         };
54 };
55
56 function users() {
57         this.pending = {
58                 toremove: new list(),
59                 toadd: new list(),
60         };
61 };
62
63 /*
64  * Slice
65  */
66 function slice(name) {
67         this.name = name;
68         this.resources = new resources();
69         this.users = new users();
70         this.leases = new leases();
71         
72 };
73
74
75 /*
76  * User
77  */
78 function user(user) {
79         this.user = user;
80         this.testbeds = new list();
81         this.slices = new list();
82         
83         for (i = 0; i < this.user.slices.length; i++) {
84                 this.slices[this.user.slices[i]] = new slice(this.user.slices[i]);
85         }
86 };
87
88 user.prototype.slice = function(name) {
89         return this.slices[name];
90 };
91
92 user.prototype.list = function() {
93     for (s in this.slices) {
94     for (o in s) {
95       if (typeof o != 'function') {
96         console.log(o);
97       } else {
98         console.log("w "+o);
99       }
100       }
101     }
102 };
103
104 /*
105  * MySlice
106  */
107 var myslice = {
108         user: {},
109         
110         get_user: function() {
111                 if ($.isEmptyObject(this.user)) {
112                         //this.login(function() { return this.user; });
113             if(localStorage.getItem('user')!='undefined'){
114                             this.user = JSON.parse(localStorage.getItem('user'));
115             }else{
116                 return false;
117             }
118                 }
119                 return this.user;
120         },
121         projects: {},
122         
123         get_projects: function() {
124                 if ($.isEmptyObject(this.projects)) {
125                         //this.login(function() { return this.user; });
126             if(localStorage.getItem('projects')!='undefined'){
127                             this.projects = JSON.parse(localStorage.getItem('projects'));
128             }else{
129                 return false;
130             }
131                 }
132                 return this.projects;
133         },
134
135     loadSlices: function(slices) {
136         if (typeof(slices) == "undefined"){
137
138             if(myslice.user != null && typeof(myslice.user.slices) != "undefined" && myslice.user.slices.length>0){
139                 slices = myslice.user.slices
140             }
141         }
142         // myslice.user is in LocalStorage
143         if(typeof(slices) != "undefined"){
144             /*
145                 This allows progressive loading per AM platform
146                 Launch queries to get the resources and leases in Manifold Cache
147                 XXX platform:object
148                 TODO support cache for prefixed objects
149                 XXX Disabled until it's supported on Manifold side
150             */
151             /*
152             $.post("/rest/platform/", function( data ) {
153                 $.each(data, function(index, p) {
154                     $.post("/rest/"+p.platform+":resource/", function( data ) {
155                     });
156                     $.post("/rest/"+p.platform+":lease/", function( data ) {
157                     });
158                     $.each( slices, function(i, val) {
159                         // Launch a Query for each slice to get it in Manifold Cache
160                         $.post("/rest/"+p.platform+":slice/", { 'filters': { 'slice_hrn' : val } }, function(data) {
161                         });
162                     });
163
164                 });
165             });
166             */
167         }
168
169     },
170         login: function(fn) {
171         user = localStorage.getItem('user');
172         if($.isEmptyObject(user)){
173             // REGISTRY ONLY TO BE REMOVED WITH MANIFOLD-V2
174                     $.post("/rest/myslice:user/",{'filters':{'user_hrn':'$user_hrn'}}, function( data ) {
175                         if (data.length > 0) {
176                             localStorage.setItem('user', JSON.stringify(data[0]));
177                     projects = [];
178                             $.each(data[0].pi_authorities, function(idx, auth) {
179                         // PI on projects
180                         if(auth.split('.').length>2){
181                             if($.inArray(auth,projects) == -1){
182                                 projects.push(auth);
183                             }
184                         }else if (auth.split('.').length>1){
185                         // PI on authorities
186                             // What are the projects under this authority?
187                             $.post("/rest/myslice:authority/",{'fields':['authority_hrn'],'filters':{'authority_hrn':'CONTAINS'+auth}}, function( data ) {
188                                         $.each(data, function(idx, project) {
189                                     if($.inArray(project.authority_hrn,projects) == -1){
190                                         projects.push(project.authority_hrn);
191                                     }
192                                 });
193                             });
194                         }else{
195                             console.log("admin account - we don't list all from root");
196                         }
197                     });
198                     localStorage.setItem('projects', JSON.stringify(projects));
199                     myslice.loadSlices(data[0].slices);
200                     if(isFunction(fn)){
201                         fn();
202                     }
203                 }
204                     });
205         }else{
206             if(isFunction(fn)){
207                 fn();
208             }
209         }
210         },
211
212
213     getSlices: function(name) {
214         
215     },
216     
217     refreshUser: function() {
218         
219     },
220     
221     apply: function() {
222
223         //$('div#loading').show();
224         //this.pending = [];
225         //this.save();
226         //setTimeout(function(){
227                 //$('div#loading').hide();
228                 //window.location.href = '/resources/' + this.slice + '?message=true';
229                 //},6000);
230         
231          
232
233          $.post("/rest/slice/", { 'fields': ['resource','slice_hrn'], 'filters': { 'slice_hrn' : this.slice  } }, function(data) {
234                  console.log(data);
235                  resources = [];
236                  reserved = [];
237                  update = [];
238                  if ('resource' in data[0]) {
239                          $.each(data[0].resource, function(idx, r) {
240                                  resources.push(r.urn);
241                          });
242                  }
243                  //myslice.pending
244                  console.log(myslice.pending);
245                  console.log(resources);
246                  $.each(resources.concat(myslice.pending), function(idx, v) {
247                          update.push( v );
248                  });
249                  console.log(update);
250                  $.post("/update/slice/", { 'filters': { 'slice_hrn' : myslice.slice  }, 'params' : update }, function(data) {
251                          console.log(data);
252                  });
253          });
254         console.log(this.slice);
255     }
256     
257 };
258
259
260 /* MySlice Init */
261
262 // var Reflector = function(obj) {
263   // this.getProperties = function() {
264     // var properties = [];
265     // for (var prop in obj) {
266       // if (typeof obj[prop] != 'function') {
267         // properties.push(prop);
268         // console.log("prop: " + prop);
269       // } else {
270         // console.log("func: " + prop);
271       // }
272     // }
273     // return properties;
274   // };
275 // };
276 // var reflector = new Reflector(myslice.slices[0].resources.pending);
277 // reflector.getProperties();
278
279
280 $(document).ready(function() {
281         //console.log(myslice.user().slices);
282         
283         // $.post("/rest/user/",{'filters':{'user_hrn':'$user_hrn'}}, function(data) {
284                 // myslice.user = new user(data[0]);
285                 // console.log(myslice.user.slices);
286                 // myslice.user.list();
287 //        
288         // }).fail(function() {
289                 // throw "error retreiving user data";
290         // });
291         // Put the object into storage
292         //localStorage.setItem('testObject', JSON.stringify(testObject));
293
294         // Retrieve the object from storage
295         //myslice.load();
296
297 });
298
299 /* EXEMPLES */
300 // add a resource to pending
301
302 //myslice.user.slice('ple.upmc.myslicedemo').resources.pending.add(resource);
303