Debian Codegraph @

stupidtable.js

raw symbols SCIP
1 // Stupid jQuery table plugin.
2
3 (function($) {
4 $.fn.stupidtable = function(sortFns) {
5 return this.each(function() {
6 var $table = $(this);
7 sortFns = sortFns || {};
8 sortFns = $.extend({}, $.fn.stupidtable.default_sort_fns, sortFns);
9 $table.data('sortFns', sortFns);
10 $table.stupidtable_build();
11
12 $table.on("click.stupidtable", "thead th", function() {
13 $(this).stupidsort();
14 });
15
16 // Sort th immediately if data-sort-onload="yes" is specified. Limit to
17 // the first one found - only one default sort column makes sense anyway.
18 var $th_onload_sort = $table.find("th[data-sort-onload=yes]").eq(0);
19 $th_onload_sort.stupidsort();
20 });
21 };
22
23 // ------------------------------------------------------------------
24 // Default settings
25 // ------------------------------------------------------------------
26 $.fn.stupidtable.default_settings = {
27 should_redraw: function(sort_info){
28 return true;
29 },
30 will_manually_build_table: false
31 };
32 $.fn.stupidtable.dir = {ASC: "asc", DESC: "desc"};
33 $.fn.stupidtable.default_sort_fns = {
34 "int": function(a, b) {
35 return parseInt(a, 10) - parseInt(b, 10);
36 },
37 "float": function(a, b) {
38 return parseFloat(a) - parseFloat(b);
39 },
40 "string": function(a, b) {
41 return a.toString().localeCompare(b.toString());
42 },
43 "string-ins": function(a, b) {
44 a = a.toString().toLocaleLowerCase();
45 b = b.toString().toLocaleLowerCase();
46 return a.localeCompare(b);
47 }
48 };
49
50 // Allow specification of settings on a per-table basis. Call on a table
51 // jquery object. Call *before* calling .stuidtable();
52 $.fn.stupidtable_settings = function(settings) {
53 return this.each(function() {
54 var $table = $(this);
55 var final_settings = $.extend({}, $.fn.stupidtable.default_settings, settings);
56 $table.stupidtable.settings = final_settings;
57 });
58 };
59
60
61 // Expects $("#mytable").stupidtable() to have already been called.
62 // Call on a table header.
63 $.fn.stupidsort = function(force_direction){
64 var $this_th = $(this);
65 var datatype = $this_th.data("sort") || null;
66
67 // No datatype? Nothing to do.
68 if (datatype === null) {
69 return;
70 }
71
72 var dir = $.fn.stupidtable.dir;
73 var $table = $this_th.closest("table");
74
75 var sort_info = {
76 $th: $this_th,
77 $table: $table,
78 datatype: datatype
79 };
80
81
82 // Bring in default settings if none provided
83 if(!$table.stupidtable.settings){
84 $table.stupidtable.settings = $.extend({}, $.fn.stupidtable.default_settings);
85 }
86
87 sort_info.compare_fn = $table.data('sortFns')[datatype];
88 sort_info.th_index = calculateTHIndex(sort_info);
89 sort_info.sort_dir = calculateSortDir(force_direction, sort_info);
90
91 $this_th.data("sort-dir", sort_info.sort_dir);
92 $table.trigger("beforetablesort", {column: sort_info.th_index, direction: sort_info.sort_dir, $th: $this_th});
93
94 // More reliable method of forcing a redraw
95 $table.css("display");
96
97 // Run sorting asynchronously on a timout to force browser redraw after
98 // `beforetablesort` callback. Also avoids locking up the browser too much.
99 setTimeout(function() {
100 if(!$table.stupidtable.settings.will_manually_build_table){
101 $table.stupidtable_build();
102 }
103 var table_structure = sortTable(sort_info);
104 var trs = getTableRowsFromTableStructure(table_structure, sort_info);
105
106 if(!$table.stupidtable.settings.should_redraw(sort_info)){
107 return;
108 }
109 $table.children("tbody").append(trs);
110
111 updateElementData(sort_info);
112 $table.trigger("aftertablesort", {column: sort_info.th_index, direction: sort_info.sort_dir, $th: $this_th});
113 $table.css("display");
114
115 }, 10);
116 return $this_th;
117 };
118
119 // Call on a sortable td to update its value in the sort. This should be the
120 // only mechanism used to update a cell's sort value. If your display value is
121 // different from your sort value, use jQuery's .text() or .html() to update
122 // the td contents, Assumes stupidtable has already been called for the table.
123 $.fn.updateSortVal = function(new_sort_val){
124 var $this_td = $(this);
125 if($this_td.is('[data-sort-value]')){
126 // For visual consistency with the .data cache
127 $this_td.attr('data-sort-value', new_sort_val);
128 }
129 $this_td.data("sort-value", new_sort_val);
130 return $this_td;
131 };
132
133
134 $.fn.stupidtable_build = function(){
135 return this.each(function() {
136 var $table = $(this);
137 var table_structure = [];
138 var trs = $table.children("tbody").children("tr");
139 trs.each(function(index,tr) {
140
141 // ====================================================================
142 // Transfer to using internal table structure
143 // ====================================================================
144 var ele = {
145 $tr: $(tr),
146 columns: [],
147 index: index
148 };
149
150 $(tr).children('td').each(function(idx, td){
151 var sort_val = $(td).data("sort-value");
152
153 // Store and read from the .data cache for display text only sorts
154 // instead of looking through the DOM every time
155 if(typeof(sort_val) === "undefined"){
156 var txt = $(td).text();
157 $(td).data('sort-value', txt);
158 sort_val = txt;
159 }
160 ele.columns.push(sort_val);
161 });
162 table_structure.push(ele);
163 });
164 $table.data('stupidsort_internaltable', table_structure);
165 });
166 };
167
168 // ====================================================================
169 // Private functions
170 // ====================================================================
171 var sortTable = function(sort_info){
172 var table_structure = sort_info.$table.data('stupidsort_internaltable');
173 var th_index = sort_info.th_index;
174 var $th = sort_info.$th;
175
176 var multicolumn_target_str = $th.data('sort-multicolumn');
177 var multicolumn_targets;
178 if(multicolumn_target_str){
179 multicolumn_targets = multicolumn_target_str.split(',');
180 }
181 else{
182 multicolumn_targets = [];
183 }
184 var multicolumn_th_targets = $.map(multicolumn_targets, function(identifier, i){
185 return get_th(sort_info.$table, identifier);
186 });
187
188 table_structure.sort(function(e1, e2){
189 var multicolumns = multicolumn_th_targets.slice(0); // shallow copy
190 var diff = sort_info.compare_fn(e1.columns[th_index], e2.columns[th_index]);
191 while(diff === 0 && multicolumns.length){
192 var multicolumn = multicolumns[0];
193 var datatype = multicolumn.$e.data("sort");
194 var multiCloumnSortMethod = sort_info.$table.data('sortFns')[datatype];
195 diff = multiCloumnSortMethod(e1.columns[multicolumn.index], e2.columns[multicolumn.index]);
196 multicolumns.shift();
197 }
198 // Sort by position in the table if values are the same. This enforces a
199 // stable sort across all browsers. See https://bugs.chromium.org/p/v8/issues/detail?id=90
200 if (diff === 0)
201 return e1.index - e2.index;
202 else
203 return diff;
204
205 });
206
207 if (sort_info.sort_dir != $.fn.stupidtable.dir.ASC){
208 table_structure.reverse();
209 }
210 return table_structure;
211 };
212
213 var get_th = function($table, identifier){
214 // identifier can be a th id or a th index number;
215 var $table_ths = $table.find('th');
216 var index = parseInt(identifier, 10);
217 var $th;
218 if(!index && index !== 0){
219 $th = $table_ths.siblings('#' + identifier);
220 index = $table_ths.index($th);
221 }
222 else{
223 $th = $table_ths.eq(index);
224 }
225 return {index: index, $e: $th};
226 };
227
228 var getTableRowsFromTableStructure = function(table_structure, sort_info){
229 // Gather individual column for callbacks
230 var column = $.map(table_structure, function(ele, i){
231 return [[ele.columns[sort_info.th_index], ele.$tr, i]];
232 });
233
234 /* Side effect */
235 sort_info.column = column;
236
237 // Replace the content of tbody with the sorted rows. Strangely
238 // enough, .append accomplishes this for us.
239 return $.map(table_structure, function(ele) { return ele.$tr; });
240
241 };
242
243 var updateElementData = function(sort_info){
244 var $table = sort_info.$table;
245 var $this_th = sort_info.$th;
246 var sort_dir = $this_th.data('sort-dir');
247 var th_index = sort_info.th_index;
248
249
250 // Reset siblings
251 $table.find("th").data("sort-dir", null).removeClass("sorting-desc sorting-asc");
252 $this_th.data("sort-dir", sort_dir).addClass("sorting-"+sort_dir);
253 };
254
255 var calculateSortDir = function(force_direction, sort_info){
256 var sort_dir;
257 var $this_th = sort_info.$th;
258 var dir = $.fn.stupidtable.dir;
259
260 if(!!force_direction){
261 sort_dir = force_direction;
262 }
263 else{
264 sort_dir = force_direction || $this_th.data("sort-default") || dir.ASC;
265 if ($this_th.data("sort-dir"))
266 sort_dir = $this_th.data("sort-dir") === dir.ASC ? dir.DESC : dir.ASC;
267 }
268 return sort_dir;
269 };
270
271 var calculateTHIndex = function(sort_info){
272 var th_index = 0;
273 var base_index = sort_info.$th.index();
274 sort_info.$th.parents("tr").find("th").slice(0, base_index).each(function() {
275 var cols = $(this).attr("colspan") || 1;
276 th_index += parseInt(cols,10);
277 });
278 return th_index;
279 };
280
281 })(jQuery);