|
| 1 | +/** |
| 2 | +* Created by Shodhan Save on Jan 23, 2018. |
| 3 | +* Updated @ Jan 25, 2018 |
| 4 | +* Modified for the Puppetboard by Greg Dubicki. |
| 5 | +*/ |
| 6 | + |
| 7 | +/** |
| 8 | +* This plug-in allows sorting of human-readable time delta, viz., |
| 9 | +* "1 week", "2 weeks 3 days", "4 weeks 5 days 6 hours", "1:24 hours" etc. |
| 10 | +* |
| 11 | +* Currently this plugin supports time range from microseconds to decades. |
| 12 | +* |
| 13 | +* The plugin also takes care of singular and plural values like week(s) |
| 14 | +* |
| 15 | +* @name Natural Time Delta |
| 16 | +* @summary Sort human-readable time delta |
| 17 | +* |
| 18 | +* @example |
| 19 | +* $("#example").DataTable({ |
| 20 | +* columnDefs: [ |
| 21 | +* { "type": "natural-time-delta", "targets": 2 } |
| 22 | +* ] |
| 23 | +* }); |
| 24 | +*/ |
| 25 | + |
| 26 | +jQuery.extend(jQuery.fn.dataTableExt.oSort,{ |
| 27 | + "natural-time-delta-pre" : function(data){ |
| 28 | + // get the non-formatted value from title |
| 29 | + data = data.match(/title="(.*?)"/)[1].toLowerCase(); |
| 30 | + |
| 31 | + var total_duration = 0; |
| 32 | + var pattern = /(\d+\s*decades?\s*)?(\d+\s*years?\s*)?(\d+\s*months?\s*)?(\d+\s*weeks?\s*)?(\d+\s*days?\s*)?(\d+:?\d*?\s*hours?\s*)?(\d+\s*minutes?\s*)?(\d+\s*seconds?\s*)?(\d+\s*milliseconds?\s*)?(\d+\s*microseconds?\s*)?/i |
| 33 | + var get_duration = function (el, unit_name, duration_in_seconds) { |
| 34 | + if (el === undefined) { |
| 35 | + return 0; |
| 36 | + } |
| 37 | + |
| 38 | + var split_by = unit_name[0] |
| 39 | + var no_of_units = el.split(split_by)[0].trim() |
| 40 | + |
| 41 | + if ((unit_name === "hour") && (no_of_units.split(':').length === 2)) { |
| 42 | + // this is hour with minutes looking like this: "1:26 hours" |
| 43 | + var hours = parseFloat(no_of_units.split(':')[0]); |
| 44 | + var minutes = parseFloat(no_of_units.split(':')[1]); |
| 45 | + return (hours * 60 * 60) + (minutes * 60); |
| 46 | + } else { |
| 47 | + return parseFloat(no_of_units) * duration_in_seconds; |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + var matches = data.match(pattern); |
| 52 | + matches.reverse(); |
| 53 | + |
| 54 | + var time_elements = [ |
| 55 | + {"unit_name": "microsecond", "duration_in_seconds": 1 / 1000 / 1000}, |
| 56 | + {"unit_name": "millisecond", "duration_in_seconds": 1 / 1000}, |
| 57 | + {"unit_name": "second", "duration_in_seconds": 1}, |
| 58 | + {"unit_name": "minute", "duration_in_seconds": 1 * 60}, |
| 59 | + {"unit_name": "hour", "duration_in_seconds": 1 * 60 * 60}, |
| 60 | + {"unit_name": "day", "duration_in_seconds": 1 * 60 * 60 * 24}, |
| 61 | + {"unit_name": "week", "duration_in_seconds": 1 * 60 * 60 * 24 * 7}, |
| 62 | + {"unit_name": "month", "duration_in_seconds": 1 * 60 * 60 * 24 * 7 * 30}, |
| 63 | + {"unit_name": "year", "duration_in_seconds": 1 * 60 * 60 * 24 * 7 * 30 * 12}, |
| 64 | + {"unit_name": "decade", "duration_in_seconds": 1 * 60 * 60 * 24 * 7 * 30 * 12 * 10}, |
| 65 | + ]; |
| 66 | + |
| 67 | + time_elements.forEach(function (el, i) { |
| 68 | + var duration = get_duration(matches[i], el["unit_name"], el["duration_in_seconds"]); |
| 69 | + total_duration += duration; |
| 70 | + }); |
| 71 | + |
| 72 | + return total_duration || -1; |
| 73 | + }, |
| 74 | + |
| 75 | + "natural-time-delta-asc" : function (a, b) { |
| 76 | + return ((a < b) ? -1 : ((a > b) ? 1 : 0)); |
| 77 | + }, |
| 78 | + |
| 79 | + "natural-time-delta-desc" : function (a, b) { |
| 80 | + return ((a < b) ? 1 : ((a > b) ? -1 : 0)); |
| 81 | + } |
| 82 | +}); |
0 commit comments