forked from cds-snc/track-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tables.js
179 lines (152 loc) · 6.15 KB
/
tables.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
var Tables = {
// wraps renderTable around a given $("table")
// e.g. Tables.init($("table"), data)
init: function(data, options) {
// assign data
if (!options.data) options.data = data;
// add common options to all renderTables requests
if (!options.responsive) options.responsive = true;
var customInit = function() {}; // noop
if (options.initComplete) customInit = options.initComplete;
options.initComplete = function() {
Utils.searchLinks(this);
customInit(this);
}
// If the table prefix is french, load french translations
if (options.prefix == 'fr') {
options.oLanguage = {
"sProcessing": "Traitement en cours...",
"sSearch": "Rechercher :",
"sLengthMenu": "Afficher _MENU_ éléments",
"sInfo": "Affichage de l'élément _START_ à _END_ sur _TOTAL_ éléments",
"sInfoEmpty": "Affichage de l'élément 0 à 0 sur 0 élément",
"sInfoFiltered": "(filtré de _MAX_ éléments au total)",
"sInfoPostFix": "",
"sLoadingRecords": "Chargement en cours...",
"sZeroRecords": "Aucun élément à afficher",
"sEmptyTable": "Aucune donnée disponible dans le tableau",
"oPaginate": {
"sPrevious": "<<",
"sNext": ">>"
},
"oAria": {
"sSortAscending": ": activer pour trier la colonne par ordre croissant",
"sSortDescending": ": activer pour trier la colonne par ordre décroissant"
}
}
}
// otherwise just load in better pagination
// can also be used to load in custom options
if(!options.oLanguage) {
options.oLanguage = {
"oPaginate": {
"sPrevious": "<<",
"sNext": ">>"
}
}
}
// Paginate to 100 per-page by default.
if (!options.dom) options.dom = 'fCtrip';
if (!options.pageLength) options.pageLength = 25;
var table = $("table").DataTable(options);
// Wire up accessible pagination controls.
Utils.updatePagination();
table.on("draw.dt",function(){
Utils.updatePagination();
});
table.on("page.dt",function(){
/* scroll page to top of table on page change */
var top = $(".dataTable").offset().top;
$("html, body").animate({ scrollTop: top }, "slow");
});
return table;
},
// sets some organization-table-specific options
initAgency: function(data, options) {
// Don't paginate organization tables by default.
if (!options.pageLength) options.pageLength = 25;
if (!options.dom) options.dom = 'fCtrip';
return Tables.init(data, options);
},
// common render function for displaying booleans as Yes/No
boolean: function(data, type) {
// Note: return "No"/"Yes" for sorting as well,
// as sorting by raw boolean values doesn't seem to work right.
return {false: "No", true: "Yes"}[data];
},
// common render function for linking domains to canonical URLs
canonical: function(data, type, row) {
if (type == "sort") return data;
else return "<a href=\"" + row.canonical + "\" target=\"blank\">" + data + "</a>";
},
// occasionally useful (see https/domains.js for example)
noop: function() {return ""},
// common render helper for percent bars
percentBar: function(data) {
return '' +
'<div class="progress-bar-indication">' +
'<span class="meter width' + data + '" style="width: ' + data + '%">' +
'<p>' + data + '%</p>' +
'</span>' +
'</div>';
},
// common pattern for percent bars:
// given row[report] and row[report][field], will
// compare against row[report].eligible
percent: function(report, field, totals) {
if (!totals) totals = false; // be explicit
return function(data, type, row) {
var set = totals ? row.totals : row;
var numerator = set[report][field];
var denominator = set[report].eligible;
var language = $( "table" ).attr("language");
// don't divide by 0!
if (denominator == 0) {
if (type == "sort") return -1; // shrug?
else return {en: "N/A", fr: "S. O."}[language]
}
var percent = Utils.percent(numerator, denominator);
if (type == "sort") return percent;
return Tables.percentBar(percent);
}
},
// helpful for reports where parent domains have totals for subdomains
percentTotals: function(report, field) {
return Tables.percent(report, field, true);
},
// common rendering function for organization service/domain counts
organizationServices: function(category) {
return function(data, type, row) {
if (type == "sort") return data;
else return "" +
"<a href=\"/" + category + "/domains/#" +
QueryString.stringify({q: row.name}) + "\">" +
data +
"</a>";
};
}
};
$(function() {
// if a datatable is searched, sync it to the URL hash
$('table').on('search.dt', function(e, settings) {
var query = $("input[type=search]").val();
if (query)
location.hash = QueryString.stringify({q: query});
// TODO: Disabled because this callback runs on table init,
// and zeroes out the location hash. Should be addressed.
// else
// location.hash = '';
});
$('table').on('draw.dt', function() {
// set max width on dataTable
$(this).css('width', '100%');
// add label for attribute for search
$('.dataTables_filter label').attr('for', 'datatables-search');
$('#DataTables_Table_0_filter').find('input[type="search"]').attr('id', 'datatables-search');
// add custom tailwind classes
$('#DataTables_Table_0_filter').attr('class', 'flex block justify-start md:justify-center');
$('#DataTables_Table_0_filter label').attr('class', 'w-full md:w-2/3');
$('#datatables-search').attr('class', 'border border-solid border-https-dark-gray bg-https-light-gray focus:border-https-blue block md:inline-block h-8 md:ml-6 mb-4 md:mb-8 w-full md:w-3/5 lg:w-3/4');
$('.dataTables_csv').attr('class', 'text-lg md:ml-4 mt-4 md:mt-6');
});
});