Merge branch 'master' into tizen_2.1
[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         Flora License
11
12         Version 1.0, April, 2013
13
14         http://floralicense.org/license/
15
16         TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
17
18         1. Definitions.
19
20         "License" shall mean the terms and conditions for use, reproduction,
21         and distribution as defined by Sections 1 through 9 of this document.
22
23         "Licensor" shall mean the copyright owner or entity authorized by
24         the copyright owner that is granting the License.
25
26         "Legal Entity" shall mean the union of the acting entity and
27         all other entities that control, are controlled by, or are
28         under common control with that entity. For the purposes of
29         this definition, "control" means (i) the power, direct or indirect,
30         to cause the direction or management of such entity,
31         whether by contract or otherwise, or (ii) ownership of fifty percent (50%)
32         or more of the outstanding shares, or (iii) beneficial ownership of
33         such entity.
34
35         "You" (or "Your") shall mean an individual or Legal Entity
36         exercising permissions granted by this License.
37
38         "Source" form shall mean the preferred form for making modifications,
39         including but not limited to software source code, documentation source,
40         and configuration files.
41
42         "Object" form shall mean any form resulting from mechanical
43         transformation or translation of a Source form, including but
44         not limited to compiled object code, generated documentation,
45         and conversions to other media types.
46
47         "Work" shall mean the work of authorship, whether in Source or Object form,
48         made available under the License, as indicated by a copyright notice
49         that is included in or attached to the work (an example is provided
50         in the Appendix below).
51
52         "Derivative Works" shall mean any work, whether in Source or Object form,
53         that is based on (or derived from) the Work and for which the editorial
54         revisions, annotations, elaborations, or other modifications represent,
55         as a whole, an original work of authorship. For the purposes of this License,
56         Derivative Works shall not include works that remain separable from,
57         or merely link (or bind by name) to the interfaces of, the Work and
58         Derivative Works thereof.
59
60         "Contribution" shall mean any work of authorship, including the original
61         version of the Work and any modifications or additions to that Work or
62         Derivative Works thereof, that is intentionally submitted to Licensor
63         for inclusion in the Work by the copyright owner or by an individual or
64         Legal Entity authorized to submit on behalf of the copyright owner.
65         For the purposes of this definition, "submitted" means any form of
66         electronic, verbal, or written communication sent to the Licensor or
67         its representatives, including but not limited to communication on
68         electronic mailing lists, source code control systems, and issue
69         tracking systems that are managed by, or on behalf of, the Licensor
70         for the purpose of discussing and improving the Work, but excluding
71         communication that is conspicuously marked or otherwise designated
72         in writing by the copyright owner as "Not a Contribution."
73
74         "Contributor" shall mean Licensor and any individual or Legal Entity
75         on behalf of whom a Contribution has been received by Licensor and
76         subsequently incorporated within the Work.
77
78         "Tizen Certified Platform" shall mean a software platform that complies
79         with the standards set forth in the Tizen Compliance Specification
80         and passes the Tizen Compliance Tests as defined from time to time
81         by the Tizen Technical Steering Group and certified by the Tizen
82         Association or its designated agent.
83
84         2. Grant of Copyright License.  Subject to the terms and conditions of
85         this License, each Contributor hereby grants to You a perpetual,
86         worldwide, non-exclusive, no-charge, royalty-free, irrevocable
87         copyright license to reproduce, prepare Derivative Works of,
88         publicly display, publicly perform, sublicense, and distribute the
89         Work and such Derivative Works in Source or Object form.
90
91         3. Grant of Patent License.  Subject to the terms and conditions of
92         this License, each Contributor hereby grants to You a perpetual,
93         worldwide, non-exclusive, no-charge, royalty-free, irrevocable
94         (except as stated in this section) patent license to make, have made,
95         use, offer to sell, sell, import, and otherwise transfer the Work
96         solely as incorporated into a Tizen Certified Platform, where such
97         license applies only to those patent claims licensable by such
98         Contributor that are necessarily infringed by their Contribution(s)
99         alone or by combination of their Contribution(s) with the Work solely
100         as incorporated into a Tizen Certified Platform to which such
101         Contribution(s) was submitted. If You institute patent litigation
102         against any entity (including a cross-claim or counterclaim
103         in a lawsuit) alleging that the Work or a Contribution incorporated
104         within the Work constitutes direct or contributory patent infringement,
105         then any patent licenses granted to You under this License for that
106         Work shall terminate as of the date such litigation is filed.
107
108         4. Redistribution.  You may reproduce and distribute copies of the
109         Work or Derivative Works thereof pursuant to the copyright license
110         above, in any medium, with or without modifications, and in Source or
111         Object form, provided that You meet the following conditions:
112
113           1. You must give any other recipients of the Work or Derivative Works
114                  a copy of this License; and
115           2. You must cause any modified files to carry prominent notices stating
116                  that You changed the files; and
117           3. You must retain, in the Source form of any Derivative Works that
118                  You distribute, all copyright, patent, trademark, and attribution
119                  notices from the Source form of the Work, excluding those notices
120                  that do not pertain to any part of the Derivative Works; and
121           4. If the Work includes a "NOTICE" text file as part of its distribution,
122                  then any Derivative Works that You distribute must include a readable
123                  copy of the attribution notices contained within such NOTICE file,
124                  excluding those notices that do not pertain to any part of
125                  the Derivative Works, in at least one of the following places:
126                  within a NOTICE text file distributed as part of the Derivative Works;
127                  within the Source form or documentation, if provided along with the
128                  Derivative Works; or, within a display generated by the Derivative Works,
129                  if and wherever such third-party notices normally appear.
130                  The contents of the NOTICE file are for informational purposes only
131                  and do not modify the License.
132
133         You may add Your own attribution notices within Derivative Works
134         that You distribute, alongside or as an addendum to the NOTICE text
135         from the Work, provided that such additional attribution notices
136         cannot be construed as modifying the License. You may add Your own
137         copyright statement to Your modifications and may provide additional or
138         different license terms and conditions for use, reproduction, or
139         distribution of Your modifications, or for any such Derivative Works
140         as a whole, provided Your use, reproduction, and distribution of
141         the Work otherwise complies with the conditions stated in this License.
142
143         5. Submission of Contributions. Unless You explicitly state otherwise,
144         any Contribution intentionally submitted for inclusion in the Work
145         by You to the Licensor shall be under the terms and conditions of
146         this License, without any additional terms or conditions.
147         Notwithstanding the above, nothing herein shall supersede or modify
148         the terms of any separate license agreement you may have executed
149         with Licensor regarding such Contributions.
150
151         6. Trademarks.  This License does not grant permission to use the trade
152         names, trademarks, service marks, or product names of the Licensor,
153         except as required for reasonable and customary use in describing the
154         origin of the Work and reproducing the content of the NOTICE file.
155
156         7. Disclaimer of Warranty. Unless required by applicable law or
157         agreed to in writing, Licensor provides the Work (and each
158         Contributor provides its Contributions) on an "AS IS" BASIS,
159         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
160         implied, including, without limitation, any warranties or conditions
161         of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
162         PARTICULAR PURPOSE. You are solely responsible for determining the
163         appropriateness of using or redistributing the Work and assume any
164         risks associated with Your exercise of permissions under this License.
165
166         8. Limitation of Liability. In no event and under no legal theory,
167         whether in tort (including negligence), contract, or otherwise,
168         unless required by applicable law (such as deliberate and grossly
169         negligent acts) or agreed to in writing, shall any Contributor be
170         liable to You for damages, including any direct, indirect, special,
171         incidental, or consequential damages of any character arising as a
172         result of this License or out of the use or inability to use the
173         Work (including but not limited to damages for loss of goodwill,
174         work stoppage, computer failure or malfunction, or any and all
175         other commercial damages or losses), even if such Contributor
176         has been advised of the possibility of such damages.
177
178         9. Accepting Warranty or Additional Liability. While redistributing
179         the Work or Derivative Works thereof, You may choose to offer,
180         and charge a fee for, acceptance of support, warranty, indemnity,
181         or other liability obligations and/or rights consistent with this
182         License. However, in accepting such obligations, You may act only
183         on Your own behalf and on Your sole responsibility, not on behalf
184         of any other Contributor, and only if You agree to indemnify,
185         defend, and hold each Contributor harmless for any liability
186         incurred by, or claims asserted against, such Contributor by reason
187         of your accepting any such warranty or additional liability.
188
189         END OF TERMS AND CONDITIONS
190
191         APPENDIX: How to apply the Flora License to your work
192
193         To apply the Flora License to your work, attach the following
194         boilerplate notice, with the fields enclosed by brackets "[]"
195         replaced with your own identifying information. (Don't include
196         the brackets!) The text should be enclosed in the appropriate
197         comment syntax for the file format. We also recommend that a
198         file or class name and description of purpose be included on the
199         same "printed page" as the copyright notice for easier
200         identification within third-party archives.
201
202            Copyright [yyyy] [name of copyright owner]
203
204            Licensed under the Flora License, Version 1.0 (the "License");
205            you may not use this file except in compliance with the License.
206            You may obtain a copy of the License at
207
208                    http://floralicense.org/license/
209
210            Unless required by applicable law or agreed to in writing, software
211            distributed under the License is distributed on an "AS IS" BASIS,
212            WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
213            See the License for the specific language governing permissions and
214            limitations under the License.
215
216  *      Author: Minkyu Kang <mk7.kang@samsung.com>
217  */
218
219 /*
220  * Pinch Event
221  *
222  * Events
223  *      pinchstart: triggered when start the touched two points
224  *      pinch: triggered when move the touch point after pinchstart event occured
225  *      pinchend: triggered when touchend event after pinchstart event occured
226  *
227  * Parameters
228  *      point: touched points
229  *      ratio: origin point-to-current point ratio for moving distance
230  *
231  *      $("#pinch").bind("pinch", function (e, p) {
232  *              console.log("point[0].x: " + p.point[0].x);
233  *              console.log("point[0].y: " + p.point[0].y);
234  *              console.log("point[1].x: " + p.point[1].x);
235  *              console.log("point[1].y: " + p.point[1].y);
236  *              console.log("ratio: " + p.ratio);
237  *      });
238  *
239  * Options
240  *      $.mobile.pinch.enabled: true or false
241  *      $.mobile.pinch.min: minimum value of ratio
242  *      $.mobile.pinch.max: maximum value of ratio
243  *      $.mobile.pinch.factor: scale factor of ratio
244  *      $.mobile.pinch.threshold: move threshold of ratio
245  *      $.mobile.pinch.interval: interval for pinch event
246  */
247
248
249 ( function( $, window, undefined ) {
250
251 pinch_event = {
252         setup: function () {
253                 var thisObject = this,
254                         $this = $( thisObject );
255
256                 if ( !$.mobile.support.touch ) {
257                         return;
258                 }
259
260                 function getSize( point ) {
261                         var x = point[0].x - point[1].x,
262                                 y = point[0].y - point[1].y;
263
264                         return Math.abs( x * y );
265                 }
266
267                 function getParameter( point, ratio ) {
268                         return { point: point, ratio: ratio };
269                 }
270
271                 $this.bind( "touchstart", function ( event ) {
272                         var data = event.originalEvent.touches,
273                                 origin,
274                                 last_ratio = 1,
275                                 processing = false;
276
277                         if ( !$.mobile.pinch.enabled ) {
278                                 return;
279                         }
280
281                         if ( data.length != 2 ) {
282                                 return;
283                         }
284
285                         origin = [
286                                         { x: data[0].pageX, y: data[0].pageY },
287                                         { x: data[1].pageX, y: data[1].pageY }
288                         ];
289
290                         $( event.target ).trigger( "pinchstart", getParameter( origin, undefined ) );
291
292                         function pinchHandler( event ) {
293                                 var data = event.originalEvent.touches,
294                                         current,
295                                         ratio,
296                                         delta,
297                                         factor = $( window ).width() / $.mobile.pinch.factor;
298
299                                 if ( processing ) {
300                                         return;
301                                 }
302
303                                 if ( !origin ) {
304                                         return;
305                                 }
306
307                                 current = [
308                                                 { x: data[0].pageX, y: data[0].pageY },
309                                                 { x: data[1].pageX, y: data[1].pageY }
310                                 ];
311
312                                 delta = Math.sqrt( getSize( current ) / getSize( origin )  ) ;
313                                 if( delta ) {
314                                         ratio = delta;
315                                 }
316
317                                 if ( ratio < $.mobile.pinch.min ) {
318                                         ratio = $.mobile.pinch.min;
319                                 } else if ( ratio > $.mobile.pinch.max ) {
320                                         ratio = $.mobile.pinch.max;
321                                 }
322
323                                 if ( Math.abs( ratio - last_ratio ) < $.mobile.pinch.threshold ) {
324                                         return;
325                                 }
326
327                                 $( event.target ).trigger( "pinch", getParameter( current, ratio ) );
328
329                                 last_ratio = ratio;
330
331                                 if ( $.mobile.pinch.interval ) {
332                                         processing = true;
333
334                                         setTimeout( function () {
335                                                 processing = false;
336                                         }, $.mobile.pinch.interval );
337                                 }
338                         }
339
340                         $this.bind( "touchmove", pinchHandler )
341                                 .one( "touchend", function ( event ) {
342                                         $this.unbind( "touchmove", pinchHandler );
343                                         $( event.target ).trigger( "pinchend",
344                                                                 getParameter( undefined, last_ratio ) );
345
346                                         origin = undefined;
347                                         current = undefined;
348                                         last_ratio = 1;
349                                         processing = false;
350                                 });
351                 });
352         }
353 };
354
355 $.event.special["pinch"] = pinch_event;
356
357 $.mobile.pinch = {
358         enabled: true,
359         min: 0.1,
360         max: 3,
361         factor: 4,
362         threshold: 0.01,
363         interval: 50
364 };
365
366 })( jQuery, this );
367
368 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
369 } );
370 //>>excludeEnd("jqmBuildExclude");