core.js 95 KB

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