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

Commit 981cae7

Browse files
author
Gabriel Schulhof
committed
Button: Implement widget(), _destroy(), and perform _setOptions() via buttonMarkup()
1 parent ab07a96 commit 981cae7

File tree

1 file changed

+65
-44
lines changed

1 file changed

+65
-44
lines changed

js/widgets/forms/button.js

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ define( [ "jquery", "../../jquery.mobile.widget", "../../jquery.mobile.buttonMar
99
//>>excludeEnd("jqmBuildExclude");
1010
(function( $, undefined ) {
1111

12+
function splitOptions( o ) {
13+
var key, ret = { btn: {}, widget: {} };
14+
15+
for ( key in o ) {
16+
if ( o[ key ] !== null ) {
17+
if ( key === "disabled" ) {
18+
ret.widget.disabled = o[ key ];
19+
ret.haveWidget = true;
20+
} else if ( key !== "initSelector" ) {
21+
ret.btn[ key ] = o[ key ];
22+
ret.haveBtn = true;
23+
}
24+
}
25+
}
26+
27+
return ret;
28+
}
29+
1230
$.widget( "mobile.button", $.mobile.widget, {
1331
options: {
1432
theme: null,
@@ -21,10 +39,11 @@ $.widget( "mobile.button", $.mobile.widget, {
2139
mini: null,
2240
initSelector: "button, [type='button'], [type='submit'], [type='reset']"
2341
},
42+
2443
_create: function() {
25-
var $el = this.element,
26-
$button,
27-
o,
44+
var $button,
45+
o = this.options,
46+
$el = this.element,
2847
classes = "";
2948

3049
// if this is a link, check if it's been enhanced and, if not, use the right function
@@ -34,53 +53,44 @@ $.widget( "mobile.button", $.mobile.widget, {
3453
}
3554
return;
3655
}
37-
// create a copy of this.options we can pass to buttonMarkup
38-
o = ( function( tdo ) {
39-
var key, ret = {};
40-
41-
for ( key in tdo ) {
42-
if ( tdo[ key ] !== null && key !== "initSelector" ) {
43-
ret[ key ] = tdo[ key ];
44-
}
45-
}
46-
47-
return ret;
48-
} )( this.options );
4956

5057
// get the inherited theme
5158
// TODO centralize for all widgets
52-
if ( !this.options.theme ) {
53-
this.options.theme = $.mobile.getInheritedTheme( this.element, "c" );
59+
if ( !o.theme ) {
60+
o.theme = $.mobile.getInheritedTheme( $el, "c" );
5461
}
62+
o.disabled = $el.prop( "disabled" );
63+
o = splitOptions( o );
5564

5665
// TODO: Post 1.1--once we have time to test thoroughly--any classes manually applied to the original element should be carried over to the enhanced element, with an `-enhanced` suffix. See https://github.com/jquery/jquery-mobile/issues/3577
5766
/* if ( $el[0].className.length ) {
5867
classes = $el[0].className;
5968
} */
60-
if ( !!~$el[0].className.indexOf( "ui-btn-left" ) ) {
69+
if ( !!~$el[ 0 ].className.indexOf( "ui-btn-left" ) ) {
6170
classes = "ui-btn-left";
6271
}
6372

64-
if ( !!~$el[0].className.indexOf( "ui-btn-right" ) ) {
73+
if ( !!~$el[ 0 ].className.indexOf( "ui-btn-right" ) ) {
6574
classes = "ui-btn-right";
6675
}
6776

68-
if ( $el.attr( "type" ) === "submit" || $el.attr( "type" ) === "reset" ) {
69-
classes ? classes += " ui-submit" : classes = "ui-submit";
77+
if ( $el.attr( "type" ) === "submit" || $el.attr( "type" ) === "reset" ) {
78+
classes ? classes += " ui-submit" : classes = "ui-submit";
7079
}
7180
$( "label[for='" + $el.attr( "id" ) + "']" ).addClass( "ui-submit" );
7281

7382
// Add ARIA role
7483
this.button = $( "<div></div>" )
7584
[ $el.html() ? "html" : "text" ]( $el.html() || $el.val() )
7685
.insertBefore( $el )
77-
.buttonMarkup( o )
86+
.buttonMarkup( o.btn )
7887
.addClass( classes )
7988
.append( $el.addClass( "ui-btn-hidden" ) );
89+
this._setOption( "disabled", o.widget.disabled );
8090

81-
$button = this.button;
91+
$button = this.button;
8292

83-
$el.bind({
93+
this._on( $el, {
8494
focus: function() {
8595
$button.addClass( $.mobile.focusClass );
8696
},
@@ -89,42 +99,53 @@ $.widget( "mobile.button", $.mobile.widget, {
8999
$button.removeClass( $.mobile.focusClass );
90100
}
91101
});
102+
},
103+
104+
widget: function() {
105+
return this.button;
106+
},
92107

93-
this.refresh();
108+
_destroy: function() {
109+
var b = this.button;
110+
this.element.insertBefore( b );
111+
b.remove();
112+
},
113+
114+
_setOptions: function( o ) {
115+
o = splitOptions( o );
116+
117+
// Resolve the buttonMarkup options
118+
if ( o.haveBtn ) {
119+
this.button.buttonMarkup( o.btn );
120+
}
121+
122+
// ... and pass the rest up
123+
if ( o.haveWidget ) {
124+
this._super( o.widget );
125+
}
94126
},
95127

96128
_setOption: function( key, value ) {
97129
var op = {};
98130

99131
op[ key ] = value;
100-
if ( key !== "initSelector" ) {
132+
if ( key === "disabled" ) {
133+
value = !!value;
134+
this.element.prop( "disabled", value );
135+
// FIXME: We should be using ui-state-disabled, so we can get rid of this line
136+
this.button.toggleClass( "ui-disabled", value );
137+
} else if ( key !== "initSelector" ) {
101138
this.button.buttonMarkup( op );
102139
// Record the option change in the options and in the DOM data-* attributes
103140
this.element.attr( "data-" + ( $.mobile.ns || "" ) + ( key.replace( /([A-Z])/, "-$1" ).toLowerCase() ), value );
104141
}
105-
this._super( "_setOption", key, value );
106-
},
107-
108-
enable: function() {
109-
this.element.attr( "disabled", false );
110-
this.button.removeClass( "ui-disabled" ).attr( "aria-disabled", false );
111-
return this._setOption( "disabled", false );
112-
},
113-
114-
disable: function() {
115-
this.element.attr( "disabled", true );
116-
this.button.addClass( "ui-disabled" ).attr( "aria-disabled", true );
117-
return this._setOption( "disabled", true );
142+
this._super( key, value );
118143
},
119144

120145
refresh: function() {
121146
var $el = this.element;
122147

123-
if ( $el.prop("disabled") ) {
124-
this.disable();
125-
} else {
126-
this.enable();
127-
}
148+
this._setOption( "disabled", $el.prop( "disabled" ) );
128149

129150
// Grab the button's text element from its implementation-independent data item
130151
$( this.button.data( 'buttonElements' ).text )[ $el.html() ? "html" : "text" ]( $el.html() || $el.val() );

0 commit comments

Comments
 (0)