| @@ -1,130 +1,143 @@ | ||
| 1 | +/*! © SpryMedia Ltd - datatables.net/license */ | |
| 2 | + | |
| 3 | +(function( factory ){ | |
| 4 | + if ( typeof define === 'function' && define.amd ) { | |
| 5 | + // AMD | |
| 6 | + define( ['jquery', 'datatables.net'], function ( $ ) { | |
| 7 | + return factory( $, window, document ); | |
| 8 | + } ); | |
| 9 | + } | |
| 10 | + else if ( typeof exports === 'object' ) { | |
| 11 | + // CommonJS | |
| 12 | + var jq = require('jquery'); | |
| 13 | + var cjsRequires = function (root, $) { | |
| 14 | + if ( ! $.fn.dataTable ) { | |
| 15 | + require('datatables.net')(root, $); | |
| 16 | + } | |
| 17 | + }; | |
| 18 | + | |
| 19 | + if (typeof window === 'undefined') { | |
| 20 | + module.exports = function (root, $) { | |
| 21 | + if ( ! root ) { | |
| 22 | + // CommonJS environments without a window global must pass a | |
| 23 | + // root. This will give an error otherwise | |
| 24 | + root = window; | |
| 25 | + } | |
| 26 | + | |
| 27 | + if ( ! $ ) { | |
| 28 | + $ = jq( root ); | |
| 29 | + } | |
| 30 | + | |
| 31 | + cjsRequires( root, $ ); | |
| 32 | + return factory( $, root, root.document ); | |
| 33 | + }; | |
| 34 | + } | |
| 35 | + else { | |
| 36 | + cjsRequires( window, jq ); | |
| 37 | + module.exports = factory( jq, window, window.document ); | |
| 38 | + } | |
| 39 | + } | |
| 40 | + else { | |
| 41 | + // Browser | |
| 42 | + factory( jQuery, window, document ); | |
| 43 | + } | |
| 44 | +}(function( $, window, document ) { | |
| 45 | +'use strict'; | |
| 46 | +var DataTable = $.fn.dataTable; | |
| 47 | + | |
| 48 | + | |
| 1 | 49 | /** |
| 2 | 50 | * Data can often be a complicated mix of numbers and letters (file names |
| 3 | 51 | * are a common example) and sorting them in a natural manner is quite a |
| 4 | 52 | * difficult problem. |
| 5 | - * | |
| 6 | - * Fortunately a deal of work has already been done in this area by other | |
| 7 | - * authors - the following plug-in uses the [naturalSort() function by Jim | |
| 8 | - * Palmer](http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm-with-unicode-support) to provide natural sorting in DataTables. | |
| 9 | 53 | * |
| 54 | + * Fortunately the Javascript `localeCompare` method is now widely supported | |
| 55 | + * and provides a natural sorting method we can use with DataTables. | |
| 56 | + * | |
| 10 | 57 | * @name Natural sorting |
| 11 | 58 | * @summary Sort data with a mix of numbers and letters _naturally_. |
| 12 | - * @author [Jim Palmer](http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm-with-unicode-support) | |
| 13 | - * @author [Michael Buehler] (https://github.com/AnimusMachina) | |
| 59 | + * @author Allan Jardine | |
| 60 | + * @requires DataTables 2+ | |
| 14 | 61 | * |
| 15 | 62 | * @example |
| 16 | - * $('#example').dataTable( { | |
| 63 | + * // Natural sorting | |
| 64 | + * new DataTable('#myTable', | |
| 17 | 65 | * columnDefs: [ |
| 18 | - * { type: 'natural', targets: 0 } | |
| 66 | + * { type: 'natural', target: 0 } | |
| 19 | 67 | * ] |
| 68 | + * } ); | |
| 69 | + * | |
| 70 | + * @example | |
| 71 | + * // Html can be stripped from sorting by using 'natural-nohtml' such as | |
| 72 | + * new DataTable('#myTable', | |
| 73 | + * columnDefs: [ | |
| 74 | + * { type: 'natural-nohtml', target: 0 } | |
| 75 | + * ] | |
| 20 | 76 | * } ); |
| 21 | 77 | * |
| 22 | - * Html can be stripped from sorting by using 'natural-nohtml' such as | |
| 23 | - * | |
| 24 | - * $('#example').dataTable( { | |
| 78 | + * @example | |
| 79 | + * // Case insensitive natural sorting | |
| 80 | + * new DataTable('#myTable', | |
| 25 | 81 | * columnDefs: [ |
| 26 | - * { type: 'natural-nohtml', targets: 0 } | |
| 82 | + * { type: 'natural-ci', target: 0 } | |
| 27 | 83 | * ] |
| 28 | 84 | * } ); |
| 29 | 85 | * |
| 30 | 86 | */ |
| 87 | +DataTable.type("natural", { | |
| 88 | + order: { | |
| 89 | + asc: function (a, b) { | |
| 90 | + return a.localeCompare(b, navigator.languages[0] || navigator.language, { | |
| 91 | + numeric: true, | |
| 92 | + ignorePunctuation: true, | |
| 93 | + }); | |
| 94 | + }, | |
| 95 | + desc: function (a, b) { | |
| 96 | + return (a.localeCompare(b, navigator.languages[0] || navigator.language, { numeric: true, ignorePunctuation: true }) * | |
| 97 | + -1); | |
| 98 | + }, | |
| 99 | + }, | |
| 100 | + className: "natural-sort", | |
| 101 | +}); | |
| 102 | +DataTable.type("natural-nohtml", { | |
| 103 | + order: { | |
| 104 | + asc: function (a, b) { | |
| 105 | + a = DataTable.util.stripHtml(a); | |
| 106 | + b = DataTable.util.stripHtml(b); | |
| 107 | + return a.localeCompare(b, navigator.languages[0] || navigator.language, { | |
| 108 | + numeric: true, | |
| 109 | + ignorePunctuation: true, | |
| 110 | + }); | |
| 111 | + }, | |
| 112 | + desc: function (a, b) { | |
| 113 | + a = DataTable.util.stripHtml(a); | |
| 114 | + b = DataTable.util.stripHtml(b); | |
| 115 | + return (a.localeCompare(b, navigator.languages[0] || navigator.language, { numeric: true, ignorePunctuation: true }) * | |
| 116 | + -1); | |
| 117 | + }, | |
| 118 | + }, | |
| 119 | + className: "natural-sort", | |
| 120 | +}); | |
| 121 | +DataTable.type("natural-ci", { | |
| 122 | + order: { | |
| 123 | + asc: function (a, b) { | |
| 124 | + a = a.toString().toLowerCase(); | |
| 125 | + b = b.toString().toLowerCase(); | |
| 126 | + return a.localeCompare(b, navigator.languages[0] || navigator.language, { | |
| 127 | + numeric: true, | |
| 128 | + ignorePunctuation: true, | |
| 129 | + }); | |
| 130 | + }, | |
| 131 | + desc: function (a, b) { | |
| 132 | + a = a.toString().toLowerCase(); | |
| 133 | + b = b.toString().toLowerCase(); | |
| 134 | + return (a.localeCompare(b, navigator.languages[0] || navigator.language, { numeric: true, ignorePunctuation: true }) * | |
| 135 | + -1); | |
| 136 | + }, | |
| 137 | + }, | |
| 138 | + className: "natural-sort", | |
| 139 | +}); | |
| 31 | 140 | |
| 32 | -/* eslint-disable */ | |
| 33 | 141 | |
| 34 | -(function() { | |
| 35 | - | |
| 36 | -/* | |
| 37 | - * Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license | |
| 38 | - * Author: Jim Palmer (based on chunking idea from Dave Koelle) | |
| 39 | - * Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo | |
| 40 | - * See: http://js-naturalsort.googlecode.com/svn/trunk/naturalSort.js | |
| 41 | - */ | |
| 42 | -function naturalSort (a, b, html) { | |
| 43 | - var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?%?$|^0x[0-9a-f]+$|[0-9]+)/gi, | |
| 44 | - sre = /(^[ ]*|[ ]*$)/g, | |
| 45 | - dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/, | |
| 46 | - hre = /^0x[0-9a-f]+$/i, | |
| 47 | - ore = /^0/, | |
| 48 | - htmre = /(<([^>]+)>)/ig, | |
| 49 | - // convert all to strings and trim() | |
| 50 | - x = a.toString().replace(sre, '') || '', | |
| 51 | - y = b.toString().replace(sre, '') || ''; | |
| 52 | - // remove html from strings if desired | |
| 53 | - if (!html) { | |
| 54 | - x = x.replace(htmre, ''); | |
| 55 | - y = y.replace(htmre, ''); | |
| 56 | - } | |
| 57 | - // chunk/tokenize | |
| 58 | - var xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), | |
| 59 | - yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), | |
| 60 | - // numeric, hex or date detection | |
| 61 | - xD = parseInt(x.match(hre), 10) || (xN.length !== 1 && x.match(dre) && Date.parse(x)), | |
| 62 | - yD = parseInt(y.match(hre), 10) || xD && y.match(dre) && Date.parse(y) || null; | |
| 63 | - | |
| 64 | - // first try and sort Hex codes or Dates | |
| 65 | - if (yD) { | |
| 66 | - if ( xD < yD ) { | |
| 67 | - return -1; | |
| 68 | - } | |
| 69 | - else if ( xD > yD ) { | |
| 70 | - return 1; | |
| 71 | - } | |
| 72 | - } | |
| 73 | - | |
| 74 | - // natural sorting through split numeric strings and default strings | |
| 75 | - for(var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) { | |
| 76 | - // find floats not starting with '0', string or 0 if not defined (Clint Priest) | |
| 77 | - var oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc], 10) || xN[cLoc] || 0; | |
| 78 | - var oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc], 10) || yN[cLoc] || 0; | |
| 79 | - // handle numeric vs string comparison - number < string - (Kyle Adams) | |
| 80 | - if (isNaN(oFxNcL) !== isNaN(oFyNcL)) { | |
| 81 | - return (isNaN(oFxNcL)) ? 1 : -1; | |
| 82 | - } | |
| 83 | - // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2' | |
| 84 | - else if (typeof oFxNcL !== typeof oFyNcL) { | |
| 85 | - oFxNcL += ''; | |
| 86 | - oFyNcL += ''; | |
| 87 | - } | |
| 88 | - if (oFxNcL < oFyNcL) { | |
| 89 | - return -1; | |
| 90 | - } | |
| 91 | - if (oFxNcL > oFyNcL) { | |
| 92 | - return 1; | |
| 93 | - } | |
| 94 | - } | |
| 95 | - return 0; | |
| 96 | -} | |
| 97 | - | |
| 98 | -jQuery.extend( jQuery.fn.dataTableExt.oSort, { | |
| 99 | - "natural-asc": function ( a, b ) { | |
| 100 | - return naturalSort(a,b,true); | |
| 101 | - }, | |
| 102 | - | |
| 103 | - "natural-desc": function ( a, b ) { | |
| 104 | - return naturalSort(a,b,true) * -1; | |
| 105 | - }, | |
| 106 | - | |
| 107 | - "natural-nohtml-asc": function( a, b ) { | |
| 108 | - return naturalSort(a,b,false); | |
| 109 | - }, | |
| 110 | - | |
| 111 | - "natural-nohtml-desc": function( a, b ) { | |
| 112 | - return naturalSort(a,b,false) * -1; | |
| 113 | - }, | |
| 114 | - | |
| 115 | - "natural-ci-asc": function( a, b ) { | |
| 116 | - a = a.toString().toLowerCase(); | |
| 117 | - b = b.toString().toLowerCase(); | |
| 118 | - | |
| 119 | - return naturalSort(a,b,true); | |
| 120 | - }, | |
| 121 | - | |
| 122 | - "natural-ci-desc": function( a, b ) { | |
| 123 | - a = a.toString().toLowerCase(); | |
| 124 | - b = b.toString().toLowerCase(); | |
| 125 | - | |
| 126 | - return naturalSort(a,b,true) * -1; | |
| 127 | - } | |
| 128 | -} ); | |
| 129 | - | |
| 130 | -}()); | |
| 142 | +return DataTable; | |
| 143 | +})); | |