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 var myslice = {
6         slice: null,
7     pending: [],
8     reserved: [],
9     
10     add: function(resource) {
11         if (!this.has(resource)) {
12                 this.pending.push(resource);
13                 this.save();
14         }
15     },
16     
17     del: function(resource) {
18         if (this.has(resource)) {
19                 this.pending.splice(index, 1);
20         }
21     },
22     
23     has: function(resource) {
24         index = jQuery.inArray( resource, this.pending );
25         if (index != -1) {
26                 return true;
27         }
28         return false;
29     },
30     
31     count: function() {
32         return this.pending.length;
33     },
34     
35     save: function() {
36         localStorage.setItem('pending', JSON.stringify(this.pending));
37     },
38     
39     load: function() {
40         this.pending = JSON.parse(localStorage.getItem('pending'));
41         if (!this.pending) {
42                 this.pending = [];
43         }
44     },
45     
46     apply: function() {
47         //$('div#loading').show();
48         //this.pending = [];
49         //this.save();
50         //setTimeout(function(){
51                 //$('div#loading').hide();
52                 //window.location.href = '/resources/' + this.slice + '?message=true';
53                 //},6000);
54         
55          
56
57          $.post("/rest/slice/", { 'fields': ['resource','slice_hrn'], 'filters': { 'slice_hrn' : this.slice  } }, function(data) {
58                  console.log(data);
59                  resources = [];
60                  reserved = [];
61                  update = [];
62                  if ('resource' in data[0]) {
63                          $.each(data[0].resource, function(idx, r) {
64                                  resources.push(r.urn);
65                          });
66                  }
67                  //myslice.pending
68                  console.log(myslice.pending);
69                  console.log(resources);
70                  $.each(resources.concat(myslice.pending), function(idx, v) {
71                          update.push( v );
72                  });
73                  console.log(update);
74                  $.post("/update/slice/", { 'filters': { 'slice_hrn' : myslice.slice  }, 'params' : update }, function(data) {
75                          console.log(data);
76                  });
77          });
78         console.log(this.slice);
79     }
80     
81 };
82
83 $(document).ready(function() {
84         // Put the object into storage
85         //localStorage.setItem('testObject', JSON.stringify(testObject));
86
87         // Retrieve the object from storage
88         myslice.load();
89
90 });