/
ext.renderer.js
133 lines (111 loc) · 3.87 KB
/
ext.renderer.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
$.extend( true, DataTable.ext.renderer, {
footer: {
_: function ( settings, cell, classes ) {
cell.addClass(classes.tfoot.cell);
}
},
header: {
_: function ( settings, cell, classes ) {
cell.addClass(classes.thead.cell);
if (! settings.oFeatures.bSort) {
cell.addClass(classes.order.none);
}
var legacyTop = settings.bSortCellsTop;
var headerRows = cell.closest('thead').find('tr');
var rowIdx = cell.parent().index();
// Conditions to not apply the ordering icons
if (
// Cells and rows which have the attribute to disable the icons
cell.attr('data-dt-order') === 'disable' ||
cell.parent().attr('data-dt-order') === 'disable' ||
// Legacy support for `orderCellsTop`. If it is set, then cells
// which are not in the top or bottom row of the header (depending
// on the value) do not get the sorting classes applied to them
(legacyTop === true && rowIdx !== 0) ||
(legacyTop === false && rowIdx !== headerRows.length - 1)
) {
return;
}
// No additional mark-up required
// Attach a sort listener to update on sort - note that using the
// `DT` namespace will allow the event to be removed automatically
// on destroy, while the `dt` namespaced event is the one we are
// listening for
$(settings.nTable).on( 'order.dt.DT', function ( e, ctx, sorting ) {
if ( settings !== ctx ) { // need to check this this is the host
return; // table, not a nested one
}
var orderClasses = classes.order;
var columns = ctx.api.columns( cell );
var col = settings.aoColumns[columns.flatten()[0]];
var orderable = columns.orderable().includes(true);
var ariaType = '';
var indexes = columns.indexes();
var sortDirs = columns.orderable(true).flatten();
var orderedColumns = sorting.map( function (val) {
return val.col;
} ).join(',');
cell
.removeClass(
orderClasses.isAsc +' '+
orderClasses.isDesc
)
.toggleClass( orderClasses.none, ! orderable )
.toggleClass( orderClasses.canAsc, orderable && sortDirs.includes('asc') )
.toggleClass( orderClasses.canDesc, orderable && sortDirs.includes('desc') );
var sortIdx = orderedColumns.indexOf( indexes.toArray().join(',') );
if ( sortIdx !== -1 ) {
// Get the ordering direction for the columns under this cell
// Note that it is possible for a cell to be asc and desc sorting
// (column spanning cells)
var orderDirs = columns.order();
cell.addClass(
orderDirs.includes('asc') ? orderClasses.isAsc : '' +
orderDirs.includes('desc') ? orderClasses.isDesc : ''
);
}
// The ARIA spec says that only one column should be marked with aria-sort
if ( sortIdx === 0 && orderedColumns.length === indexes.count() ) {
var firstSort = sorting[0];
var sortOrder = col.asSorting;
cell.attr('aria-sort', firstSort.dir === 'asc' ? 'ascending' : 'descending');
// Determine if the next click will remove sorting or change the sort
ariaType = ! sortOrder[firstSort.index + 1] ? 'Remove' : 'Reverse';
}
else {
cell.removeAttr('aria-sort');
}
cell.attr('aria-label', orderable
? col.ariaTitle + ctx.api.i18n('oAria.orderable' + ariaType)
: col.ariaTitle
);
if (orderable) {
cell.find('.dt-column-title').attr('role', 'button');
cell.attr('tabindex', 0)
}
} );
}
},
layout: {
_: function ( settings, container, items ) {
var row = $('<div/>')
.addClass('dt-layout-row')
.appendTo( container );
$.each( items, function (key, val) {
var klass = ! val.table ?
'dt-'+key+' ' :
'';
if (val.table) {
row.addClass('dt-layout-table');
}
$('<div/>')
.attr({
id: val.id || null,
"class": 'dt-layout-cell '+klass+(val.className || '')
})
.append( val.contents )
.appendTo( row );
} );
}
}
} );