|
5 | 5 | //>>css.structure: ../css/structure/jquery.mobile.table.columntoggle.css |
6 | 6 |
|
7 | 7 |
|
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 ) { |
9 | 16 | //>>excludeEnd("jqmBuildExclude"); |
10 | 17 | (function( $, undefined ) { |
11 | 18 |
|
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 | + } |
47 | 50 |
|
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 ); |
50 | 77 |
|
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 ); |
53 | 80 |
|
54 | | - if( priority ){ |
| 81 | + $menuButton |
| 82 | + .insertBefore( $table ) |
| 83 | + .buttonMarkup( { |
| 84 | + theme: o.columnBtnTheme |
| 85 | + }); |
55 | 86 |
|
56 | | - $cells.addClass( o.classes.priorityPrefix + priority ); |
| 87 | + $popup |
| 88 | + .insertBefore( $table ) |
| 89 | + .popup(); |
57 | 90 |
|
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 |
68 | 92 |
|
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" } ); |
78 | 94 |
|
79 | | - $menuButton |
80 | | - .insertBefore( $table ) |
81 | | - .buttonMarkup({ |
82 | | - theme: o.columnBtnTheme |
| 95 | + $.extend( this, { |
| 96 | + _menu: $menu, |
| 97 | + _menuInputChange: menuInputChange |
83 | 98 | }); |
84 | 99 |
|
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 | + }, |
96 | 102 |
|
97 | | - $.mobile.window.on( "throttledresize", self.refresh ); |
| 103 | + _destroy: function() { |
| 104 | + this._menu.off( "change", "input", this._menuInputChange ); |
| 105 | + this._super(); |
| 106 | + }, |
98 | 107 |
|
99 | | - self.refresh(); |
| 108 | + refresh: function() { |
| 109 | + this._menu.find( "input" ).each( function() { |
| 110 | + var $this = $( this ); |
100 | 111 |
|
| 112 | + this.checked = $this.jqmData( "cells" ).eq( 0 ).css( "display" ) === "table-cell"; |
| 113 | + $this.checkboxradio( "refresh" ); |
| 114 | + }); |
| 115 | + } |
101 | 116 | }); |
102 | 117 |
|
103 | 118 | })( jQuery ); |
|
0 commit comments