Permalink
Browse files

initial commit.

  • Loading branch information...
0 parents commit d939adc4933f14567efcef6e84c2f2fa173a5746 @kamicane committed Jul 19, 2009
Showing with 138 additions and 0 deletions.
  1. +11 −0 README.md
  2. +21 −0 license.txt
  3. +106 −0 touch.js
11 README.md
@@ -0,0 +1,11 @@
+# Touch
+
+Touch is a no-frills ultra fast drag class for MooTools 1.2. It serves the purpose to enable drag for normal-sized browsers and mobile safari. Supports safari 3+, mobile safari 2+, firefox 2+, ie 6+. It's API consists in 4 events, 2 public methods and 2 delta values.
+
+## Requirements
+
+Requires [MooTools 1.2.3+](http://mootools.net/download)
+
+## Documentation
+
+Read the source!
21 license.txt
@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2006-2009 Valerio Proietti, <http://mad4milk.net/>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
106 touch.js
@@ -0,0 +1,106 @@
+/*=
+name: Touch
+version: 0.1
+description: Simple drag implementation that works with standard-sized browsers and hooks with mobile safari touch events.
+license: MooTools MIT-Style License (http://mootools.net/license.txt)
+copyright: Valerio Proietti (http://mad4milk.net)
+authors: Valerio Proietti (http://mad4milk.net)
+requires: MooTools 1.2.3+ (Core) (http://mootools.net/download)
+=*/
+
+var Touch = new Class({
+
+ Implements: Events,
+
+ initialize: function(element){
+ this.element = document.id(element);
+
+ this.bound = {
+ start: this.start.bind(this),
+ move: this.move.bind(this),
+ end: this.end.bind(this)
+ };
+
+ if (Browser.Platform.ipod){
+ this.context = this.element;
+ this.startEvent = 'touchstart';
+ this.endEvent = 'touchend';
+ this.moveEvent = 'touchmove';
+ } else {
+ this.context = document;
+ this.startEvent = 'mousedown';
+ this.endEvent = 'mouseup';
+ this.moveEvent = 'mousemove';
+ }
+
+ this.attach();
+ },
+
+ // public methods
+
+ attach: function(){
+ this.element.addListener(this.startEvent, this.bound.start);
+ },
+
+ detach: function(){
+ this.element.removeListener(this.startEvent, this.bound.start);
+ },
+
+ // protected methods
+
+ start: function(event){
+ this.preventDefault(event);
+ // this prevents the copy-paste dialog to show up when dragging. it only affects mobile safari.
+ document.body.style.WebkitUserSelect = 'none';
+
+ this.hasDragged = false;
+
+ this.context.addListener(this.moveEvent, this.bound.move);
+ this.context.addListener(this.endEvent, this.bound.end);
+
+ var page = this.getPage(event);
+
+ this.startX = page.pageX;
+ this.startY = page.pageY;
+
+ this.fireEvent('start');
+ },
+
+ move: function(event){
+ this.preventDefault(event);
+
+ this.hasDragged = true;
+
+ var page = this.getPage(event);
+
+ this.deltaX = page.pageX - this.startX;
+ this.deltaY = page.pageY - this.startY;
+
+ this.fireEvent('move', [this.deltaX, this.deltaY]);
+ },
+
+ end: function(event){
+ this.preventDefault(event);
+ // we re-enable the copy-paste dialog on drag end
+ document.body.style.WebkitUserSelect = '';
+
+ this.context.removeListener(this.moveEvent, this.bound.move);
+ this.context.removeListener(this.endEvent, this.bound.end);
+
+ this.fireEvent((this.hasDragged) ? 'end' : 'cancel');
+ },
+
+ preventDefault: function(event){
+ if (event.preventDefault) event.preventDefault();
+ else event.returnValue = false;
+ },
+
+ getPage: function(event){
+ //when on mobile safari, the coordinates information is inside the targetTouches object
+ if (event.targetTouches) event = event.targetTouches[0];
+ if (event.pageX != null && event.pageY != null) return {pageX: event.pageX, pageY: event.pageY};
+ var element = (!document.compatMode || document.compatMode == 'CSS1Compat') ? document.documentElement : document.body;
+ return {pageX: event.clientX + element.scrollLeft, pageY: event.clientY + element.scrollTop};
+ }
+
+});

0 comments on commit d939adc

Please sign in to comment.