-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResponsiveLayout.js
169 lines (142 loc) · 4.95 KB
/
ResponsiveLayout.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
//@ts-nocheck
import ResponsiveLayout from "../../../../node_modules/tabulator-tables/src/js/modules/ResponsiveLayout/ResponsiveLayout.js";
/**
* Introduce a new "flexCollapse" mode that depends on flex order for showing responsive rows
* flexCollapse allows editable collapsed cells
*
* Rows are toggled with row-responsive-toggled that is triggered by the updated responsiveCollapse formatter
*/
class MyResponsiveLayout extends ResponsiveLayout {
initialize() {
super.initialize();
if (this.table.options.responsiveLayout === "flexCollapse") {
this.subscribe("row-init", this.initializeRow.bind(this));
this.subscribe("row-responsive-toggled", this.toggleFlexRow.bind(this));
}
}
initializeResponsivity() {
var columns = [];
this.mode = this.table.options.responsiveLayout;
this.collapseFormatter = this.table.options.responsiveLayoutCollapseFormatter || this.formatCollapsedData;
this.collapseStartOpen = this.table.options.responsiveLayoutCollapseStartOpen;
this.hiddenColumns = [];
//determine level of responsivity for each column
this.table.columnManager.columnsByIndex.forEach((column, i) => {
if (column.modules.responsive) {
if (column.modules.responsive.order && column.modules.responsive.visible) {
column.modules.responsive.index = i;
columns.push(column);
if (!column.visible && (this.mode === "collapse" || this.mode === "flexCollapse")) {
this.hiddenColumns.push(column);
}
}
}
});
//sort list by responsivity
columns = columns.reverse();
columns = columns.sort((a, b) => {
var diff = b.modules.responsive.order - a.modules.responsive.order;
return diff || b.modules.responsive.index - a.modules.responsive.index;
});
this.columns = columns;
if (this.mode === "collapse") {
this.generateCollapsedContent();
}
//assign collapse column
for (let col of this.table.columnManager.columnsByIndex) {
if (col.definition.formatter == "responsiveCollapse") {
this.collapseHandleColumn = col;
break;
}
}
if (this.collapseHandleColumn) {
if (this.hiddenColumns.length) {
this.collapseHandleColumn.show();
} else {
this.collapseHandleColumn.hide();
}
}
}
toggleFlexRow(row, isOpen) {
if (isOpen) {
row.getElement().classList.add("tabulator-responsive-flex-open");
row.getCells().forEach(function (cell) {
var el = cell.getElement();
var title = cell.getColumn().getDefinition().title;
if (el.style.display === "none") {
el.style.display = "block";
el.style.width = "100%";
if (title && typeof title !== "undefined") {
el.dataset.label = title;
}
el.classList.add("tabulator-responsive-flex-cell");
}
});
} else {
row.getElement().classList.remove("tabulator-responsive-flex-open");
row.getCells().forEach(function (cell) {
var el = cell.getElement();
if (!el.classList.contains("tabulator-responsive-flex-cell")) {
return;
}
el.style.display = "none";
el.classList.remove("tabulator-responsive-flex-cell");
});
}
}
initializeRow(row) {
var el;
if (row.type !== "calc") {
if (this.table.options.responsiveLayout === "collapse") {
el = document.createElement("div");
el.classList.add("tabulator-responsive-collapse");
}
row.modules.responsiveLayout = {
element: el,
open: this.collapseStartOpen,
};
if (this.collapseStartOpen) {
el.style.display = "none";
}
}
}
hideColumn(column) {
var colCount = this.hiddenColumns.length;
column.hide(false, true);
if (this.mode === "collapse" || this.mode === "flexCollapse") {
this.hiddenColumns.unshift(column);
if (this.mode === "collapse") {
this.generateCollapsedContent();
}
if (this.collapseHandleColumn && !colCount) {
this.collapseHandleColumn.show();
}
}
}
showColumn(column) {
var index;
column.show(false, true);
//set column width to prevent calculation loops on uninitialized columns
column.setWidth(column.getWidth());
if (this.mode === "collapse" || this.mode === "flexCollapse") {
index = this.hiddenColumns.indexOf(column);
if (index > -1) {
this.hiddenColumns.splice(index, 1);
}
if (this.mode === "collapse") {
this.generateCollapsedContent();
}
if (this.mode === "flexCollapse") {
column.getCells().forEach(function (cell) {
var el = cell.getElement();
el.classList.remove("tabulator-responsive-flex-cell");
});
}
if (this.collapseHandleColumn && !this.hiddenColumns.length) {
this.collapseHandleColumn.hide();
}
}
}
}
MyResponsiveLayout.moduleName = "responsiveLayout";
export default MyResponsiveLayout;