summaryrefslogtreecommitdiffstats
path: root/data/js/tablesorter.js
blob: d500f2aded1dde78d3e7a2d98327a6db499990ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
(function () {
'use strict';
define(['jquery', 'jquery.tablesorter'], function ($) {
	var PrivateFunctions = {
		setupParser: function () {
			// source: https://projects.archlinux.org/archweb.git/tree/sitestatic/archweb.js
			$.tablesorter.addParser({
				id: 'filesize',
				re: /^(\d+(?:\.\d+)?)(bytes?|[KMGTPEZY]i?B|B)$/,
				is: function(s) {
					return this.re.test(s);
				},
				format: function(s) {
					var matches = this.re.exec(s);
					if (!matches) {
						return 0;
					}
					var size = parseFloat(matches[1]),
						suffix = matches[2];

					switch(suffix) {
						/* intentional fall-through at each level */
						case 'YB':
						case 'YiB':
							size *= 1024;
						case 'ZB':
						case 'ZiB':
							size *= 1024;
						case 'EB':
						case 'EiB':
							size *= 1024;
						case 'PB':
						case 'PiB':
							size *= 1024;
						case 'TB':
						case 'TiB':
							size *= 1024;
						case 'GB':
						case 'GiB':
							size *= 1024;
						case 'MB':
						case 'MiB':
							size *= 1024;
						case 'KB':
						case 'KiB':
							size *= 1024;
					}
					return size;
				},
				type: 'numeric'
			});
		},

		textExtraction: function (node) {
			var attr = $(node).attr('data-sort-value');
			if (!_.isUndefined(attr) && attr !== false) {
				var intAttr = parseInt(attr);
				if (!_.isNaN(intAttr)) {
					return intAttr;
				}
				return attr;
			}
			return $(node).text();
		},

		setupTableSorter: function () {
			$(".tablesorter").tablesorter({
				textExtraction: this.textExtraction
			});
		}
	};

	var TableSorter = {
		initialize: function () {
			PrivateFunctions.setupParser();
			PrivateFunctions.setupTableSorter();
		}
	};

	return TableSorter;
});
})();