af.scroller.js 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  1. /**
  2. * af.scroller
  3. * created by appMobi with modifications by Carlos Ouro @ Badoo and Intel
  4. * Supports iOS native touch scrolling
  5. * Optimizations and bug improvements by Intel
  6. * @copyright Intel
  7. */ (function ($) {
  8. var HIDE_REFRESH_TIME = 325; // hide animation of pull2ref duration in ms
  9. var cache = [];
  10. var objId = function (obj) {
  11. if (!obj.afScrollerId) obj.afScrollerId = $.uuid();
  12. return obj.afScrollerId;
  13. };
  14. $.fn["scroller"] = function (opts) {
  15. var tmp, id;
  16. for (var i = 0; i < this.length; i++) {
  17. //cache system
  18. id = objId(this[i]);
  19. if (!cache[id]) {
  20. if (!opts) opts = {};
  21. if (!$.feat.nativeTouchScroll) opts.useJsScroll = true;
  22. tmp = scroller(this[i], opts);
  23. cache[id] = tmp;
  24. } else {
  25. tmp = cache[id];
  26. }
  27. }
  28. return this.length == 1 ? tmp : this;
  29. };
  30. var boundTouchLayer = false;
  31. function checkConsistency(id) {
  32. if (!cache[id].el) {
  33. delete cache[id];
  34. return false;
  35. }
  36. return true;
  37. }
  38. function bindTouchLayer() {
  39. //use a single bind for all scrollers
  40. if (af.os.android && !af.os.chrome && af.os.webkit) {
  41. var androidFixOn = false;
  42. //connect to touchLayer to detect editMode
  43. $.bind($.touchLayer, ['cancel-enter-edit', 'exit-edit'], function (focusEl) {
  44. if (androidFixOn) {
  45. androidFixOn = false;
  46. //dehactivate on scroller
  47. for (var el in cache)
  48. if (checkConsistency(el) && cache[el].androidFormsMode) cache[el].stopFormsMode();
  49. }
  50. });
  51. }
  52. boundTouchLayer = true;
  53. }
  54. var scroller = (function () {
  55. var translateOpen = $.feat.cssTransformStart;
  56. var translateClose = $.feat.cssTransformEnd;
  57. var jsScroller, nativeScroller;
  58. //initialize and js/native mode selector
  59. var scroller = function (elID, opts) {
  60. var el;
  61. if (!boundTouchLayer && $.touchLayer && $.isObject($.touchLayer)) bindTouchLayer();
  62. else if (!$.touchLayer || !$.isObject($.touchLayer)) $.touchLayer = {};
  63. if (typeof elID == "string" || elID instanceof String) {
  64. el = document.getElementById(elID);
  65. } else {
  66. el = elID;
  67. }
  68. if (!el) {
  69. alert("Could not find element for scroller " + elID);
  70. return;
  71. }
  72. if (af.os.desktop)
  73. return new scrollerCore(el, opts);
  74. else if (opts.useJsScroll) return new jsScroller(el, opts);
  75. return new nativeScroller(el, opts);
  76. };
  77. //parent abstract class (common functionality)
  78. var scrollerCore = function (el, opts) {
  79. this.el = el;
  80. this.afEl = $(this.el);
  81. for (var j in opts) {
  82. this[j] = opts[j];
  83. }
  84. };
  85. scrollerCore.prototype = {
  86. //core default properties
  87. refresh: false,
  88. refreshContent: "Pull to Refresh",
  89. refreshHangTimeout: 2000,
  90. refreshHeight: 60,
  91. refreshElement: null,
  92. refreshCancelCB: null,
  93. refreshRunning: false,
  94. scrollTop: 0,
  95. scrollLeft: 0,
  96. preventHideRefresh: true,
  97. verticalScroll: false,
  98. horizontalScroll: true,
  99. refreshTriggered: false,
  100. moved: false,
  101. eventsActive: false,
  102. rememberEventsActive: false,
  103. scrollingLocked: false,
  104. autoEnable: true,
  105. blockFormsFix: false,
  106. loggedPcentY: 0,
  107. loggedPcentX: 0,
  108. infinite: false,
  109. infiniteEndCheck: false,
  110. infiniteTriggered: false,
  111. scrollSkip: false,
  112. scrollTopInterval: null,
  113. scrollLeftInterval: null,
  114. bubbles:true,
  115. lockBounce:false,
  116. _scrollTo: function (params, time) {
  117. time = parseInt(time, 10);
  118. if (time === 0 || isNaN(time)) {
  119. this.el.scrollTop = Math.abs(params.y);
  120. this.el.scrollLeft = Math.abs(params.x);
  121. return;
  122. }
  123. var singleTick = 10;
  124. var distPerTick = (this.el.scrollTop - params.y) / Math.ceil(time / singleTick);
  125. var distLPerTick = (this.el.scrollLeft - params.x) / Math.ceil(time / singleTick);
  126. var self = this;
  127. var toRunY = Math.ceil(this.el.scrollTop - params.y) / distPerTick;
  128. var toRunX = Math.ceil(this.el.scrollLeft - params.x) / distPerTick;
  129. var xRun =0, yRun = 0;
  130. self.scrollTopInterval = window.setInterval(function () {
  131. self.el.scrollTop -= distPerTick;
  132. yRun++;
  133. if (yRun >= toRunY) {
  134. self.el.scrollTop = params.y;
  135. clearInterval(self.scrollTopInterval);
  136. }
  137. }, singleTick);
  138. self.scrollLeftInterval = window.setInterval(function () {
  139. self.el.scrollLeft -= distLPerTick;
  140. xRun++;
  141. if (xRun >= toRunX) {
  142. self.el.scrollLeft = params.x;
  143. clearInterval(self.scrollLeftInterval);
  144. }
  145. }, singleTick);
  146. },
  147. enable: function () {},
  148. disable: function () {},
  149. hideScrollbars: function () {},
  150. addPullToRefresh: function () {},
  151. /**
  152. * We do step animations for 'native' - iOS is acceptable and desktop browsers are fine
  153. * instead of css3
  154. */
  155. _scrollToTop: function (time) {
  156. this._scrollTo({
  157. x: 0,
  158. y: 0
  159. }, time);
  160. },
  161. _scrollToBottom: function (time) {
  162. this._scrollTo({
  163. x: 0,
  164. y: this.el.scrollHeight - this.el.offsetHeight
  165. }, time);
  166. },
  167. scrollToBottom: function (time) {
  168. return this._scrollToBottom(time);
  169. },
  170. scrollToTop: function (time) {
  171. return this._scrollToTop(time);
  172. },
  173. //methods
  174. init: function (el, opts) {
  175. this.el = el;
  176. this.afEl = $(this.el);
  177. this.defaultProperties();
  178. for (var j in opts) {
  179. this[j] = opts[j];
  180. }
  181. //assign self destruct
  182. var that = this;
  183. var orientationChangeProxy = function () {
  184. //no need to readjust if disabled...
  185. if (that.eventsActive&&!$.feat.nativeTouchScroll) that.adjustScroll();
  186. };
  187. this.afEl.bind('destroy', function () {
  188. that.disable(true); //with destroy notice
  189. var id = that.el.afScrollerId;
  190. if (cache[id]) delete cache[id];
  191. $.unbind($.touchLayer, 'orientationchange-reshape', orientationChangeProxy);
  192. });
  193. $.bind($.touchLayer, 'orientationchange-reshape', orientationChangeProxy);
  194. $(window).bind('resize', orientationChangeProxy);
  195. },
  196. needsFormsFix: function (focusEl) {
  197. return this.useJsScroll && this.isEnabled() && this.el.style.display != "none" && $(focusEl).closest(this.afEl).size() > 0;
  198. },
  199. handleEvent: function (e) {
  200. if (!this.scrollingLocked) {
  201. switch (e.type) {
  202. case 'touchstart':
  203. clearInterval(this.scrollTopInterval);
  204. this.preventHideRefresh = !this.refreshRunning; // if it's not running why prevent it xD
  205. this.moved = false;
  206. this.onTouchStart(e);
  207. if(!this.bubbles)
  208. e.stopPropagation();
  209. break;
  210. case 'touchmove':
  211. this.onTouchMove(e);
  212. if(!this.bubbles)
  213. e.stopPropagation();
  214. break;
  215. case 'touchend':
  216. this.onTouchEnd(e);
  217. if(!this.bubbles)
  218. e.stopPropagation();
  219. break;
  220. case 'scroll':
  221. this.onScroll(e);
  222. break;
  223. }
  224. }
  225. },
  226. coreAddPullToRefresh: function (rEl) {
  227. if (rEl) this.refreshElement = rEl;
  228. //Add the pull to refresh text. Not optimal but keeps from others overwriting the content and worrying about italics
  229. //add the refresh div
  230. var afEl;
  231. if (this.refreshElement === null) {
  232. var orginalEl = document.getElementById(this.container.id + "_pulldown");
  233. if (orginalEl !== null) {
  234. afEl = af(orginalEl);
  235. } else {
  236. afEl = af("<div id='" + this.container.id + "_pulldown' class='afscroll_refresh' style='position:relative;height:60px;text-align:center;line-height:60px;font-weight:bold;'>" + this.refreshContent + "</div>");
  237. }
  238. } else {
  239. afEl = af(this.refreshElement);
  240. }
  241. var el = afEl.get(0);
  242. this.refreshContainer = af('<div style="overflow:hidden;height:0;width:100%;display:none;"></div>');
  243. $(this.el).prepend(this.refreshContainer.prepend(el));
  244. this.refreshContainer = this.refreshContainer[0];
  245. },
  246. fireRefreshRelease: function (triggered, allowHide) {
  247. if (!this.refresh || !triggered) return;
  248. this.setRefreshContent("Refreshing...");
  249. var autoCancel = $.trigger(this, 'refresh-release', [triggered]) !== false;
  250. this.preventHideRefresh = false;
  251. this.refreshRunning = true;
  252. if (autoCancel) {
  253. var that = this;
  254. if (this.refreshHangTimeout > 0) this.refreshCancelCB = setTimeout(function () {
  255. that.hideRefresh();
  256. }, this.refreshHangTimeout);
  257. }
  258. },
  259. setRefreshContent: function (content) {
  260. af(this.container).find(".afscroll_refresh").html(content);
  261. },
  262. lock: function () {
  263. if (this.scrollingLocked) return;
  264. this.scrollingLocked = true;
  265. this.rememberEventsActive = this.eventsActive;
  266. if (this.eventsActive) {
  267. this.disable();
  268. }
  269. },
  270. unlock: function () {
  271. if (!this.scrollingLocked) return;
  272. this.scrollingLocked = false;
  273. if (this.rememberEventsActive) {
  274. this.enable();
  275. }
  276. },
  277. scrollToItem: function (el, where) { //TODO: add functionality for x position
  278. if (!$.is$(el)) el = $(el);
  279. var newTop,itemPos,panelTop,itemTop;
  280. if (where == 'bottom') {
  281. itemPos = el.offset();
  282. newTop = itemPos.top - this.afEl.offset().bottom + itemPos.height;
  283. newTop += 4; //add a small space
  284. } else {
  285. itemTop = el.offset().top;
  286. newTop = itemTop - document.body.scrollTop;
  287. panelTop = this.afEl.offset().top;
  288. if (document.body.scrollTop < panelTop) {
  289. newTop -= panelTop;
  290. }
  291. newTop -= 4; //add a small space
  292. }
  293. this.scrollBy({
  294. y: newTop,
  295. x: 0
  296. }, 0);
  297. },
  298. setPaddings: function (top, bottom) {
  299. var el = $(this.el);
  300. var curTop = numOnly(el.css('paddingTop'));
  301. el.css('paddingTop', top + "px").css('paddingBottom', bottom + "px");
  302. //don't let padding mess with scroll
  303. this.scrollBy({
  304. y: top - curTop,
  305. x: 0
  306. });
  307. },
  308. //freak of mathematics, but for our cases it works
  309. divide: function (a, b) {
  310. return b !== 0 ? a / b : 0;
  311. },
  312. isEnabled: function () {
  313. return this.eventsActive;
  314. },
  315. addInfinite: function () {
  316. this.infinite = true;
  317. },
  318. clearInfinite: function () {
  319. this.infiniteTriggered = false;
  320. this.scrollSkip = true;
  321. },
  322. scrollTo:function (pos, time) {
  323. return this._scrollTo(pos, time);
  324. }
  325. };
  326. //extend to jsScroller and nativeScroller (constructs)
  327. jsScroller = function (el, opts) {
  328. this.init(el, opts);
  329. //test
  330. //this.refresh=true;
  331. this.container = this.el.parentNode;
  332. this.container.afScrollerId = el.afScrollerId;
  333. this.afEl = $(this.container);
  334. if (this.container.style.overflow != 'hidden') this.container.style.overflow = 'hidden';
  335. this.addPullToRefresh(null, true);
  336. if (this.autoEnable) this.enable(true);
  337. var scrollDiv;
  338. //create vertical scroll
  339. if (this.verticalScroll && this.verticalScroll === true && this.scrollBars === true) {
  340. scrollDiv = createScrollBar(5, 20);
  341. scrollDiv.style.top = "0px";
  342. if (this.vScrollCSS) scrollDiv.className = this.vScrollCSS;
  343. //scrollDiv.style.opacity = "0";
  344. scrollDiv.style.display='none';
  345. this.container.appendChild(scrollDiv);
  346. this.vscrollBar = scrollDiv;
  347. scrollDiv = null;
  348. }
  349. //create horizontal scroll
  350. if (this.horizontalScroll && this.horizontalScroll === true && this.scrollBars === true) {
  351. scrollDiv = createScrollBar(20, 5);
  352. scrollDiv.style.bottom = "0px";
  353. if (this.hScrollCSS) scrollDiv.className = this.hScrollCSS;
  354. //scrollDiv.style.opacity = "0";
  355. scrollDiv.style.display='none';
  356. this.container.appendChild(scrollDiv);
  357. this.hscrollBar = scrollDiv;
  358. scrollDiv = null;
  359. }
  360. if (this.horizontalScroll) this.el.style['float'] = "left";
  361. this.el.hasScroller = true;
  362. };
  363. nativeScroller = function (el, opts) {
  364. if(opts.nativeParent){
  365. el=el.parentNode;
  366. }
  367. this.init(el, opts);
  368. var $el = $(el);
  369. if (opts.noParent !== true) {
  370. var oldParent = $el.parent();
  371. $el.css('height', oldParent.height()).css("width", oldParent.width());
  372. $el.insertBefore($el.parent());
  373. //$el.parent().parent().append($el);
  374. oldParent.remove();
  375. }
  376. this.container = this.el;
  377. $el.css("-webkit-overflow-scrolling", "touch");
  378. if(opts.autoEnable)
  379. this.enable();
  380. };
  381. nativeScroller.prototype = new scrollerCore();
  382. jsScroller.prototype = new scrollerCore();
  383. ///Native scroller
  384. nativeScroller.prototype.defaultProperties = function () {
  385. this.refreshContainer = null;
  386. this.dY = this.cY = 0;
  387. this.dX = this.cX = 0;
  388. this.cancelPropagation = false;
  389. this.loggedPcentY = 0;
  390. this.loggedPcentX = 0;
  391. var that = this;
  392. this.adjustScrollOverflowProxy_ = function () {
  393. that.afEl.css('overflow', 'auto');
  394. that.afEl.parent().css("overflow","hidden");
  395. };
  396. };
  397. nativeScroller.prototype.enable = function (firstExecution) {
  398. if (this.eventsActive) return;
  399. this.eventsActive = true;
  400. //unlock overflow
  401. this.el.style.overflow = 'auto';
  402. this.el.parentNode.style.overflow="hidden";
  403. //set current scroll
  404. if (!firstExecution) this.adjustScroll();
  405. //set events
  406. this.el.addEventListener('touchstart', this, false);
  407. this.el.addEventListener('scroll', this, false);
  408. };
  409. nativeScroller.prototype.disable = function (destroy) {
  410. if (!this.eventsActive) return;
  411. //log current scroll
  412. this.logPos(this.el.scrollLeft, this.el.scrollTop);
  413. //lock overflow
  414. if (!destroy&&!$.ui) {
  415. this.el.style.overflow = 'hidden';
  416. }
  417. //remove events
  418. this.el.removeEventListener('touchstart', this, false);
  419. this.el.removeEventListener('touchmove', this, false);
  420. this.el.removeEventListener('touchend', this, false);
  421. this.el.removeEventListener('scroll', this, false);
  422. this.eventsActive = false;
  423. };
  424. nativeScroller.prototype.addPullToRefresh = function (el, leaveRefresh) {
  425. this.el.removeEventListener('touchstart', this, false);
  426. this.el.addEventListener('touchstart', this, false);
  427. if (!leaveRefresh) this.refresh = true;
  428. if (this.refresh && this.refresh === true) {
  429. this.coreAddPullToRefresh(el);
  430. this.refreshContainer.style.position = "absolute";
  431. this.refreshContainer.style.top = "-60px";
  432. this.refreshContainer.style.height = "60px";
  433. this.refreshContainer.style.display = "block";
  434. }
  435. };
  436. nativeScroller.prototype.onTouchStart = function (e) {
  437. if(this.el.scrollTop===0)
  438. this.el.scrollTop=1;
  439. if(this.el.scrollTop===(this.el.scrollHeight - this.el.clientHeight))
  440. this.el.scrollTop-=1;
  441. if(this.horizontalScroll){
  442. if(this.el.scrollLeft===0)
  443. this.el.scrollLeft=1;
  444. if(this.el.scrollLeft===(this.el.scrollWidth-this.el.clientWidth))
  445. this.el.scrollLeft-=1;
  446. }
  447. if (this.refreshCancelCB) clearTimeout(this.refreshCancelCB);
  448. //get refresh ready
  449. this.el.addEventListener('touchmove', this,false);
  450. this.dY = e.touches[0].pageY;
  451. if (this.refresh || this.infinite) {
  452. if (this.refresh && this.dY < 0) {
  453. this.showRefresh();
  454. }
  455. }
  456. };
  457. nativeScroller.prototype.onTouchMove = function (e) {
  458. var newcY = e.touches[0].pageY - this.dY;
  459. var newcX = e.touches[0].pageX - this.dX;
  460. if(this.hasVertScroll&&this.el.clientHeight==this.el.scrollHeight){
  461. e.preventDefault();
  462. }
  463. if(this.hasHorScroll&&this.el.clientWidth==this.el.scrollWidth){
  464. e.preventDefault();
  465. }
  466. if (!this.moved) {
  467. $.trigger(this, "scrollstart", [this.el]);
  468. $.trigger($.touchLayer, "scrollstart", [this.el]);
  469. this.el.addEventListener('touchend', this, false);
  470. this.moved = true;
  471. }
  472. var difY = newcY - this.cY;
  473. var difX = newcX-this.cX;
  474. //check for trigger
  475. if (this.refresh && (this.el.scrollTop < -this.refreshHeight)) {
  476. this.showRefresh();
  477. //check for cancel when refresh is running
  478. } else if (this.refresh && this.refreshTriggered && this.refreshRunning && (this.el.scrollTop > this.refreshHeight)) {
  479. this.refreshTriggered = false;
  480. this.refreshRunning = false;
  481. if (this.refreshCancelCB) clearTimeout(this.refreshCancelCB);
  482. this.hideRefresh(false);
  483. this.setRefreshContent("Pull to Refresh");
  484. $.trigger(this, 'refresh-cancel');
  485. //check for cancel when refresh is not running
  486. } else if (this.refresh && this.refreshTriggered && !this.refreshRunning && (this.el.scrollTop > -this.refreshHeight)) {
  487. this.refreshTriggered = false;
  488. this.refreshRunning = false;
  489. if (this.refreshCancelCB) clearTimeout(this.refreshCancelCB);
  490. this.hideRefresh(false);
  491. this.setRefreshContent("Pull to Refresh");
  492. $.trigger(this, 'refresh-cancel');
  493. }
  494. this.cY = newcY;
  495. this.cX = newcX;
  496. };
  497. nativeScroller.prototype.showRefresh = function () {
  498. if (!this.refreshTriggered) {
  499. this.refreshTriggered = true;
  500. this.setRefreshContent("Release to Refresh");
  501. $.trigger(this, 'refresh-trigger');
  502. }
  503. };
  504. nativeScroller.prototype.onTouchEnd = function (e) {
  505. var triggered = this.el.scrollTop <= -(this.refreshHeight);
  506. this.fireRefreshRelease(triggered, true);
  507. if (triggered&&this.refresh) {
  508. //lock in place
  509. this.refreshContainer.style.position = "relative";
  510. this.refreshContainer.style.top = "0px";
  511. }
  512. this.dY = this.cY = 0;
  513. this.el.removeEventListener('touchmove', this, false);
  514. this.el.removeEventListener('touchend', this, false);
  515. this.infiniteEndCheck = true;
  516. if (this.infinite && !this.infiniteTriggered && (Math.abs(this.el.scrollTop) >= (this.el.scrollHeight - this.el.clientHeight))) {
  517. this.infiniteTriggered = true;
  518. $.trigger(this, "infinite-scroll");
  519. this.infiniteEndCheck = true;
  520. }
  521. this.touchEndFired = true;
  522. //pollyfil for scroll end since webkit doesn't give any events during the "flick"
  523. var max = 200;
  524. var self = this;
  525. var currPos = {
  526. top: this.el.scrollTop,
  527. left: this.el.scrollLeft
  528. };
  529. var counter = 0;
  530. self.nativePolling = setInterval(function () {
  531. counter++;
  532. if (counter >= max) {
  533. clearInterval(self.nativePolling);
  534. return;
  535. }
  536. if (self.el.scrollTop != currPos.top || self.el.scrollLeft != currPos.left) {
  537. clearInterval(self.nativePolling);
  538. $.trigger($.touchLayer, 'scrollend', [self.el]); //notify touchLayer of this elements scrollend
  539. $.trigger(self, "scrollend", [self.el]);
  540. }
  541. }, 20);
  542. };
  543. nativeScroller.prototype.hideRefresh = function (animate) {
  544. if (this.preventHideRefresh) return;
  545. var that = this;
  546. var endAnimationCb = function (canceled) {
  547. that.refreshContainer.style.top = "-60px";
  548. that.refreshContainer.style.position = "absolute";
  549. that.dY = that.cY = 0;
  550. if (!canceled) { //not sure if this should be the correct logic....
  551. that.el.style[$.feat.cssPrefix + "Transform"] = "none";
  552. that.el.style[$.feat.cssPrefix + "TransitionProperty"] = "none";
  553. that.el.scrollTop = 0;
  554. that.logPos(that.el.scrollLeft, 0);
  555. that.refreshRunning = false;
  556. that.setRefreshContent("Pull to Refresh");
  557. $.trigger(that, "refresh-finish");
  558. }
  559. };
  560. if (animate === false || !that.afEl.css3Animate) {
  561. endAnimationCb();
  562. } else {
  563. that.afEl.css3Animate({
  564. y: (that.el.scrollTop - that.refreshHeight) + "px",
  565. x: "0%",
  566. time: HIDE_REFRESH_TIME + "ms",
  567. complete: endAnimationCb
  568. });
  569. }
  570. this.refreshTriggered = false;
  571. //this.el.addEventListener('touchend', this, false);
  572. };
  573. nativeScroller.prototype.hideScrollbars = function () {};
  574. nativeScroller.prototype.scrollTo = function (pos, time) {
  575. this.logPos(pos.x, pos.y);
  576. pos.x *= -1;
  577. pos.y *= -1;
  578. return this._scrollTo(pos, time);
  579. };
  580. nativeScroller.prototype.scrollBy = function (pos, time) {
  581. pos.x += this.el.scrollLeft;
  582. pos.y += this.el.scrollTop;
  583. this.logPos(this.el.scrollLeft, this.el.scrollTop);
  584. return this._scrollTo(pos, time);
  585. };
  586. nativeScroller.prototype.scrollToBottom = function (time) {
  587. //this.el.scrollTop = this.el.scrollHeight;
  588. this._scrollToBottom(time);
  589. this.logPos(this.el.scrollLeft, this.el.scrollTop);
  590. };
  591. nativeScroller.prototype.onScroll = function (e) {
  592. if (this.infinite && this.touchEndFired) {
  593. this.touchEndFired = false;
  594. return;
  595. }
  596. if (this.scrollSkip) {
  597. this.scrollSkip = false;
  598. return;
  599. }
  600. if (this.infinite) {
  601. if (!this.infiniteTriggered && (Math.abs(this.el.scrollTop) >= (this.el.scrollHeight - this.el.clientHeight))) {
  602. this.infiniteTriggered = true;
  603. $.trigger(this, "infinite-scroll");
  604. this.infiniteEndCheck = true;
  605. }
  606. }
  607. var that = this;
  608. if (this.infinite && this.infiniteEndCheck && this.infiniteTriggered) {
  609. this.infiniteEndCheck = false;
  610. $.trigger(that, "infinite-scroll-end");
  611. }
  612. };
  613. nativeScroller.prototype.logPos = function (x, y) {
  614. this.loggedPcentX = this.divide(x, (this.el.scrollWidth));
  615. this.loggedPcentY = this.divide(y, (this.el.scrollHeight));
  616. this.scrollLeft = x;
  617. this.scrollTop = y;
  618. if (isNaN(this.loggedPcentX))
  619. this.loggedPcentX = 0;
  620. if (isNaN(this.loggedPcentY))
  621. this.loggedPcentY = 0;
  622. };
  623. nativeScroller.prototype.adjustScroll = function () {
  624. this.adjustScrollOverflowProxy_();
  625. this.el.scrollLeft = this.loggedPcentX * (this.el.scrollWidth);
  626. this.el.scrollTop = this.loggedPcentY * (this.el.scrollHeight);
  627. this.logPos(this.el.scrollLeft, this.el.scrollTop);
  628. };
  629. //JS scroller
  630. jsScroller.prototype.defaultProperties = function () {
  631. this.boolScrollLock = false;
  632. this.currentScrollingObject = null;
  633. this.elementInfo = null;
  634. this.verticalScroll = true;
  635. this.horizontalScroll = false;
  636. this.scrollBars = true;
  637. this.vscrollBar = null;
  638. this.hscrollBar = null;
  639. this.hScrollCSS = "scrollBar";
  640. this.vScrollCSS = "scrollBar";
  641. this.firstEventInfo = null;
  642. this.moved = false;
  643. this.preventPullToRefresh = true;
  644. this.isScrolling = false;
  645. this.androidFormsMode = false;
  646. this.refreshSafeKeep = false;
  647. this.lastScrollbar = "";
  648. this.finishScrollingObject = null;
  649. this.container = null;
  650. this.scrollingFinishCB = null;
  651. this.loggedPcentY = 0;
  652. this.loggedPcentX = 0;
  653. };
  654. function createScrollBar(width, height) {
  655. var scrollDiv = document.createElement("div");
  656. scrollDiv.style.position = 'absolute';
  657. scrollDiv.style.width = width + "px";
  658. scrollDiv.style.height = height + "px";
  659. scrollDiv.style[$.feat.cssPrefix + 'BorderRadius'] = "2px";
  660. scrollDiv.style.borderRadius = "2px";
  661. scrollDiv.style.display="none";
  662. scrollDiv.className = 'scrollBar';
  663. scrollDiv.style.background = "black";
  664. return scrollDiv;
  665. }
  666. jsScroller.prototype.enable = function (firstExecution) {
  667. if (this.eventsActive) return;
  668. this.eventsActive = true;
  669. if (!firstExecution) this.adjustScroll();
  670. else
  671. this.scrollerMoveCSS({
  672. x: 0,
  673. y: 0
  674. }, 0);
  675. //add listeners
  676. this.container.addEventListener('touchstart', this, false);
  677. this.container.addEventListener('touchmove', this, false);
  678. this.container.addEventListener('touchend', this, false);
  679. };
  680. jsScroller.prototype.adjustScroll = function () {
  681. //set top/left
  682. var size = this.getViewportSize();
  683. this.scrollerMoveCSS({
  684. x: Math.round(this.loggedPcentX * (this.el.clientWidth - size.w)),
  685. y: Math.round(this.loggedPcentY * (this.el.clientHeight - size.h))
  686. }, 0);
  687. };
  688. jsScroller.prototype.disable = function () {
  689. if (!this.eventsActive) return;
  690. //log top/left
  691. var cssMatrix = this.getCSSMatrix(this.el);
  692. this.logPos((numOnly(cssMatrix.e) - numOnly(this.container.scrollLeft)), (numOnly(cssMatrix.f) - numOnly(this.container.scrollTop)));
  693. //remove event listeners
  694. this.container.removeEventListener('touchstart', this, false);
  695. this.container.removeEventListener('touchmove', this, false);
  696. this.container.removeEventListener('touchend', this, false);
  697. this.eventsActive = false;
  698. };
  699. jsScroller.prototype.addPullToRefresh = function (el, leaveRefresh) {
  700. if (!leaveRefresh) this.refresh = true;
  701. if (this.refresh && this.refresh === true) {
  702. this.coreAddPullToRefresh(el);
  703. this.el.style.overflow = 'visible';
  704. }
  705. };
  706. jsScroller.prototype.hideScrollbars = function () {
  707. if (this.hscrollBar) {
  708. this.hscrollBar.style.display="none";
  709. this.hscrollBar.style[$.feat.cssPrefix + 'TransitionDuration'] = "0ms";
  710. }
  711. if (this.vscrollBar) {
  712. this.vscrollBar.style.display="none";
  713. this.vscrollBar.style[$.feat.cssPrefix + 'TransitionDuration'] = "0ms";
  714. }
  715. };
  716. jsScroller.prototype.getViewportSize = function () {
  717. var style = window.getComputedStyle(this.container);
  718. if (isNaN(numOnly(style.paddingTop))) alert((typeof style.paddingTop) + '::' + style.paddingTop + ':');
  719. return {
  720. h: (this.container.clientHeight > window.innerHeight ? window.innerHeight : this.container.clientHeight - numOnly(style.paddingTop) - numOnly(style.paddingBottom)),
  721. w: (this.container.clientWidth > window.innerWidth ? window.innerWidth : this.container.clientWidth - numOnly(style.paddingLeft) - numOnly(style.paddingRight))
  722. };
  723. };
  724. jsScroller.prototype.onTouchStart = function (event) {
  725. this.moved = false;
  726. this.currentScrollingObject = null;
  727. if (!this.container) return;
  728. if (this.refreshCancelCB) {
  729. clearTimeout(this.refreshCancelCB);
  730. this.refreshCancelCB = null;
  731. }
  732. if (this.scrollingFinishCB) {
  733. clearTimeout(this.scrollingFinishCB);
  734. this.scrollingFinishCB = null;
  735. }
  736. //disable if locked
  737. if (event.touches.length != 1 || this.boolScrollLock) return;
  738. // Allow interaction to legit calls, like select boxes, etc.
  739. if (event.touches[0].target && event.touches[0].target.type !== undefined) {
  740. var tagname = event.touches[0].target.tagName.toLowerCase();
  741. var tagtype=event.touches[0].target.type.toLowerCase();
  742. if (tagname == "select" ) // stuff we need to allow
  743. // access to legit calls
  744. return;
  745. }
  746. //default variables
  747. var scrollInfo = {
  748. //current position
  749. top: 0,
  750. left: 0,
  751. //current movement
  752. speedY: 0,
  753. speedX: 0,
  754. absSpeedY: 0,
  755. absSpeedX: 0,
  756. deltaY: 0,
  757. deltaX: 0,
  758. absDeltaY: 0,
  759. absDeltaX: 0,
  760. y: 0,
  761. x: 0,
  762. duration: 0
  763. };
  764. //element info
  765. this.elementInfo = {};
  766. var size = this.getViewportSize();
  767. this.elementInfo.bottomMargin = size.h;
  768. this.elementInfo.maxTop = (this.el.clientHeight - this.elementInfo.bottomMargin);
  769. if (this.elementInfo.maxTop < 0) this.elementInfo.maxTop = 0;
  770. this.elementInfo.divHeight = this.el.clientHeight;
  771. this.elementInfo.rightMargin = size.w;
  772. this.elementInfo.maxLeft = (this.el.clientWidth - this.elementInfo.rightMargin);
  773. if (this.elementInfo.maxLeft < 0) this.elementInfo.maxLeft = 0;
  774. this.elementInfo.divWidth = this.el.clientWidth;
  775. this.elementInfo.hasVertScroll = this.verticalScroll || this.elementInfo.maxTop > 0;
  776. this.elementInfo.hasHorScroll = this.elementInfo.maxLeft > 0;
  777. this.elementInfo.requiresVScrollBar = this.vscrollBar && this.elementInfo.hasVertScroll;
  778. this.elementInfo.requiresHScrollBar = this.hscrollBar && this.elementInfo.hasHorScroll;
  779. //save event
  780. this.saveEventInfo(event);
  781. this.saveFirstEventInfo(event);
  782. //get the current top
  783. var cssMatrix = this.getCSSMatrix(this.el);
  784. scrollInfo.top = numOnly(cssMatrix.f) - numOnly(this.container.scrollTop);
  785. scrollInfo.left = numOnly(cssMatrix.e) - numOnly(this.container.scrollLeft);
  786. this.container.scrollTop = this.container.scrollLeft = 0;
  787. this.currentScrollingObject = this.el;
  788. //get refresh ready
  789. if (this.refresh && scrollInfo.top === 0) {
  790. this.refreshContainer.style.display = "block";
  791. this.refreshHeight = this.refreshContainer.firstChild.clientHeight;
  792. this.refreshContainer.firstChild.style.top = (-this.refreshHeight) + 'px';
  793. this.refreshContainer.style.overflow = 'visible';
  794. this.preventPullToRefresh = false;
  795. } else if (scrollInfo.top < 0) {
  796. this.preventPullToRefresh = true;
  797. if (this.refresh) this.refreshContainer.style.overflow = 'hidden';
  798. }
  799. //set target
  800. scrollInfo.x = scrollInfo.left;
  801. scrollInfo.y = scrollInfo.top;
  802. //vertical scroll bar
  803. if (this.setVScrollBar(scrollInfo, 0, 0)) {
  804. if (this.container.clientWidth > window.innerWidth)
  805. this.vscrollBar.style.right = "0px";
  806. else
  807. this.vscrollBar.style.right = "0px";
  808. this.vscrollBar.style[$.feat.cssPrefix + "Transition"] = '';
  809. // this.vscrollBar.style.opacity = 1;
  810. }
  811. //horizontal scroll
  812. if (this.setHScrollBar(scrollInfo, 0, 0)) {
  813. if (this.container.clientHeight > window.innerHeight)
  814. this.hscrollBar.style.top = (window.innerHeight - numOnly(this.hscrollBar.style.height)) + "px";
  815. else
  816. this.hscrollBar.style.bottom = numOnly(this.hscrollBar.style.height);
  817. this.hscrollBar.style[$.feat.cssPrefix + "Transition"] = '';
  818. // this.hscrollBar.style.opacity = 1;
  819. }
  820. //save scrollInfo
  821. this.lastScrollInfo = scrollInfo;
  822. this.hasMoved = false;
  823. if(this.elementInfo.maxTop==0&&this.elementInfo.maxLeft==0){
  824. this.currentScrollingObject=null;
  825. this.scrollToTop(0);
  826. }else{
  827. this.scrollerMoveCSS(this.lastScrollInfo, 0);
  828. }
  829. };
  830. jsScroller.prototype.getCSSMatrix = function (el) {
  831. if (this.androidFormsMode) {
  832. //absolute mode
  833. var top = parseInt(el.style.marginTop,10);
  834. var left = parseInt(el.style.marginLeft,10);
  835. if (isNaN(top)) top = 0;
  836. if (isNaN(left)) left = 0;
  837. return {
  838. f: top,
  839. e: left
  840. };
  841. } else {
  842. //regular transform
  843. var obj = $.getCssMatrix(el);
  844. return obj;
  845. }
  846. };
  847. jsScroller.prototype.saveEventInfo = function (event) {
  848. this.lastEventInfo = {
  849. pageX: event.touches[0].pageX,
  850. pageY: event.touches[0].pageY,
  851. time: event.timeStamp
  852. };
  853. };
  854. jsScroller.prototype.saveFirstEventInfo = function (event) {
  855. this.firstEventInfo = {
  856. pageX: event.touches[0].pageX,
  857. pageY: event.touches[0].pageY,
  858. time: event.timeStamp
  859. };
  860. };
  861. jsScroller.prototype.setVScrollBar = function (scrollInfo, time, timingFunction) {
  862. if (!this.elementInfo.requiresVScrollBar) return false;
  863. var newHeight = (parseFloat(this.elementInfo.bottomMargin / this.elementInfo.divHeight) * this.elementInfo.bottomMargin) + "px";
  864. if(numOnly(newHeight)>this.elementInfo.bottomMargin)
  865. newHeight=this.elementInfo.bottomMargin+"px";
  866. if (newHeight != this.vscrollBar.style.height) this.vscrollBar.style.height = newHeight;
  867. var pos = (this.elementInfo.bottomMargin - numOnly(this.vscrollBar.style.height)) - (((this.elementInfo.maxTop + scrollInfo.y) / this.elementInfo.maxTop) * (this.elementInfo.bottomMargin - numOnly(this.vscrollBar.style.height)));
  868. if (pos > this.elementInfo.bottomMargin) pos = this.elementInfo.bottomMargin;
  869. if (pos < 0) pos = 0;
  870. this.scrollbarMoveCSS(this.vscrollBar, {
  871. x: 0,
  872. y: pos
  873. }, time, timingFunction);
  874. return true;
  875. };
  876. jsScroller.prototype.setHScrollBar = function (scrollInfo, time, timingFunction) {
  877. if (!this.elementInfo.requiresHScrollBar) return false;
  878. var newWidth = (parseFloat(this.elementInfo.rightMargin / this.elementInfo.divWidth) * this.elementInfo.rightMargin) + "px";
  879. if (newWidth != this.hscrollBar.style.width) this.hscrollBar.style.width = newWidth;
  880. var pos = (this.elementInfo.rightMargin - numOnly(this.hscrollBar.style.width)) - (((this.elementInfo.maxLeft + scrollInfo.x) / this.elementInfo.maxLeft) * (this.elementInfo.rightMargin - numOnly(this.hscrollBar.style.width)));
  881. if (pos > this.elementInfo.rightMargin) pos = this.elementInfo.rightMargin;
  882. if (pos < 0) pos = 0;
  883. this.scrollbarMoveCSS(this.hscrollBar, {
  884. x: pos,
  885. y: 0
  886. }, time, timingFunction);
  887. return true;
  888. };
  889. jsScroller.prototype.onTouchMove = function (event) {
  890. if (this.currentScrollingObject === null) return;
  891. //event.preventDefault();
  892. var scrollInfo = this.calculateMovement(event);
  893. this.calculateTarget(scrollInfo);
  894. this.lastScrollInfo = scrollInfo;
  895. if (!this.moved) {
  896. $.trigger(this, "scrollstart");
  897. $.trigger($.touchLayer, "scrollstart", [this.el]);
  898. if (this.elementInfo.requiresVScrollBar) this.vscrollBar.style.display="block";
  899. if (this.elementInfo.requiresHScrollBar) this.hscrollBar.style.display="block";
  900. }
  901. this.moved = true;
  902. if (this.refresh && scrollInfo.top === 0) {
  903. this.refreshContainer.style.display = "block";
  904. this.refreshHeight = this.refreshContainer.firstChild.clientHeight;
  905. this.refreshContainer.firstChild.style.top = (-this.refreshHeight) + 'px';
  906. this.refreshContainer.style.overflow = 'visible';
  907. this.preventPullToRefresh = false;
  908. } else if (scrollInfo.top < 0) {
  909. this.preventPullToRefresh = true;
  910. if (this.refresh) this.refreshContainer.style.overflow = 'hidden';
  911. }
  912. this.saveEventInfo(event);
  913. if (this.isScrolling===false){ // && (this.lastScrollInfo.x != this.lastScrollInfo.left || this.lastScrollInfo.y != this.lastScrollInfo.top)) {
  914. this.isScrolling = true;
  915. if (this.onScrollStart) this.onScrollStart();
  916. }
  917. //proceed normally
  918. var cssMatrix = this.getCSSMatrix(this.el);
  919. this.lastScrollInfo.top = numOnly(cssMatrix.f);
  920. this.lastScrollInfo.left = numOnly(cssMatrix.e);
  921. this.recalculateDeltaY(this.lastScrollInfo);
  922. this.recalculateDeltaX(this.lastScrollInfo);
  923. //boundaries control
  924. this.checkYboundary(this.lastScrollInfo);
  925. if (this.elementInfo.hasHorScroll) this.checkXboundary(this.lastScrollInfo);
  926. //pull to refresh elastic
  927. var positiveOverflow = this.lastScrollInfo.y > 0 && this.lastScrollInfo.deltaY > 0;
  928. var negativeOverflow = this.lastScrollInfo.y < -this.elementInfo.maxTop && this.lastScrollInfo.deltaY < 0;
  929. if (positiveOverflow || negativeOverflow) {
  930. var overflow = positiveOverflow ? this.lastScrollInfo.y : -this.lastScrollInfo.y - this.elementInfo.maxTop;
  931. var pcent = (this.container.clientHeight - overflow) / this.container.clientHeight;
  932. if (pcent < 0.5) pcent = 0.5;
  933. //cur top, maxTop or 0?
  934. var baseTop = 0;
  935. if ((positiveOverflow && this.lastScrollInfo.top > 0) || (negativeOverflow && this.lastScrollInfo.top < -this.elementInfo.maxTop)) {
  936. baseTop = this.lastScrollInfo.top;
  937. } else if (negativeOverflow) {
  938. baseTop = -this.elementInfo.maxTop;
  939. }
  940. var changeY = this.lastScrollInfo.deltaY * pcent;
  941. var absChangeY = Math.abs(this.lastScrollInfo.deltaY * pcent);
  942. if (absChangeY < 1) changeY = positiveOverflow ? 1 : -1;
  943. this.lastScrollInfo.y = baseTop + changeY;
  944. }
  945. if(this.elementInfo.hasHorScroll){
  946. positiveOverflow = this.lastScrollInfo.x > 0 && this.lastScrollInfo.deltaX > 0;
  947. negativeOverflow = this.lastScrollInfo.x < -this.elementInfo.maxLeft && this.lastScrollInfo.deltaX < 0;
  948. if (positiveOverflow || negativeOverflow) {
  949. var overflow = positiveOverflow ? this.lastScrollInfo.x : -this.lastScrollInfo.x - this.elementInfo.maxLeft;
  950. var pcent = (this.container.clientWidth - overflow) / this.container.clientWidth;
  951. if (pcent < 0.5) pcent = 0.5;
  952. //cur top, maxTop or 0?
  953. var baseTop = 0;
  954. if ((positiveOverflow && this.lastScrollInfo.left > 0) || (negativeOverflow && this.lastScrollInfo.left < -this.elementInfo.maxLeft)) {
  955. baseTop = this.lastScrollInfo.left;
  956. } else if (negativeOverflow) {
  957. baseTop = -this.elementInfo.maxLeft;
  958. }
  959. var changeX = this.lastScrollInfo.deltaX * pcent;
  960. var absChangeX = Math.abs(this.lastScrollInfo.deltaX * pcent);
  961. if (absChangeX < 1) changeX = positiveOverflow ? 1 : -1;
  962. this.lastScrollInfo.x = baseTop + changeX;
  963. }
  964. }
  965. if(this.lockBounce){
  966. if(this.lastScrollInfo.x>0)
  967. this.lastScrollInfo.x=0;
  968. else if(this.lastScrollInfo.x*-1>this.elementInfo.maxLeft)
  969. this.lastScrollInfo.x=this.elementInfo.maxLeft*-1;
  970. if(this.lastScrollInfo.y>0)
  971. this.lastScrollInfo.y=0;
  972. else if(this.lastScrollInfo.y*-1>this.elementInfo.maxTop)
  973. this.lastScrollInfo.y=this.elementInfo.maxTop*-1;
  974. }
  975. //move
  976. this.scrollerMoveCSS(this.lastScrollInfo, 0);
  977. this.setVScrollBar(this.lastScrollInfo, 0, 0);
  978. this.setHScrollBar(this.lastScrollInfo, 0, 0);
  979. //check refresh triggering
  980. if (this.refresh && !this.preventPullToRefresh) {
  981. if (!this.refreshTriggered && this.lastScrollInfo.top > this.refreshHeight) {
  982. this.refreshTriggered = true;
  983. this.setRefreshContent("Release to Refresh");
  984. $.trigger(this, 'refresh-trigger');
  985. } else if (this.refreshTriggered && this.lastScrollInfo.top < this.refreshHeight) {
  986. this.refreshTriggered = false;
  987. this.setRefreshContent("Pull to Refresh");
  988. $.trigger(this, 'refresh-cancel');
  989. }
  990. }
  991. if (this.infinite && !this.infiniteTriggered) {
  992. if ((Math.abs(this.lastScrollInfo.top) > (this.el.clientHeight - this.container.clientHeight))) {
  993. this.infiniteTriggered = true;
  994. $.trigger(this, "infinite-scroll");
  995. }
  996. }
  997. };
  998. jsScroller.prototype.calculateMovement = function (event, last) {
  999. //default variables
  1000. var scrollInfo = {
  1001. //current position
  1002. top: 0,
  1003. left: 0,
  1004. //current movement
  1005. speedY: 0,
  1006. speedX: 0,
  1007. absSpeedY: 0,
  1008. absSpeedX: 0,
  1009. deltaY: 0,
  1010. deltaX: 0,
  1011. absDeltaY: 0,
  1012. absDeltaX: 0,
  1013. y: 0,
  1014. x: 0,
  1015. duration: 0
  1016. };
  1017. var prevEventInfo = last ? this.firstEventInfo : this.lastEventInfo;
  1018. var pageX = last ? event.pageX : event.touches[0].pageX;
  1019. var pageY = last ? event.pageY : event.touches[0].pageY;
  1020. var time = last ? event.time : event.timeStamp;
  1021. scrollInfo.deltaY = this.elementInfo.hasVertScroll ? pageY - prevEventInfo.pageY : 0;
  1022. scrollInfo.deltaX = this.elementInfo.hasHorScroll ? pageX - prevEventInfo.pageX : 0;
  1023. scrollInfo.time = time;
  1024. scrollInfo.duration = time - prevEventInfo.time;
  1025. return scrollInfo;
  1026. };
  1027. jsScroller.prototype.calculateTarget = function (scrollInfo) {
  1028. scrollInfo.y = this.lastScrollInfo.y + scrollInfo.deltaY;
  1029. scrollInfo.x = this.lastScrollInfo.x + scrollInfo.deltaX;
  1030. };
  1031. jsScroller.prototype.checkYboundary = function (scrollInfo) {
  1032. var minTop = this.container.clientHeight / 2;
  1033. var maxTop = this.elementInfo.maxTop + minTop;
  1034. //y boundaries
  1035. if (scrollInfo.y > minTop) scrollInfo.y = minTop;
  1036. else if (-scrollInfo.y > maxTop) scrollInfo.y = -maxTop;
  1037. else return;
  1038. this.recalculateDeltaY(scrollInfo);
  1039. };
  1040. jsScroller.prototype.checkXboundary = function (scrollInfo) {
  1041. //x boundaries
  1042. var minLeft=this.container.clientWidth/2;
  1043. var maxLeft=this.elementInfo.maxLeft+minLeft;
  1044. if (scrollInfo.x > minLeft) scrollInfo.x = minLeft;
  1045. else if (-scrollInfo.x > maxLeft) scrollInfo.x = -maxLeft;
  1046. else return;
  1047. this.recalculateDeltaX(scrollInfo);
  1048. };
  1049. jsScroller.prototype.recalculateDeltaY = function (scrollInfo) {
  1050. //recalculate delta
  1051. var oldAbsDeltaY = Math.abs(scrollInfo.deltaY);
  1052. scrollInfo.deltaY = scrollInfo.y - scrollInfo.top;
  1053. newAbsDeltaY = Math.abs(scrollInfo.deltaY);
  1054. //recalculate duration at same speed
  1055. scrollInfo.duration = scrollInfo.duration * newAbsDeltaY / oldAbsDeltaY;
  1056. };
  1057. jsScroller.prototype.recalculateDeltaX = function (scrollInfo) {
  1058. //recalculate delta
  1059. var oldAbsDeltaX = Math.abs(scrollInfo.deltaX);
  1060. scrollInfo.deltaX = scrollInfo.x - scrollInfo.left;
  1061. newAbsDeltaX = Math.abs(scrollInfo.deltaX);
  1062. //recalculate duration at same speed
  1063. scrollInfo.duration = scrollInfo.duration * newAbsDeltaX / oldAbsDeltaX;
  1064. };
  1065. jsScroller.prototype.hideRefresh = function (animate) {
  1066. if (this.preventHideRefresh) return;
  1067. var that = this;
  1068. var endAnimationCb = function () {
  1069. that.setRefreshContent("Pull to Refresh");
  1070. $.trigger(that, "refresh-finish");
  1071. };
  1072. this.scrollerMoveCSS({x: 0, y: 0}, HIDE_REFRESH_TIME);
  1073. if (animate === false || !that.afEl.css3Animate) {
  1074. endAnimationCb();
  1075. } else {
  1076. that.afEl.css3Animate({
  1077. time: HIDE_REFRESH_TIME + "ms",
  1078. complete: endAnimationCb
  1079. });
  1080. }
  1081. this.refreshTriggered = false;
  1082. };
  1083. jsScroller.prototype.setMomentum = function (scrollInfo) {
  1084. var deceleration = 0.0012;
  1085. //calculate movement speed
  1086. scrollInfo.speedY = this.divide(scrollInfo.deltaY, scrollInfo.duration);
  1087. scrollInfo.speedX = this.divide(scrollInfo.deltaX, scrollInfo.duration);
  1088. scrollInfo.absSpeedY = Math.abs(scrollInfo.speedY);
  1089. scrollInfo.absSpeedX = Math.abs(scrollInfo.speedX);
  1090. scrollInfo.absDeltaY = Math.abs(scrollInfo.deltaY);
  1091. scrollInfo.absDeltaX = Math.abs(scrollInfo.deltaX);
  1092. //set momentum
  1093. if (scrollInfo.absDeltaY > 0) {
  1094. scrollInfo.deltaY = (scrollInfo.deltaY < 0 ? -1 : 1) * (scrollInfo.absSpeedY * scrollInfo.absSpeedY) / (2 * deceleration);
  1095. scrollInfo.absDeltaY = Math.abs(scrollInfo.deltaY);
  1096. scrollInfo.duration = scrollInfo.absSpeedY / deceleration;
  1097. scrollInfo.speedY = scrollInfo.deltaY / scrollInfo.duration;
  1098. scrollInfo.absSpeedY = Math.abs(scrollInfo.speedY);
  1099. if (scrollInfo.absSpeedY < deceleration * 100 || scrollInfo.absDeltaY < 5) scrollInfo.deltaY = scrollInfo.absDeltaY = scrollInfo.duration = scrollInfo.speedY = scrollInfo.absSpeedY = 0;
  1100. } else if (scrollInfo.absDeltaX) {
  1101. scrollInfo.deltaX = (scrollInfo.deltaX < 0 ? -1 : 1) * (scrollInfo.absSpeedX * scrollInfo.absSpeedX) / (2 * deceleration);
  1102. scrollInfo.absDeltaX = Math.abs(scrollInfo.deltaX);
  1103. scrollInfo.duration = scrollInfo.absSpeedX / deceleration;
  1104. scrollInfo.speedX = scrollInfo.deltaX / scrollInfo.duration;
  1105. scrollInfo.absSpeedX = Math.abs(scrollInfo.speedX);
  1106. if (scrollInfo.absSpeedX < deceleration * 100 || scrollInfo.absDeltaX < 5) scrollInfo.deltaX = scrollInfo.absDeltaX = scrollInfo.duration = scrollInfo.speedX = scrollInfo.absSpeedX = 0;
  1107. } else scrollInfo.duration = 0;
  1108. };
  1109. jsScroller.prototype.onTouchEnd = function (event) {
  1110. if (this.currentScrollingObject === null || !this.moved) return;
  1111. //event.preventDefault();
  1112. this.finishScrollingObject = this.currentScrollingObject;
  1113. this.currentScrollingObject = null;
  1114. var scrollInfo = this.calculateMovement(this.lastEventInfo, true);
  1115. if (!this.androidFormsMode) {
  1116. this.setMomentum(scrollInfo);
  1117. }
  1118. this.calculateTarget(scrollInfo);
  1119. //get the current top
  1120. var cssMatrix = this.getCSSMatrix(this.el);
  1121. scrollInfo.top = numOnly(cssMatrix.f);
  1122. scrollInfo.left = numOnly(cssMatrix.e);
  1123. //boundaries control
  1124. this.checkYboundary(scrollInfo);
  1125. if (this.elementInfo.hasHorScroll) this.checkXboundary(scrollInfo);
  1126. var triggered = !this.preventPullToRefresh && (scrollInfo.top > this.refreshHeight || scrollInfo.y > this.refreshHeight);
  1127. this.fireRefreshRelease(triggered, scrollInfo.top > 0);
  1128. //refresh hang in
  1129. if (this.refresh && triggered) {
  1130. scrollInfo.y = this.refreshHeight;
  1131. scrollInfo.duration = HIDE_REFRESH_TIME;
  1132. //top boundary
  1133. } else if (scrollInfo.y >= 0) {
  1134. scrollInfo.y = 0;
  1135. if (scrollInfo.top >= 0) scrollInfo.duration = HIDE_REFRESH_TIME;
  1136. //lower boundary
  1137. } else if (-scrollInfo.y > this.elementInfo.maxTop || this.elementInfo.maxTop === 0) {
  1138. scrollInfo.y = -this.elementInfo.maxTop;
  1139. if (-scrollInfo.top > this.elementInfo.maxTop) scrollInfo.duration = HIDE_REFRESH_TIME;
  1140. //all others
  1141. }
  1142. ;
  1143. if(this.elementInfo.hasHorScroll){
  1144. if(scrollInfo.x>=0)
  1145. {
  1146. scrollInfo.x=0;
  1147. if(scrollInfo.left>=0) scrollInfo.duration=HIDE_REFRESH_TIME;
  1148. }
  1149. else if(-scrollInfo.x>this.elementInfo.maxLeft||this.elementInfo.maxLeft===0){
  1150. scrollInfo.x=-this.elementInfo.maxLeft;
  1151. if(-scrollInfo.left>this.elementInfo.maxLeft) scrollInfo.duration=HIDE_REFRESH_TIME;
  1152. }
  1153. }
  1154. if (this.androidFormsMode) scrollInfo.duration = 0;
  1155. this.scrollerMoveCSS(scrollInfo, scrollInfo.duration, "cubic-bezier(0.33,0.66,0.66,1)");
  1156. this.setVScrollBar(scrollInfo, scrollInfo.duration, "cubic-bezier(0.33,0.66,0.66,1)");
  1157. this.setHScrollBar(scrollInfo, scrollInfo.duration, "cubic-bezier(0.33,0.66,0.66,1)");
  1158. this.setFinishCalback(scrollInfo.duration);
  1159. if (this.infinite && !this.infiniteTriggered) {
  1160. if ((Math.abs(scrollInfo.y) >= (this.el.clientHeight - this.container.clientHeight))) {
  1161. this.infiniteTriggered = true;
  1162. $.trigger(this, "infinite-scroll");
  1163. }
  1164. }
  1165. };
  1166. //finish callback
  1167. jsScroller.prototype.setFinishCalback = function (duration) {
  1168. var that = this;
  1169. this.scrollingFinishCB = setTimeout(function () {
  1170. that.hideScrollbars();
  1171. $.trigger($.touchLayer, 'scrollend', [that.el]); //notify touchLayer of this elements scrollend
  1172. $.trigger(that, "scrollend", [that.el]);
  1173. that.isScrolling = false;
  1174. that.elementInfo = null; //reset elementInfo when idle
  1175. if (that.infinite) $.trigger(that, "infinite-scroll-end");
  1176. }, duration);
  1177. };
  1178. //Android Forms Fix
  1179. jsScroller.prototype.startFormsMode = function () {
  1180. if (this.blockFormsFix) return;
  1181. //get prev values
  1182. var cssMatrix = this.getCSSMatrix(this.el);
  1183. //toggle vars
  1184. this.refreshSafeKeep = this.refresh;
  1185. this.refresh = false;
  1186. this.androidFormsMode = true;
  1187. //set new css rules
  1188. this.el.style[$.feat.cssPrefix + "Transform"] = "none";
  1189. this.el.style[$.feat.cssPrefix + "Transition"] = "none";
  1190. this.el.style[$.feat.cssPrefix + "Perspective"] = "none";
  1191. //set position
  1192. this.scrollerMoveCSS({
  1193. x: numOnly(cssMatrix.e),
  1194. y: numOnly(cssMatrix.f)
  1195. }, 0);
  1196. //container
  1197. this.container.style[$.feat.cssPrefix + "Perspective"] = "none";
  1198. this.container.style[$.feat.cssPrefix + "BackfaceVisibility"] = "visible";
  1199. //scrollbars
  1200. if (this.vscrollBar) {
  1201. this.vscrollBar.style[$.feat.cssPrefix + "Transform"] = "none";
  1202. this.vscrollBar.style[$.feat.cssPrefix + "Transition"] = "none";
  1203. this.vscrollBar.style[$.feat.cssPrefix + "Perspective"] = "none";
  1204. this.vscrollBar.style[$.feat.cssPrefix + "BackfaceVisibility"] = "visible";
  1205. }
  1206. if (this.hscrollBar) {
  1207. this.hscrollBar.style[$.feat.cssPrefix + "Transform"] = "none";
  1208. this.hscrollBar.style[$.feat.cssPrefix + "Transition"] = "none";
  1209. this.hscrollBar.style[$.feat.cssPrefix + "Perspective"] = "none";
  1210. this.hscrollBar.style[$.feat.cssPrefix + "BackfaceVisibility"] = "visible";
  1211. }
  1212. };
  1213. jsScroller.prototype.stopFormsMode = function () {
  1214. if (this.blockFormsFix) return;
  1215. //get prev values
  1216. var cssMatrix = this.getCSSMatrix(this.el);
  1217. //toggle vars
  1218. this.refresh = this.refreshSafeKeep;
  1219. this.androidFormsMode = false;
  1220. //set new css rules
  1221. this.el.style[$.feat.cssPrefix + "Perspective"] = 1000;
  1222. this.el.style.marginTop = 0;
  1223. this.el.style.marginLeft = 0;
  1224. this.el.style[$.feat.cssPrefix + "Transition"] = '0ms linear'; //reactivate transitions
  1225. //set position
  1226. this.scrollerMoveCSS({
  1227. x: numOnly(cssMatrix.e),
  1228. y: numOnly(cssMatrix.f)
  1229. }, 0);
  1230. //container
  1231. this.container.style[$.feat.cssPrefix + "Perspective"] = 1000;
  1232. this.container.style[$.feat.cssPrefix + "BackfaceVisibility"] = "hidden";
  1233. //scrollbars
  1234. if (this.vscrollBar) {
  1235. this.vscrollBar.style[$.feat.cssPrefix + "Perspective"] = 1000;
  1236. this.vscrollBar.style[$.feat.cssPrefix + "BackfaceVisibility"] = "hidden";
  1237. }
  1238. if (this.hscrollBar) {
  1239. this.hscrollBar.style[$.feat.cssPrefix + "Perspective"] = 1000;
  1240. this.hscrollBar.style[$.feat.cssPrefix + "BackfaceVisibility"] = "hidden";
  1241. }
  1242. };
  1243. jsScroller.prototype.scrollerMoveCSS = function (distanceToMove, time, timingFunction) {
  1244. if (!time) time = 0;
  1245. if (!timingFunction) timingFunction = "linear";
  1246. time = numOnly(time);
  1247. if (this.el && this.el.style) {
  1248. //do not touch the DOM if disabled
  1249. if (this.eventsActive) {
  1250. if (this.androidFormsMode) {
  1251. this.el.style.marginTop = Math.round(distanceToMove.y) + "px";
  1252. this.el.style.marginLeft = Math.round(distanceToMove.x) + "px";
  1253. } else {
  1254. this.el.style[$.feat.cssPrefix + "Transform"] = "translate" + translateOpen + distanceToMove.x + "px," + distanceToMove.y + "px" + translateClose;
  1255. this.el.style[$.feat.cssPrefix + "TransitionDuration"] = time + "ms";
  1256. this.el.style[$.feat.cssPrefix + "TransitionTimingFunction"] = timingFunction;
  1257. }
  1258. }
  1259. // Position should be updated even when the scroller is disabled so we log the change
  1260. this.logPos(distanceToMove.x, distanceToMove.y);
  1261. }
  1262. };
  1263. jsScroller.prototype.logPos = function (x, y) {
  1264. var size;
  1265. if (!this.elementInfo) {
  1266. size = this.getViewportSize();
  1267. } else {
  1268. size = {
  1269. h: this.elementInfo.bottomMargin,
  1270. w: this.elementInfo.rightMargin
  1271. };
  1272. }
  1273. this.loggedPcentX = this.divide(x, (this.el.clientWidth - size.w));
  1274. this.loggedPcentY = this.divide(y, (this.el.clientHeight - size.h));
  1275. this.scrollTop = y;
  1276. this.scrollLeft = x;
  1277. };
  1278. jsScroller.prototype.scrollbarMoveCSS = function (el, distanceToMove, time, timingFunction) {
  1279. if (!time) time = 0;
  1280. if (!timingFunction) timingFunction = "linear";
  1281. if (el && el.style) {
  1282. if (this.androidFormsMode) {
  1283. el.style.marginTop = Math.round(distanceToMove.y) + "px";
  1284. el.style.marginLeft = Math.round(distanceToMove.x) + "px";
  1285. } else {
  1286. el.style[$.feat.cssPrefix + "Transform"] = "translate" + translateOpen + distanceToMove.x + "px," + distanceToMove.y + "px" + translateClose;
  1287. el.style[$.feat.cssPrefix + "TransitionDuration"] = time + "ms";
  1288. el.style[$.feat.cssPrefix + "TransitionTimingFunction"] = timingFunction;
  1289. }
  1290. }
  1291. };
  1292. jsScroller.prototype.scrollTo = function (pos, time) {
  1293. if (!time) time = 0;
  1294. this.scrollerMoveCSS(pos, time);
  1295. };
  1296. jsScroller.prototype.scrollBy = function (pos, time) {
  1297. var cssMatrix = this.getCSSMatrix(this.el);
  1298. var startTop = numOnly(cssMatrix.f);
  1299. var startLeft = numOnly(cssMatrix.e);
  1300. this.scrollTo({
  1301. y: startTop - pos.y,
  1302. x: startLeft - pos.x
  1303. }, time);
  1304. };
  1305. jsScroller.prototype.scrollToBottom = function (time) {
  1306. this.scrollTo({
  1307. y: -1 * (this.el.clientHeight - this.container.clientHeight),
  1308. x: 0
  1309. }, time);
  1310. };
  1311. jsScroller.prototype.scrollToTop = function (time) {
  1312. this.scrollTo({
  1313. x: 0,
  1314. y: 0
  1315. }, time);
  1316. };
  1317. return scroller;
  1318. })();
  1319. })(af);