core.esm.js 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  1. /*!
  2. * better-scroll / core
  3. * (c) 2016-2020 ustbhuangyi
  4. * Released under the MIT License.
  5. */
  6. /*! *****************************************************************************
  7. Copyright (c) Microsoft Corporation.
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  11. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  12. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  13. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  14. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  15. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  16. PERFORMANCE OF THIS SOFTWARE.
  17. ***************************************************************************** */
  18. /* global Reflect, Promise */
  19. var extendStatics = function(d, b) {
  20. extendStatics = Object.setPrototypeOf ||
  21. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  22. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  23. return extendStatics(d, b);
  24. };
  25. function __extends(d, b) {
  26. extendStatics(d, b);
  27. function __() { this.constructor = d; }
  28. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  29. }
  30. var __assign = function() {
  31. __assign = Object.assign || function __assign(t) {
  32. for (var s, i = 1, n = arguments.length; i < n; i++) {
  33. s = arguments[i];
  34. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  35. }
  36. return t;
  37. };
  38. return __assign.apply(this, arguments);
  39. };
  40. function __spreadArrays() {
  41. for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  42. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  43. for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  44. r[k] = a[j];
  45. return r;
  46. }
  47. var propertiesConfig = [
  48. {
  49. sourceKey: 'scroller.scrollBehaviorX.currentPos',
  50. key: 'x'
  51. },
  52. {
  53. sourceKey: 'scroller.scrollBehaviorY.currentPos',
  54. key: 'y'
  55. },
  56. {
  57. sourceKey: 'scroller.scrollBehaviorX.hasScroll',
  58. key: 'hasHorizontalScroll'
  59. },
  60. {
  61. sourceKey: 'scroller.scrollBehaviorY.hasScroll',
  62. key: 'hasVerticalScroll'
  63. },
  64. {
  65. sourceKey: 'scroller.scrollBehaviorX.contentSize',
  66. key: 'scrollerWidth'
  67. },
  68. {
  69. sourceKey: 'scroller.scrollBehaviorY.contentSize',
  70. key: 'scrollerHeight'
  71. },
  72. {
  73. sourceKey: 'scroller.scrollBehaviorX.maxScrollPos',
  74. key: 'maxScrollX'
  75. },
  76. {
  77. sourceKey: 'scroller.scrollBehaviorY.maxScrollPos',
  78. key: 'maxScrollY'
  79. },
  80. {
  81. sourceKey: 'scroller.scrollBehaviorX.minScrollPos',
  82. key: 'minScrollX'
  83. },
  84. {
  85. sourceKey: 'scroller.scrollBehaviorY.minScrollPos',
  86. key: 'minScrollY'
  87. },
  88. {
  89. sourceKey: 'scroller.scrollBehaviorX.movingDirection',
  90. key: 'movingDirectionX'
  91. },
  92. {
  93. sourceKey: 'scroller.scrollBehaviorY.movingDirection',
  94. key: 'movingDirectionY'
  95. },
  96. {
  97. sourceKey: 'scroller.scrollBehaviorX.direction',
  98. key: 'directionX'
  99. },
  100. {
  101. sourceKey: 'scroller.scrollBehaviorY.direction',
  102. key: 'directionY'
  103. },
  104. {
  105. sourceKey: 'scroller.actions.enabled',
  106. key: 'enabled'
  107. },
  108. {
  109. sourceKey: 'scroller.animater.pending',
  110. key: 'pending'
  111. },
  112. {
  113. sourceKey: 'scroller.animater.stop',
  114. key: 'stop'
  115. },
  116. {
  117. sourceKey: 'scroller.scrollTo',
  118. key: 'scrollTo'
  119. },
  120. {
  121. sourceKey: 'scroller.scrollBy',
  122. key: 'scrollBy'
  123. },
  124. {
  125. sourceKey: 'scroller.scrollToElement',
  126. key: 'scrollToElement'
  127. },
  128. {
  129. sourceKey: 'scroller.resetPosition',
  130. key: 'resetPosition'
  131. }
  132. ];
  133. function warn(msg) {
  134. console.error("[BScroll warn]: " + msg);
  135. }
  136. // ssr support
  137. var inBrowser = typeof window !== 'undefined';
  138. var ua = inBrowser && navigator.userAgent.toLowerCase();
  139. var isWeChatDevTools = !!(ua && /wechatdevtools/.test(ua));
  140. var isAndroid = ua && ua.indexOf('android') > 0;
  141. var isIOSBadVersion = (function () {
  142. if (typeof ua === 'string') {
  143. var regex = /os (\d\d?_\d(_\d)?)/;
  144. var matches = regex.exec(ua);
  145. if (!matches)
  146. return false;
  147. var parts = matches[1].split('_').map(function (item) {
  148. return parseInt(item, 10);
  149. });
  150. // ios version >= 13.4 issue 982
  151. return !!(parts[0] >= 13 && parts[1] >= 4);
  152. }
  153. return false;
  154. })();
  155. function getNow() {
  156. return window.performance &&
  157. window.performance.now &&
  158. window.performance.timing
  159. ? window.performance.now() + window.performance.timing.navigationStart
  160. : +new Date();
  161. }
  162. var extend = function (target, source) {
  163. for (var key in source) {
  164. target[key] = source[key];
  165. }
  166. return target;
  167. };
  168. function isUndef(v) {
  169. return v === undefined || v === null;
  170. }
  171. var elementStyle = (inBrowser &&
  172. document.createElement('div').style);
  173. var vendor = (function () {
  174. if (!inBrowser) {
  175. return false;
  176. }
  177. var transformNames = [
  178. {
  179. key: 'standard',
  180. value: 'transform',
  181. },
  182. {
  183. key: 'webkit',
  184. value: 'webkitTransform',
  185. },
  186. {
  187. key: 'Moz',
  188. value: 'MozTransform',
  189. },
  190. {
  191. key: 'O',
  192. value: 'OTransform',
  193. },
  194. {
  195. key: 'ms',
  196. value: 'msTransform',
  197. },
  198. ];
  199. for (var _i = 0, transformNames_1 = transformNames; _i < transformNames_1.length; _i++) {
  200. var obj = transformNames_1[_i];
  201. if (elementStyle[obj.value] !== undefined) {
  202. return obj.key;
  203. }
  204. }
  205. return false;
  206. })();
  207. function prefixStyle(style) {
  208. if (vendor === false) {
  209. return style;
  210. }
  211. if (vendor === 'standard') {
  212. if (style === 'transitionEnd') {
  213. return 'transitionend';
  214. }
  215. return style;
  216. }
  217. return vendor + style.charAt(0).toUpperCase() + style.substr(1);
  218. }
  219. function getElement(el) {
  220. return (typeof el === 'string'
  221. ? document.querySelector(el)
  222. : el);
  223. }
  224. function addEvent(el, type, fn, capture) {
  225. el.addEventListener(type, fn, {
  226. passive: false,
  227. capture: !!capture,
  228. });
  229. }
  230. function removeEvent(el, type, fn, capture) {
  231. el.removeEventListener(type, fn, {
  232. capture: !!capture,
  233. });
  234. }
  235. function offset(el) {
  236. var left = 0;
  237. var top = 0;
  238. while (el) {
  239. left -= el.offsetLeft;
  240. top -= el.offsetTop;
  241. el = el.offsetParent;
  242. }
  243. return {
  244. left: left,
  245. top: top,
  246. };
  247. }
  248. var cssVendor = vendor && vendor !== 'standard' ? '-' + vendor.toLowerCase() + '-' : '';
  249. var transform = prefixStyle('transform');
  250. var transition = prefixStyle('transition');
  251. var hasPerspective = inBrowser && prefixStyle('perspective') in elementStyle;
  252. // fix issue #361
  253. var hasTouch = inBrowser && ('ontouchstart' in window || isWeChatDevTools);
  254. var hasTransition = inBrowser && transition in elementStyle;
  255. var style = {
  256. transform: transform,
  257. transition: transition,
  258. transitionTimingFunction: prefixStyle('transitionTimingFunction'),
  259. transitionDuration: prefixStyle('transitionDuration'),
  260. transitionDelay: prefixStyle('transitionDelay'),
  261. transformOrigin: prefixStyle('transformOrigin'),
  262. transitionEnd: prefixStyle('transitionEnd'),
  263. transitionProperty: prefixStyle('transitionProperty'),
  264. };
  265. var eventTypeMap = {
  266. touchstart: 1,
  267. touchmove: 1,
  268. touchend: 1,
  269. mousedown: 2,
  270. mousemove: 2,
  271. mouseup: 2,
  272. };
  273. function getRect(el) {
  274. if (el instanceof window.SVGElement) {
  275. var rect = el.getBoundingClientRect();
  276. return {
  277. top: rect.top,
  278. left: rect.left,
  279. width: rect.width,
  280. height: rect.height,
  281. };
  282. }
  283. else {
  284. return {
  285. top: el.offsetTop,
  286. left: el.offsetLeft,
  287. width: el.offsetWidth,
  288. height: el.offsetHeight,
  289. };
  290. }
  291. }
  292. function preventDefaultExceptionFn(el, exceptions) {
  293. for (var i in exceptions) {
  294. if (exceptions[i].test(el[i])) {
  295. return true;
  296. }
  297. }
  298. return false;
  299. }
  300. var tagExceptionFn = preventDefaultExceptionFn;
  301. function tap(e, eventName) {
  302. var ev = document.createEvent('Event');
  303. ev.initEvent(eventName, true, true);
  304. ev.pageX = e.pageX;
  305. ev.pageY = e.pageY;
  306. e.target.dispatchEvent(ev);
  307. }
  308. function click(e, event) {
  309. if (event === void 0) { event = 'click'; }
  310. var eventSource;
  311. if (e.type === 'mouseup') {
  312. eventSource = e;
  313. }
  314. else if (e.type === 'touchend' || e.type === 'touchcancel') {
  315. eventSource = e.changedTouches[0];
  316. }
  317. var posSrc = {};
  318. if (eventSource) {
  319. posSrc.screenX = eventSource.screenX || 0;
  320. posSrc.screenY = eventSource.screenY || 0;
  321. posSrc.clientX = eventSource.clientX || 0;
  322. posSrc.clientY = eventSource.clientY || 0;
  323. }
  324. var ev;
  325. var bubbles = true;
  326. var cancelable = true;
  327. var ctrlKey = e.ctrlKey, shiftKey = e.shiftKey, altKey = e.altKey, metaKey = e.metaKey;
  328. var pressedKeysMap = {
  329. ctrlKey: ctrlKey,
  330. shiftKey: shiftKey,
  331. altKey: altKey,
  332. metaKey: metaKey,
  333. };
  334. if (typeof MouseEvent !== 'undefined') {
  335. try {
  336. ev = new MouseEvent(event, extend(__assign({ bubbles: bubbles,
  337. cancelable: cancelable }, pressedKeysMap), posSrc));
  338. }
  339. catch (e) {
  340. createEvent();
  341. }
  342. }
  343. else {
  344. createEvent();
  345. }
  346. function createEvent() {
  347. ev = document.createEvent('Event');
  348. ev.initEvent(event, bubbles, cancelable);
  349. extend(ev, posSrc);
  350. }
  351. // forwardedTouchEvent set to true in case of the conflict with fastclick
  352. ev.forwardedTouchEvent = true;
  353. ev._constructed = true;
  354. e.target.dispatchEvent(ev);
  355. }
  356. function dblclick(e) {
  357. click(e, 'dblclick');
  358. }
  359. var ease = {
  360. // easeOutQuint
  361. swipe: {
  362. style: 'cubic-bezier(0.23, 1, 0.32, 1)',
  363. fn: function (t) {
  364. return 1 + --t * t * t * t * t;
  365. }
  366. },
  367. // easeOutQuard
  368. swipeBounce: {
  369. style: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)',
  370. fn: function (t) {
  371. return t * (2 - t);
  372. }
  373. },
  374. // easeOutQuart
  375. bounce: {
  376. style: 'cubic-bezier(0.165, 0.84, 0.44, 1)',
  377. fn: function (t) {
  378. return 1 - --t * t * t * t;
  379. }
  380. }
  381. };
  382. var DEFAULT_INTERVAL = 100 / 60;
  383. var windowCompat = inBrowser && window;
  384. function noop() { }
  385. var requestAnimationFrame = (function () {
  386. if (!inBrowser) {
  387. /* istanbul ignore if */
  388. return noop;
  389. }
  390. return (windowCompat.requestAnimationFrame ||
  391. windowCompat.webkitRequestAnimationFrame ||
  392. windowCompat.mozRequestAnimationFrame ||
  393. windowCompat.oRequestAnimationFrame ||
  394. // if all else fails, use setTimeout
  395. function (callback) {
  396. return window.setTimeout(callback, (callback.interval || DEFAULT_INTERVAL) / 2); // make interval as precise as possible.
  397. });
  398. })();
  399. var cancelAnimationFrame = (function () {
  400. if (!inBrowser) {
  401. /* istanbul ignore if */
  402. return noop;
  403. }
  404. return (windowCompat.cancelAnimationFrame ||
  405. windowCompat.webkitCancelAnimationFrame ||
  406. windowCompat.mozCancelAnimationFrame ||
  407. windowCompat.oCancelAnimationFrame ||
  408. function (id) {
  409. window.clearTimeout(id);
  410. });
  411. })();
  412. var noop$1 = function (val) { };
  413. var sharedPropertyDefinition = {
  414. enumerable: true,
  415. configurable: true,
  416. get: noop$1,
  417. set: noop$1
  418. };
  419. var getProperty = function (obj, key) {
  420. var keys = key.split('.');
  421. for (var i = 0; i < keys.length - 1; i++) {
  422. obj = obj[keys[i]];
  423. if (typeof obj !== 'object' || !obj)
  424. return;
  425. }
  426. var lastKey = keys.pop();
  427. if (typeof obj[lastKey] === 'function') {
  428. return function () {
  429. return obj[lastKey].apply(obj, arguments);
  430. };
  431. }
  432. else {
  433. return obj[lastKey];
  434. }
  435. };
  436. var setProperty = function (obj, key, value) {
  437. var keys = key.split('.');
  438. var temp;
  439. for (var i = 0; i < keys.length - 1; i++) {
  440. temp = keys[i];
  441. if (!obj[temp])
  442. obj[temp] = {};
  443. obj = obj[temp];
  444. }
  445. obj[keys.pop()] = value;
  446. };
  447. function propertiesProxy(target, sourceKey, key) {
  448. sharedPropertyDefinition.get = function proxyGetter() {
  449. return getProperty(this, sourceKey);
  450. };
  451. sharedPropertyDefinition.set = function proxySetter(val) {
  452. setProperty(this, sourceKey, val);
  453. };
  454. Object.defineProperty(target, key, sharedPropertyDefinition);
  455. }
  456. var EventEmitter = /** @class */ (function () {
  457. function EventEmitter(names) {
  458. this.events = {};
  459. this.eventTypes = {};
  460. this.registerType(names);
  461. }
  462. EventEmitter.prototype.on = function (type, fn, context) {
  463. if (context === void 0) { context = this; }
  464. this.hasType(type);
  465. if (!this.events[type]) {
  466. this.events[type] = [];
  467. }
  468. this.events[type].push([fn, context]);
  469. return this;
  470. };
  471. EventEmitter.prototype.once = function (type, fn, context) {
  472. var _this = this;
  473. if (context === void 0) { context = this; }
  474. this.hasType(type);
  475. var magic = function () {
  476. var args = [];
  477. for (var _i = 0; _i < arguments.length; _i++) {
  478. args[_i] = arguments[_i];
  479. }
  480. _this.off(type, magic);
  481. fn.apply(context, args);
  482. };
  483. magic.fn = fn;
  484. this.on(type, magic);
  485. return this;
  486. };
  487. EventEmitter.prototype.off = function (type, fn) {
  488. if (!type && !fn) {
  489. this.events = {};
  490. return this;
  491. }
  492. if (type) {
  493. this.hasType(type);
  494. if (!fn) {
  495. this.events[type] = [];
  496. return this;
  497. }
  498. var events = this.events[type];
  499. if (!events) {
  500. return this;
  501. }
  502. var count = events.length;
  503. while (count--) {
  504. if (events[count][0] === fn ||
  505. (events[count][0] && events[count][0].fn === fn)) {
  506. events.splice(count, 1);
  507. }
  508. }
  509. return this;
  510. }
  511. };
  512. EventEmitter.prototype.trigger = function (type) {
  513. var args = [];
  514. for (var _i = 1; _i < arguments.length; _i++) {
  515. args[_i - 1] = arguments[_i];
  516. }
  517. this.hasType(type);
  518. var events = this.events[type];
  519. if (!events) {
  520. return;
  521. }
  522. var len = events.length;
  523. var eventsCopy = __spreadArrays(events);
  524. var ret;
  525. for (var i = 0; i < len; i++) {
  526. var event_1 = eventsCopy[i];
  527. var fn = event_1[0], context = event_1[1];
  528. if (fn) {
  529. ret = fn.apply(context, args);
  530. if (ret === true) {
  531. return ret;
  532. }
  533. }
  534. }
  535. };
  536. EventEmitter.prototype.registerType = function (names) {
  537. var _this = this;
  538. names.forEach(function (type) {
  539. _this.eventTypes[type] = type;
  540. });
  541. };
  542. EventEmitter.prototype.destroy = function () {
  543. this.events = {};
  544. this.eventTypes = {};
  545. };
  546. EventEmitter.prototype.hasType = function (type) {
  547. var types = this.eventTypes;
  548. var isType = types[type] === type;
  549. if (!isType) {
  550. warn("EventEmitter has used unknown event type: \"" + type + "\", should be oneof [" +
  551. ("" + Object.keys(types).map(function (_) { return JSON.stringify(_); })) +
  552. "]");
  553. }
  554. };
  555. return EventEmitter;
  556. }());
  557. var EventRegister = /** @class */ (function () {
  558. function EventRegister(wrapper, events) {
  559. this.wrapper = wrapper;
  560. this.events = events;
  561. this.addDOMEvents();
  562. }
  563. EventRegister.prototype.destroy = function () {
  564. this.removeDOMEvents();
  565. this.events = [];
  566. };
  567. EventRegister.prototype.addDOMEvents = function () {
  568. this.handleDOMEvents(addEvent);
  569. };
  570. EventRegister.prototype.removeDOMEvents = function () {
  571. this.handleDOMEvents(removeEvent);
  572. };
  573. EventRegister.prototype.handleDOMEvents = function (eventOperation) {
  574. var _this = this;
  575. var wrapper = this.wrapper;
  576. this.events.forEach(function (event) {
  577. eventOperation(wrapper, event.name, _this, !!event.capture);
  578. });
  579. };
  580. EventRegister.prototype.handleEvent = function (e) {
  581. var eventType = e.type;
  582. this.events.some(function (event) {
  583. if (event.name === eventType) {
  584. event.handler(e);
  585. return true;
  586. }
  587. return false;
  588. });
  589. };
  590. return EventRegister;
  591. }());
  592. var CustomOptions = /** @class */ (function () {
  593. function CustomOptions() {
  594. }
  595. return CustomOptions;
  596. }());
  597. var OptionsConstructor = /** @class */ (function (_super) {
  598. __extends(OptionsConstructor, _super);
  599. function OptionsConstructor() {
  600. var _this = _super.call(this) || this;
  601. _this.startX = 0;
  602. _this.startY = 0;
  603. _this.scrollX = false;
  604. _this.scrollY = true;
  605. _this.freeScroll = false;
  606. _this.directionLockThreshold = 5;
  607. _this.eventPassthrough = "" /* None */;
  608. _this.click = false;
  609. _this.dblclick = false;
  610. _this.tap = '';
  611. _this.bounce = {
  612. top: true,
  613. bottom: true,
  614. left: true,
  615. right: true,
  616. };
  617. _this.bounceTime = 800;
  618. _this.momentum = true;
  619. _this.momentumLimitTime = 300;
  620. _this.momentumLimitDistance = 15;
  621. _this.swipeTime = 2500;
  622. _this.swipeBounceTime = 500;
  623. _this.deceleration = 0.0015;
  624. _this.flickLimitTime = 200;
  625. _this.flickLimitDistance = 100;
  626. _this.resizePolling = 60;
  627. _this.probeType = 0 /* Default */;
  628. _this.stopPropagation = false;
  629. _this.preventDefault = true;
  630. _this.preventDefaultException = {
  631. tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT|AUDIO)$/,
  632. };
  633. _this.tagException = {
  634. tagName: /^TEXTAREA$/,
  635. };
  636. _this.HWCompositing = true;
  637. _this.useTransition = true;
  638. _this.bindToWrapper = false;
  639. _this.bindToTarget = false;
  640. _this.disableMouse = hasTouch;
  641. _this.disableTouch = !hasTouch;
  642. _this.autoBlur = true;
  643. _this.autoEndDistance = 5;
  644. _this.outOfBoundaryDampingFactor = 1 / 3;
  645. _this.specifiedIndexAsContent = 0;
  646. return _this;
  647. }
  648. OptionsConstructor.prototype.merge = function (options) {
  649. if (!options)
  650. return this;
  651. for (var key in options) {
  652. if (key === 'bounce') {
  653. this.bounce = this.resolveBounce(options[key]);
  654. continue;
  655. }
  656. this[key] = options[key];
  657. }
  658. return this;
  659. };
  660. OptionsConstructor.prototype.process = function () {
  661. this.translateZ =
  662. this.HWCompositing && hasPerspective ? ' translateZ(1px)' : '';
  663. this.useTransition = this.useTransition && hasTransition;
  664. this.preventDefault = !this.eventPassthrough && this.preventDefault;
  665. // If you want eventPassthrough I have to lock one of the axes
  666. this.scrollX =
  667. this.eventPassthrough === "horizontal" /* Horizontal */
  668. ? false
  669. : this.scrollX;
  670. this.scrollY =
  671. this.eventPassthrough === "vertical" /* Vertical */ ? false : this.scrollY;
  672. // With eventPassthrough we also need lockDirection mechanism
  673. this.freeScroll = this.freeScroll && !this.eventPassthrough;
  674. // force true when freeScroll is true
  675. this.scrollX = this.freeScroll ? true : this.scrollX;
  676. this.scrollY = this.freeScroll ? true : this.scrollY;
  677. this.directionLockThreshold = this.eventPassthrough
  678. ? 0
  679. : this.directionLockThreshold;
  680. return this;
  681. };
  682. OptionsConstructor.prototype.resolveBounce = function (bounceOptions) {
  683. var DEFAULT_BOUNCE = {
  684. top: true,
  685. right: true,
  686. bottom: true,
  687. left: true,
  688. };
  689. var NEGATED_BOUNCE = {
  690. top: false,
  691. right: false,
  692. bottom: false,
  693. left: false,
  694. };
  695. var ret;
  696. if (typeof bounceOptions === 'object') {
  697. ret = extend(DEFAULT_BOUNCE, bounceOptions);
  698. }
  699. else {
  700. ret = bounceOptions ? DEFAULT_BOUNCE : NEGATED_BOUNCE;
  701. }
  702. return ret;
  703. };
  704. return OptionsConstructor;
  705. }(CustomOptions));
  706. var ActionsHandler = /** @class */ (function () {
  707. function ActionsHandler(wrapper, options) {
  708. this.wrapper = wrapper;
  709. this.options = options;
  710. this.hooks = new EventEmitter([
  711. 'beforeStart',
  712. 'start',
  713. 'move',
  714. 'end',
  715. 'click',
  716. ]);
  717. this.handleDOMEvents();
  718. }
  719. ActionsHandler.prototype.handleDOMEvents = function () {
  720. var _a = this.options, bindToWrapper = _a.bindToWrapper, disableMouse = _a.disableMouse, disableTouch = _a.disableTouch, click = _a.click;
  721. var wrapper = this.wrapper;
  722. var target = bindToWrapper ? wrapper : window;
  723. var wrapperEvents = [];
  724. var targetEvents = [];
  725. var shouldRegisterTouch = !disableTouch;
  726. var shouldRegisterMouse = !disableMouse;
  727. if (click) {
  728. wrapperEvents.push({
  729. name: 'click',
  730. handler: this.click.bind(this),
  731. capture: true,
  732. });
  733. }
  734. if (shouldRegisterTouch) {
  735. wrapperEvents.push({
  736. name: 'touchstart',
  737. handler: this.start.bind(this),
  738. });
  739. targetEvents.push({
  740. name: 'touchmove',
  741. handler: this.move.bind(this),
  742. }, {
  743. name: 'touchend',
  744. handler: this.end.bind(this),
  745. }, {
  746. name: 'touchcancel',
  747. handler: this.end.bind(this),
  748. });
  749. }
  750. if (shouldRegisterMouse) {
  751. wrapperEvents.push({
  752. name: 'mousedown',
  753. handler: this.start.bind(this),
  754. });
  755. targetEvents.push({
  756. name: 'mousemove',
  757. handler: this.move.bind(this),
  758. }, {
  759. name: 'mouseup',
  760. handler: this.end.bind(this),
  761. });
  762. }
  763. this.wrapperEventRegister = new EventRegister(wrapper, wrapperEvents);
  764. this.targetEventRegister = new EventRegister(target, targetEvents);
  765. };
  766. ActionsHandler.prototype.beforeHandler = function (e, type) {
  767. var _a = this.options, preventDefault = _a.preventDefault, stopPropagation = _a.stopPropagation, preventDefaultException = _a.preventDefaultException;
  768. var preventDefaultConditions = {
  769. start: function () {
  770. return (preventDefault &&
  771. !preventDefaultExceptionFn(e.target, preventDefaultException));
  772. },
  773. end: function () {
  774. return (preventDefault &&
  775. !preventDefaultExceptionFn(e.target, preventDefaultException));
  776. },
  777. move: function () {
  778. return preventDefault;
  779. },
  780. };
  781. if (preventDefaultConditions[type]()) {
  782. e.preventDefault();
  783. }
  784. if (stopPropagation) {
  785. e.stopPropagation();
  786. }
  787. };
  788. ActionsHandler.prototype.setInitiated = function (type) {
  789. if (type === void 0) { type = 0; }
  790. this.initiated = type;
  791. };
  792. ActionsHandler.prototype.start = function (e) {
  793. var _eventType = eventTypeMap[e.type];
  794. if (this.initiated && this.initiated !== _eventType) {
  795. return;
  796. }
  797. this.setInitiated(_eventType);
  798. // if textarea or other html tags in options.tagException is manipulated
  799. // do not make bs scroll
  800. if (tagExceptionFn(e.target, this.options.tagException)) {
  801. this.setInitiated();
  802. return;
  803. }
  804. // only allow mouse left button
  805. if (_eventType === 2 /* Mouse */ && e.button !== 0 /* Left */)
  806. return;
  807. if (this.hooks.trigger(this.hooks.eventTypes.beforeStart, e)) {
  808. return;
  809. }
  810. this.beforeHandler(e, 'start');
  811. var point = (e.touches ? e.touches[0] : e);
  812. this.pointX = point.pageX;
  813. this.pointY = point.pageY;
  814. this.hooks.trigger(this.hooks.eventTypes.start, e);
  815. };
  816. ActionsHandler.prototype.move = function (e) {
  817. if (eventTypeMap[e.type] !== this.initiated) {
  818. return;
  819. }
  820. this.beforeHandler(e, 'move');
  821. var point = (e.touches ? e.touches[0] : e);
  822. var deltaX = point.pageX - this.pointX;
  823. var deltaY = point.pageY - this.pointY;
  824. this.pointX = point.pageX;
  825. this.pointY = point.pageY;
  826. if (this.hooks.trigger(this.hooks.eventTypes.move, {
  827. deltaX: deltaX,
  828. deltaY: deltaY,
  829. e: e,
  830. })) {
  831. return;
  832. }
  833. // auto end when out of viewport
  834. var scrollLeft = document.documentElement.scrollLeft ||
  835. window.pageXOffset ||
  836. document.body.scrollLeft;
  837. var scrollTop = document.documentElement.scrollTop ||
  838. window.pageYOffset ||
  839. document.body.scrollTop;
  840. var pX = this.pointX - scrollLeft;
  841. var pY = this.pointY - scrollTop;
  842. var autoEndDistance = this.options.autoEndDistance;
  843. if (pX > document.documentElement.clientWidth - autoEndDistance ||
  844. pY > document.documentElement.clientHeight - autoEndDistance ||
  845. pX < autoEndDistance ||
  846. pY < autoEndDistance) {
  847. this.end(e);
  848. }
  849. };
  850. ActionsHandler.prototype.end = function (e) {
  851. if (eventTypeMap[e.type] !== this.initiated) {
  852. return;
  853. }
  854. this.setInitiated();
  855. this.beforeHandler(e, 'end');
  856. this.hooks.trigger(this.hooks.eventTypes.end, e);
  857. };
  858. ActionsHandler.prototype.click = function (e) {
  859. this.hooks.trigger(this.hooks.eventTypes.click, e);
  860. };
  861. ActionsHandler.prototype.setContent = function (content) {
  862. if (content !== this.wrapper) {
  863. this.wrapper = content;
  864. this.wrapperEventRegister.destroy();
  865. this.targetEventRegister.destroy();
  866. this.handleDOMEvents();
  867. }
  868. };
  869. ActionsHandler.prototype.destroy = function () {
  870. this.wrapperEventRegister.destroy();
  871. this.targetEventRegister.destroy();
  872. this.hooks.destroy();
  873. };
  874. return ActionsHandler;
  875. }());
  876. var translaterMetaData = {
  877. x: ['translateX', 'px'],
  878. y: ['translateY', 'px'],
  879. };
  880. var Translater = /** @class */ (function () {
  881. function Translater(content) {
  882. this.setContent(content);
  883. this.hooks = new EventEmitter(['beforeTranslate', 'translate']);
  884. }
  885. Translater.prototype.getComputedPosition = function () {
  886. var cssStyle = window.getComputedStyle(this.content, null);
  887. var matrix = cssStyle[style.transform].split(')')[0].split(', ');
  888. var x = +(matrix[12] || matrix[4]) || 0;
  889. var y = +(matrix[13] || matrix[5]) || 0;
  890. return {
  891. x: x,
  892. y: y,
  893. };
  894. };
  895. Translater.prototype.translate = function (point) {
  896. var transformStyle = [];
  897. Object.keys(point).forEach(function (key) {
  898. if (!translaterMetaData[key]) {
  899. return;
  900. }
  901. var transformFnName = translaterMetaData[key][0];
  902. if (transformFnName) {
  903. var transformFnArgUnit = translaterMetaData[key][1];
  904. var transformFnArg = point[key];
  905. transformStyle.push(transformFnName + "(" + transformFnArg + transformFnArgUnit + ")");
  906. }
  907. });
  908. this.hooks.trigger(this.hooks.eventTypes.beforeTranslate, transformStyle, point);
  909. this.style[style.transform] = transformStyle.join(' ');
  910. this.hooks.trigger(this.hooks.eventTypes.translate, point);
  911. };
  912. Translater.prototype.setContent = function (content) {
  913. if (this.content !== content) {
  914. this.content = content;
  915. this.style = content.style;
  916. }
  917. };
  918. Translater.prototype.destroy = function () {
  919. this.hooks.destroy();
  920. };
  921. return Translater;
  922. }());
  923. var Base = /** @class */ (function () {
  924. function Base(content, translater, options) {
  925. this.translater = translater;
  926. this.options = options;
  927. this.timer = 0;
  928. this.hooks = new EventEmitter([
  929. 'move',
  930. 'end',
  931. 'beforeForceStop',
  932. 'forceStop',
  933. 'callStop',
  934. 'time',
  935. 'timeFunction',
  936. ]);
  937. this.setContent(content);
  938. }
  939. Base.prototype.translate = function (endPoint) {
  940. this.translater.translate(endPoint);
  941. };
  942. Base.prototype.setPending = function (pending) {
  943. this.pending = pending;
  944. };
  945. Base.prototype.setForceStopped = function (forceStopped) {
  946. this.forceStopped = forceStopped;
  947. };
  948. Base.prototype.setContent = function (content) {
  949. if (this.content !== content) {
  950. this.content = content;
  951. this.style = content.style;
  952. this.stop();
  953. }
  954. };
  955. Base.prototype.destroy = function () {
  956. this.hooks.destroy();
  957. cancelAnimationFrame(this.timer);
  958. };
  959. return Base;
  960. }());
  961. var Transition = /** @class */ (function (_super) {
  962. __extends(Transition, _super);
  963. function Transition() {
  964. return _super !== null && _super.apply(this, arguments) || this;
  965. }
  966. Transition.prototype.startProbe = function () {
  967. var _this = this;
  968. var probe = function () {
  969. var pos = _this.translater.getComputedPosition();
  970. _this.hooks.trigger(_this.hooks.eventTypes.move, pos);
  971. // transition ends should dispatch end hook.
  972. // but when call stop() in animation.hooks.move or bs.scroll
  973. // should not dispatch end hook, because forceStop hook will do this.
  974. if (!_this.pending && !_this.forceStopped) {
  975. _this.hooks.trigger(_this.hooks.eventTypes.end, pos);
  976. }
  977. if (_this.pending) {
  978. _this.timer = requestAnimationFrame(probe);
  979. }
  980. };
  981. cancelAnimationFrame(this.timer);
  982. probe();
  983. };
  984. Transition.prototype.transitionTime = function (time) {
  985. if (time === void 0) { time = 0; }
  986. this.style[style.transitionDuration] = time + 'ms';
  987. this.hooks.trigger(this.hooks.eventTypes.time, time);
  988. };
  989. Transition.prototype.transitionTimingFunction = function (easing) {
  990. this.style[style.transitionTimingFunction] = easing;
  991. this.hooks.trigger(this.hooks.eventTypes.timeFunction, easing);
  992. };
  993. Transition.prototype.transitionProperty = function () {
  994. this.style[style.transitionProperty] = style.transform;
  995. };
  996. Transition.prototype.move = function (startPoint, endPoint, time, easingFn) {
  997. this.setPending(time > 0);
  998. this.transitionTimingFunction(easingFn);
  999. this.transitionProperty();
  1000. this.transitionTime(time);
  1001. this.translate(endPoint);
  1002. if (time && this.options.probeType === 3 /* Realtime */) {
  1003. this.startProbe();
  1004. }
  1005. // if we change content's transformY in a tick
  1006. // such as: 0 -> 50px -> 0
  1007. // transitionend will not be triggered
  1008. // so we forceupdate by reflow
  1009. if (!time) {
  1010. this._reflow = this.content.offsetHeight;
  1011. this.hooks.trigger(this.hooks.eventTypes.move, endPoint);
  1012. this.hooks.trigger(this.hooks.eventTypes.end, endPoint);
  1013. }
  1014. };
  1015. Transition.prototype.doStop = function () {
  1016. var pending = this.pending;
  1017. this.setForceStopped(false);
  1018. // still in transition
  1019. if (pending) {
  1020. this.setPending(false);
  1021. cancelAnimationFrame(this.timer);
  1022. var _a = this.translater.getComputedPosition(), x = _a.x, y = _a.y;
  1023. this.transitionTime();
  1024. this.translate({ x: x, y: y });
  1025. this.setForceStopped(true);
  1026. if (this.hooks.trigger(this.hooks.eventTypes.beforeForceStop, { x: x, y: y })) {
  1027. return true;
  1028. }
  1029. this.hooks.trigger(this.hooks.eventTypes.forceStop, { x: x, y: y });
  1030. }
  1031. return pending;
  1032. };
  1033. Transition.prototype.stop = function () {
  1034. var stopFromTransition = this.doStop();
  1035. if (stopFromTransition) {
  1036. this.hooks.trigger(this.hooks.eventTypes.callStop);
  1037. this.setForceStopped(false);
  1038. }
  1039. };
  1040. return Transition;
  1041. }(Base));
  1042. var Animation = /** @class */ (function (_super) {
  1043. __extends(Animation, _super);
  1044. function Animation() {
  1045. return _super !== null && _super.apply(this, arguments) || this;
  1046. }
  1047. Animation.prototype.move = function (startPoint, endPoint, time, easingFn) {
  1048. // time is 0
  1049. if (!time) {
  1050. this.translate(endPoint);
  1051. this.hooks.trigger(this.hooks.eventTypes.move, endPoint);
  1052. this.hooks.trigger(this.hooks.eventTypes.end, endPoint);
  1053. return;
  1054. }
  1055. this.animate(startPoint, endPoint, time, easingFn);
  1056. };
  1057. Animation.prototype.animate = function (startPoint, endPoint, duration, easingFn) {
  1058. var _this = this;
  1059. var startTime = getNow();
  1060. var destTime = startTime + duration;
  1061. var step = function () {
  1062. var now = getNow();
  1063. // js animation end
  1064. if (now >= destTime) {
  1065. _this.translate(endPoint);
  1066. _this.hooks.trigger(_this.hooks.eventTypes.move, endPoint);
  1067. _this.hooks.trigger(_this.hooks.eventTypes.end, endPoint);
  1068. return;
  1069. }
  1070. now = (now - startTime) / duration;
  1071. var easing = easingFn(now);
  1072. var newPoint = {};
  1073. Object.keys(endPoint).forEach(function (key) {
  1074. var startValue = startPoint[key];
  1075. var endValue = endPoint[key];
  1076. newPoint[key] = (endValue - startValue) * easing + startValue;
  1077. });
  1078. _this.translate(newPoint);
  1079. if (_this.options.probeType === 3 /* Realtime */) {
  1080. _this.hooks.trigger(_this.hooks.eventTypes.move, newPoint);
  1081. }
  1082. if (_this.pending) {
  1083. _this.timer = requestAnimationFrame(step);
  1084. }
  1085. // when call stop() in animation.hooks.move or bs.scroll
  1086. // should not dispatch end hook, because forceStop hook will do this.
  1087. if (!_this.pending && !_this.forceStopped) {
  1088. _this.hooks.trigger(_this.hooks.eventTypes.end, endPoint);
  1089. }
  1090. };
  1091. this.setPending(true);
  1092. cancelAnimationFrame(this.timer);
  1093. step();
  1094. };
  1095. Animation.prototype.doStop = function () {
  1096. var pending = this.pending;
  1097. this.setForceStopped(false);
  1098. // still in requestFrameAnimation
  1099. if (pending) {
  1100. this.setPending(false);
  1101. cancelAnimationFrame(this.timer);
  1102. var pos = this.translater.getComputedPosition();
  1103. this.setForceStopped(true);
  1104. if (this.hooks.trigger(this.hooks.eventTypes.beforeForceStop, pos)) {
  1105. return true;
  1106. }
  1107. this.hooks.trigger(this.hooks.eventTypes.forceStop, pos);
  1108. }
  1109. return pending;
  1110. };
  1111. Animation.prototype.stop = function () {
  1112. var stopFromAnimation = this.doStop();
  1113. if (stopFromAnimation) {
  1114. this.hooks.trigger(this.hooks.eventTypes.callStop);
  1115. this.setForceStopped(false);
  1116. }
  1117. };
  1118. return Animation;
  1119. }(Base));
  1120. function createAnimater(element, translater, options) {
  1121. var useTransition = options.useTransition;
  1122. var animaterOptions = {};
  1123. Object.defineProperty(animaterOptions, 'probeType', {
  1124. enumerable: true,
  1125. configurable: false,
  1126. get: function () {
  1127. return options.probeType;
  1128. }
  1129. });
  1130. if (useTransition) {
  1131. return new Transition(element, translater, animaterOptions);
  1132. }
  1133. else {
  1134. return new Animation(element, translater, animaterOptions);
  1135. }
  1136. }
  1137. var Behavior = /** @class */ (function () {
  1138. function Behavior(wrapper, content, options) {
  1139. this.wrapper = wrapper;
  1140. this.options = options;
  1141. this.hooks = new EventEmitter([
  1142. 'beforeComputeBoundary',
  1143. 'computeBoundary',
  1144. 'momentum',
  1145. 'end',
  1146. 'ignoreHasScroll',
  1147. ]);
  1148. this.refresh(content);
  1149. }
  1150. Behavior.prototype.start = function () {
  1151. this.dist = 0;
  1152. this.setMovingDirection(0 /* Default */);
  1153. this.setDirection(0 /* Default */);
  1154. };
  1155. Behavior.prototype.move = function (delta) {
  1156. delta = this.hasScroll ? delta : 0;
  1157. this.setMovingDirection(delta);
  1158. return this.performDampingAlgorithm(delta, this.options.outOfBoundaryDampingFactor);
  1159. };
  1160. Behavior.prototype.setMovingDirection = function (delta) {
  1161. this.movingDirection =
  1162. delta > 0
  1163. ? -1 /* Negative */
  1164. : delta < 0
  1165. ? 1 /* Positive */
  1166. : 0 /* Default */;
  1167. };
  1168. Behavior.prototype.setDirection = function (delta) {
  1169. this.direction =
  1170. delta > 0
  1171. ? -1 /* Negative */
  1172. : delta < 0
  1173. ? 1 /* Positive */
  1174. : 0 /* Default */;
  1175. };
  1176. Behavior.prototype.performDampingAlgorithm = function (delta, dampingFactor) {
  1177. var newPos = this.currentPos + delta;
  1178. // Slow down or stop if outside of the boundaries
  1179. if (newPos > this.minScrollPos || newPos < this.maxScrollPos) {
  1180. if ((newPos > this.minScrollPos && this.options.bounces[0]) ||
  1181. (newPos < this.maxScrollPos && this.options.bounces[1])) {
  1182. newPos = this.currentPos + delta * dampingFactor;
  1183. }
  1184. else {
  1185. newPos =
  1186. newPos > this.minScrollPos ? this.minScrollPos : this.maxScrollPos;
  1187. }
  1188. }
  1189. return newPos;
  1190. };
  1191. Behavior.prototype.end = function (duration) {
  1192. var momentumInfo = {
  1193. duration: 0,
  1194. };
  1195. var absDist = Math.abs(this.currentPos - this.startPos);
  1196. // start momentum animation if needed
  1197. if (this.options.momentum &&
  1198. duration < this.options.momentumLimitTime &&
  1199. absDist > this.options.momentumLimitDistance) {
  1200. var wrapperSize = (this.direction === -1 /* Negative */ && this.options.bounces[0]) ||
  1201. (this.direction === 1 /* Positive */ && this.options.bounces[1])
  1202. ? this.wrapperSize
  1203. : 0;
  1204. momentumInfo = this.hasScroll
  1205. ? this.momentum(this.currentPos, this.startPos, duration, this.maxScrollPos, this.minScrollPos, wrapperSize, this.options)
  1206. : { destination: this.currentPos, duration: 0 };
  1207. }
  1208. else {
  1209. this.hooks.trigger(this.hooks.eventTypes.end, momentumInfo);
  1210. }
  1211. return momentumInfo;
  1212. };
  1213. Behavior.prototype.momentum = function (current, start, time, lowerMargin, upperMargin, wrapperSize, options) {
  1214. if (options === void 0) { options = this.options; }
  1215. var distance = current - start;
  1216. var speed = Math.abs(distance) / time;
  1217. var deceleration = options.deceleration, swipeBounceTime = options.swipeBounceTime, swipeTime = options.swipeTime;
  1218. var momentumData = {
  1219. destination: current + (speed / deceleration) * (distance < 0 ? -1 : 1),
  1220. duration: swipeTime,
  1221. rate: 15,
  1222. };
  1223. this.hooks.trigger(this.hooks.eventTypes.momentum, momentumData, distance);
  1224. if (momentumData.destination < lowerMargin) {
  1225. momentumData.destination = wrapperSize
  1226. ? Math.max(lowerMargin - wrapperSize / 4, lowerMargin - (wrapperSize / momentumData.rate) * speed)
  1227. : lowerMargin;
  1228. momentumData.duration = swipeBounceTime;
  1229. }
  1230. else if (momentumData.destination > upperMargin) {
  1231. momentumData.destination = wrapperSize
  1232. ? Math.min(upperMargin + wrapperSize / 4, upperMargin + (wrapperSize / momentumData.rate) * speed)
  1233. : upperMargin;
  1234. momentumData.duration = swipeBounceTime;
  1235. }
  1236. momentumData.destination = Math.round(momentumData.destination);
  1237. return momentumData;
  1238. };
  1239. Behavior.prototype.updateDirection = function () {
  1240. var absDist = Math.round(this.currentPos) - this.absStartPos;
  1241. this.setDirection(absDist);
  1242. };
  1243. Behavior.prototype.refresh = function (content) {
  1244. var _a = this.options.rect, size = _a.size, position = _a.position;
  1245. var isWrapperStatic = window.getComputedStyle(this.wrapper, null).position === 'static';
  1246. var wrapperRect = getRect(this.wrapper);
  1247. this.wrapperSize = wrapperRect[size];
  1248. this.setContent(content);
  1249. var contentRect = getRect(this.content);
  1250. this.contentSize = contentRect[size];
  1251. this.relativeOffset = contentRect[position];
  1252. if (isWrapperStatic) {
  1253. this.relativeOffset -= wrapperRect[position];
  1254. }
  1255. this.computeBoundary();
  1256. this.setDirection(0 /* Default */);
  1257. };
  1258. Behavior.prototype.setContent = function (content) {
  1259. if (content !== this.content) {
  1260. this.content = content;
  1261. this.resetState();
  1262. }
  1263. };
  1264. Behavior.prototype.resetState = function () {
  1265. this.currentPos = 0;
  1266. this.startPos = 0;
  1267. this.dist = 0;
  1268. this.setDirection(0 /* Default */);
  1269. this.setMovingDirection(0 /* Default */);
  1270. this.resetStartPos();
  1271. };
  1272. Behavior.prototype.computeBoundary = function () {
  1273. this.hooks.trigger(this.hooks.eventTypes.beforeComputeBoundary);
  1274. var boundary = {
  1275. minScrollPos: 0,
  1276. maxScrollPos: this.wrapperSize - this.contentSize,
  1277. };
  1278. if (boundary.maxScrollPos < 0) {
  1279. boundary.maxScrollPos -= this.relativeOffset;
  1280. if (this.options.specifiedIndexAsContent === 0) {
  1281. boundary.minScrollPos = -this.relativeOffset;
  1282. }
  1283. }
  1284. this.hooks.trigger(this.hooks.eventTypes.computeBoundary, boundary);
  1285. this.minScrollPos = boundary.minScrollPos;
  1286. this.maxScrollPos = boundary.maxScrollPos;
  1287. this.hasScroll =
  1288. this.options.scrollable && this.maxScrollPos < this.minScrollPos;
  1289. if (!this.hasScroll && this.minScrollPos < this.maxScrollPos) {
  1290. this.maxScrollPos = this.minScrollPos;
  1291. this.contentSize = this.wrapperSize;
  1292. }
  1293. };
  1294. Behavior.prototype.updatePosition = function (pos) {
  1295. this.currentPos = pos;
  1296. };
  1297. Behavior.prototype.getCurrentPos = function () {
  1298. return Math.round(this.currentPos);
  1299. };
  1300. Behavior.prototype.checkInBoundary = function () {
  1301. var position = this.adjustPosition(this.currentPos);
  1302. var inBoundary = position === this.getCurrentPos();
  1303. return {
  1304. position: position,
  1305. inBoundary: inBoundary,
  1306. };
  1307. };
  1308. // adjust position when out of boundary
  1309. Behavior.prototype.adjustPosition = function (pos) {
  1310. var roundPos = Math.round(pos);
  1311. if (!this.hasScroll &&
  1312. !this.hooks.trigger(this.hooks.eventTypes.ignoreHasScroll)) {
  1313. roundPos = this.minScrollPos;
  1314. }
  1315. else if (roundPos > this.minScrollPos) {
  1316. roundPos = this.minScrollPos;
  1317. }
  1318. else if (roundPos < this.maxScrollPos) {
  1319. roundPos = this.maxScrollPos;
  1320. }
  1321. return roundPos;
  1322. };
  1323. Behavior.prototype.updateStartPos = function () {
  1324. this.startPos = this.currentPos;
  1325. };
  1326. Behavior.prototype.updateAbsStartPos = function () {
  1327. this.absStartPos = this.currentPos;
  1328. };
  1329. Behavior.prototype.resetStartPos = function () {
  1330. this.updateStartPos();
  1331. this.updateAbsStartPos();
  1332. };
  1333. Behavior.prototype.getAbsDist = function (delta) {
  1334. this.dist += delta;
  1335. return Math.abs(this.dist);
  1336. };
  1337. Behavior.prototype.destroy = function () {
  1338. this.hooks.destroy();
  1339. };
  1340. return Behavior;
  1341. }());
  1342. var _a, _b, _c, _d;
  1343. var PassthroughHandlers = (_a = {},
  1344. _a["yes" /* Yes */] = function (e) {
  1345. return true;
  1346. },
  1347. _a["no" /* No */] = function (e) {
  1348. e.preventDefault();
  1349. return false;
  1350. },
  1351. _a);
  1352. var DirectionMap = (_b = {},
  1353. _b["horizontal" /* Horizontal */] = (_c = {},
  1354. _c["yes" /* Yes */] = "horizontal" /* Horizontal */,
  1355. _c["no" /* No */] = "vertical" /* Vertical */,
  1356. _c),
  1357. _b["vertical" /* Vertical */] = (_d = {},
  1358. _d["yes" /* Yes */] = "vertical" /* Vertical */,
  1359. _d["no" /* No */] = "horizontal" /* Horizontal */,
  1360. _d),
  1361. _b);
  1362. var DirectionLockAction = /** @class */ (function () {
  1363. function DirectionLockAction(directionLockThreshold, freeScroll, eventPassthrough) {
  1364. this.directionLockThreshold = directionLockThreshold;
  1365. this.freeScroll = freeScroll;
  1366. this.eventPassthrough = eventPassthrough;
  1367. this.reset();
  1368. }
  1369. DirectionLockAction.prototype.reset = function () {
  1370. this.directionLocked = "" /* Default */;
  1371. };
  1372. DirectionLockAction.prototype.checkMovingDirection = function (absDistX, absDistY, e) {
  1373. this.computeDirectionLock(absDistX, absDistY);
  1374. return this.handleEventPassthrough(e);
  1375. };
  1376. DirectionLockAction.prototype.adjustDelta = function (deltaX, deltaY) {
  1377. if (this.directionLocked === "horizontal" /* Horizontal */) {
  1378. deltaY = 0;
  1379. }
  1380. else if (this.directionLocked === "vertical" /* Vertical */) {
  1381. deltaX = 0;
  1382. }
  1383. return {
  1384. deltaX: deltaX,
  1385. deltaY: deltaY
  1386. };
  1387. };
  1388. DirectionLockAction.prototype.computeDirectionLock = function (absDistX, absDistY) {
  1389. // If you are scrolling in one direction, lock it
  1390. if (this.directionLocked === "" /* Default */ && !this.freeScroll) {
  1391. if (absDistX > absDistY + this.directionLockThreshold) {
  1392. this.directionLocked = "horizontal" /* Horizontal */; // lock horizontally
  1393. }
  1394. else if (absDistY >= absDistX + this.directionLockThreshold) {
  1395. this.directionLocked = "vertical" /* Vertical */; // lock vertically
  1396. }
  1397. else {
  1398. this.directionLocked = "none" /* None */; // no lock
  1399. }
  1400. }
  1401. };
  1402. DirectionLockAction.prototype.handleEventPassthrough = function (e) {
  1403. var handleMap = DirectionMap[this.directionLocked];
  1404. if (handleMap) {
  1405. if (this.eventPassthrough === handleMap["yes" /* Yes */]) {
  1406. return PassthroughHandlers["yes" /* Yes */](e);
  1407. }
  1408. else if (this.eventPassthrough === handleMap["no" /* No */]) {
  1409. return PassthroughHandlers["no" /* No */](e);
  1410. }
  1411. }
  1412. return false;
  1413. };
  1414. return DirectionLockAction;
  1415. }());
  1416. var ScrollerActions = /** @class */ (function () {
  1417. function ScrollerActions(scrollBehaviorX, scrollBehaviorY, actionsHandler, animater, options) {
  1418. this.hooks = new EventEmitter([
  1419. 'start',
  1420. 'beforeMove',
  1421. 'scrollStart',
  1422. 'scroll',
  1423. 'beforeEnd',
  1424. 'end',
  1425. 'scrollEnd'
  1426. ]);
  1427. this.scrollBehaviorX = scrollBehaviorX;
  1428. this.scrollBehaviorY = scrollBehaviorY;
  1429. this.actionsHandler = actionsHandler;
  1430. this.animater = animater;
  1431. this.options = options;
  1432. this.directionLockAction = new DirectionLockAction(options.directionLockThreshold, options.freeScroll, options.eventPassthrough);
  1433. this.enabled = true;
  1434. this.bindActionsHandler();
  1435. }
  1436. ScrollerActions.prototype.bindActionsHandler = function () {
  1437. var _this = this;
  1438. // [mouse|touch]start event
  1439. this.actionsHandler.hooks.on(this.actionsHandler.hooks.eventTypes.start, function (e) {
  1440. if (!_this.enabled)
  1441. return true;
  1442. return _this.handleStart(e);
  1443. });
  1444. // [mouse|touch]move event
  1445. this.actionsHandler.hooks.on(this.actionsHandler.hooks.eventTypes.move, function (_a) {
  1446. var deltaX = _a.deltaX, deltaY = _a.deltaY, e = _a.e;
  1447. if (!_this.enabled)
  1448. return true;
  1449. return _this.handleMove(deltaX, deltaY, e);
  1450. });
  1451. // [mouse|touch]end event
  1452. this.actionsHandler.hooks.on(this.actionsHandler.hooks.eventTypes.end, function (e) {
  1453. if (!_this.enabled)
  1454. return true;
  1455. return _this.handleEnd(e);
  1456. });
  1457. // click
  1458. this.actionsHandler.hooks.on(this.actionsHandler.hooks.eventTypes.click, function (e) {
  1459. // handle native click event
  1460. if (_this.enabled && !e._constructed) {
  1461. _this.handleClick(e);
  1462. }
  1463. });
  1464. };
  1465. ScrollerActions.prototype.handleStart = function (e) {
  1466. var timestamp = getNow();
  1467. this.moved = false;
  1468. this.startTime = timestamp;
  1469. this.directionLockAction.reset();
  1470. this.scrollBehaviorX.start();
  1471. this.scrollBehaviorY.start();
  1472. // force stopping last transition or animation
  1473. this.animater.doStop();
  1474. this.scrollBehaviorX.resetStartPos();
  1475. this.scrollBehaviorY.resetStartPos();
  1476. this.hooks.trigger(this.hooks.eventTypes.start, e);
  1477. };
  1478. ScrollerActions.prototype.handleMove = function (deltaX, deltaY, e) {
  1479. if (this.hooks.trigger(this.hooks.eventTypes.beforeMove, e)) {
  1480. return;
  1481. }
  1482. var absDistX = this.scrollBehaviorX.getAbsDist(deltaX);
  1483. var absDistY = this.scrollBehaviorY.getAbsDist(deltaY);
  1484. var timestamp = getNow();
  1485. // We need to move at least momentumLimitDistance pixels
  1486. // for the scrolling to initiate
  1487. if (this.checkMomentum(absDistX, absDistY, timestamp)) {
  1488. return true;
  1489. }
  1490. if (this.directionLockAction.checkMovingDirection(absDistX, absDistY, e)) {
  1491. this.actionsHandler.setInitiated();
  1492. return true;
  1493. }
  1494. var delta = this.directionLockAction.adjustDelta(deltaX, deltaY);
  1495. var newX = this.scrollBehaviorX.move(delta.deltaX);
  1496. var newY = this.scrollBehaviorY.move(delta.deltaY);
  1497. if (!this.moved) {
  1498. this.moved = true;
  1499. this.hooks.trigger(this.hooks.eventTypes.scrollStart);
  1500. }
  1501. this.animater.translate({
  1502. x: newX,
  1503. y: newY
  1504. });
  1505. this.dispatchScroll(timestamp);
  1506. };
  1507. ScrollerActions.prototype.dispatchScroll = function (timestamp) {
  1508. // dispatch scroll in interval time
  1509. if (timestamp - this.startTime > this.options.momentumLimitTime) {
  1510. // refresh time and starting position to initiate a momentum
  1511. this.startTime = timestamp;
  1512. this.scrollBehaviorX.updateStartPos();
  1513. this.scrollBehaviorY.updateStartPos();
  1514. if (this.options.probeType === 1 /* Throttle */) {
  1515. this.hooks.trigger(this.hooks.eventTypes.scroll, this.getCurrentPos());
  1516. }
  1517. }
  1518. // dispatch scroll all the time
  1519. if (this.options.probeType > 1 /* Throttle */) {
  1520. this.hooks.trigger(this.hooks.eventTypes.scroll, this.getCurrentPos());
  1521. }
  1522. };
  1523. ScrollerActions.prototype.checkMomentum = function (absDistX, absDistY, timestamp) {
  1524. return (timestamp - this.endTime > this.options.momentumLimitTime &&
  1525. absDistY < this.options.momentumLimitDistance &&
  1526. absDistX < this.options.momentumLimitDistance);
  1527. };
  1528. ScrollerActions.prototype.handleEnd = function (e) {
  1529. if (this.hooks.trigger(this.hooks.eventTypes.beforeEnd, e)) {
  1530. return;
  1531. }
  1532. var currentPos = this.getCurrentPos();
  1533. this.scrollBehaviorX.updateDirection();
  1534. this.scrollBehaviorY.updateDirection();
  1535. if (this.hooks.trigger(this.hooks.eventTypes.end, e, currentPos)) {
  1536. return true;
  1537. }
  1538. this.animater.translate(currentPos);
  1539. this.endTime = getNow();
  1540. var duration = this.endTime - this.startTime;
  1541. this.hooks.trigger(this.hooks.eventTypes.scrollEnd, currentPos, duration);
  1542. };
  1543. ScrollerActions.prototype.handleClick = function (e) {
  1544. if (!preventDefaultExceptionFn(e.target, this.options.preventDefaultException)) {
  1545. e.preventDefault();
  1546. e.stopPropagation();
  1547. }
  1548. };
  1549. ScrollerActions.prototype.getCurrentPos = function () {
  1550. return {
  1551. x: this.scrollBehaviorX.getCurrentPos(),
  1552. y: this.scrollBehaviorY.getCurrentPos()
  1553. };
  1554. };
  1555. ScrollerActions.prototype.refresh = function () {
  1556. this.endTime = 0;
  1557. };
  1558. ScrollerActions.prototype.destroy = function () {
  1559. this.hooks.destroy();
  1560. };
  1561. return ScrollerActions;
  1562. }());
  1563. function createActionsHandlerOptions(bsOptions) {
  1564. var options = [
  1565. 'click',
  1566. 'bindToWrapper',
  1567. 'disableMouse',
  1568. 'disableTouch',
  1569. 'preventDefault',
  1570. 'stopPropagation',
  1571. 'tagException',
  1572. 'preventDefaultException',
  1573. 'autoEndDistance',
  1574. ].reduce(function (prev, cur) {
  1575. prev[cur] = bsOptions[cur];
  1576. return prev;
  1577. }, {});
  1578. return options;
  1579. }
  1580. function createBehaviorOptions(bsOptions, extraProp, bounces, rect) {
  1581. var options = [
  1582. 'momentum',
  1583. 'momentumLimitTime',
  1584. 'momentumLimitDistance',
  1585. 'deceleration',
  1586. 'swipeBounceTime',
  1587. 'swipeTime',
  1588. 'outOfBoundaryDampingFactor',
  1589. 'specifiedIndexAsContent',
  1590. ].reduce(function (prev, cur) {
  1591. prev[cur] = bsOptions[cur];
  1592. return prev;
  1593. }, {});
  1594. // add extra property
  1595. options.scrollable = !!bsOptions[extraProp];
  1596. options.bounces = bounces;
  1597. options.rect = rect;
  1598. return options;
  1599. }
  1600. function bubbling(source, target, events) {
  1601. events.forEach(function (event) {
  1602. var sourceEvent;
  1603. var targetEvent;
  1604. if (typeof event === 'string') {
  1605. sourceEvent = targetEvent = event;
  1606. }
  1607. else {
  1608. sourceEvent = event.source;
  1609. targetEvent = event.target;
  1610. }
  1611. source.on(sourceEvent, function () {
  1612. var args = [];
  1613. for (var _i = 0; _i < arguments.length; _i++) {
  1614. args[_i] = arguments[_i];
  1615. }
  1616. return target.trigger.apply(target, __spreadArrays([targetEvent], args));
  1617. });
  1618. });
  1619. }
  1620. function isSamePoint(startPoint, endPoint) {
  1621. // keys of startPoint and endPoint should be equal
  1622. var keys = Object.keys(startPoint);
  1623. for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
  1624. var key = keys_1[_i];
  1625. if (startPoint[key] !== endPoint[key])
  1626. return false;
  1627. }
  1628. return true;
  1629. }
  1630. var Scroller = /** @class */ (function () {
  1631. function Scroller(wrapper, content, options) {
  1632. this.wrapper = wrapper;
  1633. this.content = content;
  1634. this.resizeTimeout = 0;
  1635. this.hooks = new EventEmitter([
  1636. 'beforeStart',
  1637. 'beforeMove',
  1638. 'beforeScrollStart',
  1639. 'scrollStart',
  1640. 'scroll',
  1641. 'beforeEnd',
  1642. 'scrollEnd',
  1643. 'resize',
  1644. 'touchEnd',
  1645. 'end',
  1646. 'flick',
  1647. 'scrollCancel',
  1648. 'momentum',
  1649. 'scrollTo',
  1650. 'scrollToElement',
  1651. 'beforeRefresh',
  1652. ]);
  1653. this.options = options;
  1654. var _a = this.options.bounce, left = _a.left, right = _a.right, top = _a.top, bottom = _a.bottom;
  1655. // direction X
  1656. this.scrollBehaviorX = new Behavior(wrapper, content, createBehaviorOptions(options, 'scrollX', [left, right], {
  1657. size: 'width',
  1658. position: 'left',
  1659. }));
  1660. // direction Y
  1661. this.scrollBehaviorY = new Behavior(wrapper, content, createBehaviorOptions(options, 'scrollY', [top, bottom], {
  1662. size: 'height',
  1663. position: 'top',
  1664. }));
  1665. this.translater = new Translater(this.content);
  1666. this.animater = createAnimater(this.content, this.translater, this.options);
  1667. this.actionsHandler = new ActionsHandler(this.options.bindToTarget ? this.content : wrapper, createActionsHandlerOptions(this.options));
  1668. this.actions = new ScrollerActions(this.scrollBehaviorX, this.scrollBehaviorY, this.actionsHandler, this.animater, this.options);
  1669. var resizeHandler = this.resize.bind(this);
  1670. this.resizeRegister = new EventRegister(window, [
  1671. {
  1672. name: 'orientationchange',
  1673. handler: resizeHandler,
  1674. },
  1675. {
  1676. name: 'resize',
  1677. handler: resizeHandler,
  1678. },
  1679. ]);
  1680. this.registerTransitionEnd();
  1681. this.init();
  1682. }
  1683. Scroller.prototype.init = function () {
  1684. var _this = this;
  1685. this.bindTranslater();
  1686. this.bindAnimater();
  1687. this.bindActions();
  1688. // enable pointer events when scrolling ends
  1689. this.hooks.on(this.hooks.eventTypes.scrollEnd, function () {
  1690. _this.togglePointerEvents(true);
  1691. });
  1692. };
  1693. Scroller.prototype.registerTransitionEnd = function () {
  1694. this.transitionEndRegister = new EventRegister(this.content, [
  1695. {
  1696. name: style.transitionEnd,
  1697. handler: this.transitionEnd.bind(this),
  1698. },
  1699. ]);
  1700. };
  1701. Scroller.prototype.bindTranslater = function () {
  1702. var _this = this;
  1703. var hooks = this.translater.hooks;
  1704. hooks.on(hooks.eventTypes.beforeTranslate, function (transformStyle) {
  1705. if (_this.options.translateZ) {
  1706. transformStyle.push(_this.options.translateZ);
  1707. }
  1708. });
  1709. // disable pointer events when scrolling
  1710. hooks.on(hooks.eventTypes.translate, function (pos) {
  1711. var prevPos = _this.getCurrentPos();
  1712. _this.updatePositions(pos);
  1713. // a valid translate
  1714. if (pos.x !== prevPos.x || pos.y !== prevPos.y) {
  1715. _this.togglePointerEvents(false);
  1716. }
  1717. });
  1718. };
  1719. Scroller.prototype.bindAnimater = function () {
  1720. var _this = this;
  1721. // reset position
  1722. this.animater.hooks.on(this.animater.hooks.eventTypes.end, function (pos) {
  1723. if (!_this.resetPosition(_this.options.bounceTime)) {
  1724. _this.animater.setPending(false);
  1725. _this.hooks.trigger(_this.hooks.eventTypes.scrollEnd, pos);
  1726. }
  1727. });
  1728. bubbling(this.animater.hooks, this.hooks, [
  1729. {
  1730. source: this.animater.hooks.eventTypes.move,
  1731. target: this.hooks.eventTypes.scroll,
  1732. },
  1733. {
  1734. source: this.animater.hooks.eventTypes.forceStop,
  1735. target: this.hooks.eventTypes.scrollEnd,
  1736. },
  1737. ]);
  1738. };
  1739. Scroller.prototype.bindActions = function () {
  1740. var _this = this;
  1741. var actions = this.actions;
  1742. bubbling(actions.hooks, this.hooks, [
  1743. {
  1744. source: actions.hooks.eventTypes.start,
  1745. target: this.hooks.eventTypes.beforeStart,
  1746. },
  1747. {
  1748. source: actions.hooks.eventTypes.start,
  1749. target: this.hooks.eventTypes.beforeScrollStart,
  1750. },
  1751. {
  1752. source: actions.hooks.eventTypes.beforeMove,
  1753. target: this.hooks.eventTypes.beforeMove,
  1754. },
  1755. {
  1756. source: actions.hooks.eventTypes.scrollStart,
  1757. target: this.hooks.eventTypes.scrollStart,
  1758. },
  1759. {
  1760. source: actions.hooks.eventTypes.scroll,
  1761. target: this.hooks.eventTypes.scroll,
  1762. },
  1763. {
  1764. source: actions.hooks.eventTypes.beforeEnd,
  1765. target: this.hooks.eventTypes.beforeEnd,
  1766. },
  1767. ]);
  1768. actions.hooks.on(actions.hooks.eventTypes.end, function (e, pos) {
  1769. _this.hooks.trigger(_this.hooks.eventTypes.touchEnd, pos);
  1770. if (_this.hooks.trigger(_this.hooks.eventTypes.end, pos)) {
  1771. return true;
  1772. }
  1773. // check if it is a click operation
  1774. if (!actions.moved) {
  1775. _this.hooks.trigger(_this.hooks.eventTypes.scrollCancel);
  1776. if (_this.checkClick(e)) {
  1777. return true;
  1778. }
  1779. }
  1780. // reset if we are outside of the boundaries
  1781. if (_this.resetPosition(_this.options.bounceTime, ease.bounce)) {
  1782. _this.animater.setForceStopped(false);
  1783. return true;
  1784. }
  1785. });
  1786. actions.hooks.on(actions.hooks.eventTypes.scrollEnd, function (pos, duration) {
  1787. var deltaX = Math.abs(pos.x - _this.scrollBehaviorX.startPos);
  1788. var deltaY = Math.abs(pos.y - _this.scrollBehaviorY.startPos);
  1789. if (_this.checkFlick(duration, deltaX, deltaY)) {
  1790. _this.animater.setForceStopped(false);
  1791. _this.hooks.trigger(_this.hooks.eventTypes.flick);
  1792. return;
  1793. }
  1794. if (_this.momentum(pos, duration)) {
  1795. _this.animater.setForceStopped(false);
  1796. return;
  1797. }
  1798. // force stop from transition or animation when click a point
  1799. if (!_this.animater.forceStopped || actions.moved) {
  1800. _this.hooks.trigger(_this.hooks.eventTypes.scrollEnd, pos);
  1801. }
  1802. else {
  1803. _this.animater.setForceStopped(false);
  1804. }
  1805. });
  1806. };
  1807. Scroller.prototype.checkFlick = function (duration, deltaX, deltaY) {
  1808. var flickMinMovingDistance = 1; // distinguish flick from click
  1809. if (this.hooks.events.flick.length > 1 &&
  1810. duration < this.options.flickLimitTime &&
  1811. deltaX < this.options.flickLimitDistance &&
  1812. deltaY < this.options.flickLimitDistance &&
  1813. (deltaY > flickMinMovingDistance || deltaX > flickMinMovingDistance)) {
  1814. return true;
  1815. }
  1816. };
  1817. Scroller.prototype.momentum = function (pos, duration) {
  1818. var meta = {
  1819. time: 0,
  1820. easing: ease.swiper,
  1821. newX: pos.x,
  1822. newY: pos.y,
  1823. };
  1824. // start momentum animation if needed
  1825. var momentumX = this.scrollBehaviorX.end(duration);
  1826. var momentumY = this.scrollBehaviorY.end(duration);
  1827. meta.newX = isUndef(momentumX.destination)
  1828. ? meta.newX
  1829. : momentumX.destination;
  1830. meta.newY = isUndef(momentumY.destination)
  1831. ? meta.newY
  1832. : momentumY.destination;
  1833. meta.time = Math.max(momentumX.duration, momentumY.duration);
  1834. this.hooks.trigger(this.hooks.eventTypes.momentum, meta, this);
  1835. // when x or y changed, do momentum animation now!
  1836. if (meta.newX !== pos.x || meta.newY !== pos.y) {
  1837. // change easing function when scroller goes out of the boundaries
  1838. if (meta.newX > this.scrollBehaviorX.minScrollPos ||
  1839. meta.newX < this.scrollBehaviorX.maxScrollPos ||
  1840. meta.newY > this.scrollBehaviorY.minScrollPos ||
  1841. meta.newY < this.scrollBehaviorY.maxScrollPos) {
  1842. meta.easing = ease.swipeBounce;
  1843. }
  1844. this.scrollTo(meta.newX, meta.newY, meta.time, meta.easing);
  1845. return true;
  1846. }
  1847. };
  1848. Scroller.prototype.checkClick = function (e) {
  1849. var cancelable = {
  1850. preventClick: this.animater.forceStopped,
  1851. };
  1852. // we scrolled less than momentumLimitDistance pixels
  1853. if (this.hooks.trigger(this.hooks.eventTypes.checkClick)) {
  1854. this.animater.setForceStopped(false);
  1855. return true;
  1856. }
  1857. if (!cancelable.preventClick) {
  1858. var _dblclick = this.options.dblclick;
  1859. var dblclickTrigged = false;
  1860. if (_dblclick && this.lastClickTime) {
  1861. var _a = _dblclick.delay, delay = _a === void 0 ? 300 : _a;
  1862. if (getNow() - this.lastClickTime < delay) {
  1863. dblclickTrigged = true;
  1864. dblclick(e);
  1865. }
  1866. }
  1867. if (this.options.tap) {
  1868. tap(e, this.options.tap);
  1869. }
  1870. if (this.options.click &&
  1871. !preventDefaultExceptionFn(e.target, this.options.preventDefaultException)) {
  1872. click(e);
  1873. }
  1874. this.lastClickTime = dblclickTrigged ? null : getNow();
  1875. return true;
  1876. }
  1877. return false;
  1878. };
  1879. Scroller.prototype.resize = function () {
  1880. var _this = this;
  1881. if (!this.actions.enabled) {
  1882. return;
  1883. }
  1884. // fix a scroll problem under Android condition
  1885. if (isAndroid) {
  1886. this.wrapper.scrollTop = 0;
  1887. }
  1888. clearTimeout(this.resizeTimeout);
  1889. this.resizeTimeout = window.setTimeout(function () {
  1890. _this.hooks.trigger(_this.hooks.eventTypes.resize);
  1891. }, this.options.resizePolling);
  1892. };
  1893. Scroller.prototype.transitionEnd = function (e) {
  1894. if (e.target !== this.content || !this.animater.pending) {
  1895. return;
  1896. }
  1897. var animater = this.animater;
  1898. animater.transitionTime();
  1899. if (!this.resetPosition(this.options.bounceTime, ease.bounce)) {
  1900. this.animater.setPending(false);
  1901. if (this.options.probeType !== 3 /* Realtime */) {
  1902. this.hooks.trigger(this.hooks.eventTypes.scrollEnd, this.getCurrentPos());
  1903. }
  1904. }
  1905. };
  1906. Scroller.prototype.togglePointerEvents = function (enabled) {
  1907. if (enabled === void 0) { enabled = true; }
  1908. var el = this.content.children.length
  1909. ? this.content.children
  1910. : [this.content];
  1911. var pointerEvents = enabled ? 'auto' : 'none';
  1912. for (var i = 0; i < el.length; i++) {
  1913. var node = el[i];
  1914. // ignore BetterScroll instance's wrapper DOM
  1915. if (node.isBScrollContainer) {
  1916. continue;
  1917. }
  1918. node.style.pointerEvents = pointerEvents;
  1919. }
  1920. };
  1921. Scroller.prototype.refresh = function (content) {
  1922. var contentChanged = this.setContent(content);
  1923. this.hooks.trigger(this.hooks.eventTypes.beforeRefresh);
  1924. this.scrollBehaviorX.refresh(content);
  1925. this.scrollBehaviorY.refresh(content);
  1926. if (contentChanged) {
  1927. this.translater.setContent(content);
  1928. this.animater.setContent(content);
  1929. this.transitionEndRegister.destroy();
  1930. this.registerTransitionEnd();
  1931. if (this.options.bindToTarget) {
  1932. this.actionsHandler.setContent(content);
  1933. }
  1934. }
  1935. this.actions.refresh();
  1936. this.wrapperOffset = offset(this.wrapper);
  1937. };
  1938. Scroller.prototype.setContent = function (content) {
  1939. var contentChanged = content !== this.content;
  1940. if (contentChanged) {
  1941. this.content = content;
  1942. }
  1943. return contentChanged;
  1944. };
  1945. Scroller.prototype.scrollBy = function (deltaX, deltaY, time, easing) {
  1946. if (time === void 0) { time = 0; }
  1947. var _a = this.getCurrentPos(), x = _a.x, y = _a.y;
  1948. easing = !easing ? ease.bounce : easing;
  1949. deltaX += x;
  1950. deltaY += y;
  1951. this.scrollTo(deltaX, deltaY, time, easing);
  1952. };
  1953. Scroller.prototype.scrollTo = function (x, y, time, easing, extraTransform) {
  1954. if (time === void 0) { time = 0; }
  1955. if (easing === void 0) { easing = ease.bounce; }
  1956. if (extraTransform === void 0) { extraTransform = {
  1957. start: {},
  1958. end: {},
  1959. }; }
  1960. var easingFn = this.options.useTransition ? easing.style : easing.fn;
  1961. var currentPos = this.getCurrentPos();
  1962. var startPoint = __assign({ x: currentPos.x, y: currentPos.y }, extraTransform.start);
  1963. var endPoint = __assign({ x: x,
  1964. y: y }, extraTransform.end);
  1965. this.hooks.trigger(this.hooks.eventTypes.scrollTo, endPoint);
  1966. // it is an useless move
  1967. if (isSamePoint(startPoint, endPoint))
  1968. return;
  1969. this.animater.move(startPoint, endPoint, time, easingFn);
  1970. };
  1971. Scroller.prototype.scrollToElement = function (el, time, offsetX, offsetY, easing) {
  1972. var targetEle = getElement(el);
  1973. var pos = offset(targetEle);
  1974. var getOffset = function (offset, size, wrapperSize) {
  1975. if (typeof offset === 'number') {
  1976. return offset;
  1977. }
  1978. // if offsetX/Y are true we center the element to the screen
  1979. return offset ? Math.round(size / 2 - wrapperSize / 2) : 0;
  1980. };
  1981. offsetX = getOffset(offsetX, targetEle.offsetWidth, this.wrapper.offsetWidth);
  1982. offsetY = getOffset(offsetY, targetEle.offsetHeight, this.wrapper.offsetHeight);
  1983. var getPos = function (pos, wrapperPos, offset, scrollBehavior) {
  1984. pos -= wrapperPos;
  1985. pos = scrollBehavior.adjustPosition(pos - offset);
  1986. return pos;
  1987. };
  1988. pos.left = getPos(pos.left, this.wrapperOffset.left, offsetX, this.scrollBehaviorX);
  1989. pos.top = getPos(pos.top, this.wrapperOffset.top, offsetY, this.scrollBehaviorY);
  1990. if (this.hooks.trigger(this.hooks.eventTypes.scrollToElement, targetEle, pos)) {
  1991. return;
  1992. }
  1993. this.scrollTo(pos.left, pos.top, time, easing);
  1994. };
  1995. Scroller.prototype.resetPosition = function (time, easing) {
  1996. if (time === void 0) { time = 0; }
  1997. if (easing === void 0) { easing = ease.bounce; }
  1998. var _a = this.scrollBehaviorX.checkInBoundary(), x = _a.position, xInBoundary = _a.inBoundary;
  1999. var _b = this.scrollBehaviorY.checkInBoundary(), y = _b.position, yInBoundary = _b.inBoundary;
  2000. if (xInBoundary && yInBoundary) {
  2001. return false;
  2002. }
  2003. if (isIOSBadVersion) {
  2004. // fix ios 13.4 bouncing
  2005. // see it in issues 982
  2006. this._reflow = this.content.offsetHeight;
  2007. }
  2008. // out of boundary
  2009. this.scrollTo(x, y, time, easing);
  2010. return true;
  2011. };
  2012. Scroller.prototype.updatePositions = function (pos) {
  2013. this.scrollBehaviorX.updatePosition(pos.x);
  2014. this.scrollBehaviorY.updatePosition(pos.y);
  2015. };
  2016. Scroller.prototype.getCurrentPos = function () {
  2017. return this.actions.getCurrentPos();
  2018. };
  2019. Scroller.prototype.enable = function () {
  2020. this.actions.enabled = true;
  2021. };
  2022. Scroller.prototype.disable = function () {
  2023. cancelAnimationFrame(this.animater.timer);
  2024. this.actions.enabled = false;
  2025. };
  2026. Scroller.prototype.destroy = function () {
  2027. var _this = this;
  2028. var keys = [
  2029. 'resizeRegister',
  2030. 'transitionEndRegister',
  2031. 'actionsHandler',
  2032. 'actions',
  2033. 'hooks',
  2034. 'animater',
  2035. 'translater',
  2036. 'scrollBehaviorX',
  2037. 'scrollBehaviorY',
  2038. ];
  2039. keys.forEach(function (key) { return _this[key].destroy(); });
  2040. };
  2041. return Scroller;
  2042. }());
  2043. var BScrollConstructor = /** @class */ (function (_super) {
  2044. __extends(BScrollConstructor, _super);
  2045. function BScrollConstructor(el, options) {
  2046. var _this = _super.call(this, [
  2047. 'refresh',
  2048. 'contentChanged',
  2049. 'enable',
  2050. 'disable',
  2051. 'beforeScrollStart',
  2052. 'scrollStart',
  2053. 'scroll',
  2054. 'scrollEnd',
  2055. 'scrollCancel',
  2056. 'touchEnd',
  2057. 'flick',
  2058. 'destroy',
  2059. ]) || this;
  2060. var wrapper = getElement(el);
  2061. if (!wrapper) {
  2062. warn('Can not resolve the wrapper DOM.');
  2063. return _this;
  2064. }
  2065. _this.plugins = {};
  2066. _this.options = new OptionsConstructor().merge(options).process();
  2067. if (!_this.setContent(wrapper).valid) {
  2068. return _this;
  2069. }
  2070. _this.hooks = new EventEmitter([
  2071. 'refresh',
  2072. 'enable',
  2073. 'disable',
  2074. 'destroy',
  2075. 'beforeInitialScrollTo',
  2076. 'contentChanged',
  2077. ]);
  2078. _this.init(wrapper);
  2079. return _this;
  2080. }
  2081. BScrollConstructor.use = function (ctor) {
  2082. var name = ctor.pluginName;
  2083. var installed = BScrollConstructor.plugins.some(function (plugin) { return ctor === plugin.ctor; });
  2084. if (installed)
  2085. return BScrollConstructor;
  2086. if (isUndef(name)) {
  2087. warn("Plugin Class must specify plugin's name in static property by 'pluginName' field.");
  2088. return BScrollConstructor;
  2089. }
  2090. if (BScrollConstructor.pluginsMap[name]) {
  2091. warn("This plugin has been registered, maybe you need change plugin's name");
  2092. return BScrollConstructor;
  2093. }
  2094. BScrollConstructor.pluginsMap[name] = true;
  2095. BScrollConstructor.plugins.push({
  2096. name: name,
  2097. applyOrder: ctor.applyOrder,
  2098. ctor: ctor,
  2099. });
  2100. return BScrollConstructor;
  2101. };
  2102. BScrollConstructor.prototype.setContent = function (wrapper) {
  2103. var contentChanged = false;
  2104. var valid = true;
  2105. var content = wrapper.children[this.options.specifiedIndexAsContent];
  2106. if (!content) {
  2107. warn('The wrapper need at least one child element to be content element to scroll.');
  2108. valid = false;
  2109. }
  2110. else {
  2111. contentChanged = this.content !== content;
  2112. if (contentChanged) {
  2113. this.content = content;
  2114. }
  2115. }
  2116. return {
  2117. valid: valid,
  2118. contentChanged: contentChanged,
  2119. };
  2120. };
  2121. BScrollConstructor.prototype.init = function (wrapper) {
  2122. var _this = this;
  2123. this.wrapper = wrapper;
  2124. // mark wrapper to recognize bs instance by DOM attribute
  2125. wrapper.isBScrollContainer = true;
  2126. this.scroller = new Scroller(wrapper, this.content, this.options);
  2127. this.scroller.hooks.on(this.scroller.hooks.eventTypes.resize, function () {
  2128. _this.refresh();
  2129. });
  2130. this.eventBubbling();
  2131. this.handleAutoBlur();
  2132. this.enable();
  2133. this.proxy(propertiesConfig);
  2134. this.applyPlugins();
  2135. // maybe boundary has changed, should refresh
  2136. this.refreshWithoutReset(this.content);
  2137. var _a = this.options, startX = _a.startX, startY = _a.startY;
  2138. var position = {
  2139. x: startX,
  2140. y: startY,
  2141. };
  2142. // maybe plugins want to control scroll position
  2143. if (this.hooks.trigger(this.hooks.eventTypes.beforeInitialScrollTo, position)) {
  2144. return;
  2145. }
  2146. this.scroller.scrollTo(position.x, position.y);
  2147. };
  2148. BScrollConstructor.prototype.applyPlugins = function () {
  2149. var _this = this;
  2150. var options = this.options;
  2151. BScrollConstructor.plugins
  2152. .sort(function (a, b) {
  2153. var _a;
  2154. var applyOrderMap = (_a = {},
  2155. _a["pre" /* Pre */] = -1,
  2156. _a["post" /* Post */] = 1,
  2157. _a);
  2158. var aOrder = a.applyOrder ? applyOrderMap[a.applyOrder] : 0;
  2159. var bOrder = b.applyOrder ? applyOrderMap[b.applyOrder] : 0;
  2160. return aOrder - bOrder;
  2161. })
  2162. .forEach(function (item) {
  2163. var ctor = item.ctor;
  2164. if (options[item.name] && typeof ctor === 'function') {
  2165. _this.plugins[item.name] = new ctor(_this);
  2166. }
  2167. });
  2168. };
  2169. BScrollConstructor.prototype.handleAutoBlur = function () {
  2170. if (this.options.autoBlur) {
  2171. this.on(this.eventTypes.beforeScrollStart, function () {
  2172. var activeElement = document.activeElement;
  2173. if (activeElement &&
  2174. (activeElement.tagName === 'INPUT' ||
  2175. activeElement.tagName === 'TEXTAREA')) {
  2176. activeElement.blur();
  2177. }
  2178. });
  2179. }
  2180. };
  2181. BScrollConstructor.prototype.eventBubbling = function () {
  2182. bubbling(this.scroller.hooks, this, [
  2183. this.eventTypes.beforeScrollStart,
  2184. this.eventTypes.scrollStart,
  2185. this.eventTypes.scroll,
  2186. this.eventTypes.scrollEnd,
  2187. this.eventTypes.scrollCancel,
  2188. this.eventTypes.touchEnd,
  2189. this.eventTypes.flick,
  2190. ]);
  2191. };
  2192. BScrollConstructor.prototype.refreshWithoutReset = function (content) {
  2193. this.scroller.refresh(content);
  2194. this.hooks.trigger(this.hooks.eventTypes.refresh, content);
  2195. this.trigger(this.eventTypes.refresh, content);
  2196. };
  2197. BScrollConstructor.prototype.proxy = function (propertiesConfig) {
  2198. var _this = this;
  2199. propertiesConfig.forEach(function (_a) {
  2200. var key = _a.key, sourceKey = _a.sourceKey;
  2201. propertiesProxy(_this, sourceKey, key);
  2202. });
  2203. };
  2204. BScrollConstructor.prototype.refresh = function () {
  2205. var _a = this.setContent(this.wrapper), contentChanged = _a.contentChanged, valid = _a.valid;
  2206. if (valid) {
  2207. var content = this.content;
  2208. this.refreshWithoutReset(content);
  2209. if (contentChanged) {
  2210. this.hooks.trigger(this.hooks.eventTypes.contentChanged, content);
  2211. this.trigger(this.eventTypes.contentChanged, content);
  2212. }
  2213. this.scroller.resetPosition();
  2214. }
  2215. };
  2216. BScrollConstructor.prototype.enable = function () {
  2217. this.scroller.enable();
  2218. this.hooks.trigger(this.hooks.eventTypes.enable);
  2219. this.trigger(this.eventTypes.enable);
  2220. };
  2221. BScrollConstructor.prototype.disable = function () {
  2222. this.scroller.disable();
  2223. this.hooks.trigger(this.hooks.eventTypes.disable);
  2224. this.trigger(this.eventTypes.disable);
  2225. };
  2226. BScrollConstructor.prototype.destroy = function () {
  2227. this.hooks.trigger(this.hooks.eventTypes.destroy);
  2228. this.trigger(this.eventTypes.destroy);
  2229. this.scroller.destroy();
  2230. };
  2231. BScrollConstructor.prototype.eventRegister = function (names) {
  2232. this.registerType(names);
  2233. };
  2234. BScrollConstructor.plugins = [];
  2235. BScrollConstructor.pluginsMap = {};
  2236. return BScrollConstructor;
  2237. }(EventEmitter));
  2238. function createBScroll(el, options) {
  2239. var bs = new BScrollConstructor(el, options);
  2240. return bs;
  2241. }
  2242. createBScroll.use = BScrollConstructor.use;
  2243. createBScroll.plugins = BScrollConstructor.plugins;
  2244. createBScroll.pluginsMap = BScrollConstructor.pluginsMap;
  2245. var BScroll = createBScroll;
  2246. export default BScroll;
  2247. export { Behavior, CustomOptions, createBScroll };