This repository was archived by the owner on Jan 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathpatternfly.dataTables.pfSelect.js
More file actions
281 lines (258 loc) · 9.1 KB
/
patternfly.dataTables.pfSelect.js
File metadata and controls
281 lines (258 loc) · 9.1 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
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
/**
* @summary pfSelect for DataTables
* @description A collection of API methods providing individual row selection and select all functionality in
* DataTables using traditional HTML checkboxes. This ensures DataTables meets the Patternfly design pattern while
* maintaining accessibility.
*
* The following selection styles are supported for user interaction with DataTables:
*
* api - Selection can only be performed via the API
* multi - Multiple items can be selected
* multi+shift - a hybrid between the os style and multi
* os - Operating system style selection with complex behaviors such as ctrl/cmd, shift and an unmodified click
* single - Only a single item can be selected
*
* For details see: https://datatables.net/reference/option/select.style
*
* Note that when a selection is made, the selection results text is also updated in the toolbar. The toolbar layouts is
* expected to contain the classes as shown in the example below. Selection checkboxes are also expected to be located
* in the first column.
*
* Example:
*
* <!-- NOTE: Some configuration may be omitted for clarity -->
* <div class="row toolbar-pf table-view-pf-toolbar" id="toolbar1">
* <div class="col-sm-12">
* ...
* <div class="row toolbar-pf-results">
* <div class="col-sm-9">
* ...
* </div>
* <div class="col-sm-3 table-view-pf-select-results">
* <strong>0</strong> of <strong>0</strong> selected
* </div>
* </div>
* </div>
* </div>
* <table class="table table-striped table-bordered table-hover" id="table1">
* <thead>
* <tr>
* <th><input type="checkbox" name="selectAll"></th>
* <th>Rendering Engine</th>
* <th>Browser</th>
* </tr>
* </thead>
* </table>
* ...
* <script>
* // NOTE: Some properties may be omitted for clarity
* $(document).ready(function() {
* var dt = $("#table1").DataTable({
* columns: [
* { data: null,
* className: "table-view-pf-select",
* render: function (data, type, full, meta) {
* return '<input type="checkbox" name="select">';
* },
* sortable: false
* },
* { data: "engine" },
* { data: "browser" }
* ],
* data: [
* { engine: "Gecko", browser: "Firefox" }
* { engine: "Trident", browser: "Mozilla" }
* ],
* dom: "t",
* order: [[ 1, "asc" ]],
* pfConfig: {
* ...
* toolbarSelector: "#toolbar1",
* selectAllSelector: 'th:first-child input[type="checkbox"]'
* }
* select: {
* selector: 'td:first-child input[type="checkbox"]',
* style: "multi"
* }
* });
* dt.table().pfSelect.selectAllRows(true); // Optional API to select all rows
* });
* </script>
*
* Note: This functionality requires the following Javascript library files to be loaded:
*
* https://cdn.datatables.net/select/1.2.0/js/dataTables.select.min.js
*/
(function (factory) {
"use strict";
if (typeof define === "function" && define.amd ) {
// AMD
define (["jquery", "datatables.net"], function ($) {
return factory ($, window, document);
});
} else if (typeof exports === "object") {
// CommonJS
module.exports = function (root, $) {
if (!root) {
root = window;
}
if (!$ || !$.fn.dataTable) {
$ = require("datatables.net")(root, $).$;
}
return factory($, root, root.document);
};
} else {
// Browser
factory(jQuery, window, document);
}
}(function ($, window, document, undefined) {
"use strict";
var DataTable = $.fn.dataTable;
var RESULTS_SELECTOR = ".table-view-pf-select-results"; // Toolbar selection results
var SELECT_ALL_SELECTOR = 'th:first-child input[type="checkbox"]'; // Default checkbox for selecting all rows
var SELECT_SELECTOR = 'td:first-child input[type="checkbox"]'; // Default checkboxes for row selection
DataTable.pfSelect = {};
/**
* Initialize
*
* @param {DataTable.Api} dt DataTable
* @private
*/
DataTable.pfSelect.init = function (dt) {
var ctx = dt.settings()[0];
var opts = (ctx.oInit.pfConfig) ? ctx.oInit.pfConfig : {};
var select = (ctx.oInit.select) ? ctx.oInit.select : {};
var style = dt.select.style();
ctx._pfSelect = {};
ctx._pfSelect.selectAllSelector = (opts.selectAllSelector !== undefined)
? opts.selectAllSelector : SELECT_ALL_SELECTOR; // Select all checkbox
ctx._pfSelect.selector = (select.selector !== undefined)
? select.selector : SELECT_SELECTOR; // Select checkbox
ctx._pfSelect.results = $(RESULTS_SELECTOR, opts.toolbarSelector); // Toolbar selection results
if (style === "api") {
// Select all checkbox
$(dt.table().container()).on("click", ctx._pfSelect.selectAllSelector, function (evt) {
evt.preventDefault();
});
// Select checkboxes
$(dt.table().container()).on("click", ctx._pfSelect.selector, function (evt) {
evt.preventDefault();
});
dt.table().on("select.dt", function () {
syncSelectCheckboxes(dt);
});
} else {
// Select all checkbox
$(dt.table().container()).on("click", ctx._pfSelect.selectAllSelector, function (evt) {
selectAllRows(dt, evt.target.checked);
});
// Select checkboxes
$(dt.table().container()).on("click", ctx._pfSelect.selector, function (evt) {
if (style !== "multi" || style !== "multi+shift") {
syncSelectCheckboxes(dt); // No need to sync checkbox selections when "multi" is used
} else {
syncSelectAllCheckbox(dt); // Still need to sync select all checkbox
}
});
}
// Sync checkbox selections when paging and filtering is applied
dt.table().on("draw.dt", function () {
syncSelectCheckboxes(dt);
});
// Initialize selected rows text
updateSelectedRowsText(dt);
};
// Local functions
/**
* Select all rows on current page
*
* @param {DataTable.Api} dt DataTable
* @param {boolean} select True to select all rows on current page, defaults to false
* @private
*/
function selectAllRows (dt, select) {
var ctx = dt.settings()[0];
// Retrieve all rows taking into account currently applied filter
var filteredRows = dt.rows({"page": "current", "search": "applied"});
// Check if style is single
if (dt.select.style() === "single") {
throw new Error("Cannot select all rows with selection style 'single'");
}
// Select rows
if (select) {
filteredRows.select();
} else {
filteredRows.deselect();
}
$(ctx._pfSelect.selector, dt.table().container()).prop("checked", select); // De/select checkboxes in view
syncSelectAllCheckbox(dt);
}
/**
* Sync select all checkbox with row selections on current page
*
* @param {DataTable.Api} dt DataTable
* @private
*/
function syncSelectAllCheckbox (dt) {
var ctx = dt.settings()[0];
// Retrieve all rows taking into account currently applied filter
var filteredRows = dt.rows({"page": "current", "search": "applied"}).flatten().length;
var selectedFilteredRows = dt.rows({"page": "current", "search": "applied", "selected": true}).flatten().length;
// De/select the select all checkbox
var selectAll = $(ctx._pfSelect.selectAllSelector, dt.table().container())[0];
if (selectAll) {
selectAll.checked = (filteredRows !== 0 && filteredRows === selectedFilteredRows);
}
updateSelectedRowsText(dt);
}
/**
* Sync select checkboxes with row selections on current page
*
* @param {DataTable.Api} dt DataTable
* @private
*/
function syncSelectCheckboxes (dt) {
var ctx = dt.settings()[0];
$(ctx._pfSelect.selector, dt.table().container()).prop("checked", false); // Deselect all checkboxes
dt.rows({"page": "current", "search": "applied", "selected": true}).every(function (index) {
$(ctx._pfSelect.selector, dt.table().row(index).node()).prop("checked", true); // Select checkbox for selected row
});
syncSelectAllCheckbox(dt);
}
/**
* Update selection results text
*
* @param {DataTable.Api} dt DataTable
* @private
*/
function updateSelectedRowsText (dt) {
var ctx = dt.settings()[0];
var selectedRows = dt.rows({"selected": true}).flatten().length;
var totalRows = dt.rows().flatten().length;
if (ctx._pfSelect.results !== undefined && ctx._pfSelect.results.length !== 0) {
$(ctx._pfSelect.results).html("<strong>" + selectedRows + "</strong> of <strong>" +
totalRows + "</strong> selected");
}
}
// DataTables API
/**
* Select all rows on current page
*
* Example: dt.table().pfSelect.selectAllRows(true);
*
* @param {boolean} select True to select all rows on current page, defaults to false
*/
DataTable.Api.register("pfSelect.selectAllRows()", function (select) {
return this.iterator("table", function (ctx) {
selectAllRows(new DataTable.Api(ctx), select);
});
});
// DataTables creation
$(document).on("preInit.dt.dtSelect", function (e, ctx) {
if (e.namespace !== "dt") {
return;
}
DataTable.pfSelect.init(new DataTable.Api(ctx));
});
return DataTable.pfSelect;
}));