jquery.mobile.touch.min.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*! jQuery Mobile v1.4.5 | Copyright 2010, 2014 jQuery Foundation, Inc. | jquery.org/license */
  2. (function (e, t, n) {
  3. typeof define == "function" && define.amd ? define(["jquery"], function (r) {
  4. return n(r, e, t), r.mobile
  5. }) : n(e.jQuery, e, t)
  6. })(this, document, function (e, t, n, r) {
  7. (function (e, t, n, r) {
  8. function T(e) {
  9. while (e && typeof e.originalEvent != "undefined")e = e.originalEvent;
  10. return e
  11. }
  12. function N(t, n) {
  13. var i = t.type, s, o, a, l, c, h, p, d, v;
  14. t = e.Event(t), t.type = n, s = t.originalEvent, o = e.event.props, i.search(/^(mouse|click)/) > -1 && (o = f);
  15. if (s)for (p = o.length, l; p;)l = o[--p], t[l] = s[l];
  16. i.search(/mouse(down|up)|click/) > -1 && !t.which && (t.which = 1);
  17. if (i.search(/^touch/) !== -1) {
  18. a = T(s), i = a.touches, c = a.changedTouches, h = i && i.length ? i[0] : c && c.length ? c[0] : r;
  19. if (h)for (d = 0, v = u.length; d < v; d++)l = u[d], t[l] = h[l]
  20. }
  21. return t
  22. }
  23. function C(t) {
  24. var n = {}, r, s;
  25. while (t) {
  26. r = e.data(t, i);
  27. for (s in r)r[s] && (n[s] = n.hasVirtualBinding = !0);
  28. t = t.parentNode
  29. }
  30. return n
  31. }
  32. function k(t, n) {
  33. var r;
  34. while (t) {
  35. r = e.data(t, i);
  36. if (r && (!n || r[n]))return t;
  37. t = t.parentNode
  38. }
  39. return null
  40. }
  41. function L() {
  42. g = !1
  43. }
  44. function A() {
  45. g = !0
  46. }
  47. function O() {
  48. E = 0, v.length = 0, m = !1, A()
  49. }
  50. function M() {
  51. L()
  52. }
  53. function _() {
  54. D(), c = setTimeout(function () {
  55. c = 0, O()
  56. }, e.vmouse.resetTimerDuration)
  57. }
  58. function D() {
  59. c && (clearTimeout(c), c = 0)
  60. }
  61. function P(t, n, r) {
  62. var i;
  63. if (r && r[t] || !r && k(n.target, t))i = N(n, t), e(n.target).trigger(i);
  64. return i
  65. }
  66. function H(t) {
  67. var n = e.data(t.target, s), r;
  68. !m && (!E || E !== n) && (r = P("v" + t.type, t), r && (r.isDefaultPrevented() && t.preventDefault(), r.isPropagationStopped() && t.stopPropagation(), r.isImmediatePropagationStopped() && t.stopImmediatePropagation()))
  69. }
  70. function B(t) {
  71. var n = T(t).touches, r, i, o;
  72. n && n.length === 1 && (r = t.target, i = C(r), i.hasVirtualBinding && (E = w++, e.data(r, s, E), D(), M(), d = !1, o = T(t).touches[0], h = o.pageX, p = o.pageY, P("vmouseover", t, i), P("vmousedown", t, i)))
  73. }
  74. function j(e) {
  75. if (g)return;
  76. d || P("vmousecancel", e, C(e.target)), d = !0, _()
  77. }
  78. function F(t) {
  79. if (g)return;
  80. var n = T(t).touches[0], r = d, i = e.vmouse.moveDistanceThreshold, s = C(t.target);
  81. d = d || Math.abs(n.pageX - h) > i || Math.abs(n.pageY - p) > i, d && !r && P("vmousecancel", t, s), P("vmousemove", t, s), _()
  82. }
  83. function I(e) {
  84. if (g)return;
  85. A();
  86. var t = C(e.target), n, r;
  87. P("vmouseup", e, t), d || (n = P("vclick", e, t), n && n.isDefaultPrevented() && (r = T(e).changedTouches[0], v.push({touchID: E, x: r.clientX, y: r.clientY}), m = !0)), P("vmouseout", e, t), d = !1, _()
  88. }
  89. function q(t) {
  90. var n = e.data(t, i), r;
  91. if (n)for (r in n)if (n[r])return !0;
  92. return !1
  93. }
  94. function R() {
  95. }
  96. function U(t) {
  97. var n = t.substr(1);
  98. return {
  99. setup: function () {
  100. q(this) || e.data(this, i, {});
  101. var r = e.data(this, i);
  102. r[t] = !0, l[t] = (l[t] || 0) + 1, l[t] === 1 && b.bind(n, H), e(this).bind(n, R), y && (l.touchstart = (l.touchstart || 0) + 1, l.touchstart === 1 && b.bind("touchstart", B).bind("touchend", I).bind("touchmove", F).bind("scroll", j))
  103. }, teardown: function () {
  104. --l[t], l[t] || b.unbind(n, H), y && (--l.touchstart, l.touchstart || b.unbind("touchstart", B).unbind("touchmove", F).unbind("touchend", I).unbind("scroll", j));
  105. var r = e(this), s = e.data(this, i);
  106. s && (s[t] = !1), r.unbind(n, R), q(this) || r.removeData(i)
  107. }
  108. }
  109. }
  110. var i = "virtualMouseBindings", s = "virtualTouchID", o = "vmouseover vmousedown vmousemove vmouseup vclick vmouseout vmousecancel".split(" "), u = "clientX clientY pageX pageY screenX screenY".split(" "), a = e.event.mouseHooks ? e.event.mouseHooks.props : [], f = e.event.props.concat(a), l = {}, c = 0, h = 0, p = 0, d = !1, v = [], m = !1, g = !1, y = "addEventListener"in n, b = e(n), w = 1, E = 0, S, x;
  111. e.vmouse = {moveDistanceThreshold: 10, clickDistanceThreshold: 10, resetTimerDuration: 1500};
  112. for (x = 0; x < o.length; x++)e.event.special[o[x]] = U(o[x]);
  113. y && n.addEventListener("click", function (t) {
  114. var n = v.length, r = t.target, i, o, u, a, f, l;
  115. if (n) {
  116. i = t.clientX, o = t.clientY, S = e.vmouse.clickDistanceThreshold, u = r;
  117. while (u) {
  118. for (a = 0; a < n; a++) {
  119. f = v[a], l = 0;
  120. if (u === r && Math.abs(f.x - i) < S && Math.abs(f.y - o) < S || e.data(u, s) === f.touchID) {
  121. t.preventDefault(), t.stopPropagation();
  122. return
  123. }
  124. }
  125. u = u.parentNode
  126. }
  127. }
  128. }, !0)
  129. })(e, t, n), function (e) {
  130. e.mobile = {}
  131. }(e), function (e, t) {
  132. var r = {touch: "ontouchend"in n};
  133. e.mobile.support = e.mobile.support || {}, e.extend(e.support, r), e.extend(e.mobile.support, r)
  134. }(e), function (e, t, r) {
  135. function l(t, n, i, s) {
  136. var o = i.type;
  137. i.type = n, s ? e.event.trigger(i, r, t) : e.event.dispatch.call(t, i), i.type = o
  138. }
  139. var i = e(n), s = e.mobile.support.touch, o = "touchmove scroll", u = s ? "touchstart" : "mousedown", a = s ? "touchend" : "mouseup", f = s ? "touchmove" : "mousemove";
  140. e.each("touchstart touchmove touchend tap taphold swipe swipeleft swiperight scrollstart scrollstop".split(" "), function (t, n) {
  141. e.fn[n] = function (e) {
  142. return e ? this.bind(n, e) : this.trigger(n)
  143. }, e.attrFn && (e.attrFn[n] = !0)
  144. }), e.event.special.scrollstart = {
  145. enabled: !0, setup: function () {
  146. function s(e, n) {
  147. r = n, l(t, r ? "scrollstart" : "scrollstop", e)
  148. }
  149. var t = this, n = e(t), r, i;
  150. n.bind(o, function (t) {
  151. if (!e.event.special.scrollstart.enabled)return;
  152. r || s(t, !0), clearTimeout(i), i = setTimeout(function () {
  153. s(t, !1)
  154. }, 50)
  155. })
  156. }, teardown: function () {
  157. e(this).unbind(o)
  158. }
  159. }, e.event.special.tap = {
  160. tapholdThreshold: 750, emitTapOnTaphold: !0, setup: function () {
  161. var t = this, n = e(t), r = !1;
  162. n.bind("vmousedown", function (s) {
  163. function a() {
  164. clearTimeout(u)
  165. }
  166. function f() {
  167. a(), n.unbind("vclick", c).unbind("vmouseup", a), i.unbind("vmousecancel", f)
  168. }
  169. function c(e) {
  170. f(), !r && o === e.target ? l(t, "tap", e) : r && e.preventDefault()
  171. }
  172. r = !1;
  173. if (s.which && s.which !== 1)return !1;
  174. var o = s.target, u;
  175. n.bind("vmouseup", a).bind("vclick", c), i.bind("vmousecancel", f), u = setTimeout(function () {
  176. e.event.special.tap.emitTapOnTaphold || (r = !0), l(t, "taphold", e.Event("taphold", {target: o}))
  177. }, e.event.special.tap.tapholdThreshold)
  178. })
  179. }, teardown: function () {
  180. e(this).unbind("vmousedown").unbind("vclick").unbind("vmouseup"), i.unbind("vmousecancel")
  181. }
  182. }, e.event.special.swipe = {
  183. scrollSupressionThreshold: 30, durationThreshold: 1e3, horizontalDistanceThreshold: 30, verticalDistanceThreshold: 30, getLocation: function (e) {
  184. var n = t.pageXOffset, r = t.pageYOffset, i = e.clientX, s = e.clientY;
  185. if (e.pageY === 0 && Math.floor(s) > Math.floor(e.pageY) || e.pageX === 0 && Math.floor(i) > Math.floor(e.pageX))i -= n, s -= r; else if (s < e.pageY - r || i < e.pageX - n)i = e.pageX - n, s = e.pageY - r;
  186. return {x: i, y: s}
  187. }, start: function (t) {
  188. var n = t.originalEvent.touches ? t.originalEvent.touches[0] : t, r = e.event.special.swipe.getLocation(n);
  189. return {time: (new Date).getTime(), coords: [r.x, r.y], origin: e(t.target)}
  190. }, stop: function (t) {
  191. var n = t.originalEvent.touches ? t.originalEvent.touches[0] : t, r = e.event.special.swipe.getLocation(n);
  192. return {time: (new Date).getTime(), coords: [r.x, r.y]}
  193. }, handleSwipe: function (t, n, r, i) {
  194. if (n.time - t.time < e.event.special.swipe.durationThreshold && Math.abs(t.coords[0] - n.coords[0]) > e.event.special.swipe.horizontalDistanceThreshold && Math.abs(t.coords[1] - n.coords[1]) < e.event.special.swipe.verticalDistanceThreshold) {
  195. var s = t.coords[0] > n.coords[0] ? "swipeleft" : "swiperight";
  196. return l(r, "swipe", e.Event("swipe", {target: i, swipestart: t, swipestop: n}), !0), l(r, s, e.Event(s, {target: i, swipestart: t, swipestop: n}), !0), !0
  197. }
  198. return !1
  199. }, eventInProgress: !1, setup: function () {
  200. var t, n = this, r = e(n), s = {};
  201. t = e.data(this, "mobile-events"), t || (t = {length: 0}, e.data(this, "mobile-events", t)), t.length++, t.swipe = s, s.start = function (t) {
  202. if (e.event.special.swipe.eventInProgress)return;
  203. e.event.special.swipe.eventInProgress = !0;
  204. var r, o = e.event.special.swipe.start(t), u = t.target, l = !1;
  205. s.move = function (t) {
  206. if (!o || t.isDefaultPrevented())return;
  207. r = e.event.special.swipe.stop(t), l || (l = e.event.special.swipe.handleSwipe(o, r, n, u), l && (e.event.special.swipe.eventInProgress = !1)), Math.abs(o.coords[0] - r.coords[0]) > e.event.special.swipe.scrollSupressionThreshold && t.preventDefault()
  208. }, s.stop = function () {
  209. l = !0, e.event.special.swipe.eventInProgress = !1, i.off(f, s.move), s.move = null
  210. }, i.on(f, s.move).one(a, s.stop)
  211. }, r.on(u, s.start)
  212. }, teardown: function () {
  213. var t, n;
  214. t = e.data(this, "mobile-events"), t && (n = t.swipe, delete t.swipe, t.length--, t.length === 0 && e.removeData(this, "mobile-events")), n && (n.start && e(this).off(u, n.start), n.move && i.off(f, n.move), n.stop && i.off(a, n.stop))
  215. }
  216. }, e.each({scrollstop: "scrollstart", taphold: "tap", swipeleft: "swipe.left", swiperight: "swipe.right"}, function (t, n) {
  217. e.event.special[t] = {
  218. setup: function () {
  219. e(this).bind(n, e.noop)
  220. }, teardown: function () {
  221. e(this).unbind(n)
  222. }
  223. }
  224. })
  225. }(e, this)
  226. });
  227. (function() {
  228. var supportTouch = $.support.touch,
  229. scrollEvent = "touchmove scroll",
  230. touchStartEvent = supportTouch ? "touchstart" : "mousedown",
  231. touchStopEvent = supportTouch ? "touchend" : "mouseup",
  232. touchMoveEvent = supportTouch ? "touchmove" : "mousemove";
  233. $.event.special.swipeupdown = {
  234. setup: function() {
  235. var thisObject = this;
  236. var $this = $(thisObject);
  237. $this.bind(touchStartEvent, function(event) {
  238. var data = event.originalEvent.touches ?
  239. event.originalEvent.touches[ 0 ] :
  240. event,
  241. start = {
  242. time: (new Date).getTime(),
  243. coords: [ data.pageX, data.pageY ],
  244. origin: $(event.target)
  245. },
  246. stop;
  247. function moveHandler(event) {
  248. if (!start) {
  249. return;
  250. }
  251. var data = event.originalEvent.touches ?
  252. event.originalEvent.touches[ 0 ] :
  253. event;
  254. stop = {
  255. time: (new Date).getTime(),
  256. coords: [ data.pageX, data.pageY ]
  257. };
  258. // prevent scrolling
  259. if (Math.abs(start.coords[1] - stop.coords[1]) > 10) {
  260. event.preventDefault();
  261. }
  262. }
  263. $this
  264. .bind(touchMoveEvent, moveHandler)
  265. .one(touchStopEvent, function(event) {
  266. $this.unbind(touchMoveEvent, moveHandler);
  267. if (start && stop) {
  268. if (stop.time - start.time < 1000 &&
  269. Math.abs(start.coords[1] - stop.coords[1]) > 30 &&
  270. Math.abs(start.coords[0] - stop.coords[0]) < 75) {
  271. start.origin
  272. .trigger("swipeupdown")
  273. .trigger(start.coords[1] > stop.coords[1] ? "swipeup" : "swipedown");
  274. }
  275. }
  276. start = stop = undefined;
  277. });
  278. });
  279. }
  280. };
  281. $.each({
  282. swipedown: "swipeupdown",
  283. swipeup: "swipeupdown"
  284. }, function(event, sourceEvent){
  285. $.event.special[event] = {
  286. setup: function(){
  287. $(this).bind(sourceEvent, $.noop);
  288. }
  289. };
  290. });
  291. })();