diff --git a/js/sortable.js b/js/sortable.js index cb3e293..87ed09a 100644 --- a/js/sortable.js +++ b/js/sortable.js @@ -1,5 +1,5 @@ (function() { - var SELECTOR, addEventListener, clickEvents, numberRegExp, sortable, touchDevice, trimRegExp; + var SELECTOR, addEventListener, clickEvents, numberRegExp, sortable, touchDevice, trimRegExp, hasMoment, momentDateFormat; SELECTOR = 'table[data-sortable]'; @@ -11,6 +11,10 @@ touchDevice = 'ontouchstart' in document.documentElement; + hasMoment = (typeof window.moment === 'function'); + + momentDateFormat = 'YYYY-MM-DD HH:mm:ss'; + if (touchDevice) { clickEvents.push('touchstart'); } @@ -32,6 +36,9 @@ if (options.selector == null) { options.selector = SELECTOR; } + if (options.momentDateFormat !== null) { + momentDateFormat = options.momentDateFormat; + } tables = document.querySelectorAll(options.selector); _results = []; for (_i = 0, _len = tables.length; _i < _len; _i++) { @@ -204,10 +211,19 @@ defaultSortDirection: 'ascending', reverse: true, match: function(a) { - return !isNaN(Date.parse(a)); + if (hasMoment) { + return moment(a, momentDateFormat).isValid(); + } else { + return !isNaN(Date.parse(a)); + } }, comparator: function(a) { - return Date.parse(a) || 0; + if (hasMoment) { + var d = moment(a, momentDateFormat); + return d.isValid() ? d.toDate() : 0; + } else { + return Date.parse(a) || 0; + } } }, { name: 'alpha',