3 var ResponsiveLayout =
function ResponsiveLayout(table) {
6 this.hiddenColumns = [];
9 this.collapseFormatter = [];
10 this.collapseStartOpen =
true;
11 this.collapseHandleColumn =
false;
15 ResponsiveLayout.prototype.initialize =
function () {
19 this.mode = this.table.options.responsiveLayout;
20 this.collapseFormatter = this.table.options.responsiveLayoutCollapseFormatter || this.formatCollapsedData;
21 this.collapseStartOpen = this.table.options.responsiveLayoutCollapseStartOpen;
22 this.hiddenColumns = [];
25 this.table.columnManager.columnsByIndex.forEach(
function (column, i) {
26 if (column.modules.responsive) {
27 if (column.modules.responsive.order && column.modules.responsive.visible) {
28 column.modules.responsive.index = i;
31 if (!column.visible &&
self.mode ===
"collapse") {
32 self.hiddenColumns.push(column);
39 columns = columns.reverse();
40 columns = columns.sort(
function (a, b) {
41 var diff = b.modules.responsive.order - a.modules.responsive.order;
42 return diff || b.modules.responsive.index - a.modules.responsive.index;
45 this.columns = columns;
47 if (this.mode ===
"collapse") {
48 this.generateCollapsedContent();
52 for (var _iterator = this.table.columnManager.columnsByIndex, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
56 if (_i >= _iterator.length)
break;
57 _ref = _iterator[_i++];
59 _i = _iterator.next();
66 if (col.definition.formatter ==
"responsiveCollapse") {
67 this.collapseHandleColumn = col;
72 if (this.collapseHandleColumn) {
73 if (this.hiddenColumns.length) {
74 this.collapseHandleColumn.show();
76 this.collapseHandleColumn.hide();
82 ResponsiveLayout.prototype.initializeColumn =
function (column) {
83 var def = column.getDefinition();
85 column.modules.responsive = { order: typeof def.responsive ===
"undefined" ? 1 : def.responsive, visible: def.visible ===
false ?
false :
true };
88 ResponsiveLayout.prototype.initializeRow =
function (row) {
91 if (row.type !==
"calc") {
92 el = document.createElement(
"div");
93 el.classList.add(
"tabulator-responsive-collapse");
95 row.modules.responsiveLayout = {
97 open: this.collapseStartOpen
100 if (!this.collapseStartOpen) {
101 el.style.display =
'none';
106 ResponsiveLayout.prototype.layoutRow =
function (row) {
107 var rowEl = row.getElement();
109 if (row.modules.responsiveLayout) {
110 rowEl.appendChild(row.modules.responsiveLayout.element);
111 this.generateCollapsedRowContent(row);
116 ResponsiveLayout.prototype.updateColumnVisibility =
function (column, visible) {
118 if (column.modules.responsive) {
119 column.modules.responsive.visible = visible;
124 ResponsiveLayout.prototype.hideColumn =
function (column) {
125 var colCount = this.hiddenColumns.length;
127 column.hide(
false,
true);
129 if (this.mode ===
"collapse") {
130 this.hiddenColumns.unshift(column);
131 this.generateCollapsedContent();
133 if (this.collapseHandleColumn && !colCount) {
134 this.collapseHandleColumn.show();
139 ResponsiveLayout.prototype.showColumn =
function (column) {
142 column.show(
false,
true);
144 column.setWidth(column.getWidth());
146 if (this.mode ===
"collapse") {
147 index = this.hiddenColumns.indexOf(column);
150 this.hiddenColumns.splice(index, 1);
153 this.generateCollapsedContent();
155 if (this.collapseHandleColumn && !this.hiddenColumns.length) {
156 this.collapseHandleColumn.hide();
162 ResponsiveLayout.prototype.update =
function () {
168 var width =
self.table.modules.layout.getMode() ==
"fitColumns" ?
self.table.columnManager.getFlexBaseWidth() :
self.table.columnManager.getWidth();
170 var diff = (
self.table.options.headerVisible ?
self.table.columnManager.element.clientWidth :
self.table.element.clientWidth) - width;
174 var column =
self.columns[
self.index];
177 self.hideColumn(column);
185 var _column =
self.columns[
self.index - 1];
189 if (diff >= _column.getWidth()) {
190 self.showColumn(_column);
203 if (!
self.table.rowManager.activeRowsCount) {
204 self.table.rowManager.renderEmptyScroll();
209 ResponsiveLayout.prototype.generateCollapsedContent =
function () {
211 rows = this.table.rowManager.getDisplayRows();
213 rows.forEach(
function (row) {
214 self.generateCollapsedRowContent(row);
218 ResponsiveLayout.prototype.generateCollapsedRowContent =
function (row) {
221 if (row.modules.responsiveLayout) {
222 el = row.modules.responsiveLayout.element;
224 while (el.firstChild) {
225 el.removeChild(el.firstChild);
226 }contents = this.collapseFormatter(this.generateCollapsedRowData(row));
228 el.appendChild(contents);
233 ResponsiveLayout.prototype.generateCollapsedRowData =
function (row) {
235 data = row.getData(),
239 this.hiddenColumns.forEach(
function (column) {
240 var value = column.getFieldValue(data);
242 if (column.definition.title && column.field) {
243 if (column.modules.format &&
self.table.options.responsiveLayoutCollapseUseFormatters) {
245 mockCellComponent = {
248 getValue:
function getValue() {
251 getData:
function getData() {
254 getElement:
function getElement() {
255 return document.createElement(
"div");
257 getRow:
function getRow() {
258 return row.getComponent();
260 getColumn:
function getColumn() {
261 return column.getComponent();
266 title: column.definition.title,
267 value: column.modules.format.formatter.call(
self.table.modules.format, mockCellComponent, column.modules.format.params)
271 title: column.definition.title,
281 ResponsiveLayout.prototype.formatCollapsedData =
function (data) {
282 var list = document.createElement(
"table"),
285 data.forEach(
function (item) {
286 var div = document.createElement(
"div");
288 if (item.value instanceof Node) {
289 div.appendChild(item.value);
290 item.value = div.innerHTML;
293 listContents +=
"<tr><td><strong>" + item.title +
"</strong></td><td>" + item.value +
"</td></tr>";
296 list.innerHTML = listContents;
298 return Object.keys(data).length ? list :
"";
301 Tabulator.prototype.registerModule(
"responsiveLayout", ResponsiveLayout);