Add datatables-1.9.4 and jquery-1.10.2 libraries
[proteocache.git] / webapp / resources / js / DT_bootstrap.js
1 /* Set the defaults for DataTables initialisation */
2 $
3                 .extend(
4                                 true,
5                                 $.fn.dataTable.defaults,
6                                 {
7                                         "sDom" : "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
8                                         "sPaginationType" : "bootstrap",
9                                         "oLanguage" : {
10                                                 "sLengthMenu" : "_MENU_ records per page"
11                                         }
12                                 });
13
14 /* Default class modification */
15 $.extend($.fn.dataTableExt.oStdClasses, {
16         "sWrapper" : "dataTables_wrapper form-inline"
17 });
18
19 /* API method to get paging information */
20 $.fn.dataTableExt.oApi.fnPagingInfo = function(oSettings) {
21         return {
22                 "iStart" : oSettings._iDisplayStart,
23                 "iEnd" : oSettings.fnDisplayEnd(),
24                 "iLength" : oSettings._iDisplayLength,
25                 "iTotal" : oSettings.fnRecordsTotal(),
26                 "iFilteredTotal" : oSettings.fnRecordsDisplay(),
27                 "iPage" : oSettings._iDisplayLength === -1 ? 0 : Math
28                                 .ceil(oSettings._iDisplayStart / oSettings._iDisplayLength),
29                 "iTotalPages" : oSettings._iDisplayLength === -1 ? 0 : Math
30                                 .ceil(oSettings.fnRecordsDisplay() / oSettings._iDisplayLength)
31         };
32 };
33
34 /* Bootstrap style pagination control */
35 $.extend($.fn.dataTableExt.oPagination, {
36         "bootstrap" : {
37                 "fnInit" : function(oSettings, nPaging, fnDraw) {
38                         var oLang = oSettings.oLanguage.oPaginate;
39                         var fnClickHandler = function(e) {
40                                 e.preventDefault();
41                                 if (oSettings.oApi._fnPageChange(oSettings, e.data.action)) {
42                                         fnDraw(oSettings);
43                                 }
44                         };
45
46                         /* original code!!!
47                          * $(nPaging).addClass('pagination').append(
48                          *              '<ul>' +
49                          *                      '<li class="prev disabled"><a href="#">&larr; ' + oLang.sPrevious + '</a></li>' + 
50                          *                      '<li class="next disabled"><a href="#">' + oLang.sNext + ' &rarr; </a></li>' + 
51                          *              '</ul>');
52                         */
53                         $(nPaging).addClass('pagination').append(
54                                                 '<a href="#">&larr; ' + oLang.sPrevious + '</a>' + 
55                                                 '&nbsp;&nbsp;&nbsp;<a href="#">' + oLang.sNext + ' &rarr; </a>');
56                         var els = $('a', nPaging);
57                         $(els[0]).bind('click.DT', {
58                                 action : "previous"
59                         }, fnClickHandler);
60                         $(els[1]).bind('click.DT', {
61                                 action : "next"
62                         }, fnClickHandler);
63                 },
64
65                 "fnUpdate" : function(oSettings, fnDraw) {
66                         //var iListLength = 5; // original code!!!
67                         var iListLength = 0;
68                         var oPaging = oSettings.oInstance.fnPagingInfo();
69                         var an = oSettings.aanFeatures.p;
70                         var i, ien, j, sClass, iStart, iEnd, iHalf = Math.floor(iListLength / 2);
71
72                         if (oPaging.iTotalPages < iListLength) {
73                                 iStart = 1;
74                                 iEnd = oPaging.iTotalPages;
75                         } else if (oPaging.iPage <= iHalf) {
76                                 iStart = 1;
77                                 iEnd = iListLength;
78                         } else if (oPaging.iPage >= (oPaging.iTotalPages - iHalf)) {
79                                 iStart = oPaging.iTotalPages - iListLength + 1;
80                                 iEnd = oPaging.iTotalPages;
81                         } else {
82                                 iStart = oPaging.iPage - iHalf + 1;
83                                 iEnd = iStart + iListLength - 1;
84                         }
85
86                         for (i = 0, ien = an.length; i < ien; i++) {
87                                 // Remove the middle elements
88                                 $('li:gt(0)', an[i]).filter(':not(:last)').remove();
89
90                                 // Add the new list items and their event handlers
91                                 for (j = iStart; j <= iEnd; j++) {
92                                         sClass = (j == oPaging.iPage + 1) ? 'class="active"' : '';
93                                         $('<li ' + sClass + '><a href="#">' + j + '</a></li>')
94                                                         .insertBefore($('li:last', an[i])[0]).bind(
95                                                                         'click',
96                                                                         function(e) {
97                                                                                 e.preventDefault();
98                                                                                 oSettings._iDisplayStart = (parseInt($(
99                                                                                                 'a', this).text(), 10) - 1)
100                                                                                                 * oPaging.iLength;
101                                                                                 fnDraw(oSettings);
102                                                                         });
103                                 }
104
105                                 // Add / remove disabled classes from the static elements
106                                 if (oPaging.iPage === 0) {
107                                         $('li:first', an[i]).addClass('disabled');
108                                 } else {
109                                         $('li:first', an[i]).removeClass('disabled');
110                                 }
111
112                                 if (oPaging.iPage === oPaging.iTotalPages - 1
113                                                 || oPaging.iTotalPages === 0) {
114                                         $('li:last', an[i]).addClass('disabled');
115                                 } else {
116                                         $('li:last', an[i]).removeClass('disabled');
117                                 }
118                         }
119                 }
120         }
121 });
122
123 /*
124  * TableTools Bootstrap compatibility Required TableTools 2.1+
125  */
126 if ($.fn.DataTable.TableTools) {
127         // Set the classes that TableTools uses to something suitable for Bootstrap
128         $.extend(true, $.fn.DataTable.TableTools.classes, {
129                 "container" : "DTTT btn-group",
130                 "buttons" : {
131                         "normal" : "btn",
132                         "disabled" : "disabled"
133                 },
134                 "collection" : {
135                         "container" : "DTTT_dropdown dropdown-menu",
136                         "buttons" : {
137                                 "normal" : "",
138                                 "disabled" : "disabled"
139                         }
140                 },
141                 "print" : {
142                         "info" : "DTTT_print_info modal"
143                 },
144                 "select" : {
145                         "row" : "active"
146                 }
147         });
148
149         // Have the collection use a bootstrap compatible dropdown
150         $.extend(true, $.fn.DataTable.TableTools.DEFAULTS.oTags, {
151                 "collection" : {
152                         "container" : "ul",
153                         "button" : "li",
154                         "liner" : "a"
155                 }
156         });
157 }
158
159 /* Table initialisation */
160 $(document).ready(function() {
161         $('#example').dataTable({
162                 "sDom" : "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
163                 "sPaginationType" : "bootstrap",
164                 "oLanguage" : {
165                         "sLengthMenu" : "_MENU_ records per page"
166                 }
167         });
168 });