Bug 11555: Make "All" one of the default options for datatables
[koha_fer] / koha-tmpl / intranet-tmpl / prog / en / js / datatables.js
1 // These default options are for translation but can be used
2 // for any other datatables settings
3 // MSG_DT_* variables comes from datatables-strings.inc
4 // To use it, write:
5 //  $("#table_id").dataTable($.extend(true, {}, dataTableDefaults, {
6 //      // other settings
7 //  } ) );
8 var dataTablesDefaults = {
9     "oLanguage": {
10         "oPaginate": {
11             "sFirst"    : window.MSG_DT_FIRST || "First",
12             "sLast"     : window.MSG_DT_LAST || "Last",
13             "sNext"     : window.MSG_DT_NEXT || "Next",
14             "sPrevious" : window.MSG_DT_PREVIOUS || "Previous"
15         },
16         "sEmptyTable"       : window.MSG_DT_EMPTY_TABLE || "No data available in table",
17         "sInfo"             : window.MSG_DT_INFO || "Showing _START_ to _END_ of _TOTAL_ entries",
18         "sInfoEmpty"        : window.MSG_DT_INFO_EMPTY || "No entries to show",
19         "sInfoFiltered"     : window.MSG_DT_INFO_FILTERED || "(filtered from _MAX_ total entries)",
20         "sLengthMenu"       : window.MSG_DT_LENGTH_MENU || "Show _MENU_ entries",
21         "sLoadingRecords"   : window.MSG_DT_LOADING_RECORDS || "Loading...",
22         "sProcessing"       : window.MSG_DT_PROCESSING || "Processing...",
23         "sSearch"           : window.MSG_DT_SEARCH || "Search:",
24         "sZeroRecords"      : window.MSG_DT_ZERO_RECORDS || "No matching records found"
25     },
26     "sDom": '<"top pager"ilpf>t<"bottom pager"ip>',
27     "aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, window.MSG_DT_ALL || "All"]]
28 };
29
30
31 // Return an array of string containing the values of a particular column
32 $.fn.dataTableExt.oApi.fnGetColumnData = function ( oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty ) {
33     // check that we have a column id
34     if ( typeof iColumn == "undefined" ) return new Array();
35     // by default we only wany unique data
36     if ( typeof bUnique == "undefined" ) bUnique = true;
37     // by default we do want to only look at filtered data
38     if ( typeof bFiltered == "undefined" ) bFiltered = true;
39     // by default we do not wany to include empty values
40     if ( typeof bIgnoreEmpty == "undefined" ) bIgnoreEmpty = true;
41     // list of rows which we're going to loop through
42     var aiRows;
43     // use only filtered rows
44     if (bFiltered == true) aiRows = oSettings.aiDisplay;
45     // use all rows
46     else aiRows = oSettings.aiDisplayMaster; // all row numbers
47
48     // set up data array
49     var asResultData = new Array();
50     for (var i=0,c=aiRows.length; i<c; i++) {
51         iRow = aiRows[i];
52         var aData = this.fnGetData(iRow);
53         var sValue = aData[iColumn];
54         // ignore empty values?
55         if (bIgnoreEmpty == true && sValue.length == 0) continue;
56         // ignore unique values?
57         else if (bUnique == true && jQuery.inArray(sValue, asResultData) > -1) continue;
58         // else push the value onto the result data array
59         else asResultData.push(sValue);
60     }
61     return asResultData;
62 }
63
64 // List of unbind keys (Ctrl, Alt, Direction keys, etc.)
65 // These keys must not launch filtering
66 var blacklist_keys = new Array(0, 16, 17, 18, 37, 38, 39, 40);
67
68 // Set a filtering delay for global search field
69 jQuery.fn.dataTableExt.oApi.fnSetFilteringDelay = function ( oSettings, iDelay ) {
70     /*
71      * Inputs:      object:oSettings - dataTables settings object - automatically given
72      *              integer:iDelay - delay in milliseconds
73      * Usage:       $('#example').dataTable().fnSetFilteringDelay(250);
74      * Author:      Zygimantas Berziunas (www.zygimantas.com) and Allan Jardine
75      * License:     GPL v2 or BSD 3 point style
76      * Contact:     zygimantas.berziunas /AT\ hotmail.com
77      */
78     var
79         _that = this,
80         iDelay = (typeof iDelay == 'undefined') ? 250 : iDelay;
81
82     this.each( function ( i ) {
83         $.fn.dataTableExt.iApiIndex = i;
84         var
85             $this = this,
86             oTimerId = null,
87             sPreviousSearch = null,
88             anControl = $( 'input', _that.fnSettings().aanFeatures.f );
89
90         anControl.unbind( 'keyup.DT' ).bind( 'keyup.DT', function(event) {
91             var $$this = $this;
92             if (blacklist_keys.indexOf(event.keyCode) != -1) {
93                 return this;
94             }else if ( event.keyCode == '13' ) {
95                 $.fn.dataTableExt.iApiIndex = i;
96                 _that.fnFilter( $(this).val() );
97             } else {
98                 if (sPreviousSearch === null || sPreviousSearch != anControl.val()) {
99                     window.clearTimeout(oTimerId);
100                     sPreviousSearch = anControl.val();
101                     oTimerId = window.setTimeout(function() {
102                         $.fn.dataTableExt.iApiIndex = i;
103                         _that.fnFilter( anControl.val() );
104                     }, iDelay);
105                 }
106             }
107         });
108
109         return this;
110     } );
111     return this;
112 }
113
114 // Add a filtering delay on general search and on all input (with a class 'filter')
115 jQuery.fn.dataTableExt.oApi.fnAddFilters = function ( oSettings, sClass, iDelay ) {
116     var table = this;
117     this.fnSetFilteringDelay(iDelay);
118     var filterTimerId = null;
119     $(table).find("input."+sClass).keyup(function(event) {
120       if (blacklist_keys.indexOf(event.keyCode) != -1) {
121         return this;
122       }else if ( event.keyCode == '13' ) {
123         table.fnFilter( $(this).val(), $(this).attr('data-column_num') );
124       } else {
125         window.clearTimeout(filterTimerId);
126         var input = this;
127         filterTimerId = window.setTimeout(function() {
128           table.fnFilter($(input).val(), $(input).attr('data-column_num'));
129         }, iDelay);
130       }
131     });
132 }
133
134 // Useful if you want to filter on dates with 2 inputs (start date and end date)
135 // You have to include calendar.inc to use it
136 function dt_add_rangedate_filter(begindate_id, enddate_id, dateCol) {
137     $.fn.dataTableExt.afnFiltering.push(
138         function( oSettings, aData, iDataIndex ) {
139
140             var beginDate = Date_from_syspref($("#"+begindate_id).val()).getTime();
141             var endDate   = Date_from_syspref($("#"+enddate_id).val()).getTime();
142
143             var data = Date_from_syspref(aData[dateCol]).getTime();
144
145             if ( !parseInt(beginDate) && ! parseInt(endDate) ) {
146                 return true;
147             }
148             else if ( beginDate <= data && !parseInt(endDate) ) {
149                 return true;
150             }
151             else if ( data <= endDate && !parseInt(beginDate) ) {
152                 return true;
153             }
154             else if ( beginDate <= data && data <= endDate) {
155                 return true;
156             }
157             return false;
158         }
159     );
160 }
161
162 //Sorting for dates (uk format)
163 function dt_add_type_uk_date() {
164   jQuery.fn.dataTableExt.aTypes.unshift(
165     function ( sData )
166     {
167       if (sData.match(/(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20|21)\d\d/))
168       {
169         return 'uk_date';
170       }
171       return null;
172     }
173   );
174
175   jQuery.fn.dataTableExt.oSort['uk_date-asc']  = function(a,b) {
176     var re = /(\d{2}\/\d{2}\/\d{4})/;
177     a.match(re);
178     var ukDatea = RegExp.$1.split("/");
179     b.match(re);
180     var ukDateb = RegExp.$1.split("/");
181
182     var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
183     var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
184
185     return ((x < y) ? -1 : ((x > y) ?  1 : 0));
186   };
187
188   jQuery.fn.dataTableExt.oSort['uk_date-desc'] = function(a,b) {
189     var re = /(\d{2}\/\d{2}\/\d{4})/;
190     a.match(re);
191     var ukDatea = RegExp.$1.split("/");
192     b.match(re);
193     var ukDateb = RegExp.$1.split("/");
194
195     var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
196     var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
197
198     return ((x < y) ? 1 : ((x > y) ?  -1 : 0));
199   };
200 }
201
202 // Sorting on html contains
203 // <a href="foo.pl">bar</a> sort on 'bar'
204 function dt_overwrite_html_sorting_localeCompare() {
205     jQuery.fn.dataTableExt.oSort['html-asc']  = function(a,b) {
206         a = a.replace(/<.*?>/g, "").replace(/\s+/g, " ");
207         b = b.replace(/<.*?>/g, "").replace(/\s+/g, " ");
208         if (typeof(a.localeCompare == "function")) {
209            return a.localeCompare(b);
210         } else {
211            return (a > b) ? 1 : ((a < b) ? -1 : 0);
212         }
213     };
214
215     jQuery.fn.dataTableExt.oSort['html-desc'] = function(a,b) {
216         a = a.replace(/<.*?>/g, "").replace(/\s+/g, " ");
217         b = b.replace(/<.*?>/g, "").replace(/\s+/g, " ");
218         if(typeof(b.localeCompare == "function")) {
219             return b.localeCompare(a);
220         } else {
221             return (b > a) ? 1 : ((b < a) ? -1 : 0);
222         }
223     };
224
225     jQuery.fn.dataTableExt.oSort['num-html-asc']  = function(a,b) {
226         var x = a.replace( /<.*?>/g, "" );
227         var y = b.replace( /<.*?>/g, "" );
228         x = parseFloat( x );
229         y = parseFloat( y );
230         return ((x < y) ? -1 : ((x > y) ?  1 : 0));
231     };
232
233     jQuery.fn.dataTableExt.oSort['num-html-desc'] = function(a,b) {
234         var x = a.replace( /<.*?>/g, "" );
235         var y = b.replace( /<.*?>/g, "" );
236         x = parseFloat( x );
237         y = parseFloat( y );
238         return ((x < y) ?  1 : ((x > y) ? -1 : 0));
239     };
240 }
241
242 // Sorting on string without accentued characters
243 function dt_overwrite_string_sorting_localeCompare() {
244     jQuery.fn.dataTableExt.oSort['string-asc']  = function(a,b) {
245         a = a.replace(/<.*?>/g, "").replace(/\s+/g, " ");
246         b = b.replace(/<.*?>/g, "").replace(/\s+/g, " ");
247         if (typeof(a.localeCompare == "function")) {
248            return a.localeCompare(b);
249         } else {
250            return (a > b) ? 1 : ((a < b) ? -1 : 0);
251         }
252     };
253
254     jQuery.fn.dataTableExt.oSort['string-desc'] = function(a,b) {
255         a = a.replace(/<.*?>/g, "").replace(/\s+/g, " ");
256         b = b.replace(/<.*?>/g, "").replace(/\s+/g, " ");
257         if(typeof(b.localeCompare == "function")) {
258             return b.localeCompare(a);
259         } else {
260             return (b > a) ? 1 : ((b < a) ? -1 : 0);
261         }
262     };
263 }
264
265 // Replace a node with a html and js contain.
266 function replace_html( original_node, type ) {
267     switch ( $(original_node).attr('data-type') ) {
268         case "range_dates":
269             var id = $(original_node).attr("data-id");
270             var format = $(original_node).attr("data-format");
271             replace_html_date( original_node, id, format );
272             break;
273         default:
274             alert("_(This node can't be replaced)");
275     }
276 }
277
278 // Replace a node with a "From [date] To [date]" element
279 // Used on tfoot > td
280 function replace_html_date( original_node, id, format ) {
281     var node = $('<span style="white-space:nowrap">' + _("From") + '<input type="text" id="' + id + 'from" readonly="readonly" placeholder=\'' + _("Pick date") + '\' size="7" /><a title="Delete this filter" style="cursor:pointer" onclick=\'$("#' + id + 'from").val("").change();\' >&times;</a></span><br/><span style="white-space:nowrap">' + _("To") + '<input type="text" id="' + id + 'to" readonly="readonly" placeholder=\'' + _("Pick date") + '\' size="7" /><a title="Delete this filter" style="cursor:pointer" onclick=\'$("#' + id + 'to").val("").change();\' >&times;</a></span>');
282     $(original_node).replaceWith(node);
283     var script = document.createElement( 'script' );
284     script.type = 'text/javascript';
285     var script_content = "Calendar.setup({";
286     script_content += "    inputField: \"" + id + "from\",";
287     script_content += "    ifFormat: \"" + format + "\",";
288     script_content += "    button: \"" + id + "from\",";
289     script_content += "    onClose: function(){ $(\"#" + id + "from\").change(); this.hide();}";
290     script_content += "  });";
291     script_content += "  Calendar.setup({";
292     script_content += "    inputField: \"" + id + "to\",";
293     script_content += "    ifFormat: \"" + format + "\",";
294     script_content += "    button: \"" + id + "to\",";
295     script_content += "    onClose: function(){ $(\"#" + id + "to\").change(); this.hide();}";
296     script_content += "  });";
297     script.text = script_content;
298     $(original_node).append( script );
299 }
300
301 $.fn.dataTableExt.oPagination.four_button = {
302     /*
303      * Function: oPagination.four_button.fnInit
304      * Purpose:  Initalise dom elements required for pagination with a list of the pages
305      * Returns:  -
306      * Inputs:   object:oSettings - dataTables settings object
307      *           node:nPaging - the DIV which contains this pagination control
308      *           function:fnCallbackDraw - draw function which must be called on update
309      */
310     "fnInit": function ( oSettings, nPaging, fnCallbackDraw )
311     {
312         nFirst = document.createElement( 'span' );
313         nPrevious = document.createElement( 'span' );
314         nNext = document.createElement( 'span' );
315         nLast = document.createElement( 'span' );
316
317         nFirst.appendChild( document.createTextNode( oSettings.oLanguage.oPaginate.sFirst ) );
318         nPrevious.appendChild( document.createTextNode( oSettings.oLanguage.oPaginate.sPrevious ) );
319         nNext.appendChild( document.createTextNode( oSettings.oLanguage.oPaginate.sNext ) );
320         nLast.appendChild( document.createTextNode( oSettings.oLanguage.oPaginate.sLast ) );
321
322         nFirst.className = "paginate_button first";
323         nPrevious.className = "paginate_button previous";
324         nNext.className="paginate_button next";
325         nLast.className = "paginate_button last";
326
327         nPaging.appendChild( nFirst );
328         nPaging.appendChild( nPrevious );
329         nPaging.appendChild( nNext );
330         nPaging.appendChild( nLast );
331
332         $(nFirst).click( function () {
333             oSettings.oApi._fnPageChange( oSettings, "first" );
334             fnCallbackDraw( oSettings );
335         } );
336
337         $(nPrevious).click( function() {
338             oSettings.oApi._fnPageChange( oSettings, "previous" );
339             fnCallbackDraw( oSettings );
340         } );
341
342         $(nNext).click( function() {
343             oSettings.oApi._fnPageChange( oSettings, "next" );
344             fnCallbackDraw( oSettings );
345         } );
346
347         $(nLast).click( function() {
348             oSettings.oApi._fnPageChange( oSettings, "last" );
349             fnCallbackDraw( oSettings );
350         } );
351
352         /* Disallow text selection */
353         $(nFirst).bind( 'selectstart', function () { return false; } );
354         $(nPrevious).bind( 'selectstart', function () { return false; } );
355         $(nNext).bind( 'selectstart', function () { return false; } );
356         $(nLast).bind( 'selectstart', function () { return false; } );
357     },
358
359     /*
360      * Function: oPagination.four_button.fnUpdate
361      * Purpose:  Update the list of page buttons shows
362      * Returns:  -
363      * Inputs:   object:oSettings - dataTables settings object
364      *           function:fnCallbackDraw - draw function which must be called on update
365      */
366     "fnUpdate": function ( oSettings, fnCallbackDraw )
367     {
368         if ( !oSettings.aanFeatures.p )
369         {
370             return;
371         }
372
373         /* Loop over each instance of the pager */
374         var an = oSettings.aanFeatures.p;
375         for ( var i=0, iLen=an.length ; i<iLen ; i++ )
376         {
377             var buttons = an[i].getElementsByTagName('span');
378             if ( oSettings._iDisplayStart === 0 )
379             {
380                 buttons[0].className = "paginate_disabled_previous";
381                 buttons[1].className = "paginate_disabled_previous";
382             }
383             else
384             {
385                 buttons[0].className = "paginate_enabled_previous";
386                 buttons[1].className = "paginate_enabled_previous";
387             }
388
389             if ( oSettings.fnDisplayEnd() == oSettings.fnRecordsDisplay() )
390             {
391                 buttons[2].className = "paginate_disabled_next";
392                 buttons[3].className = "paginate_disabled_next";
393             }
394             else
395             {
396                 buttons[2].className = "paginate_enabled_next";
397                 buttons[3].className = "paginate_enabled_next";
398             }
399         }
400     }
401 };
402
403 $.fn.dataTableExt.oSort['num-html-asc']  = function(a,b) {
404     var x = a.replace( /<.*?>/g, "" );
405     var y = b.replace( /<.*?>/g, "" );
406     x = parseFloat( x );
407     y = parseFloat( y );
408     return ((x < y) ? -1 : ((x > y) ?  1 : 0));
409 };
410
411 $.fn.dataTableExt.oSort['num-html-desc'] = function(a,b) {
412     var x = a.replace( /<.*?>/g, "" );
413     var y = b.replace( /<.*?>/g, "" );
414     x = parseFloat( x );
415     y = parseFloat( y );
416     return ((x < y) ?  1 : ((x > y) ? -1 : 0));
417 };
418
419 (function() {
420
421 /*
422  * Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license
423  * Author: Jim Palmer (based on chunking idea from Dave Koelle)
424  * Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo
425  * See: http://js-naturalsort.googlecode.com/svn/trunk/naturalSort.js
426  */
427 function naturalSort (a, b) {
428     var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
429         sre = /(^[ ]*|[ ]*$)/g,
430         dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,
431         hre = /^0x[0-9a-f]+$/i,
432         ore = /^0/,
433         // convert all to strings and trim()
434         x = a.toString().replace(sre, '') || '',
435         y = b.toString().replace(sre, '') || '',
436         // chunk/tokenize
437         xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
438         yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
439         // numeric, hex or date detection
440         xD = parseInt(x.match(hre), 10) || (xN.length != 1 && x.match(dre) && Date.parse(x)),
441         yD = parseInt(y.match(hre), 10) || xD && y.match(dre) && Date.parse(y) || null;
442     // first try and sort Hex codes or Dates
443     if (yD)
444         if ( xD < yD ) return -1;
445         else if ( xD > yD )  return 1;
446     // natural sorting through split numeric strings and default strings
447     for(var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
448         // find floats not starting with '0', string or 0 if not defined (Clint Priest)
449         var oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;
450         var oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;
451         // handle numeric vs string comparison - number < string - (Kyle Adams)
452         if (isNaN(oFxNcL) !== isNaN(oFyNcL)) return (isNaN(oFxNcL)) ? 1 : -1;
453         // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
454         else if (typeof oFxNcL !== typeof oFyNcL) {
455             oFxNcL += '';
456             oFyNcL += '';
457         }
458         if (oFxNcL < oFyNcL) return -1;
459         if (oFxNcL > oFyNcL) return 1;
460     }
461     return 0;
462 }
463
464 jQuery.extend( jQuery.fn.dataTableExt.oSort, {
465     "natural-asc": function ( a, b ) {
466         return naturalSort(a,b);
467     },
468
469     "natural-desc": function ( a, b ) {
470         return naturalSort(a,b) * -1;
471     }
472 } );
473
474 }());
475
476 /* Plugin to allow sorting on data stored in a span's title attribute
477  *
478  * Ex: <td><span title="[% ISO_date %]">[% formatted_date %]</span></td>
479  *
480  * In DataTables config:
481  *     "aoColumns": [
482  *        { "sType": "title-string" },
483  *      ]
484  * http://datatables.net/plug-ins/sorting#hidden_title_string
485  */
486 jQuery.extend( jQuery.fn.dataTableExt.oSort, {
487     "title-string-pre": function ( a ) {
488         return a.match(/title="(.*?)"/)[1].toLowerCase();
489     },
490
491     "title-string-asc": function ( a, b ) {
492         return ((a < b) ? -1 : ((a > b) ? 1 : 0));
493     },
494
495     "title-string-desc": function ( a, b ) {
496         return ((a < b) ? 1 : ((a > b) ? -1 : 0));
497     }
498 } );
499
500 /* Plugin to allow sorting on numeric data stored in a span's title attribute
501  *
502  * Ex: <td><span title="[% decimal_number_that_JS_parseFloat_accepts %]">
503  *              [% formatted currency %]
504  *     </span></td>
505  *
506  * In DataTables config:
507  *     "aoColumns": [
508  *        { "sType": "title-numeric" },
509  *      ]
510  * http://datatables.net/plug-ins/sorting#hidden_title
511  */
512 jQuery.extend( jQuery.fn.dataTableExt.oSort, {
513     "title-numeric-pre": function ( a ) {
514         var x = a.match(/title="*(-?[0-9\.]+)/)[1];
515         return parseFloat( x );
516     },
517
518     "title-numeric-asc": function ( a, b ) {
519         return ((a < b) ? -1 : ((a > b) ? 1 : 0));
520     },
521
522     "title-numeric-desc": function ( a, b ) {
523         return ((a < b) ? 1 : ((a > b) ? -1 : 0));
524     }
525 } );
526
527 (function() {
528
529     /* Plugin to allow text sorting to ignore articles
530      *
531      * In DataTables config:
532      *     "aoColumns": [
533      *        { "sType": "anti-the" },
534      *      ]
535      * Based on the plugin found here:
536      * http://datatables.net/plug-ins/sorting#anti_the
537      * Modified to exclude HTML tags from sorting
538      * Extended to accept a string of space-separated articles
539      * from a configuration file (in English, "a," "an," and "the")
540      */
541
542     if(CONFIG_EXCLUDE_ARTICLES_FROM_SORT){
543         var articles = CONFIG_EXCLUDE_ARTICLES_FROM_SORT.split(" ");
544         var rpattern = "";
545         for(i=0;i<articles.length;i++){
546             rpattern += "^" + articles[i] + " ";
547             if(i < articles.length - 1){ rpattern += "|"; }
548         }
549         var re = new RegExp(rpattern, "i");
550     }
551
552     jQuery.extend( jQuery.fn.dataTableExt.oSort, {
553         "anti-the-pre": function ( a ) {
554             var x = String(a).replace( /<[\s\S]*?>/g, "" );
555             var y = x.trim();
556             var z = y.replace(re, "").toLowerCase();
557             return z;
558         },
559
560         "anti-the-asc": function ( a, b ) {
561             return ((a < b) ? -1 : ((a > b) ? 1 : 0));
562         },
563
564         "anti-the-desc": function ( a, b ) {
565             return ((a < b) ? 1 : ((a > b) ? -1 : 0));
566         }
567     });
568
569 }());
570
571 // Remove string between NSB NSB characters
572 $.fn.dataTableExt.oSort['nsb-nse-asc'] = function(a,b) {
573     var pattern = new RegExp("\x88.*\x89");
574     a = a.replace(pattern, "");
575     b = b.replace(pattern, "");
576     return (a > b) ? 1 : ((a < b) ? -1 : 0);
577 }
578 $.fn.dataTableExt.oSort['nsb-nse-desc'] = function(a,b) {
579     var pattern = new RegExp("\x88.*\x89");
580     a = a.replace(pattern, "");
581     b = b.replace(pattern, "");
582     return (b > a) ? 1 : ((b < a) ? -1 : 0);
583 }
584
585 /* Define two custom functions (asc and desc) for basket callnumber sorting */
586 jQuery.fn.dataTableExt.oSort['callnumbers-asc']  = function(x,y) {
587         var x_array = x.split("<div>");
588         var y_array = y.split("<div>");
589
590         /* Pop the first elements, they are empty strings */
591         x_array.shift();
592         y_array.shift();
593
594         x_array = jQuery.map( x_array, function( a ) {
595             return parse_callnumber( a );
596         });
597         y_array = jQuery.map( y_array, function( a ) {
598             return parse_callnumber( a );
599         });
600
601         x_array.sort();
602         y_array.sort();
603
604         x = x_array.shift();
605         y = y_array.shift();
606
607         if ( !x ) { x = ""; }
608         if ( !y ) { y = ""; }
609
610         return ((x < y) ? -1 : ((x > y) ?  1 : 0));
611 };
612
613 jQuery.fn.dataTableExt.oSort['callnumbers-desc'] = function(x,y) {
614         var x_array = x.split("<div>");
615         var y_array = y.split("<div>");
616
617         /* Pop the first elements, they are empty strings */
618         x_array.shift();
619         y_array.shift();
620
621         x_array = jQuery.map( x_array, function( a ) {
622             return parse_callnumber( a );
623         });
624         y_array = jQuery.map( y_array, function( a ) {
625             return parse_callnumber( a );
626         });
627
628         x_array.sort();
629         y_array.sort();
630
631         x = x_array.pop();
632         y = y_array.pop();
633
634         if ( !x ) { x = ""; }
635         if ( !y ) { y = ""; }
636
637         return ((x < y) ?  1 : ((x > y) ? -1 : 0));
638 };
639
640 function parse_callnumber ( html ) {
641     var array = html.split('<span class="callnumber">');
642     if ( array[1] ) {
643         array = array[1].split('</span>');
644         return array[0];
645     } else {
646         return "";
647     }
648 }