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