-
Notifications
You must be signed in to change notification settings - Fork 125
/
datatableBinding.js
311 lines (263 loc) · 10.1 KB
/
datatableBinding.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
define([
'jquery',
'knockout',
'datatables.net',
'appConfig',
'xss',
'moment',
'services/MomentAPI',
'utils/CommonUtils',
'datatables.net-buttons',
'colvis',
'datatables.net-buttons-html5',
'datatables.net-select',
], function (
$,
ko,
dataTables,
config,
filterXSS,
moment,
momentApi,
commonUtils,
) {
function renderSelected(s, p, d) {
return '<span class="fa fa-check-circle"></span>';
}
function _getTableData(element, type = 'selected')
{
const selector = type === 'selected' ? 'tr:has(td.select:has(span.selected))' : '';
const selectedRows = $(element).DataTable().rows(selector, {
'search': 'applied'
}).data();
var selectedData = [];
$.each(selectedRows, function(index, value) {
selectedData.push(value);
});
return selectedData;
}
function _getRows(element, predicate) {
return $(element).DataTable().rows(predicate);
}
function isUrlAbsolute(url) {
return (url.indexOf('://') > 0 || url.indexOf('//') === 0);
}
function filterAbsoluteUrls(html) {
return html.replace(/href="([^"]*)"|href='([^']*)'/g, function(match, p1, p2)
{
const link = p1 || p2;
if (isUrlAbsolute(link)) {
return match.replace(link, '#' + link);
}
return match;
}
);
}
function sortAbs(x, y) {
const abxX = Math.abs(x);
const absY = Math.abs(y);
return abxX < absY ? -1 : abxX>absY ? 1 : 0;
}
function sortRange(x, y) {
const xt = x.trim();
const yt = y.trim();
const firstNumberX = parseInt(xt.substr(0, xt.indexOf(' ')));
const firstNumberY = parseInt(yt.substr(0, yt.indexOf(' ')));
return firstNumberX < firstNumberY ? -1 : firstNumberX > firstNumberY ? 1 : 0;
}
function mapColumns(element, binding, xssOptions) {
const columns = ko.unwrap(binding.options.columns);
if (columns && columns[0] === 'select') {
columns[0] = { width:'20px', orderable: false, class: 'select', render: renderSelected };
$(element).on("click","td > span.fa.fa-check-circle", function () {
$(this).toggleClass('selected');
});
}
return columns.map((column) => {
const originalRender = column.render;
const originalDataAccessor = column.data;
const hasOriginalRender = typeof originalRender === 'function';
const hasDataAccessor = typeof originalDataAccessor === 'function';
if (binding.options.xssSafe || column.xssSafe) { // disable XSS filtering if column is marked 'safe'
return Object.assign({}, column, {
title: ko.unwrap(column.title)
});
} else {
return Object.assign({}, column, {
title: ko.unwrap(column.title),
data: hasDataAccessor
? d => filterAbsoluteUrls(filterXSS(originalDataAccessor(d), xssOptions))
: filterAbsoluteUrls(filterXSS(originalDataAccessor, xssOptions)),
render: hasOriginalRender
? (s, p, d) => filterAbsoluteUrls(filterXSS(originalRender(s, p, d), xssOptions))
// https://datatables.net/reference/option/columns.render
// "render" property having "string" or "object" data type is not obvious for filtering, so do not pass such things to UI for now
: $.fn.dataTable.render.text()
});
}
});
}
ko.bindingHandlers.dataTable = {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
jQuery.fn.dataTableExt.oSort["numberAbs-desc"] = function(x, y) {
return -1 * sortAbs(x, y);
};
jQuery.fn.dataTableExt.oSort["numberAbs-asc"] = function(x, y) {
return sortAbs(x, y);
}
jQuery.fn.dataTableExt.oSort["range-desc"] = function(x, y) {
return -1 * sortRange(x, y);
};
jQuery.fn.dataTableExt.oSort["range-asc"] = function(x, y) {
return sortRange(x, y);
}
var binding = ko.utils.unwrapObservable(valueAccessor());
// If the binding is an object with an options field,
// initialise the dataTable with those options.
if (binding.options) {
const defaultTableOptions = commonUtils.getTableOptions('M')
binding.options.pageLength = binding.options.pageLength || defaultTableOptions.pageLength;
binding.options.lengthMenu = binding.options.lengthMenu || defaultTableOptions.lengthMenu;
// allow row level binding context
const createdRow = binding.options.createdRow;
binding.options.createdRow = (row, data, index) => {
if (createdRow) {
createdRow(row, data, index);
}
ko.cleanNode(row);
ko.applyBindings(bindingContext.createChildContext(data), row);
};
// test for 'select' column (must be first column in column definition
const xssOptions = config.xssOptions;
const oColumns = mapColumns(element, binding, xssOptions);
const language = binding.options.language;
const options = Object.assign({}, binding.options, {
columns: oColumns,
language: ko.unwrap(language),
});
let selectAlls = oColumns.filter(c => c.renderSelectAll && !!c.selectAll);
if (selectAlls.length > 0) {
selectAlls.forEach((c, i) => {
c.title = `<span class="select-all-${i} fa fa-check"></span> ${ko.unwrap(c.title)}`;
$(element).on("click", "th > .select-all-" + i, (e) => {
if (binding.data().length < 100) {
e.target.classList.toggle('selected');
c.selectAll($(element).DataTable().rows( { filter : 'applied'} ).data(),
e.target.classList.contains('selected'));
} else { // more the data - slower the all-selection/deselection. add spinner
e.target.classList.remove('fa-check');
e.target.classList.add('fa-circle-notch', 'fa-spin');
setTimeout(() => {
e.target.classList.toggle('selected');
c.selectAll($(element).DataTable().rows( { filter : 'applied'} ).data(),
e.target.classList.contains('selected'));
e.target.classList.add('fa-check');
e.target.classList.remove('fa-circle-notch', 'fa-spin');
}, 50);
}
});
});
// todo: need to add logic for automatic select/deselect header checkbox on all items select/deselect
}
// For case of complex header which uses data-bindings (https://datatables.net/examples/advanced_init/complex_header.html)
if ($(element).find('thead')[0]) {
ko.applyBindings(bindingContext, $(element).find('thead')[0]);
}
let languageSubscription = null;
let columnsSubscription = null;
if (! $.fn.dataTable.isDataTable(element)) {
$(element).DataTable(Object.assign({}, options));
if (ko.isComputed(language)) {
languageSubscription = language.subscribe((newLanguage) => {
$(element).DataTable().clear().destroy();
const opts = Object.assign({}, options, {
columns: mapColumns(element, binding, xssOptions),
language: newLanguage,
destroy: true,
});
const table = $(element).DataTable(opts);
table.rows.add(ko.unwrap(binding.data || binding));
table.draw();
});
}
// dynamic columns
if (ko.isObservable(binding.options.columns)) {
columnsSubscription = binding.options.columns.subscribe(() => {
const el = $(element);
el.DataTable().clear().destroy();
// HTML table headers for new columns have to be built dynamically
// https://datatables.net/forums/discussion/42893/dynamically-generating-columns
const oldColumnsLength = options.columns.length;
const newColumns = mapColumns(element, binding, xssOptions);
const newColumnsLength = newColumns.length;
const headers = el.find("thead > tr");
for (let i = oldColumnsLength; i < newColumnsLength; i++) {
headers.append("<th></th>");
}
options.columns = newColumns;
const table = el.DataTable(options);
table.rows.add(ko.unwrap(binding.data || binding));
table.draw();
});
}
}
if (binding.api != null)
{
// expose datatable API to context's api binding.
binding.api({
getRows: function (predicate) { return _getRows(element, predicate); },
getSelectedData: function() { return _getTableData(element, 'selected');},
getFilteredData: function() { return _getTableData(element, 'filtered');},
});
}
// Workaround for bug when datatable header column width is not adjusted to column values when using scrollY datatable option
// https://stackoverflow.com/questions/32679625/jquery-datatables-header-is-not-adjusting-to-column-values-initially-but-adjust
if (!!binding.options.scrollY) {
setTimeout(() => $(element).DataTable().columns.adjust().draw('page'), 0);
}
// setup dispose callback:
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
// This will be called when the element is removed by Knockout or
// if some other part of your code calls ko.removeNode(element)
if (languageSubscription) {
languageSubscription.dispose();
}
if (columnsSubscription) {
columnsSubscription.dispose();
}
$(element).DataTable().destroy(true);
$(element).empty();
});
}
return {
controlsDescendantBindings: true
};
},
update: function (element, valueAccessor) {
var binding = ko.utils.unwrapObservable(valueAccessor());
var table = $(element).DataTable();
// assign data to either the binding's data or the actual binding.
var data = ko.utils.unwrapObservable(binding.data || binding);
// clear events that .on() attached to previously. Prior to this update, the binding may have specified an 'onRowClick' option, but no longer does.
$(element).off("click","tr");
if (binding.onRowClick != null) // attach a onRowclick handler if the options binding specifies it.
{
$(element).on("click","tr", function(evt)
{
if (this._DT_RowIndex != null)
{
binding.onRowClick(data[this._DT_RowIndex], evt, this, this._DT_RowIndex);
}
});
}
// Clear table
table.clear();
// Rebuild table from data source specified in binding
if (data && data.length > 0)
table.rows.add(data);
// drawing may access observables, which updating we do not want to trigger a redraw to the table
// see: https://knockoutjs.com/documentation/computed-dependency-tracking.html#IgnoringDependencies
ko.ignoreDependencies(table.draw);
}
};
});