forked from hammerjs/jquery.hammer.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.hammer.js
115 lines (99 loc) · 3.01 KB
/
jquery.hammer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*! jQuery plugin for Hammer.JS - v1.0.0 - 2013-11-03
* http://eightmedia.github.com/hammer.js
*
* Copyright (c) 2013 Jorik Tangelder <j.tangelder@gmail.com>;
* Licensed under the MIT license */
(function(window, undefined) {
'use strict';
function setup(Hammer, $) {
/**
* bind dom events
* this overwrites addEventListener
* @param {HTMLElement} element
* @param {String} eventTypes
* @param {Function} handler
*/
Hammer.event.bindDom = function(element, eventTypes, handler) {
$(element).on(eventTypes, function(ev) {
var data = ev.originalEvent || ev;
if(data.pageX === undefined) {
data.pageX = ev.pageX;
data.pageY = ev.pageY;
}
if(!data.target) {
data.target = ev.target;
}
if(data.which === undefined) {
data.which = data.button;
}
if(!data.preventDefault) {
data.preventDefault = ev.preventDefault;
}
if(!data.stopPropagation) {
data.stopPropagation = ev.stopPropagation;
}
handler.call(this, data);
});
};
/**
* the methods are called by the instance, but with the jquery plugin
* we use the jquery event methods instead.
* @this {Hammer.Instance}
* @return {jQuery}
*/
Hammer.Instance.prototype.on = function(types, handler) {
return $(this.element).on(types, handler);
};
Hammer.Instance.prototype.off = function(types, handler) {
return $(this.element).off(types, handler);
};
/**
* trigger events
* this is called by the gestures to trigger an event like 'tap'
* @this {Hammer.Instance}
* @param {String} gesture
* @param {Object} eventData
* @return {jQuery}
*/
Hammer.Instance.prototype.trigger = function(gesture, eventData) {
var el = $(this.element);
if(el.has(eventData.target).length) {
el = $(eventData.target);
}
return el.trigger({
type : gesture,
gesture: eventData
});
};
/**
* jQuery plugin
* create instance of Hammer and watch for gestures,
* and when called again you can change the options
* @param {Object} [options={}]
* @return {jQuery}
*/
$.fn.hammer = function(options) {
return this.each(function() {
var el = $(this);
var inst = el.data('hammer');
// start new hammer instance
if(!inst) {
el.data('hammer', new Hammer(this, options || {}));
}
// change the options
else if(inst && options) {
Hammer.utils.extend(inst.options, options);
}
});
};
}
// Based off Lo-Dash's excellent UMD wrapper (slightly modified) - https://github.com/bestiejs/lodash/blob/master/lodash.js#L5515-L5543
// some AMD build optimizers, like r.js, check for specific condition patterns like the following:
if(typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
// define as an anonymous module
define(['hammer', 'jquery'], setup);
}
else {
setup(window.Hammer, window.jQuery || window.Zepto);
}
})(this);