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