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