Pagelayout : prevent code inserting window.innerHeight when content calculate
[platform/framework/web/web-ui-fw.git] / src / js / jquery.mobile.tizen.pinch.js
1 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
2 //>>description: Tizen core library
3 //>>label: Tizen core
4 //>>group: Tizen:Core
5
6 define( [ 'jquery.mobile.core' ], function ( ) {
7 //>>excludeEnd("jqmBuildExclude");
8
9 /* ***************************************************************************
10  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd.
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in
20  * all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  * ***************************************************************************
30  *
31  *      Author: Minkyu Kang <mk7.kang@samsung.com>
32  */
33
34 /*
35  * Pinch Event
36  *
37  * Events
38  *      pinchstart: triggered when start the touched two points
39  *      pinch: triggered when move the touch point after pinchstart event occured
40  *      pinchend: triggered when touchend event after pinchstart event occured
41  *
42  * Parameters
43  *      point: touched points
44  *      ratio: origin point-to-current point ratio for moving distance
45  *
46  *      $("#pinch").bind("pinch", function (e, p) {
47  *              console.log("point[0].x: " + p.point[0].x);
48  *              console.log("point[0].y: " + p.point[0].y);
49  *              console.log("point[1].x: " + p.point[1].x);
50  *              console.log("point[1].y: " + p.point[1].y);
51  *              console.log("ratio: " + p.ratio);
52  *      });
53  *
54  * Options
55  *      $.mobile.pinch.enabled: true or false
56  *      $.mobile.pinch.min: minimum value of ratio
57  *      $.mobile.pinch.max: maximum value of ratio
58  *      $.mobile.pinch.factor: scale factor of ratio
59  *      $.mobile.pinch.threshold: move threshold of ratio
60  *      $.mobile.pinch.interval: interval for pinch event
61  */
62
63 ( function( $, window, undefined ) {
64
65 pinch_event = {
66         setup: function () {
67                 var thisObject = this,
68                         $this = $( thisObject );
69
70                 if ( !$.mobile.support.touch ) {
71                         return;
72                 }
73
74                 function getDistance( point ) {
75                         var x = point[0].x - point[1].x,
76                                 y = point[0].y - point[0].y;
77
78                         return Math.sqrt( ( x * x ) + ( y * y ) );
79                 }
80
81                 function getParameter( point, ratio ) {
82                         return { point: point, ratio: ratio };
83                 }
84
85                 $this.bind( "touchstart", function ( event ) {
86                         var data = event.originalEvent.touches,
87                                 origin,
88                                 last_ratio = 1,
89                                 processing = false;
90
91                         if ( !$.mobile.pinch.enabled ) {
92                                 return;
93                         }
94
95                         if ( data.length != 2 ) {
96                                 return;
97                         }
98
99                         origin = [
100                                         { x: data[0].pageX, y: data[0].pageY },
101                                         { x: data[1].pageX, y: data[1].pageY }
102                         ];
103
104                         $( event.target ).trigger( "pinchstart", getParameter( origin, undefined ) );
105
106                         function pinchHandler( event ) {
107                                 var data = event.originalEvent.touches,
108                                         current,
109                                         ratio,
110                                         delta,
111                                         factor = $( window ).width() / $.mobile.pinch.factor;
112
113                                 if ( processing ) {
114                                         return;
115                                 }
116
117                                 if ( !origin ) {
118                                         return;
119                                 }
120
121                                 current = [
122                                                 { x: data[0].pageX, y: data[0].pageY },
123                                                 { x: data[1].pageX, y: data[1].pageY }
124                                 ];
125
126                                 delta = getDistance( current ) - getDistance( origin );
127
128                                 ratio = 1 + delta / factor;
129
130                                 if ( ratio < $.mobile.pinch.min ) {
131                                         ratio = $.mobile.pinch.min;
132                                 } else if ( ratio > $.mobile.pinch.max ) {
133                                         ratio = $.mobile.pinch.max;
134                                 }
135
136                                 if ( Math.abs( ratio - last_ratio ) < $.mobile.pinch.threshold ) {
137                                         return;
138                                 }
139
140                                 $( event.target ).trigger( "pinch", getParameter( current, ratio ) );
141
142                                 last_ratio = ratio;
143
144                                 if ( $.mobile.pinch.interval ) {
145                                         processing = true;
146
147                                         setTimeout( function () {
148                                                 processing = false;
149                                         }, $.mobile.pinch.interval );
150                                 }
151                         }
152
153                         $this.bind( "touchmove", pinchHandler )
154                                 .one( "touchend", function ( event ) {
155                                         $this.unbind( "touchmove", pinchHandler );
156                                         $( event.target ).trigger( "pinchend",
157                                                                 getParameter( undefined, last_ratio ) );
158
159                                         origin = undefined;
160                                         current = undefined;
161                                         last_ratio = 1;
162                                         processing = false;
163                                 });
164                 });
165         }
166 };
167
168 $.event.special["pinch"] = pinch_event;
169
170 $.mobile.pinch = {
171         enabled: true,
172         min: 0.1,
173         max: 3,
174         factor: 4,
175         threshold: 0.01,
176         interval: 50
177 };
178
179 })( jQuery, this );
180
181 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
182 } );
183 //>>excludeEnd("jqmBuildExclude");