major updates to slice reservation page and plugins
[myslice.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                     /*
47                                         if (myslice.count() > 0) {
48                                                 $('#badge-pending').text(myslice.count());
49                                                 $('#badge-pending').show();
50                                         }
51                     */
52                                 }
53                     }
54                 } );
55                 
56                 
57                 $("input[type=checkbox]").click(function() {
58                         var cnt = 0;
59                         var id = $(this).val();
60                         var row = $(this).parent().parent()
61                         if (row.hasClass("active")) {
62                                 row.removeClass("active");
63                                 myslice.del(id);
64                                 cnt = myslice.count();
65                 /*
66                                 $('#badge-pending').text(cnt);
67                                 if (cnt <= 0) {
68                                         $('#badge-pending').hide();
69                                 }*/
70                         } else {
71                                 row.addClass("active");
72                                 myslice.add(id);
73                 /*
74                                 cnt = myslice.count();
75                                 $('#badge-pending').text(cnt);
76                                 if (cnt > 0) {
77                                         $('#badge-pending').show();
78                                 }*/
79                         }
80                 });
81         });
82         
83         
84 });
85
86 //http://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request
87 //make sure to expose csrf in our outcoming ajax/post requests
88 $.ajaxSetup({ 
89      beforeSend: function(xhr, settings) {
90          function getCookie(name) {
91              var cookieValue = null;
92              if (document.cookie && document.cookie != '') {
93                  var cookies = document.cookie.split(';');
94                  for (var i = 0; i < cookies.length; i++) {
95                      var cookie = jQuery.trim(cookies[i]);
96                      // Does this cookie string begin with the name we want?
97                  if (cookie.substring(0, name.length + 1) == (name + '=')) {
98                      cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
99                      break;
100                  }
101              }
102          }
103          return cookieValue;
104          }
105          if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
106              // Only send the token to relative URLs i.e. locally.
107              xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
108          }
109      } 
110 });