jquery.hammer.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*! jQuery plugin for Hammer.JS - v1.0.11 - 2014-05-20
  2. * http://eightmedia.github.com/hammer.js
  3. *
  4. * Copyright (c) 2014 Jorik Tangelder <j.tangelder@gmail.com>;
  5. * Licensed under the MIT license */
  6. (function(window, undefined) {
  7. 'use strict';
  8. function setupPlugin(Hammer, $) {
  9. // provide polyfill for Date.now()
  10. // browser support: http://kangax.github.io/es5-compat-table/#Date.now
  11. if (!Date.now) {
  12. Date.now = function now() {
  13. return new Date().getTime();
  14. };
  15. }
  16. /**
  17. * bind dom events
  18. * this overwrites addEventListener
  19. * @param {HTMLElement} element
  20. * @param {String} eventTypes
  21. * @param {Function} handler
  22. */
  23. Hammer.event.bindDom = function(element, eventTypes, handler) {
  24. $(element).on(eventTypes, function($ev) {
  25. var data = $ev.originalEvent || $ev;
  26. var props = ['pageX','pageY','clientX','clientY','target','preventDefault','stopPropagation'];
  27. Hammer.utils.each(props, function(prop) {
  28. if(data[prop] == null) {
  29. data[prop] = $ev[prop];
  30. }
  31. });
  32. // for IE
  33. if(data.which === undefined) {
  34. data.which = data.button;
  35. }
  36. handler.call(this, data);
  37. });
  38. };
  39. /**
  40. * the methods on/off are called by the instance, but with the jquery plugin
  41. * we use the jquery event methods instead.
  42. * @this {Hammer.Instance}
  43. * @return {jQuery}
  44. */
  45. Hammer.utils.each(['on','off'], function(method) {
  46. Hammer.Instance.prototype[method] = function(types, handler) {
  47. return $(this.element)[method](types, handler);
  48. };
  49. });
  50. /**
  51. * trigger events
  52. * this is called by the gestures to trigger an event like 'tap'
  53. * @this {Hammer.Instance}
  54. * @param {String} gesture
  55. * @param {Object} eventData
  56. * @return {jQuery}
  57. */
  58. Hammer.Instance.prototype.trigger = function(gesture, eventData) {
  59. var el = $(this.element);
  60. if(el.has(eventData.target).length) {
  61. el = $(eventData.target);
  62. }
  63. return el.trigger({
  64. type : gesture,
  65. gesture: eventData
  66. });
  67. };
  68. /**
  69. * jQuery plugin
  70. * create instance of Hammer and watch for gestures,
  71. * and when called again you can change the options
  72. * @param {Object} [options={}]
  73. * @return {jQuery}
  74. */
  75. $.fn.hammer = function(options) {
  76. return this.each(function() {
  77. var el = $(this);
  78. var inst = el.data('hammer');
  79. // start new hammer instance
  80. if(!inst) {
  81. el.data('hammer', new Hammer(this, options || {}));
  82. }
  83. // change the options
  84. else if(inst && options) {
  85. Hammer.utils.extend(inst.options, options);
  86. }
  87. });
  88. };
  89. }
  90. // AMD
  91. if(typeof define == 'function' && define.amd) {
  92. define(['hammerjs', 'jquery'], setupPlugin);
  93. }
  94. else {
  95. setupPlugin(window.Hammer, window.jQuery || window.Zepto);
  96. }
  97. })(window);