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