1 | (function (global, factory) {
|
2 | if (typeof define === "function" && define.amd) {
|
3 | define(["exports", "../../globals/js/feature-flags", "../../globals/js/settings", "../../globals/js/misc/mixin", "../../globals/js/mixins/create-component", "../../globals/js/mixins/init-component-by-search", "../../globals/js/mixins/evented-state", "../../globals/js/misc/event-matches"], factory);
|
4 | } else if (typeof exports !== "undefined") {
|
5 | factory(exports, require("../../globals/js/feature-flags"), require("../../globals/js/settings"), require("../../globals/js/misc/mixin"), require("../../globals/js/mixins/create-component"), require("../../globals/js/mixins/init-component-by-search"), require("../../globals/js/mixins/evented-state"), require("../../globals/js/misc/event-matches"));
|
6 | } else {
|
7 | var mod = {
|
8 | exports: {}
|
9 | };
|
10 | factory(mod.exports, global.featureFlags, global.settings, global.mixin, global.createComponent, global.initComponentBySearch, global.eventedState, global.eventMatches);
|
11 | global.dataTableV2 = mod.exports;
|
12 | }
|
13 | })(this, function (_exports, _featureFlags, _settings, _mixin2, _createComponent, _initComponentBySearch, _eventedState, _eventMatches) {
|
14 | "use strict";
|
15 |
|
16 | Object.defineProperty(_exports, "__esModule", {
|
17 | value: true
|
18 | });
|
19 | _exports.default = void 0;
|
20 | _settings = _interopRequireDefault(_settings);
|
21 | _mixin2 = _interopRequireDefault(_mixin2);
|
22 | _createComponent = _interopRequireDefault(_createComponent);
|
23 | _initComponentBySearch = _interopRequireDefault(_initComponentBySearch);
|
24 | _eventedState = _interopRequireDefault(_eventedState);
|
25 | _eventMatches = _interopRequireDefault(_eventMatches);
|
26 |
|
27 | function _interopRequireDefault(obj) {
|
28 | return obj && obj.__esModule ? obj : {
|
29 | default: obj
|
30 | };
|
31 | }
|
32 |
|
33 | function _typeof(obj) {
|
34 | if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
|
35 | _typeof = function _typeof(obj) {
|
36 | return typeof obj;
|
37 | };
|
38 | } else {
|
39 | _typeof = function _typeof(obj) {
|
40 | return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
|
41 | };
|
42 | }
|
43 |
|
44 | return _typeof(obj);
|
45 | }
|
46 |
|
47 | function _toConsumableArray(arr) {
|
48 | return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
|
49 | }
|
50 |
|
51 | function _nonIterableSpread() {
|
52 | throw new TypeError("Invalid attempt to spread non-iterable instance");
|
53 | }
|
54 |
|
55 | function _iterableToArray(iter) {
|
56 | if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
|
57 | }
|
58 |
|
59 | function _arrayWithoutHoles(arr) {
|
60 | if (Array.isArray(arr)) {
|
61 | for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) {
|
62 | arr2[i] = arr[i];
|
63 | }
|
64 |
|
65 | return arr2;
|
66 | }
|
67 | }
|
68 |
|
69 | function _classCallCheck(instance, Constructor) {
|
70 | if (!(instance instanceof Constructor)) {
|
71 | throw new TypeError("Cannot call a class as a function");
|
72 | }
|
73 | }
|
74 |
|
75 | function _defineProperties(target, props) {
|
76 | for (var i = 0; i < props.length; i++) {
|
77 | var descriptor = props[i];
|
78 | descriptor.enumerable = descriptor.enumerable || false;
|
79 | descriptor.configurable = true;
|
80 | if ("value" in descriptor) descriptor.writable = true;
|
81 | Object.defineProperty(target, descriptor.key, descriptor);
|
82 | }
|
83 | }
|
84 |
|
85 | function _createClass(Constructor, protoProps, staticProps) {
|
86 | if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
87 | if (staticProps) _defineProperties(Constructor, staticProps);
|
88 | return Constructor;
|
89 | }
|
90 |
|
91 | function _possibleConstructorReturn(self, call) {
|
92 | if (call && (_typeof(call) === "object" || typeof call === "function")) {
|
93 | return call;
|
94 | }
|
95 |
|
96 | return _assertThisInitialized(self);
|
97 | }
|
98 |
|
99 | function _assertThisInitialized(self) {
|
100 | if (self === void 0) {
|
101 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
102 | }
|
103 |
|
104 | return self;
|
105 | }
|
106 |
|
107 | function _getPrototypeOf(o) {
|
108 | _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
|
109 | return o.__proto__ || Object.getPrototypeOf(o);
|
110 | };
|
111 | return _getPrototypeOf(o);
|
112 | }
|
113 |
|
114 | function _inherits(subClass, superClass) {
|
115 | if (typeof superClass !== "function" && superClass !== null) {
|
116 | throw new TypeError("Super expression must either be null or a function");
|
117 | }
|
118 |
|
119 | subClass.prototype = Object.create(superClass && superClass.prototype, {
|
120 | constructor: {
|
121 | value: subClass,
|
122 | writable: true,
|
123 | configurable: true
|
124 | }
|
125 | });
|
126 | if (superClass) _setPrototypeOf(subClass, superClass);
|
127 | }
|
128 |
|
129 | function _setPrototypeOf(o, p) {
|
130 | _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
|
131 | o.__proto__ = p;
|
132 | return o;
|
133 | };
|
134 |
|
135 | return _setPrototypeOf(o, p);
|
136 | }
|
137 |
|
138 | var toArray = function toArray(arrayLike) {
|
139 | return Array.prototype.slice.call(arrayLike);
|
140 | };
|
141 |
|
142 | var suffix = _featureFlags.componentsX ? '' : '-v2';
|
143 |
|
144 | var DataTableV2 =
|
145 |
|
146 | function (_mixin) {
|
147 | _inherits(DataTableV2, _mixin);
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | function DataTableV2(_element, options) {
|
166 | var _this;
|
167 |
|
168 | _classCallCheck(this, DataTableV2);
|
169 |
|
170 | _this = _possibleConstructorReturn(this, _getPrototypeOf(DataTableV2).call(this, _element, options));
|
171 |
|
172 | _this._sortToggle = function (detail) {
|
173 | var element = detail.element,
|
174 | previousValue = detail.previousValue;
|
175 | toArray(_this.tableHeaders).forEach(function (header) {
|
176 | var sortEl = header.querySelector(_this.options.selectorTableSort);
|
177 |
|
178 | if (sortEl !== null && sortEl !== element) {
|
179 | sortEl.classList.remove(_this.options.classTableSortActive);
|
180 | sortEl.classList.remove(_this.options.classTableSortAscending);
|
181 | }
|
182 | });
|
183 |
|
184 | if (!previousValue) {
|
185 | element.dataset.previousValue = 'ascending';
|
186 | element.classList.add(_this.options.classTableSortActive);
|
187 | element.classList.add(_this.options.classTableSortAscending);
|
188 | } else if (previousValue === 'ascending') {
|
189 | element.dataset.previousValue = 'descending';
|
190 | element.classList.add(_this.options.classTableSortActive);
|
191 | element.classList.remove(_this.options.classTableSortAscending);
|
192 | } else if (previousValue === 'descending') {
|
193 | element.removeAttribute('data-previous-value');
|
194 | element.classList.remove(_this.options.classTableSortActive);
|
195 | element.classList.remove(_this.options.classTableSortAscending);
|
196 | }
|
197 | };
|
198 |
|
199 | _this._selectToggle = function (detail) {
|
200 | var element = detail.element;
|
201 | var checked = element.checked;
|
202 |
|
203 | _this.state.checkboxCount += checked ? 1 : -1;
|
204 | _this.countEl.textContent = _this.state.checkboxCount;
|
205 | var row = element.parentNode.parentNode;
|
206 | row.classList.toggle(_this.options.classTableSelected);
|
207 |
|
208 | _this._actionBarToggle(_this.state.checkboxCount > 0);
|
209 | };
|
210 |
|
211 | _this._selectAllToggle = function (_ref) {
|
212 | var element = _ref.element;
|
213 | var checked = element.checked;
|
214 | var inputs = toArray(_this.element.querySelectorAll(_this.options.selectorCheckbox));
|
215 | _this.state.checkboxCount = checked ? inputs.length - 1 : 0;
|
216 | inputs.forEach(function (item) {
|
217 | item.checked = checked;
|
218 | var row = item.parentNode.parentNode;
|
219 |
|
220 | if (checked && row) {
|
221 | row.classList.add(_this.options.classTableSelected);
|
222 | } else {
|
223 | row.classList.remove(_this.options.classTableSelected);
|
224 | }
|
225 | });
|
226 |
|
227 | _this._actionBarToggle(_this.state.checkboxCount > 0);
|
228 |
|
229 | if (_this.batchActionEl) {
|
230 | _this.countEl.textContent = _this.state.checkboxCount;
|
231 | }
|
232 | };
|
233 |
|
234 | _this._actionBarCancel = function () {
|
235 | var inputs = toArray(_this.element.querySelectorAll(_this.options.selectorCheckbox));
|
236 | var row = toArray(_this.element.querySelectorAll(_this.options.selectorTableSelected));
|
237 | row.forEach(function (item) {
|
238 | item.classList.remove(_this.options.classTableSelected);
|
239 | });
|
240 | inputs.forEach(function (item) {
|
241 | item.checked = false;
|
242 | });
|
243 | _this.state.checkboxCount = 0;
|
244 |
|
245 | _this._actionBarToggle(false);
|
246 |
|
247 | if (_this.batchActionEl) {
|
248 | _this.countEl.textContent = _this.state.checkboxCount;
|
249 | }
|
250 | };
|
251 |
|
252 | _this._actionBarToggle = function (toggleOn) {
|
253 | var transition = function transition(evt) {
|
254 | _this.batchActionEl.removeEventListener('transitionend', transition);
|
255 |
|
256 | if (evt.target.matches(_this.options.selectorActions)) {
|
257 | if (_this.batchActionEl.dataset.active === 'false') {
|
258 | _this.batchActionEl.setAttribute('tabIndex', -1);
|
259 | } else {
|
260 | _this.batchActionEl.setAttribute('tabIndex', 0);
|
261 | }
|
262 | }
|
263 | };
|
264 |
|
265 | if (toggleOn) {
|
266 | _this.batchActionEl.dataset.active = true;
|
267 |
|
268 | _this.batchActionEl.classList.add(_this.options.classActionBarActive);
|
269 | } else if (_this.batchActionEl) {
|
270 | _this.batchActionEl.dataset.active = false;
|
271 |
|
272 | _this.batchActionEl.classList.remove(_this.options.classActionBarActive);
|
273 | }
|
274 |
|
275 | if (_this.batchActionEl) {
|
276 | _this.batchActionEl.addEventListener('transitionend', transition);
|
277 | }
|
278 | };
|
279 |
|
280 | _this._expandableRowsInit = function (expandableRows) {
|
281 | expandableRows.forEach(function (item) {
|
282 | if (!_featureFlags.componentsX) {
|
283 | item.classList.remove(_this.options.classExpandableRowHidden);
|
284 |
|
285 | _this.tableBody.removeChild(item);
|
286 | }
|
287 | });
|
288 | };
|
289 |
|
290 | _this._rowExpandToggle = function (_ref2) {
|
291 | var element = _ref2.element,
|
292 | initialEvt = _ref2.initialEvt;
|
293 | var parent = (0, _eventMatches.default)(initialEvt, _this.options.eventParentContainer);
|
294 |
|
295 | var index = _this.expandCells.indexOf(element);
|
296 |
|
297 | if (element.dataset.previousValue === undefined || element.dataset.previousValue === 'expanded') {
|
298 | element.dataset.previousValue = 'collapsed';
|
299 | parent.classList.add(_this.options.classExpandableRow);
|
300 |
|
301 | if (!_featureFlags.componentsX) {
|
302 | _this.tableBody.insertBefore(_this.expandableRows[index], _this.parentRows[index + 1]);
|
303 | }
|
304 | } else {
|
305 | parent.classList.remove(_this.options.classExpandableRow);
|
306 |
|
307 | if (!_featureFlags.componentsX) {
|
308 | _this.tableBody.removeChild(parent.nextElementSibling);
|
309 | }
|
310 |
|
311 | element.dataset.previousValue = 'expanded';
|
312 | }
|
313 | };
|
314 |
|
315 | _this._expandableHoverToggle = function (element) {
|
316 | element.previousElementSibling.classList.add(_this.options.classExpandableRowHover);
|
317 |
|
318 | var mouseout = function mouseout() {
|
319 | element.previousElementSibling.classList.remove(_this.options.classExpandableRowHover);
|
320 | element.removeEventListener('mouseout', mouseout);
|
321 | };
|
322 |
|
323 | element.addEventListener('mouseout', mouseout);
|
324 | };
|
325 |
|
326 | _this._toggleState = function (element, evt) {
|
327 | var data = element.dataset;
|
328 | var label = data.label ? data.label : '';
|
329 | var previousValue = data.previousValue ? data.previousValue : '';
|
330 | var initialEvt = evt;
|
331 |
|
332 | _this.changeState({
|
333 | group: data.event,
|
334 | element: element,
|
335 | label: label,
|
336 | previousValue: previousValue,
|
337 | initialEvt: initialEvt
|
338 | });
|
339 | };
|
340 |
|
341 | _this._keydownHandler = function (evt) {
|
342 | var searchContainer = _this.element.querySelector(_this.options.selectorToolbarSearchContainer);
|
343 |
|
344 | var searchEvent = (0, _eventMatches.default)(evt, _this.options.selectorSearchMagnifier);
|
345 | var activeSearch = searchContainer.classList.contains(_this.options.classToolbarSearchActive);
|
346 |
|
347 | if (evt.which === 27) {
|
348 | _this._actionBarCancel();
|
349 | }
|
350 |
|
351 | if (searchContainer && searchEvent && evt.which === 13) {
|
352 | _this.activateSearch(searchContainer);
|
353 | }
|
354 |
|
355 | if (activeSearch && evt.which === 27) {
|
356 | _this.deactivateSearch(searchContainer, evt);
|
357 | }
|
358 | };
|
359 |
|
360 | _this.refreshRows = function () {
|
361 | var newExpandCells = toArray(_this.element.querySelectorAll(_this.options.selectorExpandCells));
|
362 | var newExpandableRows = toArray(_this.element.querySelectorAll(_this.options.selectorExpandableRows));
|
363 | var newParentRows = toArray(_this.element.querySelectorAll(_this.options.selectorParentRows));
|
364 |
|
365 | if (_this.parentRows.length > 0) {
|
366 | var diffParentRows = newParentRows.filter(function (newRow) {
|
367 | return !_this.parentRows.some(function (oldRow) {
|
368 | return oldRow === newRow;
|
369 | });
|
370 | });
|
371 |
|
372 | if (newExpandableRows.length > 0) {
|
373 | var diffExpandableRows = diffParentRows.map(function (newRow) {
|
374 | return newRow.nextElementSibling;
|
375 | });
|
376 | var mergedExpandableRows = [].concat(_toConsumableArray(toArray(_this.expandableRows)), _toConsumableArray(toArray(diffExpandableRows)));
|
377 |
|
378 | _this._expandableRowsInit(diffExpandableRows);
|
379 |
|
380 | _this.expandableRows = mergedExpandableRows;
|
381 | }
|
382 | } else if (newExpandableRows.length > 0) {
|
383 | _this._expandableRowsInit(newExpandableRows);
|
384 |
|
385 | _this.expandableRows = newExpandableRows;
|
386 | }
|
387 |
|
388 | _this.expandCells = newExpandCells;
|
389 | _this.parentRows = newParentRows;
|
390 | };
|
391 |
|
392 | _this.container = _element.parentNode;
|
393 | _this.toolbarEl = _this.element.querySelector(_this.options.selectorToolbar);
|
394 | _this.batchActionEl = _this.element.querySelector(_this.options.selectorActions);
|
395 | _this.countEl = _this.element.querySelector(_this.options.selectorCount);
|
396 | _this.cancelEl = _this.element.querySelector(_this.options.selectorActionCancel);
|
397 | _this.tableHeaders = _this.element.querySelectorAll('th');
|
398 | _this.tableBody = _this.element.querySelector(_this.options.selectorTableBody);
|
399 | _this.expandCells = [];
|
400 | _this.expandableRows = [];
|
401 | _this.parentRows = [];
|
402 |
|
403 | _this.refreshRows();
|
404 |
|
405 | _this.element.addEventListener('mouseover', function (evt) {
|
406 | var eventElement = (0, _eventMatches.default)(evt, _this.options.selectorChildRow);
|
407 |
|
408 | if (eventElement) {
|
409 | _this._expandableHoverToggle(eventElement, true);
|
410 | }
|
411 | });
|
412 |
|
413 | _this.element.addEventListener('click', function (evt) {
|
414 | var eventElement = (0, _eventMatches.default)(evt, _this.options.eventTrigger);
|
415 |
|
416 | var searchContainer = _this.element.querySelector(_this.options.selectorToolbarSearchContainer);
|
417 |
|
418 | if (eventElement) {
|
419 | _this._toggleState(eventElement, evt);
|
420 | }
|
421 |
|
422 | if (_featureFlags.componentsX && searchContainer) {
|
423 | _this._handleDocumentClick(evt);
|
424 | }
|
425 | });
|
426 |
|
427 | _this.element.addEventListener('keydown', _this._keydownHandler);
|
428 |
|
429 | _this.state = {
|
430 | checkboxCount: 0
|
431 | };
|
432 | return _this;
|
433 | }
|
434 |
|
435 | _createClass(DataTableV2, [{
|
436 | key: "_handleDocumentClick",
|
437 | value: function _handleDocumentClick(evt) {
|
438 | var searchContainer = this.element.querySelector(this.options.selectorToolbarSearchContainer);
|
439 | var searchEvent = (0, _eventMatches.default)(evt, this.options.selectorSearchMagnifier);
|
440 | var activeSearch = searchContainer.classList.contains(this.options.classToolbarSearchActive);
|
441 |
|
442 | if (searchContainer && searchEvent) {
|
443 | this.activateSearch(searchContainer);
|
444 | }
|
445 |
|
446 | if (activeSearch) {
|
447 | this.deactivateSearch(searchContainer, evt);
|
448 | }
|
449 | }
|
450 | }, {
|
451 | key: "activateSearch",
|
452 | value: function activateSearch(container) {
|
453 | var input = container.querySelector(this.options.selectorSearchInput);
|
454 | container.classList.add(this.options.classToolbarSearchActive);
|
455 | input.focus();
|
456 | }
|
457 | }, {
|
458 | key: "deactivateSearch",
|
459 | value: function deactivateSearch(container, evt) {
|
460 | var trigger = container.querySelector(this.options.selectorSearchMagnifier);
|
461 | var input = container.querySelector(this.options.selectorSearchInput);
|
462 | var svg = trigger.querySelector('svg');
|
463 |
|
464 | if (input.value.length === 0 && evt.target !== input && evt.target !== trigger && evt.target !== svg) {
|
465 | container.classList.remove(this.options.classToolbarSearchActive);
|
466 | trigger.focus();
|
467 | }
|
468 |
|
469 | if (evt.which === 27 && evt.target === input) {
|
470 | container.classList.remove(this.options.classToolbarSearchActive);
|
471 | trigger.focus();
|
472 | }
|
473 | }
|
474 | }, {
|
475 | key: "_changeState",
|
476 | value: function _changeState(detail, callback) {
|
477 | this[this.constructor.eventHandlers[detail.group]](detail);
|
478 | callback();
|
479 | }
|
480 | }], [{
|
481 | key: "options",
|
482 | get: function get() {
|
483 | var prefix = _settings.default.prefix;
|
484 | return {
|
485 | selectorInit: "[data-table".concat(suffix, "]"),
|
486 | selectorToolbar: ".".concat(prefix, "--table--toolbar"),
|
487 | selectorActions: ".".concat(prefix, "--batch-actions"),
|
488 | selectorCount: '[data-items-selected]',
|
489 | selectorActionCancel: ".".concat(prefix, "--batch-summary__cancel"),
|
490 | selectorCheckbox: ".".concat(prefix, "--checkbox"),
|
491 | selectorExpandCells: "td.".concat(prefix, "--table-expand").concat(suffix),
|
492 | selectorExpandableRows: ".".concat(prefix, "--expandable-row").concat(suffix),
|
493 | selectorParentRows: ".".concat(prefix, "--parent-row").concat(suffix),
|
494 | selectorChildRow: '[data-child-row]',
|
495 | selectorTableBody: 'tbody',
|
496 | selectorTableSort: ".".concat(prefix, "--table-sort").concat(suffix),
|
497 | selectorTableSelected: ".".concat(prefix, "--data-table").concat(suffix, "--selected"),
|
498 | selectorToolbarSearchContainer: ".".concat(prefix, "--toolbar-search-container-expandable"),
|
499 | selectorSearchMagnifier: ".".concat(prefix, "--search-magnifier"),
|
500 | selectorSearchInput: ".".concat(prefix, "--search-input"),
|
501 | classExpandableRow: "".concat(prefix, "--expandable-row").concat(suffix),
|
502 | classExpandableRowHidden: "".concat(prefix, "--expandable-row--hidden").concat(suffix),
|
503 | classExpandableRowHover: "".concat(prefix, "--expandable-row--hover").concat(suffix),
|
504 | classTableSortAscending: "".concat(prefix, "--table-sort").concat(suffix, "--ascending"),
|
505 | classTableSortActive: "".concat(prefix, "--table-sort").concat(suffix, "--active"),
|
506 | classToolbarSearchActive: "".concat(prefix, "--toolbar-search-container-active"),
|
507 | classActionBarActive: "".concat(prefix, "--batch-actions--active"),
|
508 | classTableSelected: "".concat(prefix, "--data-table").concat(suffix, "--selected"),
|
509 | eventBeforeExpand: "data-table".concat(suffix, "-beforetoggleexpand"),
|
510 | eventAfterExpand: "data-table".concat(suffix, "-aftertoggleexpand"),
|
511 | eventBeforeSort: "data-table".concat(suffix, "-beforetogglesort"),
|
512 | eventAfterSort: "data-table".concat(suffix, "-aftertogglesort"),
|
513 | eventTrigger: '[data-event]',
|
514 | eventParentContainer: '[data-parent-row]'
|
515 | };
|
516 | }
|
517 | }]);
|
518 |
|
519 | DataTableV2.components = new WeakMap();
|
520 | DataTableV2.eventHandlers = {
|
521 | expand: '_rowExpandToggle',
|
522 | sort: '_sortToggle',
|
523 | select: '_selectToggle',
|
524 | 'select-all': '_selectAllToggle',
|
525 | 'action-bar-cancel': '_actionBarCancel'
|
526 | };
|
527 | return DataTableV2;
|
528 | }((0, _mixin2.default)(_createComponent.default, _initComponentBySearch.default, _eventedState.default));
|
529 |
|
530 | var _default = DataTableV2;
|
531 | _exports.default = _default;
|
532 | }); |
\ | No newline at end of file |