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