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