This repository has been archived by the owner on Oct 21, 2022. It is now read-only.
/
tables.swipetoggle.js
330 lines (267 loc) · 9.11 KB
/
tables.swipetoggle.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* tablesaw: A set of plugins for responsive tables
* Swipe Toggle: swipe gesture (or buttons) to navigate which columns are shown.
* Copyright (c) 2013 Filament Group, Inc.
* MIT License
*/
(function(){
var classes = {
// TODO duplicate class, also in tables.js
toolbar: "tablesaw-bar",
hideBtn: "disabled",
persistWidths: "tablesaw-fix-persist",
allColumnsVisible: 'tablesaw-all-cols-visible'
};
var attrs = {
disableTouchEvents: "data-tablesaw-no-touch"
};
function createSwipeTable( tbl, $table ){
var $btns = $( "<div class='tablesaw-advance'></div>" );
// TODO next major version: remove .btn
var $prevBtn = $( "<a href='#' class='btn tablesaw-nav-btn tablesaw-btn btn-micro left' title='Previous Column'></a>" ).appendTo( $btns );
// TODO next major version: remove .btn
var $nextBtn = $( "<a href='#' class='btn tablesaw-nav-btn tablesaw-btn btn-micro right' title='Next Column'></a>" ).appendTo( $btns );
var $headerCells = tbl._getPrimaryHeaderCells();
var $headerCellsNoPersist = $headerCells.not( '[data-tablesaw-priority="persist"]' );
var headerWidths = [];
var $head = $( document.head || 'head' );
var tableId = $table.attr( 'id' );
if( !$headerCells.length ) {
throw new Error( "tablesaw swipe: no header cells found. Are you using <th> inside of <thead>?" );
}
$table.addClass( "tablesaw-swipe" );
// Calculate initial widths
$headerCells.each(function() {
var width = this.offsetWidth;
headerWidths.push( width );
});
$btns.appendTo( $table.prev().filter( '.tablesaw-bar' ) );
if( !tableId ) {
tableId = 'tableswipe-' + Math.round( Math.random() * 10000 );
$table.attr( 'id', tableId );
}
function $getCells( headerCell ) {
return $( headerCell.cells ).add( headerCell );
}
function showColumn( headerCell ) {
$getCells( headerCell ).removeClass( 'tablesaw-cell-hidden' );
}
function hideColumn( headerCell ) {
$getCells( headerCell ).addClass( 'tablesaw-cell-hidden' );
}
function persistColumn( headerCell ) {
$getCells( headerCell ).addClass( 'tablesaw-cell-persist' );
}
function isPersistent( headerCell ) {
return $( headerCell ).is( '[data-tablesaw-priority="persist"]' );
}
function unmaintainWidths() {
$table.removeClass( classes.persistWidths );
$( '#' + tableId + '-persist' ).remove();
}
function maintainWidths() {
var prefix = '#' + tableId + '.tablesaw-swipe ',
styles = [],
tableWidth = $table.width(),
hash = [],
newHash;
// save persistent column widths (as long as they take up less than 75% of table width)
$headerCells.each(function( index ) {
var width;
if( isPersistent( this ) ) {
width = this.offsetWidth;
if( width < tableWidth * 0.75 ) {
hash.push( index + '-' + width );
styles.push( prefix + ' .tablesaw-cell-persist:nth-child(' + ( index + 1 ) + ') { width: ' + width + 'px; }' );
}
}
});
newHash = hash.join( '_' );
if( styles.length ) {
$table.addClass( classes.persistWidths );
var $style = $( '#' + tableId + '-persist' );
// If style element not yet added OR if the widths have changed
if( !$style.length || $style.data( 'tablesaw-hash' ) !== newHash ) {
// Remove existing
$style.remove();
$( '<style>' + styles.join( "\n" ) + '</style>' )
.attr( 'id', tableId + '-persist' )
.data( 'tablesaw-hash', newHash )
.appendTo( $head );
}
}
}
function getNext(){
var next = [],
checkFound;
$headerCellsNoPersist.each(function( i ) {
var $t = $( this ),
isHidden = $t.css( "display" ) === "none" || $t.is( ".tablesaw-cell-hidden" );
if( !isHidden && !checkFound ) {
checkFound = true;
next[ 0 ] = i;
} else if( isHidden && checkFound ) {
next[ 1 ] = i;
return false;
}
});
return next;
}
function getPrev(){
var next = getNext();
return [ next[ 1 ] - 1 , next[ 0 ] - 1 ];
}
function nextpair( fwd ){
return fwd ? getNext() : getPrev();
}
function canAdvance( pair ){
return pair[ 1 ] > -1 && pair[ 1 ] < $headerCellsNoPersist.length;
}
function matchesMedia() {
var matchMedia = $table.attr( "data-tablesaw-swipe-media" );
return !matchMedia || ( "matchMedia" in win ) && win.matchMedia( matchMedia ).matches;
}
function fakeBreakpoints() {
if( !matchesMedia() ) {
return;
}
var containerWidth = $table.parent().width(),
persist = [],
sum = 0,
sums = [],
visibleNonPersistantCount = $headerCells.length;
$headerCells.each(function( index ) {
var $t = $( this ),
isPersist = $t.is( '[data-tablesaw-priority="persist"]' );
persist.push( isPersist );
sum += headerWidths[ index ];
sums.push( sum );
// is persistent or is hidden
if( isPersist || sum > containerWidth ) {
visibleNonPersistantCount--;
}
});
// We need at least one column to swipe.
var needsNonPersistentColumn = visibleNonPersistantCount === 0;
$headerCells.each(function( index ) {
if( persist[ index ] ) {
// for visual box-shadow
persistColumn( this );
return;
}
if( sums[ index ] <= containerWidth || needsNonPersistentColumn ) {
needsNonPersistentColumn = false;
showColumn( this );
} else {
hideColumn( this );
}
});
unmaintainWidths();
$table.trigger( 'tablesawcolumns' );
}
function advance( fwd ){
var pair = nextpair( fwd );
if( canAdvance( pair ) ){
if( isNaN( pair[ 0 ] ) ){
if( fwd ){
pair[0] = 0;
}
else {
pair[0] = $headerCellsNoPersist.length - 1;
}
}
maintainWidths();
hideColumn( $headerCellsNoPersist.get( pair[ 0 ] ) );
showColumn( $headerCellsNoPersist.get( pair[ 1 ] ) );
$table.trigger( 'tablesawcolumns' );
}
}
$prevBtn.add( $nextBtn ).on( "click", function( e ){
advance( !!$( e.target ).closest( $nextBtn ).length );
e.preventDefault();
});
function getCoord( event, key ) {
return ( event.touches || event.originalEvent.touches )[ 0 ][ key ];
}
if( !$table.is( "[" + attrs.disableTouchEvents + "]" ) ) {
$table
.on( "touchstart.swipetoggle", function( e ){
var originX = getCoord( e, 'pageX' );
var originY = getCoord( e, 'pageY' );
var x;
var y;
var scrollTop = window.pageYOffset;
$( win ).off( Tablesaw.events.resize, fakeBreakpoints );
$( this )
.on( "touchmove.swipetoggle", function( e ){
x = getCoord( e, 'pageX' );
y = getCoord( e, 'pageY' );
})
.on( "touchend.swipetoggle", function() {
var cfg = tbl.getConfig({
swipeHorizontalThreshold: 30,
swipeVerticalThreshold: 30
});
// This config code is a little awkward because shoestring doesn’t support deep $.extend
// Trying to work around when devs only override one of (not both) horizontalThreshold or
// verticalThreshold in their TablesawConfig.
// @TODO major version bump: remove cfg.swipe, move to just use the swipePrefix keys
var verticalThreshold = cfg.swipe ? cfg.swipe.verticalThreshold : cfg.swipeVerticalThreshold;
var horizontalThreshold = cfg.swipe ? cfg.swipe.horizontalThreshold : cfg.swipeHorizontalThreshold;
var isPageScrolled = Math.abs( window.pageYOffset - scrollTop ) >= verticalThreshold;
var isVerticalSwipe = Math.abs( y - originY ) >= verticalThreshold;
if( !isVerticalSwipe && !isPageScrolled ) {
if( x - originX < -1 * horizontalThreshold ){
advance( true );
}
if( x - originX > horizontalThreshold ){
advance( false );
}
}
window.setTimeout(function() {
$( win ).on( Tablesaw.events.resize, fakeBreakpoints );
}, 300);
$( this ).off( "touchmove.swipetoggle touchend.swipetoggle" );
});
});
}
$table
.on( "tablesawcolumns.swipetoggle", function(){
var canGoPrev = canAdvance( getPrev() );
var canGoNext = canAdvance( getNext() );
$prevBtn[ canGoPrev ? "removeClass" : "addClass" ]( classes.hideBtn );
$nextBtn[ canGoNext ? "removeClass" : "addClass" ]( classes.hideBtn );
$prevBtn.closest( "." + classes.toolbar )[ !canGoPrev && !canGoNext ? 'addClass' : 'removeClass' ]( classes.allColumnsVisible );
})
.on( "tablesawnext.swipetoggle", function(){
advance( true );
} )
.on( "tablesawprev.swipetoggle", function(){
advance( false );
} )
.on( Tablesaw.events.destroy + ".swipetoggle", function(){
var $t = $( this );
$t.removeClass( 'tablesaw-swipe' );
$t.prev().filter( '.tablesaw-bar' ).find( '.tablesaw-advance' ).remove();
$( win ).off( Tablesaw.events.resize, fakeBreakpoints );
$t.off( ".swipetoggle" );
})
.on( Tablesaw.events.refresh, function() {
// manual refresh
headerWidths = [];
$headerCells.each(function() {
var width = this.offsetWidth;
headerWidths.push( width );
});
fakeBreakpoints();
});
fakeBreakpoints();
$( win ).on( Tablesaw.events.resize, fakeBreakpoints );
}
// on tablecreate, init
$( document ).on( Tablesaw.events.create, function( e, tablesaw ){
if( tablesaw.mode === 'swipe' ){
createSwipeTable( tablesaw, tablesaw.$table );
}
} );
}());