util.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /// <reference path="jquery.js" />
  2. var caiba = {};
  3. String.prototype.format = function (args) {
  4. if (arguments.length > 0) {
  5. var result = this;
  6. if (arguments.length == 1 && typeof (args) == "object" && args.length > 0) {
  7. for (var key in args) {
  8. var reg = new RegExp("([{]" + key + "[}])", "g");
  9. result = result.replace(reg, args[key]);
  10. }
  11. } else {
  12. for (var i = 0; i < arguments.length; i++) {
  13. if (arguments[i] == undefined) {
  14. return this;
  15. } else {
  16. var reg = new RegExp("([{]" + i + "[}])", "g");
  17. result = result.replace(reg, arguments[i]);
  18. }
  19. }
  20. }
  21. return result;
  22. } else {
  23. return this;
  24. }
  25. }
  26. //page:页面
  27. //pdata:请求数据格式:json对象
  28. //successCallBack:成功回调
  29. //errorCallBack:失败回调
  30. //checkLogin:是否需要登录
  31. //confirmStr:二次确认
  32. //waitSconds:请求超时
  33. //proccessObj:等待显示区域(暂不支持table对象,可以用div包含table区域)
  34. //info:等待提示
  35. function ajaxProcess(page, pdata, successCallBack, errorCallBack, checkLogin, confirmStr, waitSconds, proccessObj, info) {
  36. var a = document.createElement("div");
  37. if (proccessObj) {
  38. a.style.position = "absolute";
  39. a.style.zIndex = "10001";
  40. a.style.height = (proccessObj.offsetHeight - 2) + "px";
  41. a.style.width = (proccessObj.offsetWidth - 2) + "px";
  42. var bcolor = "#e7edf3";
  43. if (document.all) {
  44. bcolor = proccessObj.currentStyle.backgroundColor;
  45. } else {
  46. bcolor = window.getComputedStyle(proccessObj, null).getPropertyValue('background-color')
  47. }
  48. if (bcolor == "transparent") {
  49. bcolor = "#e7edf3";
  50. }
  51. a.style.backgroundColor = bcolor;
  52. a.style.float = "left";
  53. if (proccessObj.childNodes.length > 0) {
  54. proccessObj.insertBefore(a, proccessObj.childNodes[0]);
  55. } else {
  56. proccessObj.appendChild(a);
  57. }
  58. }
  59. showDialog(confirmStr, 2, 400, 100, function () {
  60. $.ajax({ url: page, type: "post", timeout: waitSconds, data: pdata, dataType: "json", success: callSuccess, error: callError, complete: function (XMLHttpRequest, textStatus) { } });
  61. function callSuccess(result) {
  62. if (successCallBack) {
  63. if (result.err) {
  64. errorCallBack(result.err);
  65. } else {
  66. if (checkLogin) {
  67. if (result.isLogin) {
  68. successCallBack(result);
  69. if (result.user) {
  70. bindUserInfo(result.user);
  71. }
  72. } else {
  73. createLogin(page, pdata, successCallBack, errorCallBack, confirmStr, waitSconds, proccessObj, info);
  74. }
  75. } else {
  76. successCallBack(result);
  77. }
  78. }
  79. }
  80. if (proccessObj) {
  81. proccessObj.removeChild(a);
  82. }
  83. }
  84. function callError(XMLHttpRequest, errorStatus) {
  85. if (errorCallBack) {
  86. errorCallBack(errorStatus);
  87. }
  88. if (proccessObj) {
  89. proccessObj.removeChild(a);
  90. }
  91. }
  92. }, "", false);
  93. }
  94. function createLogin(page, pdata, successCallBack, errorCallBack, confirmStr, waitSconds, proccessObj, info) {
  95. $.blockUI({ message: $('#modelLogin'), css: { cursor: 'default' } });
  96. }
  97. function refreshBlance(b) {
  98. if (b) {
  99. $("#lbBalance").text(b.toFixed(2));
  100. }
  101. }
  102. function showDialog(content, type, width, height, callback, title, isAutoHeight) {
  103. if (typeof (content) == "undefined" || content == "") {
  104. if (callback) callback();
  105. return;
  106. }
  107. if (typeof (title) == "undefined") {
  108. title = "提示";
  109. }
  110. var dialog = "<div style='text-align:left;border:#9EB6D1 4px solid;'><div style='height:28px;line-height:28px; background-color:#6D84B4; color:#333;padding-left:10px;padding-right:10px;background-image:url(/Images/tips_ico.png);border-bottom:#8BC0E8 1px solid;'><span style='font-size:14px;font-weight:bold;float:left;'>{0}</span><a style='float:right;cursor:pointer;' onclick='closeDialog();return false;'>关闭</a></div><div id='dialogcontent' style='text-align:center;padding:10px;'><div style='text-align:left;display:inline-block;'>{1}</div><div style='margin-top:10px;'>{2}</div></div></div>";
  111. var btns = "&nbsp;";
  112. if (type == 1) {
  113. btns = "<span class='btn105' onclick='closeDialog();return false;'>确 定</span>";
  114. }
  115. if (type == 2) {
  116. btns = "<span class='btn105' id='btnOkDialog'>确 定</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class='btn105' onclick='closeDialog();return false;'>取 消</span>";
  117. }
  118. dialog = dialog.format(title, content, btns);
  119. $.blockUI({ message: dialog, css: { cursor: 'default', width: '{0}px'.format(width) } });
  120. $("#btnOkDialog").click(function () {
  121. if (callback) callback();
  122. closeDialog();
  123. });
  124. resetDialogHeight(isAutoHeight);
  125. }
  126. (function (e, t) {
  127. function A(e) {
  128. return i === "" ? e : (e = e.charAt(0).toUpperCase() + e.substr(1), i + e)
  129. }
  130. var n = Math,
  131. r = t.createElement("div").style,
  132. i = function () {
  133. var e = "t,webkitT,MozT,msT,OT".split(","),
  134. t,
  135. n = 0,
  136. i = e.length;
  137. for (; n < i; n++) {
  138. t = e[n] + "ransform";
  139. if (t in r) return e[n].substr(0, e[n].length - 1)
  140. }
  141. return !1
  142. }(),
  143. s = i ? "-" + i.toLowerCase() + "-" : "",
  144. o = A("transform"),
  145. u = A("transitionProperty"),
  146. a = A("transitionDuration"),
  147. f = A("transformOrigin"),
  148. l = A("transitionTimingFunction"),
  149. c = A("transitionDelay"),
  150. h = /android/gi.test(navigator.appVersion),
  151. p = /iphone|ipad/gi.test(navigator.appVersion),
  152. d = /hp-tablet/gi.test(navigator.appVersion),
  153. v = A("perspective") in r,
  154. m = "ontouchstart" in e && !d,
  155. g = i !== !1,
  156. y = A("transition") in r,
  157. b = "onorientationchange" in e ? "orientationchange" : "resize",
  158. w = m ? "touchstart" : "mousedown",
  159. E = m ? "touchmove" : "mousemove",
  160. S = m ? "touchend" : "mouseup",
  161. x = m ? "touchcancel" : "mouseup",
  162. T = function () {
  163. if (i === !1) return !1;
  164. var e = {
  165. "": "transitionend",
  166. webkit: "webkitTransitionEnd",
  167. Moz: "transitionend",
  168. O: "otransitionend",
  169. ms: "MSTransitionEnd"
  170. };
  171. return e[i]
  172. }(),
  173. N = function () {
  174. return e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame ||
  175. function (e) {
  176. return setTimeout(e, 1)
  177. }
  178. }(),
  179. C = function () {
  180. return e.cancelRequestAnimationFrame || e.webkitCancelAnimationFrame || e.webkitCancelRequestAnimationFrame || e.mozCancelRequestAnimationFrame || e.oCancelRequestAnimationFrame || e.msCancelRequestAnimationFrame || clearTimeout
  181. }(),
  182. k = v ? " translateZ(0)" : "",
  183. L = function (n, r) {
  184. var i = this,
  185. c;
  186. i.wrapper = typeof n == "object" ? n : t.getElementById(n),
  187. i.wrapper.style.overflow = "hidden",
  188. i.scroller = i.wrapper.children[0],
  189. i.options = {
  190. hScroll: !0,
  191. vScroll: !0,
  192. x: 0,
  193. y: 0,
  194. bounce: !0,
  195. bounceLock: !1,
  196. momentum: !0,
  197. lockDirection: !0,
  198. useTransform: !0,
  199. useTransition: !1,
  200. topOffset: 0,
  201. checkDOMChanges: !1,
  202. handleClick: !0,
  203. hScrollbar: !0,
  204. vScrollbar: !0,
  205. fixedScrollbar: h,
  206. hideScrollbar: p,
  207. fadeScrollbar: p && v,
  208. scrollbarClass: "",
  209. zoom: !1,
  210. zoomMin: 1,
  211. zoomMax: 4,
  212. doubleTapZoom: 2,
  213. wheelAction: "scroll",
  214. snap: !1,
  215. snapThreshold: 1,
  216. onRefresh: null,
  217. onBeforeScrollStart: function (e) {
  218. e.preventDefault()
  219. },
  220. onScrollStart: null,
  221. onBeforeScrollMove: null,
  222. onScrollMove: null,
  223. onBeforeScrollEnd: null,
  224. onScrollEnd: null,
  225. onTouchEnd: null,
  226. onDestroy: null,
  227. onZoomStart: null,
  228. onZoom: null,
  229. onZoomEnd: null
  230. };
  231. for (c in r) i.options[c] = r[c];
  232. i.x = i.options.x,
  233. i.y = i.options.y,
  234. i.options.useTransform = g && i.options.useTransform,
  235. i.options.hScrollbar = i.options.hScroll && i.options.hScrollbar,
  236. i.options.vScrollbar = i.options.vScroll && i.options.vScrollbar,
  237. i.options.zoom = i.options.useTransform && i.options.zoom,
  238. i.options.useTransition = y && i.options.useTransition,
  239. i.options.zoom && h && (k = ""),
  240. i.scroller.style[u] = i.options.useTransform ? s + "transform" : "top left",
  241. i.scroller.style[a] = "0",
  242. i.scroller.style[f] = "0 0",
  243. i.options.useTransition && (i.scroller.style[l] = "cubic-bezier(0.33,0.66,0.66,1)"),
  244. i.options.useTransform ? i.scroller.style[o] = "translate(" + i.x + "px," + i.y + "px)" + k : i.scroller.style.cssText += ";position:absolute;top:" + i.y + "px;left:" + i.x + "px",
  245. i.options.useTransition && (i.options.fixedScrollbar = !0),
  246. i.refresh(),
  247. i._bind(b, e),
  248. i._bind(w),
  249. m || i.options.wheelAction != "none" && (i._bind("DOMMouseScroll"), i._bind("mousewheel")),
  250. i.options.checkDOMChanges && (i.checkDOMTime = setInterval(function () {
  251. i._checkDOMChanges()
  252. },
  253. 500))
  254. };
  255. L.prototype = {
  256. enabled: !0,
  257. x: 0,
  258. y: 0,
  259. steps: [],
  260. scale: 1,
  261. currPageX: 0,
  262. currPageY: 0,
  263. pagesX: [],
  264. pagesY: [],
  265. aniTime: null,
  266. wheelZoomCount: 0,
  267. handleEvent: function (e) {
  268. var t = this;
  269. switch (e.type) {
  270. case w:
  271. if (!m && e.button !== 0) return;
  272. t._start(e);
  273. break;
  274. case E:
  275. t._move(e);
  276. break;
  277. case S:
  278. case x:
  279. t._end(e);
  280. break;
  281. case b:
  282. t._resize();
  283. break;
  284. case "DOMMouseScroll":
  285. case "mousewheel":
  286. t._wheel(e);
  287. break;
  288. case T:
  289. t._transitionEnd(e)
  290. }
  291. },
  292. _checkDOMChanges: function () {
  293. if (this.moved || this.zoomed || this.animating || this.scrollerW == this.scroller.offsetWidth * this.scale && this.scrollerH == this.scroller.offsetHeight * this.scale) return;
  294. this.refresh()
  295. },
  296. _scrollbar: function (e) {
  297. var r = this,
  298. i;
  299. if (!r[e + "Scrollbar"]) {
  300. r[e + "ScrollbarWrapper"] && (g && (r[e + "ScrollbarIndicator"].style[o] = ""), r[e + "ScrollbarWrapper"].parentNode.removeChild(r[e + "ScrollbarWrapper"]), r[e + "ScrollbarWrapper"] = null, r[e + "ScrollbarIndicator"] = null);
  301. return
  302. }
  303. r[e + "ScrollbarWrapper"] || (i = t.createElement("div"), r.options.scrollbarClass ? i.className = r.options.scrollbarClass + e.toUpperCase() : i.style.cssText = "position:absolute;z-index:100;" + (e == "h" ? "height:7px;bottom:1px;left:2px;right:" + (r.vScrollbar ? "7" : "2") + "px" : "width:7px;bottom:" + (r.hScrollbar ? "7" : "2") + "px;top:2px;right:1px"), i.style.cssText += ";pointer-events:none;" + s + "transition-property:opacity;" + s + "transition-duration:" + (r.options.fadeScrollbar ? "350ms" : "0") + ";overflow:hidden;opacity:" + (r.options.hideScrollbar ? "0" : "1"), r.wrapper.appendChild(i), r[e + "ScrollbarWrapper"] = i, i = t.createElement("div"), r.options.scrollbarClass || (i.style.cssText = "position:absolute;z-index:100;background:rgba(0,0,0,0.5);border:1px solid rgba(255,255,255,0.9);" + s + "background-clip:padding-box;" + s + "box-sizing:border-box;" + (e == "h" ? "height:100%" : "width:100%") + ";" + s + "border-radius:3px;border-radius:3px"), i.style.cssText += ";pointer-events:none;" + s + "transition-property:" + s + "transform;" + s + "transition-timing-function:cubic-bezier(0.33,0.66,0.66,1);" + s + "transition-duration:0;" + s + "transform: translate(0,0)" + k, r.options.useTransition && (i.style.cssText += ";" + s + "transition-timing-function:cubic-bezier(0.33,0.66,0.66,1)"), r[e + "ScrollbarWrapper"].appendChild(i), r[e + "ScrollbarIndicator"] = i),
  304. e == "h" ? (r.hScrollbarSize = r.hScrollbarWrapper.clientWidth, r.hScrollbarIndicatorSize = n.max(n.round(r.hScrollbarSize * r.hScrollbarSize / r.scrollerW), 8), r.hScrollbarIndicator.style.width = r.hScrollbarIndicatorSize + "px", r.hScrollbarMaxScroll = r.hScrollbarSize - r.hScrollbarIndicatorSize, r.hScrollbarProp = r.hScrollbarMaxScroll / r.maxScrollX) : (r.vScrollbarSize = r.vScrollbarWrapper.clientHeight, r.vScrollbarIndicatorSize = n.max(n.round(r.vScrollbarSize * r.vScrollbarSize / r.scrollerH), 8), r.vScrollbarIndicator.style.height = r.vScrollbarIndicatorSize + "px", r.vScrollbarMaxScroll = r.vScrollbarSize - r.vScrollbarIndicatorSize, r.vScrollbarProp = r.vScrollbarMaxScroll / r.maxScrollY),
  305. r._scrollbarPos(e, !0)
  306. },
  307. _resize: function () {
  308. var e = this;
  309. setTimeout(function () {
  310. e.refresh()
  311. },
  312. h ? 200 : 0)
  313. },
  314. _pos: function (e, t) {
  315. if (this.zoomed) return;
  316. e = this.hScroll ? e : 0,
  317. t = this.vScroll ? t : 0,
  318. this.options.useTransform ? this.scroller.style[o] = "translate(" + e + "px," + t + "px) scale(" + this.scale + ")" + k : (e = n.round(e), t = n.round(t), this.scroller.style.left = e + "px", this.scroller.style.top = t + "px"),
  319. this.x = e,
  320. this.y = t,
  321. this._scrollbarPos("h"),
  322. this._scrollbarPos("v")
  323. },
  324. _scrollbarPos: function (e, t) {
  325. var r = this,
  326. i = e == "h" ? r.x : r.y,
  327. s;
  328. if (!r[e + "Scrollbar"]) return;
  329. i = r[e + "ScrollbarProp"] * i,
  330. i < 0 ? (r.options.fixedScrollbar || (s = r[e + "ScrollbarIndicatorSize"] + n.round(i * 3), s < 8 && (s = 8), r[e + "ScrollbarIndicator"].style[e == "h" ? "width" : "height"] = s + "px"), i = 0) : i > r[e + "ScrollbarMaxScroll"] && (r.options.fixedScrollbar ? i = r[e + "ScrollbarMaxScroll"] : (s = r[e + "ScrollbarIndicatorSize"] - n.round((i - r[e + "ScrollbarMaxScroll"]) * 3), s < 8 && (s = 8), r[e + "ScrollbarIndicator"].style[e == "h" ? "width" : "height"] = s + "px", i = r[e + "ScrollbarMaxScroll"] + (r[e + "ScrollbarIndicatorSize"] - s))),
  331. r[e + "ScrollbarWrapper"].style[c] = "0",
  332. r[e + "ScrollbarWrapper"].style.opacity = t && r.options.hideScrollbar ? "0" : "1",
  333. r[e + "ScrollbarIndicator"].style[o] = "translate(" + (e == "h" ? i + "px,0)" : "0," + i + "px)") + k
  334. },
  335. _start: function (t) {
  336. var r = this,
  337. i = m ? t.touches[0] : t,
  338. s,
  339. u,
  340. a,
  341. f,
  342. l;
  343. if (!r.enabled) return;
  344. r.options.onBeforeScrollStart && r.options.onBeforeScrollStart.call(r, t),
  345. (r.options.useTransition || r.options.zoom) && r._transitionTime(0),
  346. r.moved = !1,
  347. r.animating = !1,
  348. r.zoomed = !1,
  349. r.distX = 0,
  350. r.distY = 0,
  351. r.absDistX = 0,
  352. r.absDistY = 0,
  353. r.dirX = 0,
  354. r.dirY = 0,
  355. r.options.zoom && m && t.touches.length > 1 && (f = n.abs(t.touches[0].pageX - t.touches[1].pageX), l = n.abs(t.touches[0].pageY - t.touches[1].pageY), r.touchesDistStart = n.sqrt(f * f + l * l), r.originX = n.abs(t.touches[0].pageX + t.touches[1].pageX - r.wrapperOffsetLeft * 2) / 2 - r.x, r.originY = n.abs(t.touches[0].pageY + t.touches[1].pageY - r.wrapperOffsetTop * 2) / 2 - r.y, r.options.onZoomStart && r.options.onZoomStart.call(r, t));
  356. if (r.options.momentum) {
  357. r.options.useTransform ? (s = getComputedStyle(r.scroller, null)[o].replace(/[^0-9\-.,]/g, "").split(","), u = +(s[12] || s[4]), a = +(s[13] || s[5])) : (u = +getComputedStyle(r.scroller, null).left.replace(/[^0-9-]/g, ""), a = +getComputedStyle(r.scroller, null).top.replace(/[^0-9-]/g, ""));
  358. if (u != r.x || a != r.y) r.options.useTransition ? r._unbind(T) : C(r.aniTime),
  359. r.steps = [],
  360. r._pos(u, a),
  361. r.options.onScrollEnd && r.options.onScrollEnd.call(r)
  362. }
  363. r.absStartX = r.x,
  364. r.absStartY = r.y,
  365. r.startX = r.x,
  366. r.startY = r.y,
  367. r.pointX = i.pageX,
  368. r.pointY = i.pageY,
  369. r.startTime = t.timeStamp || Date.now(),
  370. r.options.onScrollStart && r.options.onScrollStart.call(r, t),
  371. r._bind(E, e),
  372. r._bind(S, e),
  373. r._bind(x, e)
  374. },
  375. _move: function (e) {
  376. var t = this,
  377. r = m ? e.touches[0] : e,
  378. i = r.pageX - t.pointX,
  379. s = r.pageY - t.pointY,
  380. u = t.x + i,
  381. a = t.y + s,
  382. f,
  383. l,
  384. c,
  385. h = e.timeStamp || Date.now();
  386. t.options.onBeforeScrollMove && t.options.onBeforeScrollMove.call(t, e);
  387. if (t.options.zoom && m && e.touches.length > 1) {
  388. f = n.abs(e.touches[0].pageX - e.touches[1].pageX),
  389. l = n.abs(e.touches[0].pageY - e.touches[1].pageY),
  390. t.touchesDist = n.sqrt(f * f + l * l),
  391. t.zoomed = !0,
  392. c = 1 / t.touchesDistStart * t.touchesDist * this.scale,
  393. c < t.options.zoomMin ? c = .5 * t.options.zoomMin * Math.pow(2, c / t.options.zoomMin) : c > t.options.zoomMax && (c = 2 * t.options.zoomMax * Math.pow(.5, t.options.zoomMax / c)),
  394. t.lastScale = c / this.scale,
  395. u = this.originX - this.originX * t.lastScale + this.x,
  396. a = this.originY - this.originY * t.lastScale + this.y,
  397. this.scroller.style[o] = "translate(" + u + "px," + a + "px) scale(" + c + ")" + k,
  398. t.options.onZoom && t.options.onZoom.call(t, e);
  399. return
  400. }
  401. t.pointX = r.pageX,
  402. t.pointY = r.pageY;
  403. if (u > 0 || u < t.maxScrollX) u = t.options.bounce ? t.x + i / 2 : u >= 0 || t.maxScrollX >= 0 ? 0 : t.maxScrollX;
  404. if (a > t.minScrollY || a < t.maxScrollY) a = t.options.bounce ? t.y + s / 2 : a >= t.minScrollY || t.maxScrollY >= 0 ? t.minScrollY : t.maxScrollY;
  405. t.distX += i,
  406. t.distY += s,
  407. t.absDistX = n.abs(t.distX),
  408. t.absDistY = n.abs(t.distY);
  409. if (t.absDistX < 6 && t.absDistY < 6) return;
  410. t.options.lockDirection && (t.absDistX > t.absDistY + 5 ? (a = t.y, s = 0) : t.absDistY > t.absDistX + 5 && (u = t.x, i = 0)),
  411. t.moved = !0,
  412. t._pos(u, a),
  413. t.dirX = i > 0 ? -1 : i < 0 ? 1 : 0,
  414. t.dirY = s > 0 ? -1 : s < 0 ? 1 : 0,
  415. h - t.startTime > 300 && (t.startTime = h, t.startX = t.x, t.startY = t.y),
  416. t.options.onScrollMove && t.options.onScrollMove.call(t, { x: u, y: a })
  417. },
  418. _end: function (r) {
  419. if (m && r.touches.length !== 0) return;
  420. var i = this,
  421. s = m ? r.changedTouches[0] : r,
  422. u,
  423. f,
  424. l = {
  425. dist: 0,
  426. time: 0
  427. },
  428. c = {
  429. dist: 0,
  430. time: 0
  431. },
  432. h = (r.timeStamp || Date.now()) - i.startTime,
  433. p = i.x,
  434. d = i.y,
  435. v,
  436. g,
  437. y,
  438. b,
  439. w;
  440. i._unbind(E, e),
  441. i._unbind(S, e),
  442. i._unbind(x, e),
  443. i.options.onBeforeScrollEnd && i.options.onBeforeScrollEnd.call(i, r);
  444. if (i.zoomed) {
  445. w = i.scale * i.lastScale,
  446. w = Math.max(i.options.zoomMin, w),
  447. w = Math.min(i.options.zoomMax, w),
  448. i.lastScale = w / i.scale,
  449. i.scale = w,
  450. i.x = i.originX - i.originX * i.lastScale + i.x,
  451. i.y = i.originY - i.originY * i.lastScale + i.y,
  452. i.scroller.style[a] = "200ms",
  453. i.scroller.style[o] = "translate(" + i.x + "px," + i.y + "px) scale(" + i.scale + ")" + k,
  454. i.zoomed = !1,
  455. i.refresh(),
  456. i.options.onZoomEnd && i.options.onZoomEnd.call(i, r);
  457. return
  458. }
  459. if (!i.moved) {
  460. m && (i.doubleTapTimer && i.options.zoom ? (clearTimeout(i.doubleTapTimer), i.doubleTapTimer = null, i.options.onZoomStart && i.options.onZoomStart.call(i, r), i.zoom(i.pointX, i.pointY, i.scale == 1 ? i.options.doubleTapZoom : 1), i.options.onZoomEnd && setTimeout(function () {
  461. i.options.onZoomEnd.call(i, r)
  462. },
  463. 200)) : this.options.handleClick && (i.doubleTapTimer = setTimeout(function () {
  464. i.doubleTapTimer = null,
  465. u = s.target;
  466. while (u.nodeType != 1) u = u.parentNode;
  467. u.tagName != "SELECT" && u.tagName != "INPUT" && u.tagName != "TEXTAREA" && (f = t.createEvent("MouseEvents"), f.initMouseEvent("click", !0, !0, r.view, 1, s.screenX, s.screenY, s.clientX, s.clientY, r.ctrlKey, r.altKey, r.shiftKey, r.metaKey, 0, null), f._fake = !0, u.dispatchEvent(f))
  468. },
  469. i.options.zoom ? 250 : 0))),
  470. i._resetPos(400),
  471. i.options.onTouchEnd && i.options.onTouchEnd.call(i, r);
  472. return
  473. }
  474. if (h < 300 && i.options.momentum) {
  475. l = p ? i._momentum(p - i.startX, h, -i.x, i.scrollerW - i.wrapperW + i.x, i.options.bounce ? i.wrapperW : 0) : l,
  476. c = d ? i._momentum(d - i.startY, h, -i.y, i.maxScrollY < 0 ? i.scrollerH - i.wrapperH + i.y - i.minScrollY : 0, i.options.bounce ? i.wrapperH : 0) : c,
  477. p = i.x + l.dist,
  478. d = i.y + c.dist;
  479. if (i.x > 0 && p > 0 || i.x < i.maxScrollX && p < i.maxScrollX) l = {
  480. dist: 0,
  481. time: 0
  482. };
  483. if (i.y > i.minScrollY && d > i.minScrollY || i.y < i.maxScrollY && d < i.maxScrollY) c = {
  484. dist: 0,
  485. time: 0
  486. }
  487. }
  488. if (l.dist || c.dist) {
  489. y = n.max(n.max(l.time, c.time), 10),
  490. i.options.snap && (v = p - i.absStartX, g = d - i.absStartY, n.abs(v) < i.options.snapThreshold && n.abs(g) < i.options.snapThreshold ? i.scrollTo(i.absStartX, i.absStartY, 200) : (b = i._snap(p, d), p = b.x, d = b.y, y = n.max(b.time, y))),
  491. i.scrollTo(n.round(p), n.round(d), y),
  492. i.options.onTouchEnd && i.options.onTouchEnd.call(i, r);
  493. return
  494. }
  495. if (i.options.snap) {
  496. v = p - i.absStartX,
  497. g = d - i.absStartY,
  498. n.abs(v) < i.options.snapThreshold && n.abs(g) < i.options.snapThreshold ? i.scrollTo(i.absStartX, i.absStartY, 200) : (b = i._snap(i.x, i.y), (b.x != i.x || b.y != i.y) && i.scrollTo(b.x, b.y, b.time)),
  499. i.options.onTouchEnd && i.options.onTouchEnd.call(i, r);
  500. return
  501. }
  502. i._resetPos(200),
  503. i.options.onTouchEnd && i.options.onTouchEnd.call(i, r)
  504. },
  505. _resetPos: function (e) {
  506. var t = this,
  507. n = t.x >= 0 ? 0 : t.x < t.maxScrollX ? t.maxScrollX : t.x,
  508. r = t.y >= t.minScrollY || t.maxScrollY > 0 ? t.minScrollY : t.y < t.maxScrollY ? t.maxScrollY : t.y;
  509. if (n == t.x && r == t.y) {
  510. t.moved && (t.moved = !1, t.options.onScrollEnd && t.options.onScrollEnd.call(t)),
  511. t.hScrollbar && t.options.hideScrollbar && (i == "webkit" && (t.hScrollbarWrapper.style[c] = "300ms"), t.hScrollbarWrapper.style.opacity = "0"),
  512. t.vScrollbar && t.options.hideScrollbar && (i == "webkit" && (t.vScrollbarWrapper.style[c] = "300ms"), t.vScrollbarWrapper.style.opacity = "0");
  513. return
  514. }
  515. t.scrollTo(n, r, e || 0)
  516. },
  517. _wheel: function (e) {
  518. var t = this,
  519. n, r, i, s, o;
  520. if ("wheelDeltaX" in e) n = e.wheelDeltaX / 12,
  521. r = e.wheelDeltaY / 12;
  522. else if ("wheelDelta" in e) n = r = e.wheelDelta / 12;
  523. else {
  524. if (!("detail" in e)) return;
  525. n = r = -e.detail * 3
  526. }
  527. if (t.options.wheelAction == "zoom") {
  528. o = t.scale * Math.pow(2, 1 / 3 * (r ? r / Math.abs(r) : 0)),
  529. o < t.options.zoomMin && (o = t.options.zoomMin),
  530. o > t.options.zoomMax && (o = t.options.zoomMax),
  531. o != t.scale && (!t.wheelZoomCount && t.options.onZoomStart && t.options.onZoomStart.call(t, e), t.wheelZoomCount++, t.zoom(e.pageX, e.pageY, o, 400), setTimeout(function () {
  532. t.wheelZoomCount--,
  533. !t.wheelZoomCount && t.options.onZoomEnd && t.options.onZoomEnd.call(t, e)
  534. },
  535. 400));
  536. return
  537. }
  538. i = t.x + n,
  539. s = t.y + r,
  540. i > 0 ? i = 0 : i < t.maxScrollX && (i = t.maxScrollX),
  541. s > t.minScrollY ? s = t.minScrollY : s < t.maxScrollY && (s = t.maxScrollY),
  542. t.maxScrollY < 0 && t.scrollTo(i, s, 0)
  543. },
  544. _transitionEnd: function (e) {
  545. var t = this;
  546. if (e.target != t.scroller) return;
  547. t._unbind(T),
  548. t._startAni()
  549. },
  550. _startAni: function () {
  551. var e = this,
  552. t = e.x,
  553. r = e.y,
  554. i = Date.now(),
  555. s,
  556. o,
  557. u;
  558. if (e.animating) return;
  559. if (!e.steps.length) {
  560. e._resetPos(400);
  561. return
  562. }
  563. s = e.steps.shift(),
  564. s.x == t && s.y == r && (s.time = 0),
  565. e.animating = !0,
  566. e.moved = !0;
  567. if (e.options.useTransition) {
  568. e._transitionTime(s.time),
  569. e._pos(s.x, s.y),
  570. e.animating = !1,
  571. s.time ? e._bind(T) : e._resetPos(0);
  572. return
  573. }
  574. u = function () {
  575. var a = Date.now(),
  576. f,
  577. l;
  578. if (a >= i + s.time) {
  579. e._pos(s.x, s.y),
  580. e.animating = !1,
  581. e.options.onAnimationEnd && e.options.onAnimationEnd.call(e),
  582. e._startAni();
  583. return
  584. }
  585. a = (a - i) / s.time - 1,
  586. o = n.sqrt(1 - a * a),
  587. f = (s.x - t) * o + t,
  588. l = (s.y - r) * o + r,
  589. e._pos(f, l),
  590. e.options.onAnimating && e.options.onAnimating.call(e, { x: f, y: l }),
  591. e.animating && (e.aniTime = N(u))
  592. },
  593. u()
  594. },
  595. _transitionTime: function (e) {
  596. e += "ms",
  597. this.scroller.style[a] = e,
  598. this.hScrollbar && (this.hScrollbarIndicator.style[a] = e),
  599. this.vScrollbar && (this.vScrollbarIndicator.style[a] = e)
  600. },
  601. _momentum: function (e, t, r, i, s) {
  602. var o = 6e-4,
  603. u = n.abs(e) / t,
  604. a = u * u / (2 * o),
  605. f = 0,
  606. l = 0;
  607. return e > 0 && a > r ? (l = s / (6 / (a / u * o)), r += l, u = u * r / a, a = r) : e < 0 && a > i && (l = s / (6 / (a / u * o)), i += l, u = u * i / a, a = i),
  608. a *= e < 0 ? -1 : 1,
  609. f = u / o,
  610. {
  611. dist: a,
  612. time: n.round(f)
  613. }
  614. },
  615. _offset: function (e) {
  616. var t = -e.offsetLeft,
  617. n = -e.offsetTop;
  618. while (e = e.offsetParent) t -= e.offsetLeft,
  619. n -= e.offsetTop;
  620. return e != this.wrapper && (t *= this.scale, n *= this.scale),
  621. {
  622. left: t,
  623. top: n
  624. }
  625. },
  626. _snap: function (e, t) {
  627. var r = this,
  628. i, s, o, u, a, f;
  629. o = r.pagesX.length - 1;
  630. for (i = 0, s = r.pagesX.length; i < s; i++) if (e >= r.pagesX[i]) {
  631. o = i;
  632. break
  633. }
  634. o == r.currPageX && o > 0 && r.dirX < 0 && o--,
  635. e = r.pagesX[o],
  636. a = n.abs(e - r.pagesX[r.currPageX]),
  637. a = a ? n.abs(r.x - e) / a * 500 : 0,
  638. r.currPageX = o,
  639. o = r.pagesY.length - 1;
  640. for (i = 0; i < o; i++) if (t >= r.pagesY[i]) {
  641. o = i;
  642. break
  643. }
  644. return o == r.currPageY && o > 0 && r.dirY < 0 && o--,
  645. t = r.pagesY[o],
  646. f = n.abs(t - r.pagesY[r.currPageY]),
  647. f = f ? n.abs(r.y - t) / f * 500 : 0,
  648. r.currPageY = o,
  649. u = n.round(n.max(a, f)) || 200,
  650. {
  651. x: e,
  652. y: t,
  653. time: u
  654. }
  655. },
  656. _bind: function (e, t, n) {
  657. (t || this.scroller).addEventListener(e, this, !!n)
  658. },
  659. _unbind: function (e, t, n) {
  660. (t || this.scroller).removeEventListener(e, this, !!n)
  661. },
  662. destroy: function () {
  663. var t = this;
  664. t.scroller.style[o] = "",
  665. t.hScrollbar = !1,
  666. t.vScrollbar = !1,
  667. t._scrollbar("h"),
  668. t._scrollbar("v"),
  669. t._unbind(b, e),
  670. t._unbind(w),
  671. t._unbind(E, e),
  672. t._unbind(S, e),
  673. t._unbind(x, e),
  674. t.options.hasTouch || (t._unbind("DOMMouseScroll"), t._unbind("mousewheel")),
  675. t.options.useTransition && t._unbind(T),
  676. t.options.checkDOMChanges && clearInterval(t.checkDOMTime),
  677. t.options.onDestroy && t.options.onDestroy.call(t)
  678. },
  679. refresh: function () {
  680. var e = this,
  681. t, r, i, s, o = 0,
  682. u = 0;
  683. e.scale < e.options.zoomMin && (e.scale = e.options.zoomMin),
  684. e.wrapperW = e.wrapper.clientWidth || 1,
  685. e.wrapperH = e.wrapper.clientHeight || 1,
  686. e.minScrollY = -e.options.topOffset || 0,
  687. e.scrollerW = n.round(e.scroller.offsetWidth * e.scale),
  688. e.scrollerH = n.round((e.scroller.offsetHeight + e.minScrollY) * e.scale),
  689. e.maxScrollX = e.wrapperW - e.scrollerW,
  690. e.maxScrollY = e.wrapperH - e.scrollerH + e.minScrollY,
  691. e.dirX = 0,
  692. e.dirY = 0,
  693. e.options.onRefresh && e.options.onRefresh.call(e),
  694. e.hScroll = e.options.hScroll && e.maxScrollX < 0,
  695. e.vScroll = e.options.vScroll && (!e.options.bounceLock && !e.hScroll || e.scrollerH > e.wrapperH),
  696. e.hScrollbar = e.hScroll && e.options.hScrollbar,
  697. e.vScrollbar = e.vScroll && e.options.vScrollbar && e.scrollerH > e.wrapperH,
  698. t = e._offset(e.wrapper),
  699. e.wrapperOffsetLeft = -t.left,
  700. e.wrapperOffsetTop = -t.top;
  701. if (typeof e.options.snap == "string") {
  702. e.pagesX = [],
  703. e.pagesY = [],
  704. s = e.scroller.querySelectorAll(e.options.snap);
  705. for (r = 0, i = s.length; r < i; r++) o = e._offset(s[r]),
  706. o.left += e.wrapperOffsetLeft,
  707. o.top += e.wrapperOffsetTop,
  708. e.pagesX[r] = o.left < e.maxScrollX ? e.maxScrollX : o.left * e.scale,
  709. e.pagesY[r] = o.top < e.maxScrollY ? e.maxScrollY : o.top * e.scale
  710. } else if (e.options.snap) {
  711. e.pagesX = [];
  712. while (o >= e.maxScrollX) e.pagesX[u] = o,
  713. o -= e.wrapperW,
  714. u++;
  715. e.maxScrollX % e.wrapperW && (e.pagesX[e.pagesX.length] = e.maxScrollX - e.pagesX[e.pagesX.length - 1] + e.pagesX[e.pagesX.length - 1]),
  716. o = 0,
  717. u = 0,
  718. e.pagesY = [];
  719. while (o >= e.maxScrollY) e.pagesY[u] = o,
  720. o -= e.wrapperH,
  721. u++;
  722. e.maxScrollY % e.wrapperH && (e.pagesY[e.pagesY.length] = e.maxScrollY - e.pagesY[e.pagesY.length - 1] + e.pagesY[e.pagesY.length - 1])
  723. }
  724. e._scrollbar("h"),
  725. e._scrollbar("v"),
  726. e.zoomed || (e.scroller.style[a] = "0", e._resetPos(400))
  727. },
  728. scrollTo: function (e, t, n, r) {
  729. var i = this,
  730. s = e,
  731. o, u;
  732. i.stop(),
  733. s.length || (s = [{
  734. x: e,
  735. y: t,
  736. time: n,
  737. relative: r
  738. }]);
  739. for (o = 0, u = s.length; o < u; o++) s[o].relative && (s[o].x = i.x - s[o].x, s[o].y = i.y - s[o].y),
  740. i.steps.push({
  741. x: s[o].x,
  742. y: s[o].y,
  743. time: s[o].time || 0
  744. });
  745. i._startAni()
  746. },
  747. scrollToElement: function (e, t) {
  748. var r = this,
  749. i;
  750. e = e.nodeType ? e : r.scroller.querySelector(e);
  751. if (!e) return;
  752. i = r._offset(e),
  753. i.left += r.wrapperOffsetLeft,
  754. i.top += r.wrapperOffsetTop,
  755. i.left = i.left > 0 ? 0 : i.left < r.maxScrollX ? r.maxScrollX : i.left,
  756. i.top = i.top > r.minScrollY ? r.minScrollY : i.top < r.maxScrollY ? r.maxScrollY : i.top,
  757. t = t === undefined ? n.max(n.abs(i.left) * 2, n.abs(i.top) * 2) : t,
  758. r.scrollTo(i.left, i.top, t)
  759. },
  760. scrollToPage: function (e, t, n) {
  761. var r = this,
  762. i, s;
  763. n = n === undefined ? 400 : n,
  764. r.options.onScrollStart && r.options.onScrollStart.call(r),
  765. r.options.snap ? (e = e == "next" ? r.currPageX + 1 : e == "prev" ? r.currPageX - 1 : e, t = t == "next" ? r.currPageY + 1 : t == "prev" ? r.currPageY - 1 : t, e = e < 0 ? 0 : e > r.pagesX.length - 1 ? r.pagesX.length - 1 : e, t = t < 0 ? 0 : t > r.pagesY.length - 1 ? r.pagesY.length - 1 : t, r.currPageX = e, r.currPageY = t, i = r.pagesX[e], s = r.pagesY[t]) : (i = -r.wrapperW * e, s = -r.wrapperH * t, i < r.maxScrollX && (i = r.maxScrollX), s < r.maxScrollY && (s = r.maxScrollY)),
  766. r.scrollTo(i, s, n)
  767. },
  768. disable: function () {
  769. this.stop(),
  770. this._resetPos(0),
  771. this.enabled = !1,
  772. this._unbind(E, e),
  773. this._unbind(S, e),
  774. this._unbind(x, e)
  775. },
  776. enable: function () {
  777. this.enabled = !0
  778. },
  779. stop: function () {
  780. this.options.useTransition ? this._unbind(T) : C(this.aniTime),
  781. this.steps = [],
  782. this.moved = !1,
  783. this.animating = !1
  784. },
  785. zoom: function (e, t, n, r) {
  786. var i = this,
  787. s = n / i.scale;
  788. if (!i.options.useTransform) return;
  789. i.zoomed = !0,
  790. r = r === undefined ? 200 : r,
  791. e = e - i.wrapperOffsetLeft - i.x,
  792. t = t - i.wrapperOffsetTop - i.y,
  793. i.x = e - e * s + i.x,
  794. i.y = t - t * s + i.y,
  795. i.scale = n,
  796. i.refresh(),
  797. i.x = i.x > 0 ? 0 : i.x < i.maxScrollX ? i.maxScrollX : i.x,
  798. i.y = i.y > i.minScrollY ? i.minScrollY : i.y < i.maxScrollY ? i.maxScrollY : i.y,
  799. i.scroller.style[a] = r + "ms",
  800. i.scroller.style[o] = "translate(" + i.x + "px," + i.y + "px) scale(" + n + ")" + k,
  801. i.zoomed = !1
  802. },
  803. isReady: function () {
  804. return !this.moved && !this.zoomed && !this.animating
  805. }
  806. },
  807. r = null,
  808. typeof exports != "undefined" ? exports.iScroll = L : e.iScroll = L
  809. })(window, document);