forked from esvit/ng-table-resizable-columns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng-table-resizable-columns.src.js
168 lines (152 loc) · 5.73 KB
/
ng-table-resizable-columns.src.js
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
// based on https://github.com/dobtco/jquery-resizable-columns
angular.module('ngTableResizableColumns', [])
.directive('ngTableResizableColumns', function() {
var parseWidth = function(node) {
return parseFloat(node.style.width.replace('%', ''));
}, setWidth = function(node, width) {
return node.style.width = "" + width.toFixed(2) + "%";
}, pointerX = function(e) {
return (e.type.indexOf('touch') === 0) ? (e.originalEvent.touches[0] || e.originalEvent.changedTouches[0]).pageX : e.pageX;
};
function ResizableColumns($table) {
var __bind = function(fn, me){
return function(){
return fn.apply(me, arguments);
};
};
this.pointerdown = __bind(this.pointerdown, this);
var _this = this;
this.options = {
store: window.store,
rigidSizing: false,
resizeFromBody: true
};
this.$table = $table;
this.setHeaders();
this.restoreColumnWidths();
this.syncHandleWidths();
$(window).on('resize.rc', (function() {
return _this.syncHandleWidths();
}));
}
ResizableColumns.prototype.getColumnId = function($el) {
return this.$table.data('resizable-columns-id') + '-' + $el.data('resizable-column-id');
};
ResizableColumns.prototype.setHeaders = function() {
this.$tableHeaders = this.$table.find('thead tr:first th:visible');
this.assignPercentageWidths();
return this.createHandles();
};
ResizableColumns.prototype.destroy = function() {
this.$handleContainer.remove();
this.$table.removeData('resizableColumns');
return $(window).off('.rc');
};
ResizableColumns.prototype.assignPercentageWidths = function() {
var _this = this;
return this.$tableHeaders.each(function(_, el) {
var $el;
$el = $(el);
return setWidth($el[0], $el.outerWidth() / _this.$table.width() * 100);
});
};
ResizableColumns.prototype.createHandles = function() {
var _ref,
_this = this;
if ((_ref = this.$handleContainer) != null) {
_ref.remove();
}
this.$table.before((this.$handleContainer = $("<div class='rc-handle-container' />")));
this.$tableHeaders.each(function(i, el) {
var $handle;
if (_this.$tableHeaders.eq(i + 1).length === 0 || (_this.$tableHeaders.eq(i).attr('data-noresize') != null) || (_this.$tableHeaders.eq(i + 1).attr('data-noresize') != null)) {
return;
}
$handle = $("<div class='rc-handle' />");
$handle.data('th', $(el));
return $handle.appendTo(_this.$handleContainer);
});
return this.$handleContainer.on('mousedown touchstart', '.rc-handle', this.pointerdown);
};
ResizableColumns.prototype.syncHandleWidths = function() {
var _this = this;
this.setHeaders();
return this.$handleContainer.width(this.$table.width()).find('.rc-handle').each(function(_, el) {
var $el;
$el = $(el);
return $el.css({
left: $el.data('th').outerWidth() + ($el.data('th').offset().left - _this.$handleContainer.offset().left),
height: _this.options.resizeFromBody ? _this.$table.height() : _this.$table.find('thead').height()
});
});
};
ResizableColumns.prototype.saveColumnWidths = function() {
var _this = this;
return this.$tableHeaders.each(function(_, el) {
var $el;
$el = $(el);
if ($el.attr('data-noresize') == null) {
if (_this.options.store != null) {
return _this.options.store.set(_this.getColumnId($el), parseWidth($el[0]));
}
}
});
};
ResizableColumns.prototype.restoreColumnWidths = function() {
var _this = this;
return this.$tableHeaders.each(function(_, el) {
var $el, width;
$el = $(el);
if ((_this.options.store != null) && (width = _this.options.store.get(_this.getColumnId($el)))) {
return setWidth($el[0], width);
}
});
};
ResizableColumns.prototype.totalColumnWidths = function() {
var total,
_this = this;
total = 0;
this.$tableHeaders.each(function(_, el) {
return total += parseFloat($(el)[0].style.width.replace('%', ''));
});
return total;
};
ResizableColumns.prototype.pointerdown = function(e) {
var $currentGrip, $leftColumn, $rightColumn, startPosition, widths,
_this = this;
e.preventDefault();
startPosition = pointerX(e);
$currentGrip = $(e.currentTarget);
$leftColumn = $currentGrip.data('th');
$rightColumn = this.$tableHeaders.eq(this.$tableHeaders.index($leftColumn) + 1);
widths = {
left: parseWidth($leftColumn[0]),
right: parseWidth($rightColumn[0])
};
this.$table.addClass('rc-table-resizing');
$(document).on('mousemove.rc touchmove.rc', function(e) {
var difference;
difference = (pointerX(e) - startPosition) / _this.$table.width() * 100;
setWidth($rightColumn[0], widths.right - difference);
return setWidth($leftColumn[0], widths.left + difference);
});
return $(document).one('mouseup touchend', function() {
$(document).off('mousemove.rc touchmove.rc');
_this.$table.removeClass('rc-table-resizing');
_this.syncHandleWidths();
return _this.saveColumnWidths();
});
};
return {
restrict: 'C',
priority: 999,
link: function(scope, element, args, ngTable) {
var data;
scope.$watch('$data', function() {
data.destroy();
data = new ResizableColumns(element);
});
data = new ResizableColumns(element);
}
};
});