Multiple Messages supported when sending Renew to several AMs
[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, id) {
6         level = typeof level !== 'undefined' ? level : 'success';
7         timeout = typeof timeout !== 'undefined' ? timeout : false;
8         id = typeof id !== 'undefined' ? id : '';
9
10     // onelab.upmc.slice the dot is causing a pb in the jQuery selector, so replace it !
11     id = id.replace(/\./g,'');
12
13         var el = $('#myslice-message');
14     el.append("<div id='msg-"+level+"-"+id+"'></div>");
15     var msg_div = $('#msg-'+level+'-'+id);
16     msg_div.addClass('alert alert-dismissable myslice-message');
17     msg_div.append("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>");
18     msg_div.append("<span class='glyphicon glyphicon-exclamation-sign'></span>");
19     msg_div.append("<span class='message'></span>");
20         msg_div.find('.message').html(msg);
21         msg_div.addClass('alert-' + level);
22     el.fadeIn('fast');
23         el.parent().fadeIn('fast');
24         if (timeout) {
25                 setTimeout(function(){
26             el.hide();
27             msg_div.remove();
28         },5000);
29         }
30 };
31 /* Table initialisation */
32 $(document).ready(function() {
33         $('button[type=submit]').click(function() {
34                 $('form').submit();
35         });
36         
37         var platformParameters = {};
38
39         //mysliceAlert('hello','danger');
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                                         if (myslice.count() > 0) {
61                                                 $('#badge-pending').text(myslice.count());
62                                                 $('#badge-pending').show();
63                                         }
64                     */
65                                 }
66                     }
67                 } );
68                 
69                 
70                 $("input[type=checkbox]").click(function() {
71                         var cnt = 0;
72                         var id = $(this).val();
73                         var row = $(this).parent().parent()
74                         if (row.hasClass("active")) {
75                                 row.removeClass("active");
76                                 myslice.del(id);
77                                 cnt = myslice.count();
78                 /*
79                                 $('#badge-pending').text(cnt);
80                                 if (cnt <= 0) {
81                                         $('#badge-pending').hide();
82                                 }*/
83                         } else {
84                                 row.addClass("active");
85                                 myslice.add(id);
86                 /*
87                                 cnt = myslice.count();
88                                 $('#badge-pending').text(cnt);
89                                 if (cnt > 0) {
90                                         $('#badge-pending').show();
91                                 }*/
92                         }
93                 });
94         });
95         
96         
97 });
98
99 //http://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request
100 //make sure to expose csrf in our outcoming ajax/post requests
101 $.ajaxSetup({ 
102      beforeSend: function(xhr, settings) {
103          function getCookie(name) {
104              var cookieValue = null;
105              if (document.cookie && document.cookie != '') {
106                  var cookies = document.cookie.split(';');
107                  for (var i = 0; i < cookies.length; i++) {
108                      var cookie = jQuery.trim(cookies[i]);
109                      // Does this cookie string begin with the name we want?
110                  if (cookie.substring(0, name.length + 1) == (name + '=')) {
111                      cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
112                      break;
113                  }
114              }
115          }
116          return cookieValue;
117          }
118          if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
119              // Only send the token to relative URLs i.e. locally.
120              xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
121          }
122      } 
123 });