From b4a73ae5c79a422806075167f2e0c9083b95ba64 Mon Sep 17 00:00:00 2001 From: Jordan Wright Date: Sat, 3 Oct 2015 13:32:09 -0500 Subject: [PATCH] Fixed datetime sorting Fixes #30 --- static/js/datetime-moment.js | 48 ++++++++++++++++++++++++++++++++++++ static/js/gophish.js | 5 ++++ templates/base.html | 1 + 3 files changed, 54 insertions(+) create mode 100644 static/js/datetime-moment.js diff --git a/static/js/datetime-moment.js b/static/js/datetime-moment.js new file mode 100644 index 00000000..aae10d93 --- /dev/null +++ b/static/js/datetime-moment.js @@ -0,0 +1,48 @@ +/** + * This plug-in for DataTables represents the ultimate option in extensibility + * for sorting date / time strings correctly. It uses + * [Moment.js](http://momentjs.com) to create automatic type detection and + * sorting plug-ins for DataTables based on a given format. This way, DataTables + * will automatically detect your temporal information and sort it correctly. + * + * For usage instructions, please see the DataTables blog + * post that [introduces it](//datatables.net/blog/2014-12-18). + * + * @name Ultimate Date / Time sorting + * @summary Sort date and time in any format using Moment.js + * @author [Allan Jardine](//datatables.net) + * @depends DataTables 1.10+, Moment.js 1.7+ + * + * @example + * $.fn.dataTable.moment( 'HH:mm MMM D, YY' ); + * $.fn.dataTable.moment( 'dddd, MMMM Do, YYYY' ); + * + * $('#example').DataTable(); + */ + +(function($) { + +$.fn.dataTable.moment = function ( format, locale ) { + var types = $.fn.dataTable.ext.type; + + // Add type detection + types.detect.unshift( function ( d ) { + // Null and empty values are acceptable + if ( d === '' || d === null ) { + return 'moment-'+format; + } + + return moment( d.replace ? d.replace(/<.*?>/g, '') : d, format, locale, true ).isValid() ? + 'moment-'+format : + null; + } ); + + // Add sorting method - use an integer for the sorting + types.order[ 'moment-'+format+'-pre' ] = function ( d ) { + return d === '' || d === null ? + -Infinity : + parseInt( moment( d.replace ? d.replace(/<.*?>/g, '') : d, format, locale, true ).format( 'x' ), 10 ); + }; +}; + +}(jQuery)); diff --git a/static/js/gophish.js b/static/js/gophish.js index d56f13fd..dc9cbbe0 100644 --- a/static/js/gophish.js +++ b/static/js/gophish.js @@ -140,3 +140,8 @@ var api = { return query("/import/site", "POST", req) } } + +// Register our moment.js datatables listeners +$(document).ready(function(){ + $.fn.dataTable.moment('MMMM Do YYYY, h:mm:ss a'); +}); diff --git a/templates/base.html b/templates/base.html index 072d0599..9fc6ea3d 100644 --- a/templates/base.html +++ b/templates/base.html @@ -92,6 +92,7 @@ + {{template "scripts" .}}