fixed styles and js
[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         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         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                     myslice.loadSlices(data[0].slices);
178                     if(isFunction(fn)){
179                         fn();
180                     }
181                 }
182                     });
183         }else{
184             if(isFunction(fn)){
185                 fn();
186             }
187         }
188
189         },
190         loadProjects: function(fn) {
191             var u = localStorage.getItem('user');
192             if (u !== 'undefined') {
193             user = JSON.parse(u);
194             projects = localStorage.getItem('projects');
195             if($.isEmptyObject(projects)){
196                 if($.isEmptyObject(user) || $.isEmptyObject(user.parent_authority)){
197                         $.post("/rest/myslice:user/",{'filters':{'user_hrn':'$user_hrn'},'fields':['parent_authority']}, function( data ) {
198                         parent_authority = data[0].parent_authority;
199     
200                     });
201                 }else{
202                     parent_authority = user.parent_authority;
203                 }
204                 // REGISTRY ONLY TO BE REMOVED WITH MANIFOLD-V2
205                 $.post("/rest/myslice:authority/",{'fields':['authority_hrn'],'filters':{'authority_hrn':'CONTAINS'+parent_authority}}, function( data ) {
206                     localStorage.setItem('projects', JSON.stringify(data));
207                 });
208             }else{
209                 if(isFunction(fn)){
210                     fn();
211                 }
212             }
213         }
214
215         },
216
217     getSlices: function(name) {
218         
219     },
220     
221     refreshUser: function() {
222         
223     },
224     
225     apply: function() {
226
227         //$('div#loading').show();
228         //this.pending = [];
229         //this.save();
230         //setTimeout(function(){
231                 //$('div#loading').hide();
232                 //window.location.href = '/resources/' + this.slice + '?message=true';
233                 //},6000);
234         
235          
236
237          $.post("/rest/slice/", { 'fields': ['resource','slice_hrn'], 'filters': { 'slice_hrn' : this.slice  } }, function(data) {
238                  console.log(data);
239                  resources = [];
240                  reserved = [];
241                  update = [];
242                  if ('resource' in data[0]) {
243                          $.each(data[0].resource, function(idx, r) {
244                                  resources.push(r.urn);
245                          });
246                  }
247                  //myslice.pending
248                  console.log(myslice.pending);
249                  console.log(resources);
250                  $.each(resources.concat(myslice.pending), function(idx, v) {
251                          update.push( v );
252                  });
253                  console.log(update);
254                  $.post("/update/slice/", { 'filters': { 'slice_hrn' : myslice.slice  }, 'params' : update }, function(data) {
255                          console.log(data);
256                  });
257          });
258         console.log(this.slice);
259     }
260     
261 };
262
263
264 /* MySlice Init */
265
266 // var Reflector = function(obj) {
267   // this.getProperties = function() {
268     // var properties = [];
269     // for (var prop in obj) {
270       // if (typeof obj[prop] != 'function') {
271         // properties.push(prop);
272         // console.log("prop: " + prop);
273       // } else {
274         // console.log("func: " + prop);
275       // }
276     // }
277     // return properties;
278   // };
279 // };
280 // var reflector = new Reflector(myslice.slices[0].resources.pending);
281 // reflector.getProperties();
282
283
284 $(document).ready(function() {
285         //console.log(myslice.user().slices);
286         
287         // $.post("/rest/user/",{'filters':{'user_hrn':'$user_hrn'}}, function(data) {
288                 // myslice.user = new user(data[0]);
289                 // console.log(myslice.user.slices);
290                 // myslice.user.list();
291 //        
292         // }).fail(function() {
293                 // throw "error retreiving user data";
294         // });
295         // Put the object into storage
296         //localStorage.setItem('testObject', JSON.stringify(testObject));
297
298         // Retrieve the object from storage
299         //myslice.load();
300
301 });
302
303 /* EXEMPLES */
304 // add a resource to pending
305
306 //myslice.user.slice('ple.upmc.myslicedemo').resources.pending.add(resource);
307