This repository has been archived by the owner on Oct 21, 2022. It is now read-only.
/
tables.columntoggle.js
287 lines (234 loc) · 7.31 KB
/
tables.columntoggle.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
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
282
283
284
285
286
287
/*
* tablesaw: A set of plugins for responsive tables
* Column Toggle: allows the user to toggle which columns are visible.
* Copyright (c) 2013 Filament Group, Inc.
* MIT License
*/
(function() {
var data = {
key: "tablesaw-coltoggle"
};
var ColumnToggle = function(element) {
this.$table = $(element);
if (!this.$table.length) {
return;
}
this.tablesaw = this.$table.data("tablesaw");
this.attributes = {
btnTarget: "data-tablesaw-columntoggle-btn-target",
set: "data-tablesaw-columntoggle-set"
};
this.classes = {
columnToggleTable: "tablesaw-columntoggle",
columnBtnContain: "tablesaw-columntoggle-btnwrap tablesaw-advance",
columnBtn: "tablesaw-columntoggle-btn tablesaw-nav-btn down",
popup: "tablesaw-columntoggle-popup",
priorityPrefix: "tablesaw-priority-"
};
this.set = [];
this.$headers = this.tablesaw._getPrimaryHeaderCells();
this.$table.data(data.key, this);
};
// Column Toggle Sets (one column chooser can control multiple tables)
ColumnToggle.prototype.initSet = function() {
var set = this.$table.attr(this.attributes.set);
if (set) {
// Should not include the current table
var table = this.$table[0];
this.set = $("table[" + this.attributes.set + "='" + set + "']")
.filter(function() {
return this !== table;
})
.get();
}
};
ColumnToggle.prototype.init = function() {
if (!this.$table.length) {
return;
}
var tableId,
id,
$menuButton,
$popup,
$menu,
$btnContain,
self = this;
var cfg = this.tablesaw.getConfig({
getColumnToggleLabelTemplate: function(text) {
return "<label><input type='checkbox' checked>" + text + "</label>";
}
});
this.$table.addClass(this.classes.columnToggleTable);
tableId = this.$table.attr("id");
id = tableId + "-popup";
$btnContain = $("<div class='" + this.classes.columnBtnContain + "'></div>");
// TODO next major version: remove .btn
$menuButton = $(
"<a href='#" +
id +
"' class='btn tablesaw-btn btn-micro " +
this.classes.columnBtn +
"' data-popup-link>" +
"<span>" +
Tablesaw.i18n.columnToggleButton +
"</span></a>"
);
$popup = $("<div class='" + this.classes.popup + "' id='" + id + "'></div>");
$menu = $("<div class='btn-group'></div>");
this.$popup = $popup;
var hasNonPersistentHeaders = false;
this.$headers.each(function() {
var $this = $(this),
priority = $this.attr("data-tablesaw-priority"),
$cells = self.tablesaw._$getCells(this);
if (priority && priority !== "persist") {
$cells.addClass(self.classes.priorityPrefix + priority);
$(cfg.getColumnToggleLabelTemplate($this.text()))
.appendTo($menu)
.find('input[type="checkbox"]')
.data("tablesaw-header", this);
hasNonPersistentHeaders = true;
}
});
if (!hasNonPersistentHeaders) {
$menu.append("<label>" + Tablesaw.i18n.columnToggleError + "</label>");
}
$menu.appendTo($popup);
function onToggleCheckboxChange(checkbox) {
var checked = checkbox.checked;
var header = self.getHeaderFromCheckbox(checkbox);
var $cells = self.tablesaw._$getCells(header);
$cells[!checked ? "addClass" : "removeClass"]("tablesaw-toggle-cellhidden");
$cells[checked ? "addClass" : "removeClass"]("tablesaw-toggle-cellvisible");
self.updateColspanCells(header, checked);
self.$table.trigger("tablesawcolumns");
}
// bind change event listeners to inputs - TODO: move to a private method?
$menu.find('input[type="checkbox"]').on("change", function(e) {
onToggleCheckboxChange(e.target);
if (self.set.length) {
var index;
$(self.$popup)
.find("input[type='checkbox']")
.each(function(j) {
if (this === e.target) {
index = j;
return false;
}
});
$(self.set).each(function() {
var checkbox = $(this)
.data(data.key)
.$popup.find("input[type='checkbox']")
.get(index);
if (checkbox) {
checkbox.checked = e.target.checked;
onToggleCheckboxChange(checkbox);
}
});
}
});
$menuButton.appendTo($btnContain);
// Use a different target than the toolbar
var $btnTarget = $(this.$table.attr(this.attributes.btnTarget));
$btnContain.appendTo($btnTarget.length ? $btnTarget : this.tablesaw.$toolbar);
function closePopup(event) {
// Click came from inside the popup, ignore.
if (event && $(event.target).closest("." + self.classes.popup).length) {
return;
}
$(document).off("click." + tableId);
$menuButton.removeClass("up").addClass("down");
$btnContain.removeClass("visible");
}
var closeTimeout;
function openPopup() {
$btnContain.addClass("visible");
$menuButton.removeClass("down").addClass("up");
$(document).off("click." + tableId, closePopup);
window.clearTimeout(closeTimeout);
closeTimeout = window.setTimeout(function() {
$(document).on("click." + tableId, closePopup);
}, 15);
}
$menuButton.on("click.tablesaw", function(event) {
event.preventDefault();
if (!$btnContain.is(".visible")) {
openPopup();
} else {
closePopup();
}
});
$popup.appendTo($btnContain);
this.$menu = $menu;
// Fix for iOS not rendering shadows correctly when using `-webkit-overflow-scrolling`
var $overflow = this.$table.closest(".tablesaw-overflow");
if ($overflow.css("-webkit-overflow-scrolling")) {
var timeout;
$overflow.on("scroll", function() {
var $div = $(this);
window.clearTimeout(timeout);
timeout = window.setTimeout(function() {
$div.css("-webkit-overflow-scrolling", "auto");
window.setTimeout(function() {
$div.css("-webkit-overflow-scrolling", "touch");
}, 0);
}, 100);
});
}
$(window).on(Tablesaw.events.resize + "." + tableId, function() {
self.refreshToggle();
});
this.initSet();
this.refreshToggle();
};
ColumnToggle.prototype.getHeaderFromCheckbox = function(checkbox) {
return $(checkbox).data("tablesaw-header");
};
ColumnToggle.prototype.refreshToggle = function() {
var self = this;
var invisibleColumns = 0;
this.$menu.find("input").each(function() {
var header = self.getHeaderFromCheckbox(this);
this.checked =
self.tablesaw
._$getCells(header)
.eq(0)
.css("display") === "table-cell";
});
this.updateColspanCells();
};
ColumnToggle.prototype.updateColspanCells = function(header, userAction) {
this.tablesaw.updateColspanCells("tablesaw-toggle-cellhidden", header, userAction);
};
ColumnToggle.prototype.destroy = function() {
this.$table.removeClass(this.classes.columnToggleTable);
this.$table.find("th, td").each(function() {
var $cell = $(this);
$cell.removeClass("tablesaw-toggle-cellhidden").removeClass("tablesaw-toggle-cellvisible");
this.className = this.className.replace(/\bui\-table\-priority\-\d\b/g, "");
});
};
// on tablecreate, init
$(document).on(Tablesaw.events.create, function(e, tablesaw) {
if (tablesaw.mode === "columntoggle") {
var table = new ColumnToggle(tablesaw.table);
table.init();
}
});
$(document).on(Tablesaw.events.destroy, function(e, tablesaw) {
if (tablesaw.mode === "columntoggle") {
$(tablesaw.table)
.data(data.key)
.destroy();
}
});
$(document).on(Tablesaw.events.refresh, function(e, tablesaw) {
if (tablesaw.mode === "columntoggle") {
$(tablesaw.table)
.data(data.key)
.refreshPriority();
}
});
Tablesaw.ColumnToggle = ColumnToggle;
})();