update pending badge
[myslice.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         $("table#testbedList").load("/table/network/", { "fields" : ["network_hrn","description"], "options": ["checkbox"] }, function() {
12                         $(this).dataTable( {
13                         "sScrollY": window.innerHeight - 275,
14                         "sDom": "frtiS",
15                 "bScrollCollapse": true,
16                 "bStateSave": true,
17                 "bPaginate": false,
18                 "bLengthChange": false,
19                 "bFilter": false,
20                 "bSort": true,
21                 "bInfo": false,
22                 "bAutoWidth": true,
23                 "bAutoHeight": false
24                 } );
25         });
26         
27         $("#objectList").load("/table/resource/", {"fields" : ["hostname","hrn","country","type"], "options": ["checkbox"] }, function(data) {
28                 $(this).dataTable( {
29                         "sScrollY": window.innerHeight - 275,
30                         "sDom": "frtiS",
31                 "bScrollCollapse": true,
32                 "bStateSave": true,
33                 "bPaginate": false,
34                 "bLengthChange": false,
35                 "bFilter": false,
36                 "bSort": true,
37                 "bInfo": false,
38                 "bAutoWidth": true,
39                 "bAutoHeight": false,
40                 "fnInitComplete": function(oSettings, json) {
41                                 for(var i = 0; i < myslice.pending.length; i++) {
42                                         var el = $('*[data-key="'+myslice.pending[i]+'"]');
43                                         el.addClass("active");
44                                         el.find('input[type=checkbox]').prop('checked', true);
45                                         if (myslice.count() > 0) {
46                                                 $('#badge-pending').text(myslice.count());
47                                                 $('#badge-pending').show();
48                                         }
49                                 }
50                     }
51                 } );
52                 
53                 
54                 $("input[type=checkbox]").click(function() {
55                         var cnt = 0;
56                         var id = $(this).val();
57                         var row = $(this).parent().parent()
58                         if (row.hasClass("active")) {
59                                 row.removeClass("active");
60                                 myslice.del(id);
61                                 cnt = myslice.count();
62                                 $('#badge-pending').text(cnt);
63                                 if (cnt <= 0) {
64                                         $('#badge-pending').hide();
65                                 }
66                         } else {
67                                 row.addClass("active");
68                                 myslice.add(id);
69                                 cnt = myslice.count();
70                                 $('#badge-pending').text(cnt);
71                                 if (cnt > 0) {
72                                         $('#badge-pending').show();
73                                 }
74                         }
75                 });
76         });
77         
78         
79 });
80
81 //http://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request
82 //make sure to expose csrf in our outcoming ajax/post requests
83 $.ajaxSetup({ 
84      beforeSend: function(xhr, settings) {
85          function getCookie(name) {
86              var cookieValue = null;
87              if (document.cookie && document.cookie != '') {
88                  var cookies = document.cookie.split(';');
89                  for (var i = 0; i < cookies.length; i++) {
90                      var cookie = jQuery.trim(cookies[i]);
91                      // Does this cookie string begin with the name we want?
92                  if (cookie.substring(0, name.length + 1) == (name + '=')) {
93                      cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
94                      break;
95                  }
96              }
97          }
98          return cookieValue;
99          }
100          if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
101              // Only send the token to relative URLs i.e. locally.
102              xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
103          }
104      } 
105 });