resources google map
[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                                         var el = $('*[data-key="'+myslice.pending[i]+'"]');
33                                         el.addClass("active");
34                                         el.find('input[type=checkbox]').prop('checked', true);
35                                 }
36                     }
37                 } );
38                 
39                 
40                 $("input[type=checkbox]").click(function() {
41                         var cnt = 0;
42                         var id = $(this).val();
43                         var row = $(this).parent().parent()
44                         if (row.hasClass("active")) {
45                                 row.removeClass("active");
46                                 myslice.del(id);
47                                 cnt = myslice.count();
48                                 $('#badge-pending').text(cnt);
49                                 if (cnt <= 0) {
50                                         $('#badge-pending').hide();
51                                 }
52                         } else {
53                                 row.addClass("active");
54                                 myslice.add(id);
55                                 cnt = myslice.count();
56                                 $('#badge-pending').text(cnt);
57                                 if (cnt > 0) {
58                                         $('#badge-pending').show();
59                                 }
60                         }
61                 });
62         });
63         
64         
65 });
66
67 //http://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request
68 //make sure to expose csrf in our outcoming ajax/post requests
69 $.ajaxSetup({ 
70      beforeSend: function(xhr, settings) {
71          function getCookie(name) {
72              var cookieValue = null;
73              if (document.cookie && document.cookie != '') {
74                  var cookies = document.cookie.split(';');
75                  for (var i = 0; i < cookies.length; i++) {
76                      var cookie = jQuery.trim(cookies[i]);
77                      // Does this cookie string begin with the name we want?
78                  if (cookie.substring(0, name.length + 1) == (name + '=')) {
79                      cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
80                      break;
81                  }
82              }
83          }
84          return cookieValue;
85          }
86          if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
87              // Only send the token to relative URLs i.e. locally.
88              xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
89          }
90      } 
91 });