forked from marcopeg/MediaTable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.mediaTable.js
294 lines (182 loc) · 7.19 KB
/
jquery.mediaTable.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
/**
Copyright (c) 2012 Marco Pegoraro, http://movableapp.com/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
WHERE TO FIND MEDIA TABLE:
https://github.com/thepeg/MediaTable
http://consulenza-web.com/jquery/MediaTable/
http://www.consulenza-web.com/2012/01/mediatable-jquery-plugin/
**/
/**
* jQuery Bacheca
* Plugin Dimostrativo
*/
;(function($){
/**
* DOM Initialization Logic
*/
var __loop = function( cfg, i ) {
var $this = $(this),
wdg = $this.data( 'MediaTable' );
// Prevent re-initialization of the widget!
if ( !$.isEmptyObject(wdg) ) return;
// Build the widget context.
wdg = {
$wrap: $('<div>'), // Refer to the main content of the widget
$table: $this, // Refer to the MediaTable DOM (TABLE TAG)
$menu: false, // Refer to the column's toggler menu container
cfg: cfg, // widget local configuration object
id: $this.attr('id')
};
// Setup Widget ID if not specified into DOM Table.
if ( !wdg.id ) {
wdg.id = 'MediaTable-' + i;
wdg.$table.attr( 'id', wdg.id );
}
// Activate the MediaTable.
wdg.$table.addClass('activeMediaTable');
// Create the wrapper.
wdg.$wrap.addClass('mediaTableWrapper');
// Place the wrapper near the table and fill with MediaTable.
wdg.$table.before(wdg.$wrap).appendTo(wdg.$wrap);
// Menu initialization logic.
if ( wdg.cfg.menu ) __initMenu( wdg );
// Columns Initialization Loop.
wdg.$table.find('thead th').each(function(i){ __thInit.call( this, i, wdg ); });
// Save widget context into table DOM.
wdg.$table.data( 'MediaTable', wdg );
}; // EndOf: "__loop()" ###
var __initMenu = function( wdg ) {
// Buid menu objects
wdg.$menu = $('<div />');
wdg.$menu.$header = $('<a />');
wdg.$menu.$list = $('<ul />');
// Setup menu general properties and append to DOM.
wdg.$menu
.addClass('mediaTableMenu')
.addClass('mediaTableMenuClosed')
.append(wdg.$menu.$header)
.append(wdg.$menu.$list);
// Add a class to the wrapper to inform about menu presence.
wdg.$wrap.addClass('mediaTableWrapperWithMenu');
// Setup menu title (handler)
wdg.$menu.$header.text(wdg.cfg.menuTitle);
wdg.$table.before(wdg.$menu);
// Bind screen change events to update checkbox status of displayed fields.
$(window).bind('orientationchange resize',function(){
wdg.$menu.find('input').trigger('updateCheck');
});
// Toggle list visibility when clicking the menu title.
wdg.$menu.$header.bind('click',function(){
wdg.$menu.toggleClass('mediaTableMenuClosed');
});
wdg.$table.click(function() {
wdg.$menu.addClass('mediaTableMenuClosed');
});
// Toggle list visibilty when mouse go outside the list itself.
wdg.$menu.$list.bind('mouseleave',function(e){
wdg.$menu.toggleClass('mediaTableMenuClosed');
e.stopPropagation();
});
}; // EndOf: "__initMenu()" ###
var __thInit = function( i, wdg ) {
var $th = $(this),
id = $th.attr('id'),
classes = $th.attr('class');
// Set up an auto-generated ID for the column.
// the ID is based upon widget's ID to allow multiple tables into one page.
if ( !id ) {
id = wdg.id + '-mediaTableCol-' + i;
$th.attr( 'id', id );
}
// Add toggle link to the menu.
if ( wdg.cfg.menu && !$th.is('.persist') ) {
var $li = $('<li><input type="checkbox" name="toggle-cols" id="toggle-col-'+wdg.id+'-'+i+'" value="'+id+'" /> <label for="toggle-col-'+wdg.id+'-'+i+'">'+$th.text()+'</label></li>');
wdg.$menu.$list.append($li);
__liInitActions( $th, $li.find('input'), wdg );
}
// Propagate column's properties to each cell.
$('tbody tr',wdg.$table).each(function(){ __trInit.call( this, i, id, classes ); });
}; // EndOf: "__thInit()" ###
var __trInit = function( i, id, classes ) {
var $cell = $(this).find('td,th').eq(i);
$cell.attr( 'headers', id );
if ( classes ) $cell.addClass(classes);
}; // EndOf: "__trInit()" ###
var __liInitActions = function( $th, $checkbox, wdg ) {
var change = function() {
var val = $checkbox.val(), // this equals the header's ID, i.e. "company"
cols = wdg.$table.find("#" + val + ", [headers="+ val +"]"); // so we can easily find the matching header (id="company") and cells (headers="company")
if ( $checkbox.is(":checked")) {
cols.show();
} else {
cols.hide();
};
};
var updateCheck = function() {
//if ( $th.css("display") == "table-cell") {
if ( $th.is(':visible') ) {
$checkbox.attr("checked", true);
}
else {
$checkbox.attr("checked", false);
};
};
$checkbox
.bind('change', change )
.bind('updateCheck', updateCheck )
.trigger( 'updateCheck' );
} // EndOf: "__liInitActions()" ###
/**
* Widget Destroy Logic
*/
var __destroy = function() {
// Get the widget context.
var wdg = $(this).data( 'MediaTable' );
if ( !wdg ) return;
// Remove the wrapper from the MediaTable.
wdg.$wrap.after(wdg.$table).remove();
// Remove MediaTable active class so media-query will not work.
wdg.$table.removeClass('activeMediaTable');
// Remove DOM reference to the widget context.
wdg.$table.data( 'MediaTable', null );
}; // EndOf: "__destroy()" ###
/**
* jQuery Extension
*/
$.fn.mediaTable = function() {
var cfg = false;
// Default configuration block
if ( !arguments.length || $.isPlainObject(arguments[0]) ) cfg = $.extend({},{
// Teach the widget to create a toggle menu to declare column's visibility
menu: true,
menuTitle: 'Columns:',
t:'e'},arguments[0]);
// -- default configuration block --
// Items initialization loop:
if ( cfg !== false ) {
$(this).each(function( i ){ __loop.call( this, cfg, i ); });
// Item actions loop - switch throught actions
} else if ( arguments.length ) switch ( arguments[0] ) {
case 'destroy':
$(this).each(function(){ __destroy.call( this ); });
break;
}
// Mantengo la possibilitˆ di concatenare plugins.
return this;
}; // EndOf: "$.fn.mediaTable()" ###
})( jQuery );