Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit a45edfa

Browse files
author
Gabriel Schulhof
committed
Table: Columntoggle: Convert to proper extension.
1 parent 6e11fc8 commit a45edfa

File tree

1 file changed

+93
-78
lines changed

1 file changed

+93
-78
lines changed

js/widgets/table.columntoggle.js

Lines changed: 93 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -5,99 +5,114 @@
55
//>>css.structure: ../css/structure/jquery.mobile.table.columntoggle.css
66

77

8-
define( [ "jquery", "./table", "../jquery.mobile.buttonMarkup", "./popup", "../jquery.mobile.fieldContain", "widgets/controlgroup", "widgets/forms/checkboxradio" ], function( jQuery ) {
8+
define( [
9+
"jquery",
10+
"./table",
11+
"../jquery.mobile.buttonMarkup",
12+
"./popup",
13+
"../jquery.mobile.fieldContain",
14+
"widgets/controlgroup",
15+
"widgets/forms/checkboxradio" ], function( jQuery ) {
916
//>>excludeEnd("jqmBuildExclude");
1017
(function( $, undefined ) {
1118

12-
$.mobile.table.prototype.options.mode = "columntoggle";
13-
14-
$.mobile.table.prototype.options.columnBtnTheme = null;
15-
16-
$.mobile.table.prototype.options.columnPopupTheme = null;
17-
18-
$.mobile.table.prototype.options.columnBtnText = "Columns...";
19-
20-
$.mobile.table.prototype.options.classes = $.extend(
21-
$.mobile.table.prototype.options.classes,
22-
{
23-
popup: "ui-table-columntoggle-popup",
24-
columnBtn: "ui-table-columntoggle-btn",
25-
priorityPrefix: "ui-table-priority-",
26-
columnToggleTable: "ui-table-columntoggle"
27-
}
28-
);
29-
30-
$.mobile.document.delegate( ":jqmData(role='table')", "tablecreate", function() {
31-
32-
var $table = $( this ),
33-
self = $table.data( "mobile-table" ),
34-
o = self.options,
35-
ns = $.mobile.ns;
36-
37-
if( o.mode !== "columntoggle" ){
38-
return;
39-
}
40-
41-
self.element.addClass( o.classes.columnToggleTable );
42-
43-
var id = ( $table.attr( "id" ) || o.classes.popup ) + "-popup", //TODO BETTER FALLBACK ID HERE
44-
$menuButton = $( "<a href='#" + id + "' class='" + o.classes.columnBtn + "' data-" + ns + "rel='popup' data-" + ns + "mini='true'>" + o.columnBtnText + "</a>" ),
45-
$popup = $( "<div data-" + ns + "role='popup' data-" + ns + "role='fieldcontain' class='" + o.classes.popup + "' id='" + id + "'></div>"),
46-
$menu = $("<fieldset data-" + ns + "role='controlgroup'></fieldset>");
19+
$.widget( "mobile.table", $.mobile.table, {
20+
options: {
21+
mode: "columntoggle",
22+
columnBtnTheme: null,
23+
columnPopupTheme: null,
24+
columnBtnText: "Columns...",
25+
classes: $.extend( $.mobile.table.prototype.options.classes, {
26+
popup: "ui-table-columntoggle-popup",
27+
columnBtn: "ui-table-columntoggle-btn",
28+
priorityPrefix: "ui-table-priority-",
29+
columnToggleTable: "ui-table-columntoggle"
30+
})
31+
},
32+
33+
_create: function() {
34+
var id, $menuButton, $popup, $menu,
35+
$table = this.element,
36+
o = this.options,
37+
ns = $.mobile.ns,
38+
menuInputChange = function( e ) {
39+
var checked = this.checked;
40+
$( this ).jqmData( "cells" )
41+
.toggleClass( "ui-table-cell-hidden", !checked )
42+
.toggleClass( "ui-table-cell-visible", checked );
43+
};
44+
45+
this._super();
46+
47+
if( o.mode !== "columntoggle" ) {
48+
return;
49+
}
4750

48-
// create the hide/show toggles
49-
self.headers.not( "td" ).each(function(){
51+
this.element.addClass( o.classes.columnToggleTable );
52+
53+
id = ( $table.attr( "id" ) || o.classes.popup ) + "-popup"; //TODO BETTER FALLBACK ID HERE
54+
$menuButton = $( "<a href='#" + id + "' class='" + o.classes.columnBtn + "' data-" + ns + "rel='popup' data-" + ns + "mini='true'>" + o.columnBtnText + "</a>" );
55+
$popup = $( "<div data-" + ns + "role='popup' data-" + ns + "role='fieldcontain' class='" + o.classes.popup + "' id='" + id + "'></div>" );
56+
$menu = $( "<fieldset data-" + ns + "role='controlgroup'></fieldset>" );
57+
58+
// create the hide/show toggles
59+
this.headers.not( "td" ).each( function() {
60+
var $this = $( this ),
61+
priority = $this.jqmData( "priority" ),
62+
$cells = $this.add( $this.jqmData( "cells" ) );
63+
64+
if( priority ) {
65+
$cells.addClass( o.classes.priorityPrefix + priority );
66+
67+
$("<label><input type='checkbox' checked />" + $this.text() + "</label>" )
68+
.appendTo( $menu )
69+
.children( 0 )
70+
.jqmData( "cells", $cells )
71+
.checkboxradio( {
72+
theme: o.columnPopupTheme
73+
});
74+
}
75+
});
76+
$menu.appendTo( $popup );
5077

51-
var priority = $( this ).jqmData( "priority" ),
52-
$cells = $( this ).add( $( this ).jqmData( "cells" ) );
78+
// bind change event listeners to inputs - TODO: move to a private method?
79+
$menu.on( "change", "input", menuInputChange );
5380

54-
if( priority ){
81+
$menuButton
82+
.insertBefore( $table )
83+
.buttonMarkup( {
84+
theme: o.columnBtnTheme
85+
});
5586

56-
$cells.addClass( o.classes.priorityPrefix + priority );
87+
$popup
88+
.insertBefore( $table )
89+
.popup();
5790

58-
$("<label><input type='checkbox' checked />" + $( this ).text() + "</label>" )
59-
.appendTo( $menu )
60-
.children( 0 )
61-
.jqmData( "cells", $cells )
62-
.checkboxradio({
63-
theme: o.columnPopupTheme
64-
});
65-
}
66-
});
67-
$menu.appendTo( $popup );
91+
// refresh method
6892

69-
// bind change event listeners to inputs - TODO: move to a private method?
70-
$menu.on( "change", "input", function( e ){
71-
if( this.checked ){
72-
$( this ).jqmData( "cells" ).removeClass( "ui-table-cell-hidden" ).addClass( "ui-table-cell-visible" );
73-
}
74-
else {
75-
$( this ).jqmData( "cells" ).removeClass( "ui-table-cell-visible" ).addClass( "ui-table-cell-hidden" );
76-
}
77-
});
93+
this._on( $.mobile.window, { "throttledresize": "refresh" } );
7894

79-
$menuButton
80-
.insertBefore( $table )
81-
.buttonMarkup({
82-
theme: o.columnBtnTheme
95+
$.extend( this, {
96+
_menu: $menu,
97+
_menuInputChange: menuInputChange
8398
});
8499

85-
$popup
86-
.insertBefore( $table )
87-
.popup();
88-
89-
// refresh method
90-
self.refresh = function(){
91-
$menu.find( "input" ).each( function(){
92-
this.checked = $( this ).jqmData( "cells" ).eq(0).css( "display" ) === "table-cell";
93-
$( this ).checkboxradio( "refresh" );
94-
});
95-
};
100+
this.refresh();
101+
},
96102

97-
$.mobile.window.on( "throttledresize", self.refresh );
103+
_destroy: function() {
104+
this._menu.off( "change", "input", this._menuInputChange );
105+
this._super();
106+
},
98107

99-
self.refresh();
108+
refresh: function() {
109+
this._menu.find( "input" ).each( function() {
110+
var $this = $( this );
100111

112+
this.checked = $this.jqmData( "cells" ).eq( 0 ).css( "display" ) === "table-cell";
113+
$this.checkboxradio( "refresh" );
114+
});
115+
}
101116
});
102117

103118
})( jQuery );

0 commit comments

Comments
 (0)