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