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                 } else {
110                         return this.user;
111                 }
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                 });
118         },
119
120     getSlices: function(name) {
121         
122     },
123     
124     refreshUser: function() {
125         
126     },
127     
128     apply: function() {
129
130         //$('div#loading').show();
131         //this.pending = [];
132         //this.save();
133         //setTimeout(function(){
134                 //$('div#loading').hide();
135                 //window.location.href = '/resources/' + this.slice + '?message=true';
136                 //},6000);
137         
138          
139
140          $.post("/rest/slice/", { 'fields': ['resource','slice_hrn'], 'filters': { 'slice_hrn' : this.slice  } }, function(data) {
141                  console.log(data);
142                  resources = [];
143                  reserved = [];
144                  update = [];
145                  if ('resource' in data[0]) {
146                          $.each(data[0].resource, function(idx, r) {
147                                  resources.push(r.urn);
148                          });
149                  }
150                  //myslice.pending
151                  console.log(myslice.pending);
152                  console.log(resources);
153                  $.each(resources.concat(myslice.pending), function(idx, v) {
154                          update.push( v );
155                  });
156                  console.log(update);
157                  $.post("/update/slice/", { 'filters': { 'slice_hrn' : myslice.slice  }, 'params' : update }, function(data) {
158                          console.log(data);
159                  });
160          });
161         console.log(this.slice);
162     }
163     
164 };
165
166
167 /* MySlice Init */
168
169 // var Reflector = function(obj) {
170   // this.getProperties = function() {
171     // var properties = [];
172     // for (var prop in obj) {
173       // if (typeof obj[prop] != 'function') {
174         // properties.push(prop);
175         // console.log("prop: " + prop);
176       // } else {
177         // console.log("func: " + prop);
178       // }
179     // }
180     // return properties;
181   // };
182 // };
183 // var reflector = new Reflector(myslice.slices[0].resources.pending);
184 // reflector.getProperties();
185
186
187 $(document).ready(function() {
188         //console.log(myslice.user().slices);
189         
190         // $.post("/rest/user/",{'filters':{'user_hrn':'$user_hrn'}}, function(data) {
191                 // myslice.user = new user(data[0]);
192                 // console.log(myslice.user.slices);
193                 // myslice.user.list();
194 //        
195         // }).fail(function() {
196                 // throw "error retreiving user data";
197         // });
198         // Put the object into storage
199         //localStorage.setItem('testObject', JSON.stringify(testObject));
200
201         // Retrieve the object from storage
202         //myslice.load();
203
204 });
205
206 /* EXEMPLES */
207 // add a resource to pending
208
209 //myslice.user.slice('ple.upmc.myslicedemo').resources.pending.add(resource);
210