-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsRapTable.js
More file actions
98 lines (93 loc) · 2.67 KB
/
jsRapTable.js
File metadata and controls
98 lines (93 loc) · 2.67 KB
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
$.fn.sortElements = (function(){
var sort = [].sort;
return function(comparator, getSortable){
getSortable = getSortable || function(){return this;};
var placements = this.map(function(){
var sortElement = getSortable.call(this),parentNode = sortElement.parentNode,nextSibling = parentNode.insertBefore(document.createTextNode(''),sortElement.nextSibling);
return function(){
if (parentNode === this)
throw new Error("You can't sort elements if any one is a descendant of another.");
parentNode.insertBefore(this, nextSibling);
parentNode.removeChild(nextSibling);
};
});
return sort.call(this, comparator).each(function(i){
placements[i].call(getSortable.call(this));
});
};
})();
(function($){
$.fn.jsRapTable = function(options){
return this.each(function(){
this.opt = $.extend({
sort:{index:0,up:false},
onSort:null
},options);
let base = this;
let ths = null;
let thd = null;
let startOffset = 0;
let startWidth = 0;
$(this).addClass('rapTable');
$('th',this).each(function(){
let th = this;
$('<span>').appendTo(th);
th.addEventListener('click', function(e){
let i = $(th).index();
let u = $(th).hasClass('darr');
base.Sort(i,u);
});
});
$('td:not(:last-child)',this).each(function(){
let td = this;
let grip = $('<div>').addClass('grip').appendTo($(this))[0];
grip.addEventListener('mousedown', function(e){
let i = $(td).index();
ths = $('th',base).eq(i);
thd = $('th',base).eq(++i);
startOffset = e.clientX;
startWidth = $(ths).width();
e.stopPropagation();
});
grip.addEventListener('click',function (e){
e.stopPropagation();
});
});
document.addEventListener('mousemove',function (e) {
$('tbody tr',base).css({cursor:'w-resize'});
if(ths && thd){
let ows = $(ths).width();
let owd = $(thd).width();
let ow = ows + owd;
let dw = e.clientX - startOffset + startWidth - ows;
if(ows + dw < 3)
dw = 3 - ows;
if(owd - dw < 3)
dw = owd - 3;
$(ths).width(ows + dw);
$(thd).width(owd - dw);
if(dw > 0)
$(ths).width(ow - $(thd).width());
else
$(thd).width(ow - $(ths).width());
}else
$('tbody tr',base).css({cursor:'pointer'});
});
document.addEventListener('mouseup',function(e){
ths = null;
thd = null;
e.stopPropagation();
});
this.Sort = function(i,u){
if(!this.opt.onSort)return;
$('.uarr').removeClass('uarr');
$('.darr').removeClass('darr');
if(u)
$('th',this).eq(i).addClass('uarr');
else
$('th',this).eq(i).addClass('darr');
this.opt.onSort.call(this,i,u);
}
this.Sort(this.opt.sort.index,this.opt.sort.up);
});
}})(jQuery);