summaryrefslogtreecommitdiffstats
path: root/modules-available/js_stupidtable/clientscript.js
blob: 8b8fc107315e4632c0f9d970dc5911118c7e6961 (plain) (blame)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 Stupid jQuery table plugin.

 https://github.com/joequery/Stupid-Table-Plugin

 Copyright (c) 2012 Joseph McCullough

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 */

(function($) {
	$.fn.stupidtable = function(sortFns) {
		return this.each(function() {
			var $table = $(this);
			sortFns = sortFns || {};
			sortFns = $.extend({}, $.fn.stupidtable.default_sort_fns, sortFns);
			$table.data('sortFns', sortFns);

			$table.on("click.stupidtable", "thead th", function() {
				$(this).stupidsort();
			});

			// to show the sort-arrow next to the table header
			$table.on("aftertablesort", function (event, data) {
				var th = $(this).find("th");
				th.find(".arrow").remove();
				var dir = $.fn.stupidtable.dir;
				var arrow = data.direction === dir.ASC ? "down" : "up";
				th.eq(data.column).append(' <span class="arrow glyphicon glyphicon-chevron-'+arrow+'"></span>');
			});
		});
	};


	// Expects $("#mytable").stupidtable() to have already been called.
	// Call on a table header.
	$.fn.stupidsort = function(force_direction){
		var $this_th = $(this);
		var th_index = 0; // we'll increment this soon
		var dir = $.fn.stupidtable.dir;
		var $table = $this_th.closest("table");
		var datatype = $this_th.data("sort") || null;

		// No datatype? Nothing to do.
		if (datatype === null) {
			return;
		}

		// Account for colspans
		$this_th.parents("tr").find("th").slice(0, $(this).index()).each(function() {
			var cols = $(this).attr("colspan") || 1;
			th_index += parseInt(cols,10);
		});

		var sort_dir;
		if(arguments.length == 1){
			sort_dir = force_direction;
		}
		else{
			sort_dir = force_direction || $this_th.data("sort-default") || dir.ASC;
			if ($this_th.data("sort-dir"))
				sort_dir = $this_th.data("sort-dir") === dir.ASC ? dir.DESC : dir.ASC;
		}

		// Bail if already sorted in this direction
		if ($this_th.data("sort-dir") === sort_dir) {
			return;
		}
		// Go ahead and set sort-dir.  If immediately subsequent calls have same sort-dir they will bail
		$this_th.data("sort-dir", sort_dir);

		$table.trigger("beforetablesort", {column: th_index, direction: sort_dir});

		// More reliable method of forcing a redraw
		$table.css("display");

		// Run sorting asynchronously on a timout to force browser redraw after
		// `beforetablesort` callback. Also avoids locking up the browser too much.
		setTimeout(function() {
			// Gather the elements for this column
			var column = [];
			var sortFns = $table.data('sortFns');
			var sortMethod = sortFns[datatype];
			var collapsedCount = $table.children("tbody").children("tr.collapse").length;
			var trs = $table.children("tbody").children("tr:not(.slx-decollapse)");

			// Extract the data for the column that needs to be sorted and pair it up
			// with the TR itself into a tuple. This way sorting the values will
			// incidentally sort the trs.
			trs.each(function(index,tr) {
				var $e = $(tr).children().eq(th_index);
				var sort_val = $e.data("sort-value");

				// Store and read from the .data cache for display text only sorts
				// instead of looking through the DOM every time
				if(typeof(sort_val) === "undefined"){
					var txt = $e.text();
					$e.data('sort-value', txt);
					sort_val = txt;
				}
				column.push([sort_val, tr]);
			});

			// Sort by the data-order-by value
			column.sort(function(a, b) { return sortMethod(a[0], b[0]); });
			if (sort_dir != dir.ASC)
				column.reverse();

			// Replace the content of tbody with the sorted rows. Strangely
			// enough, .append accomplishes this for us.
			trs = $.map(column, function(kv) { return kv[1]; });

			if (collapsedCount > 0) {
				var showCount = trs.length - collapsedCount;
				for (var i = 0; i < trs.length; i++) {
					if (i < showCount) {
						$(trs[i]).removeClass("collapse");
					} else {
						$(trs[i]).addClass("collapse");
					}
				}
			}

			$table.children("tbody").prepend(trs);

			// Reset siblings
			$table.find("th").data("sort-dir", null).removeClass("sorting-desc sorting-asc");
			$this_th.data("sort-dir", sort_dir).addClass("sorting-"+sort_dir);

			$table.trigger("aftertablesort", {column: th_index, direction: sort_dir});
			$table.css("display");
		}, 10);

		return $this_th;
	};

	// Call on a sortable td to update its value in the sort. This should be the
	// only mechanism used to update a cell's sort value. If your display value is
	// different from your sort value, use jQuery's .text() or .html() to update
	// the td contents, Assumes stupidtable has already been called for the table.
	$.fn.updateSortVal = function(new_sort_val){
		var $this_td = $(this);
		if($this_td.is('[data-sort-value]')){
			// For visual consistency with the .data cache
			$this_td.attr('data-sort-value', new_sort_val);
		}
		$this_td.data("sort-value", new_sort_val);
		return $this_td;
	};

	// ------------------------------------------------------------------
	// Default settings
	// ------------------------------------------------------------------
	$.fn.stupidtable.dir = {ASC: "asc", DESC: "desc"};
	$.fn.stupidtable.default_sort_fns = {
		"int": function(a, b) {
			return parseInt(a, 10) - parseInt(b, 10);
		},
		"float": function(a, b) {
			return parseFloat(a) - parseFloat(b);
		},
		"string": function(a, b) {
			return a.toString().localeCompare(b.toString());
		},
		"string-ins": function(a, b) {
			a = a.toString().toLocaleLowerCase();
			b = b.toString().toLocaleLowerCase();
			return a.localeCompare(b);
		},
		"ipv4":function(a,b){
			var aa = a.split(".");
			var bb = b.split(".");

			var resulta = aa[0]*0x1000000 + aa[1]*0x10000 + aa[2]*0x100 + aa[3]*1;
			var resultb = bb[0]*0x1000000 + bb[1]*0x10000 + bb[2]*0x100 + bb[3]*1;

			return resulta-resultb;
		}
	};
})(jQuery);

document.addEventListener("DOMContentLoaded", function() {
	var table = $(".stupidtable");
	if (table.length) {
		table = table.stupidtable();
	}
});