@christian-bromann/webdriverio
Version:
A nodejs bindings implementation for selenium 2.0/webdriver
88 lines (79 loc) • 2.62 kB
JavaScript
/*! jQuery plugin for Hammer.JS - v1.1.3 - 2014-05-20
* http://eightmedia.github.com/hammer.js
*
* Copyright (c) 2014 Jorik Tangelder <j.tangelder@gmail.com>;
* Licensed under the MIT license */
(function(window, undefined) {
'use strict';
function setupPlugin(Hammer, $) {
// provide polyfill for Date.now()
// browser support: http://kangax.github.io/es5-compat-table/#Date.now
if(!Date.now) {
Date.now = function now() {
return new Date().getTime();
};
}
/**
* the methods on/off are called by the instance, but with the jquery plugin
* we use the jquery event methods instead.
* @this {Hammer.Instance}
* @return {jQuery}
*/
Hammer.utils.each(['on', 'off'], function(method) {
Hammer.utils[method] = function(element, type, handler) {
$(element)[method](type, function($ev) {
// append the jquery fixed properties/methods
var data = $.extend({}, $ev.originalEvent, $ev);
if(data.button === undefined) {
data.button = $ev.which - 1;
}
handler.call(this, data);
});
};
});
/**
* 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);
}
});
};
}
// AMD
if(typeof define == 'function' && define.amd) {
define(['hammerjs', 'jquery'], setupPlugin);
} else {
setupPlugin(window.Hammer, window.jQuery || window.Zepto);
}
})(window);