core.esm.js 88 KB

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