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