Export 0.2.3
[platform/framework/web/web-ui-fw.git] / src / widgets / progressbar / js / jquery.mobile.tizen.progressbar.js
1 /*
2  * jQuery UI Progressbar @VERSION
3  *
4  * Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
5  * Dual licensed under the MIT or GPL Version 2 licenses.
6  * http://jquery.org/license
7  *
8  * http://docs.jquery.com/UI/Progressbar
9  *
10  * Depends:
11  *   jquery.ui.core.js
12  *   jquery.ui.widget.js
13  * Original file:
14  *   jquery.ui.progressbar.js
15  */
16 /* This is from jquery ui plugin - progressbar 11/16/2011 */
17
18
19 /**
20         @class ProgressBar
21         The progress bar widget shows a control that indicates the progress percentage of an on-going operation. This widget can be scaled to fit inside a parent container.
22
23         To add a progress bar widget to the application, use the following code:
24
25                 <div id="foo" data-role="progressbar"</div>
26 */
27 /**
28         @event change
29         The progress bar can define a callback for the change event, which is fired when the progress value is changed:
30                 <div id="foo" data-role="progressbar"></div>
31                 $("#foo").bind("change", function (ev, val) {
32                         Console.log("Value is changed to " + val);
33                 });
34 */
35 /**
36         @method value
37         You can use the value method with the pickers to set or get the current default progress bar value:
38
39                 <div id="foo" data-role="progressbar"></div>
40                 var oldVal = $("#foo").progressbar("value");
41                 $("#foo").progressbar("value", 50);
42 */
43
44 (function ( $, window, undefined ) {
45
46         $.widget( "tizen.progressbar", $.mobile.widget, {
47                 options: {
48                         value: 0,
49                         max: 100
50                 },
51
52                 min: 0,
53
54                 _create: function () {
55                         this.element
56                                 .addClass( "ui-progressbar" )
57                                 .attr( {
58                                         role: "progressbar",
59                                         "aria-valuemin": this.min,
60                                         "aria-valuemax": this.options.max,
61                                         "aria-valuenow": this._value()
62                                 } );
63
64                         this.valueDiv = $( "<div class='ui-progressbar-value'></div>" )
65                                 .appendTo( this.element );
66
67                         this.valueDiv.wrap("<div class='ui-progressbar-bg'></div>");
68
69                         this.oldValue = this._value();
70                         this._refreshValue();
71                 },
72
73                 _destroy: function () {
74                         this.element
75                                 .removeClass( "ui-progressbar" )
76                                 .removeAttr( "role" )
77                                 .removeAttr( "aria-valuemin" )
78                                 .removeAttr( "aria-valuemax" )
79                                 .removeAttr( "aria-valuenow" );
80
81                         this.valueDiv.remove();
82                 },
83
84                 value: function ( newValue ) {
85                         if ( newValue === undefined ) {
86                                 return this._value();
87                         }
88
89                         this._setOption( "value", newValue );
90                         return this;
91                 },
92
93                 _setOption: function ( key, value ) {
94                         if ( key === "value" ) {
95                                 this.options.value = value;
96                                 this._refreshValue();
97                                 if ( this._value() === this.options.max ) {
98                                         this._trigger( "complete" );
99                                 }
100                         }
101                         // jquery.ui.widget.js MUST be updated to new version!
102                         //this._super( "_setOption", key, value );
103                 },
104
105                 _value: function () {
106                         var val = this.options.value;
107                         // normalize invalid value
108                         if ( typeof val !== "number" ) {
109                                 val = 0;
110                         }
111                         return Math.min( this.options.max, Math.max( this.min, val ) );
112                 },
113
114                 _percentage: function () {
115                         return 100 * this._value() / this.options.max;
116                 },
117
118                 _refreshValue: function () {
119                         var value = this.value(),
120                                 percentage = this._percentage();
121
122                         if ( this.oldValue !== value ) {
123                                 this.oldValue = value;
124                                 this._trigger( "change" );
125                         }
126
127                         this.valueDiv
128                                 .toggle( value > this.min )
129                                 .width( percentage.toFixed(0) + "%" );
130                         this.element.attr( "aria-valuenow", value );
131                 }
132         } );
133
134         // auto self-init widgets
135         $( document ).bind( "pagecreate", function ( e ) {
136                 $( e.target ).find( ":jqmData(role='progressbar')" ).progressbar();
137         } );
138
139 }( jQuery, this ) );