Bug 22807: Compiled CSS
[koha.git] / koha-tmpl / opac-tmpl / bootstrap / js / datatables.js
blob047fa4c438fbcd840f44d958c215a5183699ac5c
1 // These default options are for translation but can be used
2 // for any other datatables settings
3 // MSG_DT_* variables comes from datatables.inc
4 // To use it, write:
5 //  $("#table_id").dataTable($.extend(true, {}, dataTableDefaults, {
6 //      // other settings
7 //  } ) );
8 var dataTablesDefaults = {
9     "language": {
10         "paginate": {
11             "first"    : window.MSG_DT_FIRST || "First",
12             "last"     : window.MSG_DT_LAST || "Last",
13             "next"     : window.MSG_DT_NEXT || "Next",
14             "previous" : window.MSG_DT_PREVIOUS || "Previous"
15         },
16         "emptyTable"       : window.MSG_DT_EMPTY_TABLE || "No data available in table",
17         "info"             : window.MSG_DT_INFO || "Showing _START_ to _END_ of _TOTAL_ entries",
18         "infoEmpty"        : window.MSG_DT_INFO_EMPTY || "No entries to show",
19         "infoFiltered"     : window.MSG_DT_INFO_FILTERED || "(filtered from _MAX_ total entries)",
20         "lengthMenu"       : window.MSG_DT_LENGTH_MENU || "Show _MENU_ entries",
21         "loadingRecords"   : window.MSG_DT_LOADING_RECORDS || "Loading...",
22         "processing"       : window.MSG_DT_PROCESSING || "Processing...",
23         "search"           : window.MSG_DT_SEARCH || "Search:",
24         "zeroRecords"      : window.MSG_DT_ZERO_RECORDS || "No matching records found"
25     },
26     // "sorting": [$(" - select row position of th -")],
27     "dom": 't',
28     "paginate": false,
29     // "fnHeaderCallback": function() {
30     //     return $('th.sorting.nosort,th.sorting_desc.nosort,th.sorting_asc.nosort').removeClass("sorting sorting_desc sorting_asc").unbind("click");
31     // }
34 /* Plugin to allow sorting on data stored in a span's title attribute
35  *
36  * Ex: <td><span title="[% ISO_date %]">[% formatted_date %]</span></td>
37  *
38  * In DataTables config:
39  *     "columns": [
40  *        { "type": "title-string" },
41  *      ]
42  * http://datatables.net/plug-ins/sorting#hidden_title_string
43  */
44 jQuery.extend( jQuery.fn.dataTableExt.sort, {
45     "title-string-pre": function ( a ) {
46         return a.match(/title="(.*?)"/)[1].toLowerCase();
47     },
49     "title-string-asc": function ( a, b ) {
50         return ((a < b) ? -1 : ((a > b) ? 1 : 0));
51     },
53     "title-string-desc": function ( a, b ) {
54         return ((a < b) ? 1 : ((a > b) ? -1 : 0));
55     }
56 } );
58 /* Plugin to allow sorting numerically on data stored in a span's title attribute
59  *
60  * Ex: <td><span title="[% total %]">Total: [% total %]</span></td>
61  *
62  * In DataTables config:
63  *     "columns": [
64  *        { "type": "title-numeric" }
65  *     ]
66  * http://legacy.datatables.net/plug-ins/sorting#hidden_title
67  */
68 jQuery.extend( jQuery.fn.dataTableExt.sort, {
69     "title-numeric-pre": function ( a ) {
70         var x = a.match(/title="*(-?[0-9\.]+)/)[1];
71         return parseFloat( x );
72     },
74     "title-numeric-asc": function ( a, b ) {
75         return ((a < b) ? -1 : ((a > b) ? 1 : 0));
76     },
78     "title-numeric-desc": function ( a, b ) {
79         return ((a < b) ? 1 : ((a > b) ? -1 : 0));
80     }
81 } );
83 (function() {
85     /* Plugin to allow text sorting to ignore articles
86      *
87      * In DataTables config:
88      *     "columns": [
89      *        { "type": "anti-the" },
90      *      ]
91      * Based on the plugin found here:
92      * http://datatables.net/plug-ins/sorting#anti_the
93      * Modified to exclude HTML tags from sorting
94      * Extended to accept a string of space-separated articles
95      * from a configuration file (in English, "a," "an," and "the")
96      */
98     if(CONFIG_EXCLUDE_ARTICLES_FROM_SORT){
99         var articles = CONFIG_EXCLUDE_ARTICLES_FROM_SORT.split(" ");
100         var rpattern = "";
101         for(i=0;i<articles.length;i++){
102             rpattern += "^" + articles[i] + " ";
103             if(i < articles.length - 1){ rpattern += "|"; }
104         }
105         var re = new RegExp(rpattern, "i");
106     }
108     jQuery.extend( jQuery.fn.dataTableExt.sort, {
109         "anti-the-pre": function ( a ) {
110             var x = String(a).replace( /<[\s\S]*?>/g, "" );
111             var y = x.trim();
112             var z = y.replace(re, "").toLowerCase();
113             return z;
114         },
116         "anti-the-asc": function ( a, b ) {
117             return ((a < b) ? -1 : ((a > b) ? 1 : 0));
118         },
120         "anti-the-desc": function ( a, b ) {
121             return ((a < b) ? 1 : ((a > b) ? -1 : 0));
122         }
123     });
125 }());
127 jQuery.fn.dataTable.ext.errMode = function(settings, note, message) {
128     console.warn(message);
131 (function($) {
133     $.fn.api = function(options) {
134         var settings = null;
135         if(options) {
136             if(!options.criteria || ['contains', 'starts_with', 'ends_with', 'exact'].indexOf(options.criteria.toLowerCase()) === -1) options.criteria = 'contains';
137             options.criteria = options.criteria.toLowerCase();
138             settings = $.extend(true, {}, dataTablesDefaults, {
139                         'deferRender': true,
140                         "paging": true,
141                         'serverSide': true,
142                         'searching': true,
143                         'pagingType': 'full',
144                         'ajax': {
145                             'type': 'GET',
146                             'cache': true,
147                             'dataSrc': 'data',
148                             'beforeSend': function(xhr, settings) {
149                                 this._xhr = xhr;
150                                 if(options.embed) {
151                                     xhr.setRequestHeader('x-koha-embed', Array.isArray(options.embed)?options.embed.join(','):options.embed);
152                                 }
153                                 if(options.header_filter && options.query_parameters) {
154                                     xhr.setRequestHeader('x-koha-query', options.query_parameters);
155                                     delete options.query_parameters;
156                                 }
157                             },
158                             'dataFilter': function(data, type) {
159                                 var json = {data: JSON.parse(data)};
160                                 if(total = this._xhr.getResponseHeader('x-total-count')) {
161                                     json.recordsTotal = total;
162                                     json.recordsFiltered = total;
163                                 }
164                                 return JSON.stringify(json);
165                             },
166                             'data': function( data, settings ) {
167                                 var length = data.length;
168                                 var start  = data.start;
170                                 var dataSet = {
171                                     _page: Math.floor(start/length) + 1,
172                                     _per_page: length
173                                 };
175                                 var filter = data.search.value;
176                                 var query_parameters = settings.aoColumns
177                                 .filter(function(col) {
178                                     return col.bSearchable && typeof col.data == 'string' && (data.columns[col.idx].search.value != '' || filter != '')
179                                 })
180                                 .map(function(col) {
181                                     var part = {};
182                                     var value = data.columns[col.idx].search.value != '' ? data.columns[col.idx].search.value : filter;
183                                     part[!col.data.includes('.')?'me.'+col.data:col.data] = options.criteria === 'exact'?value:{like: (['contains', 'ends_with'].indexOf(options.criteria) !== -1?'%':'')+value+(['contains', 'starts_with'].indexOf(options.criteria) !== -1?'%':'')};
184                                     return part;
185                                 });
187                                 if(query_parameters.length) {
188                                     query_parameters = JSON.stringify(query_parameters.length === 1?query_parameters[0]:query_parameters);
189                                     if(options.header_filter) {
190                                         options.query_parameters = query_parameters;
191                                     } else {
192                                         dataSet.q = query_parameters;
193                                         delete options.query_parameters;
194                                     }
195                                 } else {
196                                     delete options.query_parameters;
197                                 }
199                                 dataSet._match = options.criteria;
201                                 if(options.columns) {
202                                     var order = data.order;
203                                     order.forEach(function (e,i) {
204                                         var order_col      = e.column;
205                                         var order_by       = options.columns[order_col].data;
206                                         var order_dir      = e.dir == 'asc' ? '+' : '-';
207                                         dataSet._order_by = order_dir + (!order_by.includes('.')?'me.'+order_by:order_by);
208                                     });
209                                 }
211                                 return dataSet;
212                             }
213                         }
214                     }, options);
215         }
216         return $(this).dataTable(settings);
217     };
219 })(jQuery);