8d5138da45dde23ccc875defcbc4b5cf7106d475
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.0.1pre / js / jquery.mobile.forms.button.js
1 /*
2 * "button" plugin - links that proxy to native input/buttons
3 */
4
5 (function( $, undefined ) {
6
7 $.widget( "mobile.button", $.mobile.widget, {
8         options: {
9                 theme: null,
10                 icon: null,
11                 iconpos: null,
12                 inline: null,
13                 corners: true,
14                 shadow: true,
15                 iconshadow: true,
16                 initSelector: "button, [type='button'], [type='submit'], [type='reset'], [type='image']"
17         },
18         _create: function() {
19                 var $el = this.element,
20                         o = this.options,
21                         type,
22                         name,
23                         $buttonPlaceholder;
24
25                 // Add ARIA role
26                 this.button = $( "<div></div>" )
27                         .text( $el.text() || $el.val() )
28                         .insertBefore( $el )
29                         .buttonMarkup({
30                                 theme: o.theme,
31                                 icon: o.icon,
32                                 iconpos: o.iconpos,
33                                 inline: o.inline,
34                                 corners: o.corners,
35                                 shadow: o.shadow,
36                                 iconshadow: o.iconshadow
37                         })
38                         .append( $el.addClass( "ui-btn-hidden" ) );
39
40                 type = $el.attr( "type" );
41                 name = $el.attr( "name" );
42
43                 // Add hidden input during submit if input type="submit" has a name.
44                 if ( type !== "button" && type !== "reset" && name ) {
45                                 $el.bind( "vclick", function() {
46                                         // Add hidden input if it doesn’t already exist.
47                                         if( $buttonPlaceholder === undefined ) {
48                                                 $buttonPlaceholder = $( "<input>", {
49                                                         type: "hidden",
50                                                         name: $el.attr( "name" ),
51                                                         value: $el.attr( "value" )
52                                                 }).insertBefore( $el );
53
54                                                 // Bind to doc to remove after submit handling
55                                                 $( document ).one("submit", function(){
56                                                         $buttonPlaceholder.remove();
57
58                                                         // reset the local var so that the hidden input
59                                                         // will be re-added on subsequent clicks
60                                                         $buttonPlaceholder = undefined;
61                                                 });
62                                         }
63                                 });
64                 }
65
66                 this.refresh();
67         },
68
69         enable: function() {
70                 this.element.attr( "disabled", false );
71                 this.button.removeClass( "ui-disabled" ).attr( "aria-disabled", false );
72                 return this._setOption( "disabled", false );
73         },
74
75         disable: function() {
76                 this.element.attr( "disabled", true );
77                 this.button.addClass( "ui-disabled" ).attr( "aria-disabled", true );
78                 return this._setOption( "disabled", true );
79         },
80
81         refresh: function() {
82                 var $el = this.element;
83
84                 if ( $el.prop("disabled") ) {
85                         this.disable();
86                 } else {
87                         this.enable();
88                 }
89
90                 // the textWrapper is stored as a data element on the button object
91                 // to prevent referencing by it's implementation details (eg 'class')
92                 this.button.data( 'textWrapper' ).text( $el.text() || $el.val() );
93         }
94 });
95
96 //auto self-init widgets
97 $( document ).bind( "pagecreate create", function( e ){
98         $.mobile.button.prototype.enhanceWithin( e.target );
99 });
100
101 })( jQuery );