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