core.js 98 KB

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