Merge branch 'onelab' of ssh://git.onelab.eu/git/myslice into onelab
[unfold.git] / portal / static / js / myslice.js
1 /*
2  * MySlice Class
3  */
4
5 function list() {
6         this.elements = [];
7 }
8
9 list.prototype.add = function(element) {
10         if (!this.has(element)) {
11                 this.elements.push(element);
12         }
13 };
14     
15 list.prototype.del = function(element) {
16         if (this.has(element)) {
17                 this.elements.splice(index, 1);
18         }
19 };
20     
21 list.prototype.has = function(element) {
22         index = $.inArray( element, this.elements );
23         if (index != -1) {
24                 return true;
25         }
26         return false;
27 };
28     
29 list.prototype.count = function() {
30     return this.elements.length;
31 };
32
33
34 /*
35  * resources, users, leases
36  */
37
38 function resources() {
39         this.pending = {
40                 toremove: new list(),
41                 toadd: new list(),
42         };
43 };
44
45 function leases() {
46         this.pending = {
47                 toremove: new list(),
48                 toadd: new list(),
49         };
50 };
51
52 function users() {
53         this.pending = {
54                 toremove: new list(),
55                 toadd: new list(),
56         };
57 };
58
59 /*
60  * Slice
61  */
62 function slice(name) {
63         this.name = name;
64         this.resources = new resources();
65         this.users = new users();
66         this.leases = new leases();
67         
68 };
69
70
71 /*
72  * User
73  */
74 function user(user) {
75         this.user = user;
76         this.testbeds = new list();
77         this.slices = new list();
78         
79         for (i = 0; i < this.user.slices.length; i++) {
80                 this.slices[this.user.slices[i]] = new slice(this.user.slices[i]);
81         }
82 };
83
84 user.prototype.slice = function(name) {
85         return this.slices[name];
86 };
87
88 user.prototype.list = function() {
89     for (s in this.slices) {
90     for (o in s) {
91       if (typeof o != 'function') {
92         console.log(o);
93       } else {
94         console.log("w "+o);
95       }
96       }
97     }
98 };
99
100 /*
101  * MySlice
102  */
103 var myslice = {
104         user: {},
105         
106         user: function() {
107                 if ($.isEmptyObject(this.user)) {
108                         //this.login(function() { return this.user; });
109                         this.user = localStorage.getItem('user');
110                 }
111                 return this.user;
112         },
113
114         login: function(fn) {
115                 $.post("/rest/user/",{'filters':{'user_hrn':'$user_hrn'}}, function( data ) {
116                         myslice.user = new user(data[0]);
117                         localStorage.setItem('user', JSON.stringify(myslice.user));
118                 });
119         },
120
121     getSlices: function(name) {
122         
123     },
124     
125     refreshUser: function() {
126         
127     },
128     
129     apply: function() {
130
131         //$('div#loading').show();
132         //this.pending = [];
133         //this.save();
134         //setTimeout(function(){
135                 //$('div#loading').hide();
136                 //window.location.href = '/resources/' + this.slice + '?message=true';
137                 //},6000);
138         
139          
140
141          $.post("/rest/slice/", { 'fields': ['resource','slice_hrn'], 'filters': { 'slice_hrn' : this.slice  } }, function(data) {
142                  console.log(data);
143                  resources = [];
144                  reserved = [];
145                  update = [];
146                  if ('resource' in data[0]) {
147                          $.each(data[0].resource, function(idx, r) {
148                                  resources.push(r.urn);
149                          });
150                  }
151                  //myslice.pending
152                  console.log(myslice.pending);
153                  console.log(resources);
154                  $.each(resources.concat(myslice.pending), function(idx, v) {
155                          update.push( v );
156                  });
157                  console.log(update);
158                  $.post("/update/slice/", { 'filters': { 'slice_hrn' : myslice.slice  }, 'params' : update }, function(data) {
159                          console.log(data);
160                  });
161          });
162         console.log(this.slice);
163     }
164     
165 };
166
167
168 /* MySlice Init */
169
170 // var Reflector = function(obj) {
171   // this.getProperties = function() {
172     // var properties = [];
173     // for (var prop in obj) {
174       // if (typeof obj[prop] != 'function') {
175         // properties.push(prop);
176         // console.log("prop: " + prop);
177       // } else {
178         // console.log("func: " + prop);
179       // }
180     // }
181     // return properties;
182   // };
183 // };
184 // var reflector = new Reflector(myslice.slices[0].resources.pending);
185 // reflector.getProperties();
186
187
188 $(document).ready(function() {
189         //console.log(myslice.user().slices);
190         
191         // $.post("/rest/user/",{'filters':{'user_hrn':'$user_hrn'}}, function(data) {
192                 // myslice.user = new user(data[0]);
193                 // console.log(myslice.user.slices);
194                 // myslice.user.list();
195 //        
196         // }).fail(function() {
197                 // throw "error retreiving user data";
198         // });
199         // Put the object into storage
200         //localStorage.setItem('testObject', JSON.stringify(testObject));
201
202         // Retrieve the object from storage
203         //myslice.load();
204
205 });
206
207 /* EXEMPLES */
208 // add a resource to pending
209
210 //myslice.user.slice('ple.upmc.myslicedemo').resources.pending.add(resource);
211