datatables and myslice ajax js
[unfold.git] / portal / static / js / myslice-ui.js
1
2 /* Table initialisation */
3 $(document).ready(function() {
4         
5         var platformParameters = {};
6         
7         var oTable = $("#objectList");
8                 
9         $.get("/rest/platform", function(data) {
10                 var list = '<div class="list-group-item sl-platfrom"><span class="list-group-item-heading">Testbeds</span></div>';
11                 for(i=0; i<data.length;i++) {
12                         list += '<a href="#" class="list-group-item sl-platfrom" data-platform="'+data[i].platform+'"><span class="list-group-item-heading">'+data[i].platform_longname+'</span><p class="list-group-item-text">'+data[i].platform+'</p></a>';
13                 }
14                 $('#select-platform').html(list);
15         }).done(function() {
16                 // $('a.sl-platfrom').click(function() {
17                         // console.log($(this).data('platform'));
18                         // platformParameters = { "platform" : $(this).data('platform') };
19                         // $('a.sl-platfrom').removeClass('active');
20                         // $(this).addClass('active');
21 //                      
22                         // oTable.load("/list/resource", platformParameters, function(data) {
23                                 // oTable.fnDraw();
24                         // });
25 //                      
26                         // $('body').data('filters',platformParameters);
27                 // });
28         });
29         
30         /* Testbeds list */
31         $('div#testbed-list').ready(function() {
32                 $('table#testbedList').load("/table/testbed", function(data) {
33                         $(this).dataTable( {
34                                 "bScrollInfinite": true,
35                         "bScrollCollapse": true,
36                         "sScrollY": "500px",
37                         "bStateSave": true,
38                         "bPaginate": false,
39                         "bLengthChange": false,
40                         "bFilter": true,
41                         "bSort": true,
42                         "bInfo": false,
43                         "bAutoWidth": false
44                         } );
45                 });
46         });
47         
48         
49         oTable.load("/list/resource", {}, function(data) {
50                 $(this).dataTable( {
51                         "bScrollInfinite": true,
52                 "bScrollCollapse": true,
53                 "sScrollY": "500px",
54                 "bStateSave": true,
55                 "bPaginate": false,
56                 "bLengthChange": false,
57                 "bFilter": true,
58                 "bSort": true,
59                 "bInfo": false,
60                 "bAutoWidth": false
61                 } );
62         });
63 } );
64
65 function drawTable(data) {
66         
67 }
68
69 //http://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request
70 //make sure to expose csrf in our outcoming ajax/post requests
71 $.ajaxSetup({ 
72      beforeSend: function(xhr, settings) {
73          function getCookie(name) {
74              var cookieValue = null;
75              if (document.cookie && document.cookie != '') {
76                  var cookies = document.cookie.split(';');
77                  for (var i = 0; i < cookies.length; i++) {
78                      var cookie = jQuery.trim(cookies[i]);
79                      // Does this cookie string begin with the name we want?
80                  if (cookie.substring(0, name.length + 1) == (name + '=')) {
81                      cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
82                      break;
83                  }
84              }
85          }
86          return cookieValue;
87          }
88          if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
89              // Only send the token to relative URLs i.e. locally.
90              xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
91          }
92      } 
93 });