core.js 88 KB

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