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