forked from jquery-archive/jquery-mobile
/
table.columntoggle.js
187 lines (149 loc) · 4.66 KB
/
table.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
/*!
* jQuery Mobile Column-toggling Table @VERSION
* http://jquerymobile.com
*
* Copyright jQuery Foundation and other contributors
* Released under the MIT license.
* http://jquery.org/license
*/
//>>label: Table: Column Toggle
//>>group: Widgets
//>>description: Extends the table widget to a column toggle menu and responsive column visibility
//>>docs: http://api.jquerymobile.com/table-columntoggle/
//>>demos: http://demos.jquerymobile.com/@VERSION/table-column-toggle/
//>>css.structure: ../css/structure/jquery.mobile.table.columntoggle.css
( function( factory ) {
if ( typeof define === "function" && define.amd ) {
// AMD. Register as an anonymous module.
define( [
"jquery",
"./table" ], factory );
} else {
factory( jQuery );
}
} )( function( $, undefined ) {
return $.widget( "mobile.table", $.mobile.table, {
options: {
mode: "columntoggle",
classes: {
"ui-table-cell-hidden": "",
"ui-table-cell-visible": "",
"ui-table-priority-": "",
"ui-table-columntoggle": ""
}
},
_create: function() {
// Needed because the superclass calls refresh() which needs to behave differently if
// _create() hasn't happened yet
this._instantiating = true;
this._super();
if ( this.options.mode !== "columntoggle" ) {
return;
}
if ( !this.options.enhanced ) {
this._enhanceColumnToggle();
}
// Cause refresh() to revert to normal operation
this._instantiating = false;
},
_enhanceColumnToggle: function() {
this._addClass( "ui-table-columntoggle" );
this._updateHeaderPriorities();
},
_updateVariableColumn: function( header, cells, priority ) {
this._addClass( cells, "ui-table-priority-" + priority );
},
_updateHeaderPriorities: function( state ) {
this.headers.each( $.proxy( function( index, element ) {
var header = $( element ),
priority = $.mobile.getAttribute( element, "priority" );
if ( priority ) {
this._updateVariableColumn(
header,
header.add( header.jqmData( "cells" ) ),
priority,
state );
}
}, this ) );
},
_setColumnVisibility: function( header, visible ) {
var cells = header.jqmData( "cells" );
if ( cells ) {
cells = cells.add( header );
this._unlock( cells );
this._addClass( cells,
visible ? "ui-table-cell-visible" : "ui-table-cell-hidden" );
}
},
setColumnVisibility: function( cell, visible ) {
var header;
// If cell is a number, then simply index into the headers array
if ( $.type( cell ) === "number" ) {
header = this.headers.eq( cell );
// Otherwise it's assumed to be a jQuery collection object
} else if ( cell.length > 0 ) {
// If it's one of the headers, then we already have the header we wanted
if ( this.headers.index( cell[ 0 ] ) >= 0 ) {
header = cell.first();
// Otherwise we assume it's one of the cells, so look for it in the "cells" data for
// each header
} else {
this.headers.each( $.proxy( function( index, singleHeader ) {
var possibleHeader = $( singleHeader ),
cells = possibleHeader.jqmData( "cells" );
if ( ( cells ? cells.index( cell[ 0 ] ) : -1 ) >= 0 ) {
header = possibleHeader;
return false;
}
}, this ) );
}
}
if ( header ) {
this._setColumnVisibility( header, visible );
}
},
_unlock: function( cells ) {
// Allow hide/show via CSS only = remove all toggle-locks
var locked = ( cells ||
this.element
.children( "thead, tbody" )
.children( "tr" )
.children( ".ui-table-cell-hidden, .ui-table-cell-visible" ) );
this._removeClass( locked, "ui-table-cell-hidden ui-table-cell-visible" );
},
_recordLockedColumns: $.noop,
_restoreLockedColumns: $.noop,
refresh: function() {
var lockedColumns;
// Calling _super() here updates this.headers
this._super();
if ( !this._instantiating && this.options.mode === "columntoggle" ) {
// Record which columns are locked
lockedColumns = this._recordLockedColumns();
// Columns not being replaced must be cleared from input toggle-locks
this._unlock();
// Update priorities
this._updateHeaderPriorities();
// Make sure columns that were locked before this refresh, and which are still around
// after the refresh, are restored to their locked state
this._restoreLockedColumns( lockedColumns );
}
},
_destroy: function() {
if ( this.options.mode === "columntoggle" ) {
if ( !this.options.enhanced ) {
this.headers.each( $.proxy( function( index, element ) {
var header,
priority = $.mobile.getAttribute( element, "priority" );
if ( priority ) {
header = $( element );
header
.add( header.jqmData( "cells" ) );
}
}, this ) );
}
}
return this._superApply( arguments );
}
} );
} );