source: extensions/iPiwigo/www/extensions/jqt.scaling.js @ 9188

Last change on this file since 9188 was 9188, checked in by Polly, 13 years ago

Adding the Phonegap www folder needed to compile.

  • Property svn:executable set to *
File size: 6.2 KB
Line 
1/*!*
2 *
3 * Add support for scaling using jQTouch
4 *
5 * Copyright (c) 2009 Sam Shull <http://www.google.com/profiles/brickysam26>
6 * Released under MIT license
7 *
8 * <code>
9 *
10 *        <div>
11 *            <img src="http://rlv.zcache.com/css_is_awesome_mug-p1687164350719819282objs_210.jpg" alt="CSS Is Awesome" class="scalable"/>
12 *        </div>
13 *
14 * </code>
15 *
16 * Known issues:
17 *        - using a link that is a slideSelector (ie: body > * > ul li a) on the same psuedo-page causes problems during scaling
18 *
19 *
20 *
21 */
22
23(function($)
24{
25           
26    $.fn.scalable = function (options)
27    {
28        return this.each(function ()
29        {
30            new iScale( this, options );
31        });
32    };
33
34    if ($.jQTouch)
35    {
36        $.jQTouch.addExtension(function (jQT){
37           
38            function binder (e, info)
39            {
40                info.page.find('.scalable').scalable();
41            }
42           
43            $(document.body)
44                .bind('pageInserted', binder);
45               
46            $(function()
47            {
48                $('body > *')
49                    .each(function()
50                    {
51                        binder({}, {page: $(this)});
52                    });
53            });
54           
55            return {};
56        });
57       
58       
59        function iScale (el, options)
60        {
61            var that = this;
62           
63            this.numberOfTouches = 2;
64           
65            this.element = el;
66            this.scale(1);
67            this.refresh();
68           
69            this.scaleLessThanOne = false;
70           
71            el.style.webkitTransitionTimingFunction = 'cubic-bezier(0, 0, 0.2, 1)';
72       
73            el.addEventListener('touchstart', this, false);
74            //moved up here because I didnt see any reason to add and remove them
75            el.addEventListener('touchmove', this, false);
76            el.addEventListener('touchend', this, false);
77           
78            window.addEventListener('unload', function ()
79            {
80                el.removeEventListener('touchstart', that, false);
81                el.removeEventListener('touchmove', that, false);
82                el.removeEventListener('touchend', that, false);
83               
84                this.removeEventListener('unload', arguments.callee, false);
85            }, false);
86           
87            if (options)
88            {
89                $.extend(this, options);   
90            }
91        }
92       
93        iScale.prototype = {
94            handleEvent: function(e) {
95                switch(e.type) {
96                    case 'touchstart': return this.onTouchStart(e); break;
97                    case 'touchmove': return this.onTouchMove(e); break;
98                    case 'touchend': return this.onTouchEnd(e); break;
99                }
100            },
101           
102            scale: function (scale) {
103                if (scale !== undefined)
104                {
105                    this._scale = scale;
106                    this.element.style.webkitTransform = 'scale('+scale+')';
107                    return;
108                }
109               
110                return this._scale;
111            },
112           
113            refresh: function() {
114                this.element.style.webkitTransitionDuration = '0';
115            },
116           
117            onTouchStart: function(e) {
118                if ( e.targetTouches.length != this.numberOfTouches )
119                {
120                    return;   
121                }
122               
123                this.refresh();
124               
125                this.moved = false;
126               
127                this.startDistance = Math.sqrt(
128                                                   Math.pow((e.targetTouches[1].clientX - e.targetTouches[0].clientX), 2)
129                                                + Math.pow((e.targetTouches[1].clientX - e.targetTouches[0].clientX), 2)
130                                            );
131               
132                return false;
133            },
134           
135            onTouchMove: function(e) {
136                if( e.targetTouches.length != this.numberOfTouches )
137                    return;
138               
139                e.preventDefault();
140               
141                this.moved = true;
142               
143                this.refresh();
144               
145                var newDistance = Math.sqrt(
146                                                   Math.pow((e.targetTouches[1].clientX - e.targetTouches[0].clientX), 2)
147                                                + Math.pow((e.targetTouches[1].clientY - e.targetTouches[0].clientY), 2)
148                                            ),
149                    difference = newDistance - this.startDistance,
150                    percentChange = (difference / this.startDistance) / 2;
151               
152                this.scale(this.scale() + (this.scale() * percentChange));
153               
154                this.startDistance = newDistance;
155               
156                return false;
157            },
158           
159            onTouchEnd: function(e) {
160                var theTarget,theEvent;
161               
162                if( !this.moved ) {
163                    theTarget  = e.target;
164                    if(theTarget.nodeType == 3) theTarget = theTarget.parentNode;
165                    theEvent = document.createEvent("MouseEvents");
166                    theEvent.initEvent('click', true, true);
167                    theTarget.dispatchEvent(theEvent);
168                    return;
169                }
170               
171                e.preventDefault();
172                e.stopPropagation();
173               
174                if (!this.scaleLessThanOne && this.scale() < 1)
175                {
176                    this.element.style.webkitTransitionDuration = '200ms';
177                    this.scale(1);
178                }
179               
180                return false;
181            },
182           
183            scaleTo: function(dest, runtime) {
184                this.element.style.webkitTransitionDuration = runtime ? runtime : '300ms';
185                this.scale(dest ? dest : 0);
186            }
187        };
188    }
189})(jQuery);
Note: See TracBrowser for help on using the repository browser.