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