Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Started working on removing jQuery dependency #4

Closed
wants to merge 3 commits into from

3 participants

@marioestrada

Most of the jQuery dependencies have been removed, css styles and traversing is now done via vanilla javascript.

Binds and triggers still use jQuery.

@Aratramba

Hi Mario, thanks for your work. We'll look into the changes you've made and see what we can merge. Looks great so far!

@jaap3 jaap3 closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 75 additions and 28 deletions.
  1. +75 −28 hammer.js
View
103 hammer.js
@@ -2,7 +2,7 @@
* Hammer.JS
* version 0.01
*/
-function Hammer(element, options)
+function Hammer(element, options, undefined)
{
var self = this;
@@ -29,27 +29,32 @@ function Hammer(element, options)
hold : true,
hold_timeout : 500
};
- options = $.extend({}, defaults, options);
+ options = extend({}, defaults, options);
// make sure element in a jQuery object
- element = $(element);
+ elements = $(element);
// some css hacks
- $(['-webkit-','-moz-','-ms-','-o-','']).each(function(i, vendor) {
- var css = {};
- var props = {
- "user-select": "none",
- "touch-callout": "none",
- "user-drag": "none",
- "tap-highlight-color": "rgba(0,0,0,0)"
- };
-
- for(var prop in props) {
- css[vendor + prop] = props[prop];
+ var vendors = ['-webkit-','-moz-','-ms-','-o-',''];
+ //var css = '';
+ var css_props = {
+ "user-select": "none",
+ "touch-callout": "none",
+ "user-drag": "none",
+ "tap-highlight-color": "rgba(0,0,0,0)"
+ }
+
+ for(i = 0; i < vendors.length; i++)
+ {
+ if( supports(vendors[i] + 'user-select') ) {
+ for(var prop in css_props) {
+ for(j = 0; j < elements.length; j++)
+ {
+ elements[j].style[vendors[i] + prop] = css_props[prop];
+ }
+ }
}
-
- element.css(css);
- });
+ }
// holds the distance that has been moved
var _distance = 0;
@@ -82,6 +87,47 @@ function Hammer(element, options)
// keep track of the mouse status
var _mousedown = false;
+ // We borrow each and extend from Underscore's implementation
+ var nativeForEach = Array.prototype.forEach;
+ function each (obj, iterator, context)
+ {
+ if (obj == null) return;
+ if (nativeForEach && obj.forEach === nativeForEach) {
+ obj.forEach(iterator, context);
+ } else if (obj.length === +obj.length) {
+ for (var i = 0, l = obj.length; i < l; i++) {
+ if (i in obj && iterator.call(context, obj[i], i, obj) === {}) return;
+ }
+ } else {
+ for (var key in obj) {
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
+ if (iterator.call(context, obj[key], key, obj) === {}) return;
+ }
+ }
+ }
+ }
+
+ function extend ( obj )
+ {
+ each(Array.prototype.slice.call(arguments, 1), function( source )
+ {
+ for (var prop in source) {
+ obj[prop] = source[prop];
+ }
+ });
+
+ return obj;
+ };
+
+ function supports( property )
+ {
+ return document.body.style[property] !== undefined;
+ }
+
+ function isFunction( obj )
+ {
+ return Object.prototype.toString.call( obj ) == "[object Function]";
+ }
/**
* angle to direction define
@@ -98,16 +144,18 @@ function Hammer(element, options)
};
var direction;
- $.each(directions, function(key, value){
- if(value){
+
+ for(var key in directions)
+ {
+ if(directions[key]){
direction = key;
- return false;
+ break;
}
- });
+ }
+
return direction;
};
-
/**
* count the number of fingers in the event
* when no fingers are detected, one finger is returned (mouse pointer)
@@ -121,7 +169,6 @@ function Hammer(element, options)
return event.originalEvent.touches ? event.originalEvent.touches.length : 1;
}
-
/**
* get the x and y positions from the event object
* @param jQueryEvent
@@ -166,10 +213,10 @@ function Hammer(element, options)
params.touches = getXYfromEvent(params.originalEvent);
// trigger jQuery event
- element.trigger($.Event(eventName, params));
+ elements.trigger($.Event(eventName, params));
// trigger callback
- if($.isFunction(self["on"+ eventName])) {
+ if(isFunction(self["on"+ eventName])) {
self["on"+ eventName].call(self, params);
}
}
@@ -369,7 +416,7 @@ function Hammer(element, options)
_touch_start_time = new Date().getTime();
_fingers = countFingers(event);
_first = true;
- _offset = element.offset();
+ _offset = elements.offset();
_mousedown = true;
@@ -434,13 +481,13 @@ function Hammer(element, options)
// bind events for touch devices
// except for windows phone 7.5, it doenst support touch events..!
if('ontouchstart' in window) {
- element.bind("touchstart touchmove touchend touchcancel", handleEvents);
+ elements.bind("touchstart touchmove touchend touchcancel", handleEvents);
}
// for non-touch
else {
// Listen for mouseup on the document so we know it happens
// even if the mouse has left the element.
$(document).bind("mouseup", handleEvents);
- element.bind("mousedown mousemove", handleEvents);
+ elements.bind("mousedown mousemove", handleEvents);
}
}
Something went wrong with that request. Please try again.