update pending badge
[unfold.git] / portal / static / js / myslice-ui.js
1
2 /* Table initialisation */
3 $(document).ready(function() {
4         
5         var platformParameters = {};
6         
7                 
8         
9         
10         /* Testbeds list */
11
12         $.post("/rest/network/", { "fields" : ["network_hrn", "network_longname", "description"]}, function(data) {
13                         var testbed_data = [];
14                         var testbed_row = "<thead>";
15                         testbed_row += "<tr>";
16                         testbed_row += "<th id=testbed_check><input type=\"checkbox\" name=\"network_hrn\" value=\"all\"/></th>";
17                         testbed_row += "<th id=testbed_icon></th>";
18                         testbed_row += "<th>network_hrn</th>";
19                         testbed_row += "<th>Full name</th>";
20                         testbed_row += "<th>Description</th>";
21                         testbed_row += "</tr>";
22                         testbed_row += "</thead>";
23                         testbed_data.push(testbed_row);
24                         $.each( data, function(key, val) {
25                                 testbed_row = "<tr data-keys=\""+val.network_hrn+"\" class=\"odd\">"
26                                 testbed_row += "<td><input type=\"checkbox\" name=\"network_hrn\" value=\""+val.network_hrn+"\"/></td>";
27                                 testbed_row += "<td><img src='/static/img/testbeds/"+val.network_hrn+".png' alt='' /></td>";
28                                 testbed_row += "<td>"+val.network_hrn+"</td>";
29                                 testbed_row += "<td>"+val.network_longname+"</td>";
30                                 testbed_row += "<td>"+val.description+"</td>";
31                                 testbed_row += "</thead>";
32         
33                                 testbed_data.push(testbed_row);
34                         });
35                 $("table#testbedList").html(testbed_data.join(''));
36                 $("div#testbed-list-loaded").css("display","block");
37                 $("div#testbed-list-loading").css("display","none");
38                         
39                 $("input[type=checkbox]").click(function() {
40                         var cnt = 0;
41                         var id = $(this).val();
42                         var row = $(this).parent().parent()
43                         if (row.hasClass("active")) {
44                                 row.removeClass("active");
45                         } else {
46                                 row.addClass("active");
47                                 }
48                 });
49         });
50         
51         $("#objectList").load("/table/resource/", {"fields" : ["hostname","hrn","country","type"], "options": ["checkbox"] }, function(data) {
52                 $(this).dataTable( {
53                         "sScrollY": window.innerHeight - 275,
54                         "sDom": "frtiS",
55                 "bScrollCollapse": true,
56                 "bStateSave": true,
57                 "bPaginate": false,
58                 "bLengthChange": false,
59                 "bFilter": false,
60                 "bSort": true,
61                 "bInfo": false,
62                 "bAutoWidth": true,
63                 "bAutoHeight": false,
64                 "fnInitComplete": function(oSettings, json) {
65                                 for(var i = 0; i < myslice.pending.length; i++) {
66                                         var el = $('*[data-key="'+myslice.pending[i]+'"]');
67                                         el.addClass("active");
68                                         el.find('input[type=checkbox]').prop('checked', true);
69                                         if (myslice.count() > 0) {
70                                                 $('#badge-pending').text(myslice.count());
71                                                 $('#badge-pending').show();
72                                         }
73                                 }
74                     }
75                 } );
76                 
77                 
78                 $("input[type=checkbox]").click(function() {
79                         var cnt = 0;
80                         var id = $(this).val();
81                         var row = $(this).parent().parent()
82                         if (row.hasClass("active")) {
83                                 row.removeClass("active");
84                                 myslice.del(id);
85                                 cnt = myslice.count();
86                                 $('#badge-pending').text(cnt);
87                                 if (cnt <= 0) {
88                                         $('#badge-pending').hide();
89                                 }
90                         } else {
91                                 row.addClass("active");
92                                 myslice.add(id);
93                                 cnt = myslice.count();
94                                 $('#badge-pending').text(cnt);
95                                 if (cnt > 0) {
96                                         $('#badge-pending').show();
97                                 }
98                         }
99                 });
100         });
101         
102         
103 });
104
105 //http://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request
106 //make sure to expose csrf in our outcoming ajax/post requests
107 $.ajaxSetup({ 
108      beforeSend: function(xhr, settings) {
109          function getCookie(name) {
110              var cookieValue = null;
111              if (document.cookie && document.cookie != '') {
112                  var cookies = document.cookie.split(';');
113                  for (var i = 0; i < cookies.length; i++) {
114                      var cookie = jQuery.trim(cookies[i]);
115                      // Does this cookie string begin with the name we want?
116                  if (cookie.substring(0, name.length + 1) == (name + '=')) {
117                      cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
118                      break;
119                  }
120              }
121          }
122          return cookieValue;
123          }
124          if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
125              // Only send the token to relative URLs i.e. locally.
126              xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
127          }
128      } 
129 });