vuescroll-native.js 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558
  1. /*
  2. * Vuescroll v4.16.1
  3. * (c) 2018-2020 Yi(Yves) Wang
  4. * Released under the MIT License
  5. * Github: https://github.com/YvesCoding/vuescroll
  6. * Website: http://vuescrolljs.yvescoding.org/
  7. */
  8. (function (global, factory) {
  9. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('vue')) :
  10. typeof define === 'function' && define.amd ? define(['vue'], factory) :
  11. (global.vuescroll = factory(global.Vue));
  12. }(this, (function (Vue) { 'use strict';
  13. Vue = Vue && Vue.hasOwnProperty('default') ? Vue['default'] : Vue;
  14. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
  15. return typeof obj;
  16. } : function (obj) {
  17. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  18. };
  19. var classCallCheck = function (instance, Constructor) {
  20. if (!(instance instanceof Constructor)) {
  21. throw new TypeError("Cannot call a class as a function");
  22. }
  23. };
  24. var createClass = function () {
  25. function defineProperties(target, props) {
  26. for (var i = 0; i < props.length; i++) {
  27. var descriptor = props[i];
  28. descriptor.enumerable = descriptor.enumerable || false;
  29. descriptor.configurable = true;
  30. if ("value" in descriptor) descriptor.writable = true;
  31. Object.defineProperty(target, descriptor.key, descriptor);
  32. }
  33. }
  34. return function (Constructor, protoProps, staticProps) {
  35. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  36. if (staticProps) defineProperties(Constructor, staticProps);
  37. return Constructor;
  38. };
  39. }();
  40. var defineProperty = function (obj, key, value) {
  41. if (key in obj) {
  42. Object.defineProperty(obj, key, {
  43. value: value,
  44. enumerable: true,
  45. configurable: true,
  46. writable: true
  47. });
  48. } else {
  49. obj[key] = value;
  50. }
  51. return obj;
  52. };
  53. var _extends = Object.assign || function (target) {
  54. for (var i = 1; i < arguments.length; i++) {
  55. var source = arguments[i];
  56. for (var key in source) {
  57. if (Object.prototype.hasOwnProperty.call(source, key)) {
  58. target[key] = source[key];
  59. }
  60. }
  61. }
  62. return target;
  63. };
  64. var toConsumableArray = function (arr) {
  65. if (Array.isArray(arr)) {
  66. for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
  67. return arr2;
  68. } else {
  69. return Array.from(arr);
  70. }
  71. };
  72. function isIE() {
  73. /* istanbul ignore if */
  74. if (isServer()) return false;
  75. var agent = navigator.userAgent.toLowerCase();
  76. return agent.indexOf('msie') !== -1 || agent.indexOf('trident') !== -1 || agent.indexOf(' edge/') !== -1;
  77. }
  78. var isIos = function isIos() {
  79. /* istanbul ignore if */
  80. if (isServer()) return false;
  81. var u = navigator.userAgent;
  82. return !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
  83. };
  84. /* istanbul ignore next */
  85. var isServer = function isServer() {
  86. return Vue.prototype.$isServer;
  87. };
  88. var touchManager = function () {
  89. function touchManager() {
  90. classCallCheck(this, touchManager);
  91. }
  92. createClass(touchManager, [{
  93. key: 'getEventObject',
  94. value: function getEventObject(originEvent) {
  95. return this.touchObject ? this.isTouch ? originEvent.touches : [originEvent] : null;
  96. }
  97. }, {
  98. key: 'getTouchObject',
  99. value: function getTouchObject() /* istanbul ignore next */{
  100. if (isServer()) return null;
  101. this.isTouch = false;
  102. var agent = navigator.userAgent,
  103. platform = navigator.platform,
  104. touchObject = {};
  105. touchObject.touch = !!('ontouchstart' in window && !window.opera || 'msmaxtouchpoints' in window.navigator || 'maxtouchpoints' in window.navigator || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0);
  106. touchObject.nonDeskTouch = touchObject.touch && !/win32/i.test(platform) || touchObject.touch && /win32/i.test(platform) && /mobile/i.test(agent);
  107. touchObject.eventType = 'onmousedown' in window && !touchObject.nonDeskTouch ? 'mouse' : 'ontouchstart' in window ? 'touch' : 'msmaxtouchpoints' in window.navigator || navigator.msMaxTouchPoints > 0 ? 'mstouchpoints' : 'maxtouchpoints' in window.navigator || navigator.maxTouchPoints > 0 ? 'touchpoints' : 'mouse';
  108. switch (touchObject.eventType) {
  109. case 'mouse':
  110. touchObject.touchstart = 'mousedown';
  111. touchObject.touchend = 'mouseup';
  112. touchObject.touchmove = 'mousemove';
  113. touchObject.touchenter = 'mouseenter';
  114. touchObject.touchmove = 'mousemove';
  115. touchObject.touchleave = 'mouseleave';
  116. break;
  117. case 'touch':
  118. touchObject.touchstart = 'touchstart';
  119. touchObject.touchend = 'touchend';
  120. touchObject.touchmove = 'touchmove';
  121. touchObject.touchcancel = 'touchcancel';
  122. touchObject.touchenter = 'touchstart';
  123. touchObject.touchmove = 'touchmove';
  124. touchObject.touchleave = 'touchend';
  125. this.isTouch = true;
  126. break;
  127. case 'mstouchpoints':
  128. touchObject.touchstart = 'MSPointerDown';
  129. touchObject.touchend = 'MSPointerUp';
  130. touchObject.touchmove = 'MSPointerMove';
  131. touchObject.touchcancel = 'MSPointerCancel';
  132. touchObject.touchenter = 'MSPointerDown';
  133. touchObject.touchmove = 'MSPointerMove';
  134. touchObject.touchleave = 'MSPointerUp';
  135. break;
  136. case 'touchpoints':
  137. touchObject.touchstart = 'pointerdown';
  138. touchObject.touchend = 'pointerup';
  139. touchObject.touchmove = 'pointermove';
  140. touchObject.touchcancel = 'pointercancel';
  141. touchObject.touchenter = 'pointerdown';
  142. touchObject.touchmove = 'pointermove';
  143. touchObject.touchleave = 'pointerup';
  144. break;
  145. }
  146. return this.touchObject = touchObject;
  147. }
  148. }]);
  149. return touchManager;
  150. }();
  151. function deepCopy(from, to, shallow) {
  152. if (shallow && isUndef(to)) {
  153. return from;
  154. }
  155. if (isArray(from)) {
  156. to = [];
  157. from.forEach(function (item, index) {
  158. to[index] = deepCopy(item, to[index]);
  159. });
  160. } else if (from) {
  161. if (!isPlainObj(from)) {
  162. return from;
  163. }
  164. to = {};
  165. for (var key in from) {
  166. to[key] = _typeof(from[key]) === 'object' ? deepCopy(from[key], to[key]) : from[key];
  167. }
  168. }
  169. return to;
  170. }
  171. function mergeObject(from, to, force, shallow) {
  172. if (shallow && isUndef(to)) {
  173. return from;
  174. }
  175. to = to || {};
  176. if (isArray(from)) {
  177. if (!isArray(to) && force) {
  178. to = [];
  179. }
  180. if (isArray(to)) {
  181. from.forEach(function (item, index) {
  182. to[index] = mergeObject(item, to[index], force, shallow);
  183. });
  184. }
  185. } else if (from) {
  186. if (!isPlainObj(from)) {
  187. if (force) {
  188. to = from;
  189. }
  190. } else {
  191. for (var key in from) {
  192. if (_typeof(from[key]) === 'object') {
  193. if (isUndef(to[key])) {
  194. to[key] = deepCopy(from[key], to[key], shallow);
  195. } else {
  196. mergeObject(from[key], to[key], force, shallow);
  197. }
  198. } else {
  199. if (isUndef(to[key]) || force) to[key] = from[key];
  200. }
  201. }
  202. }
  203. }
  204. return to;
  205. }
  206. function defineReactive(target, key, source, souceKey) {
  207. /* istanbul ignore if */
  208. if (!source[key] && typeof source !== 'function') {
  209. return;
  210. }
  211. souceKey = souceKey || key;
  212. Object.defineProperty(target, key, {
  213. get: function get$$1() {
  214. return source[souceKey];
  215. },
  216. configurable: true
  217. });
  218. }
  219. var scrollBarWidth = void 0;
  220. function getGutter() {
  221. /* istanbul ignore next */
  222. if (isServer()) return 0;
  223. if (scrollBarWidth !== undefined) return scrollBarWidth;
  224. var outer = document.createElement('div');
  225. outer.style.visibility = 'hidden';
  226. outer.style.width = '100px';
  227. outer.style.position = 'absolute';
  228. outer.style.top = '-9999px';
  229. document.body.appendChild(outer);
  230. var widthNoScroll = outer.offsetWidth;
  231. outer.style.overflow = 'scroll';
  232. var inner = document.createElement('div');
  233. inner.style.width = '100%';
  234. outer.appendChild(inner);
  235. var widthWithScroll = inner.offsetWidth;
  236. outer.parentNode.removeChild(outer);
  237. scrollBarWidth = widthNoScroll - widthWithScroll;
  238. return scrollBarWidth;
  239. }
  240. function eventCenter(dom, eventName, hander) {
  241. var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  242. var type = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 'on';
  243. type == 'on' ? dom.addEventListener(eventName, hander, capture) : dom.removeEventListener(eventName, hander, capture);
  244. }
  245. var warn = function warn(msg) {
  246. console.warn('[vuescroll] ' + msg);
  247. };
  248. function isChildInParent(child, parent) {
  249. var flag = false;
  250. if (!child || !parent) {
  251. return flag;
  252. }
  253. while (child.parentNode !== parent && child.parentNode.nodeType !== 9 && !child.parentNode._isVuescroll) {
  254. child = child.parentNode;
  255. }
  256. if (child.parentNode == parent) {
  257. flag = true;
  258. }
  259. return flag;
  260. }
  261. function getPrefix(global) {
  262. var docStyle = document.documentElement.style;
  263. var engine;
  264. /* istanbul ignore if */
  265. if (global.opera && Object.prototype.toString.call(opera) === '[object Opera]') {
  266. engine = 'presto';
  267. } /* istanbul ignore next */else if ('MozAppearance' in docStyle) {
  268. engine = 'gecko';
  269. } else if ('WebkitAppearance' in docStyle) {
  270. engine = 'webkit';
  271. } /* istanbul ignore next */else if (typeof navigator.cpuClass === 'string') {
  272. engine = 'trident';
  273. }
  274. var vendorPrefix = {
  275. trident: 'ms',
  276. gecko: 'moz',
  277. webkit: 'webkit',
  278. presto: 'O'
  279. }[engine];
  280. return vendorPrefix;
  281. }
  282. function getComplitableStyle(property, value) {
  283. /* istanbul ignore if */
  284. if (isServer()) return false;
  285. var compatibleValue = '-' + getPrefix(window) + '-' + value;
  286. var testElm = document.createElement('div');
  287. testElm.style[property] = compatibleValue;
  288. if (testElm.style[property] == compatibleValue) {
  289. return compatibleValue;
  290. }
  291. /* istanbul ignore next */
  292. return false;
  293. }
  294. /**
  295. * Insert children into user-passed slot at vnode level
  296. */
  297. function insertChildrenIntoSlot(h) {
  298. var parentVnode = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  299. var childVNode = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  300. var data = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  301. var swapChildren = arguments[4];
  302. /* istanbul ignore if */
  303. if (parentVnode && parentVnode.length > 1) {
  304. return swapChildren ? [].concat(toConsumableArray(childVNode), toConsumableArray(parentVnode)) : [].concat(toConsumableArray(parentVnode), toConsumableArray(childVNode));
  305. }
  306. parentVnode = parentVnode[0];
  307. var _getVnodeInfo = getVnodeInfo(parentVnode),
  308. ch = _getVnodeInfo.ch,
  309. tag = _getVnodeInfo.tag,
  310. isComponent = _getVnodeInfo.isComponent;
  311. if (isComponent) {
  312. parentVnode.data = mergeObject({ attrs: parentVnode.componentOptions.propsData }, parentVnode.data, false, // force: false
  313. true // shallow: true
  314. );
  315. }
  316. ch = swapChildren ? [].concat(toConsumableArray(childVNode), toConsumableArray(ch)) : [].concat(toConsumableArray(ch), toConsumableArray(childVNode));
  317. delete parentVnode.data.slot;
  318. return h(tag, mergeObject(data, parentVnode.data, false, true), ch);
  319. }
  320. /**
  321. * Get the info of a vnode,
  322. * vnode must be parentVnode
  323. */
  324. function getVnodeInfo(vnode) {
  325. if (!vnode || vnode.length > 1) return {};
  326. vnode = vnode[0] ? vnode[0] : vnode;
  327. var isComponent = !!vnode.componentOptions;
  328. var ch = void 0;
  329. var tag = void 0;
  330. if (isComponent) {
  331. ch = vnode.componentOptions.children || [];
  332. tag = vnode.componentOptions.tag;
  333. } else {
  334. ch = vnode.children || [];
  335. tag = vnode.tag;
  336. }
  337. return {
  338. isComponent: isComponent,
  339. ch: ch,
  340. tag: tag
  341. };
  342. }
  343. /**
  344. * Get the vuescroll instance instead of
  345. * user pass component like slot.
  346. */
  347. function getRealParent(ctx) {
  348. var parent = ctx.$parent;
  349. if (!parent._isVuescrollRoot && parent) {
  350. parent = parent.$parent;
  351. }
  352. return parent;
  353. }
  354. var isArray = function isArray(_) {
  355. return Array.isArray(_);
  356. };
  357. var isPlainObj = function isPlainObj(_) {
  358. return Object.prototype.toString.call(_) == '[object Object]';
  359. };
  360. var isUndef = function isUndef(_) {
  361. return typeof _ === 'undefined';
  362. };
  363. function getNumericValue(distance, size) {
  364. var number = void 0;
  365. if (!(number = /(-?\d+(?:\.\d+?)?)%$/.exec(distance))) {
  366. number = distance - 0;
  367. } else {
  368. number = number[1] - 0;
  369. number = size * number / 100;
  370. }
  371. return number;
  372. }
  373. function createStyle(styleId, cssText) {
  374. /* istanbul ignore if */
  375. if (isServer() || document.getElementById(styleId)) {
  376. return;
  377. }
  378. var head = document.head || doc.getElementsByTagName('head')[0];
  379. var style = document.createElement('style');
  380. style.id = styleId;
  381. style.type = 'text/css';
  382. /* istanbul ignore if */
  383. if (style.styleSheet) {
  384. style.styleSheet.cssText = cssText;
  385. } else {
  386. style.appendChild(document.createTextNode(cssText));
  387. }
  388. head.appendChild(style);
  389. }
  390. // Hide the ios native scrollbar.
  391. function createHideBarStyle() {
  392. /* istanbul ignore next */
  393. {
  394. var cssText = '.__hidebar::-webkit-scrollbar {\n width: 0;\n height: 0;\n }';
  395. createStyle('vuescroll-hide-ios-bar', cssText);
  396. }
  397. }
  398. // create slide mode style
  399. var api = {
  400. mounted: function mounted() {
  401. vsInstances[this._uid] = this;
  402. },
  403. beforeDestroy: function beforeDestroy() {
  404. delete vsInstances[this._uid];
  405. },
  406. methods: {
  407. // public api
  408. scrollTo: function scrollTo(_ref, speed, easing) {
  409. var x = _ref.x,
  410. y = _ref.y;
  411. // istanbul ignore if
  412. if (speed === true || typeof speed == 'undefined') {
  413. speed = this.mergedOptions.scrollPanel.speed;
  414. }
  415. this.internalScrollTo(x, y, speed, easing);
  416. },
  417. scrollBy: function scrollBy(_ref2, speed, easing) {
  418. var _ref2$dx = _ref2.dx,
  419. dx = _ref2$dx === undefined ? 0 : _ref2$dx,
  420. _ref2$dy = _ref2.dy,
  421. dy = _ref2$dy === undefined ? 0 : _ref2$dy;
  422. var _getPosition = this.getPosition(),
  423. _getPosition$scrollLe = _getPosition.scrollLeft,
  424. scrollLeft = _getPosition$scrollLe === undefined ? 0 : _getPosition$scrollLe,
  425. _getPosition$scrollTo = _getPosition.scrollTop,
  426. scrollTop = _getPosition$scrollTo === undefined ? 0 : _getPosition$scrollTo;
  427. if (dx) {
  428. scrollLeft += getNumericValue(dx, this.scrollPanelElm.scrollWidth - this.$el.clientWidth);
  429. }
  430. if (dy) {
  431. scrollTop += getNumericValue(dy, this.scrollPanelElm.scrollHeight - this.$el.clientHeight);
  432. }
  433. this.internalScrollTo(scrollLeft, scrollTop, speed, easing);
  434. },
  435. scrollIntoView: function scrollIntoView(elm) {
  436. var animate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  437. var parentElm = this.$el;
  438. if (typeof elm === 'string') {
  439. elm = parentElm.querySelector(elm);
  440. }
  441. if (!isChildInParent(elm, parentElm)) {
  442. warn('The element or selector you passed is not the element of Vuescroll, please pass the element that is in Vuescroll to scrollIntoView API. ');
  443. return;
  444. }
  445. // parent elm left, top
  446. var _$el$getBoundingClien = this.$el.getBoundingClientRect(),
  447. left = _$el$getBoundingClien.left,
  448. top = _$el$getBoundingClien.top;
  449. // child elm left, top
  450. var _elm$getBoundingClien = elm.getBoundingClientRect(),
  451. childLeft = _elm$getBoundingClien.left,
  452. childTop = _elm$getBoundingClien.top;
  453. var diffX = left - childLeft;
  454. var diffY = top - childTop;
  455. this.scrollBy({
  456. dx: -diffX,
  457. dy: -diffY
  458. }, animate);
  459. },
  460. refresh: function refresh() {
  461. this.refreshInternalStatus();
  462. // refresh again to keep status is correct
  463. this.$nextTick(this.refreshInternalStatus);
  464. }
  465. }
  466. };
  467. /** Public Api */
  468. /**
  469. * Refresh all
  470. */
  471. var vsInstances = {};
  472. function refreshAll() {
  473. for (var vs in vsInstances) {
  474. vsInstances[vs].refresh();
  475. }
  476. }
  477. var baseConfig = {
  478. // vuescroll
  479. vuescroll: {
  480. // vuescroll's size(height/width) should be a percent(100%)
  481. // or be a number that is equal to its parentNode's width or
  482. // height ?
  483. sizeStrategy: 'percent',
  484. /** Whether to detect dom resize or not */
  485. detectResize: true
  486. },
  487. scrollPanel: {
  488. // when component mounted.. it will automatically scrolls.
  489. initialScrollY: false,
  490. initialScrollX: false,
  491. // feat: #11
  492. scrollingX: true,
  493. scrollingY: true,
  494. speed: 300,
  495. easing: undefined,
  496. // Sometimes, the nativebar maybe on the left,
  497. // See https://github.com/YvesCoding/vuescroll/issues/64
  498. verticalNativeBarPos: 'right',
  499. maxHeight: undefined,
  500. maxWidth: undefined
  501. },
  502. //
  503. rail: {
  504. background: '#01a99a',
  505. opacity: 0,
  506. border: 'none',
  507. /** Rail's size(Height/Width) , default -> 6px */
  508. size: '6px',
  509. /** Specify rail's border-radius, or the border-radius of rail and bar will be equal to the rail's size. default -> false **/
  510. specifyBorderRadius: false,
  511. /** Rail the distance from the two ends of the X axis and Y axis. **/
  512. gutterOfEnds: null,
  513. /** Rail the distance from the side of container. **/
  514. gutterOfSide: '2px',
  515. /** Whether to keep rail show or not, default -> false, event content height is not enough */
  516. keepShow: false
  517. },
  518. bar: {
  519. /** How long to hide bar after mouseleave, default -> 500 */
  520. showDelay: 500,
  521. /** Specify bar's border-radius, or the border-radius of rail and bar will be equal to the rail's size. default -> false **/
  522. specifyBorderRadius: false,
  523. /** Whether to show bar on scrolling, default -> true */
  524. onlyShowBarOnScroll: true,
  525. /** Whether to keep show or not, default -> false */
  526. keepShow: false,
  527. /** Bar's background , default -> #00a650 */
  528. background: 'rgb(3, 185, 118)',
  529. /** Bar's opacity, default -> 1 */
  530. opacity: 1,
  531. /** bar's size(Height/Width) , default -> 6px */
  532. size: '6px',
  533. minSize: 0,
  534. disable: false
  535. },
  536. scrollButton: {
  537. enable: false,
  538. background: 'rgb(3, 185, 118)',
  539. opacity: 1,
  540. step: 180,
  541. mousedownStep: 30
  542. }
  543. };
  544. /**
  545. * validate the options
  546. * @export
  547. * @param {any} ops
  548. */
  549. function validateOps(ops) {
  550. var renderError = false;
  551. var scrollPanel = ops.scrollPanel;
  552. var _ops$bar = ops.bar,
  553. vBar = _ops$bar.vBar,
  554. hBar = _ops$bar.hBar;
  555. var _ops$rail = ops.rail,
  556. vRail = _ops$rail.vRail,
  557. hRail = _ops$rail.hRail;
  558. // validate scrollPanel
  559. var initialScrollY = scrollPanel['initialScrollY'];
  560. var initialScrollX = scrollPanel['initialScrollX'];
  561. if (initialScrollY && !String(initialScrollY).match(/^\d+(\.\d+)?(%)?$/)) {
  562. warn('The prop `initialScrollY` or `initialScrollX` should be a percent number like `10%` or an exact number that greater than or equal to 0 like `100`.');
  563. }
  564. if (initialScrollX && !String(initialScrollX).match(/^\d+(\.\d+)?(%)?$/)) {
  565. warn('The prop `initialScrollY` or `initialScrollX` should be a percent number like `10%` or an exact number that greater than or equal to 0 like `100`.');
  566. }
  567. // validate deprecated vBar/hBar vRail/hRail
  568. if (vBar || hBar || vRail || hRail) {
  569. warn('The options: vRail, hRail, vBar, hBar have been deprecated since v4.7.0,' + 'please use corresponing rail/bar instead!');
  570. }
  571. if (_extraValidate) {
  572. _extraValidate = [].concat(_extraValidate);
  573. _extraValidate.forEach(function (hasError) {
  574. if (hasError(ops)) {
  575. renderError = true;
  576. }
  577. });
  578. }
  579. return renderError;
  580. }
  581. var _extraValidate = null;
  582. var extendOpts = function extendOpts(extraOpts, extraValidate) {
  583. extraOpts = [].concat(extraOpts);
  584. extraOpts.forEach(function (opts) {
  585. mergeObject(opts, baseConfig);
  586. });
  587. _extraValidate = extraValidate;
  588. };
  589. // all modes
  590. // do nothing
  591. // some small changes.
  592. var smallChangeArray = ['mergedOptions.vuescroll.pullRefresh.tips', 'mergedOptions.vuescroll.pushLoad.tips', 'mergedOptions.vuescroll.scroller.disable', 'mergedOptions.rail', 'mergedOptions.bar'];
  593. // refresh/load dom ref/key...
  594. var scrollMap = {
  595. vertical: {
  596. size: 'height',
  597. opsSize: 'width',
  598. posName: 'top',
  599. opposName: 'bottom',
  600. sidePosName: 'right',
  601. page: 'pageY',
  602. scroll: 'scrollTop',
  603. scrollSize: 'scrollHeight',
  604. offset: 'offsetHeight',
  605. client: 'clientY',
  606. axis: 'Y',
  607. scrollButton: {
  608. start: 'top',
  609. end: 'bottom'
  610. }
  611. },
  612. horizontal: {
  613. size: 'width',
  614. opsSize: 'height',
  615. posName: 'left',
  616. opposName: 'right',
  617. sidePosName: 'bottom',
  618. page: 'pageX',
  619. scroll: 'scrollLeft',
  620. scrollSize: 'scrollWidth',
  621. offset: 'offsetWidth',
  622. client: 'clientX',
  623. axis: 'X',
  624. scrollButton: {
  625. start: 'left',
  626. end: 'right'
  627. }
  628. }
  629. };
  630. function requestAnimationFrame(global) {
  631. // Check for request animation Frame support
  632. var requestFrame = global.requestAnimationFrame || global.webkitRequestAnimationFrame || global.mozRequestAnimationFrame || global.oRequestAnimationFrame;
  633. var isNative = !!requestFrame;
  634. if (requestFrame && !/requestAnimationFrame\(\)\s*\{\s*\[native code\]\s*\}/i.test(requestFrame.toString())) {
  635. isNative = false;
  636. }
  637. if (isNative) {
  638. return function (callback, root) {
  639. requestFrame(callback, root);
  640. };
  641. }
  642. var TARGET_FPS = 60;
  643. var requests = {};
  644. var rafHandle = 1;
  645. var intervalHandle = null;
  646. var lastActive = +new Date();
  647. return function (callback) {
  648. var callbackHandle = rafHandle++;
  649. // Store callback
  650. requests[callbackHandle] = callback;
  651. // Create timeout at first request
  652. if (intervalHandle === null) {
  653. intervalHandle = setInterval(function () {
  654. var time = +new Date();
  655. var currentRequests = requests;
  656. // Reset data structure before executing callbacks
  657. requests = {};
  658. for (var key in currentRequests) {
  659. if (currentRequests.hasOwnProperty(key)) {
  660. currentRequests[key](time);
  661. lastActive = time;
  662. }
  663. }
  664. // Disable the timeout when nothing happens for a certain
  665. // period of time
  666. if (time - lastActive > 2500) {
  667. clearInterval(intervalHandle);
  668. intervalHandle = null;
  669. }
  670. }, 1000 / TARGET_FPS);
  671. }
  672. return callbackHandle;
  673. };
  674. }
  675. var colorCache = {};
  676. var rgbReg = /rgb\(/;
  677. var extractRgbColor = /rgb\((.*)\)/;
  678. // Transform a common color int oa `rgbA` color
  679. function getRgbAColor(color, opacity) {
  680. var id = color + '&' + opacity;
  681. if (colorCache[id]) {
  682. return colorCache[id];
  683. }
  684. var div = document.createElement('div');
  685. div.style.background = color;
  686. document.body.appendChild(div);
  687. var computedColor = window.getComputedStyle(div).backgroundColor;
  688. document.body.removeChild(div);
  689. /* istanbul ignore if */
  690. if (!rgbReg.test(computedColor)) {
  691. return color;
  692. }
  693. return colorCache[id] = 'rgba(' + extractRgbColor.exec(computedColor)[1] + ', ' + opacity + ')';
  694. }
  695. var bar = {
  696. name: 'bar',
  697. props: {
  698. ops: Object,
  699. state: Object,
  700. hideBar: Boolean,
  701. otherBarHide: Boolean,
  702. type: String
  703. },
  704. computed: {
  705. bar: function bar() {
  706. return scrollMap[this.type];
  707. },
  708. barSize: function barSize() {
  709. return Math.max(this.state.size, this.ops.bar.minSize);
  710. },
  711. barRatio: function barRatio() {
  712. return (1 - this.barSize) / (1 - this.state.size);
  713. }
  714. },
  715. render: function render(h) {
  716. var _style, _style2, _barStyle;
  717. var vm = this;
  718. /** Get rgbA format background color */
  719. var railBackgroundColor = getRgbAColor(vm.ops.rail.background, vm.ops.rail.opacity);
  720. if (!this.touchManager) {
  721. this.touchManager = new touchManager();
  722. }
  723. /** Rail Data */
  724. var railSize = vm.ops.rail.size;
  725. var endPos = vm.otherBarHide ? 0 : railSize;
  726. var touchObj = vm.touchManager.getTouchObject();
  727. var rail = {
  728. class: '__rail-is-' + vm.type,
  729. style: (_style = {
  730. position: 'absolute',
  731. 'z-index': '1',
  732. borderRadius: vm.ops.rail.specifyBorderRadius || railSize,
  733. background: railBackgroundColor,
  734. border: vm.ops.rail.border
  735. }, defineProperty(_style, vm.bar.opsSize, railSize), defineProperty(_style, vm.bar.posName, vm.ops.rail['gutterOfEnds'] || 0), defineProperty(_style, vm.bar.opposName, vm.ops.rail['gutterOfEnds'] || endPos), defineProperty(_style, vm.bar.sidePosName, vm.ops.rail['gutterOfSide']), _style)
  736. };
  737. if (touchObj) {
  738. var _rail$on;
  739. rail.on = (_rail$on = {}, defineProperty(_rail$on, touchObj.touchenter, function () {
  740. vm.setRailHover();
  741. }), defineProperty(_rail$on, touchObj.touchleave, function () {
  742. vm.setRailLeave();
  743. }), _rail$on);
  744. }
  745. // left space for scroll button
  746. var buttonSize = vm.ops.scrollButton.enable ? railSize : 0;
  747. var barWrapper = {
  748. class: '__bar-wrap-is-' + vm.type,
  749. style: (_style2 = {
  750. position: 'absolute',
  751. borderRadius: vm.ops.rail.specifyBorderRadius || railSize
  752. }, defineProperty(_style2, vm.bar.posName, buttonSize), defineProperty(_style2, vm.bar.opposName, buttonSize), _style2),
  753. on: {}
  754. };
  755. var scrollDistance = vm.state.posValue * vm.state.size;
  756. var pos = scrollDistance * vm.barRatio / vm.barSize;
  757. var opacity = vm.state.opacity;
  758. var parent = getRealParent(this);
  759. // set class hook
  760. parent.setClassHook(this.type == 'vertical' ? 'vBarVisible' : 'hBarVisible', !!opacity);
  761. /** Scrollbar style */
  762. var barStyle = (_barStyle = {
  763. cursor: 'pointer',
  764. position: 'absolute',
  765. margin: 'auto',
  766. transition: 'opacity 0.5s',
  767. 'user-select': 'none',
  768. 'border-radius': 'inherit'
  769. }, defineProperty(_barStyle, vm.bar.size, vm.barSize * 100 + '%'), defineProperty(_barStyle, 'background', vm.ops.bar.background), defineProperty(_barStyle, vm.bar.opsSize, vm.ops.bar.size), defineProperty(_barStyle, 'opacity', opacity), defineProperty(_barStyle, 'transform', 'translate' + scrollMap[vm.type].axis + '(' + pos + '%)'), _barStyle);
  770. var bar = {
  771. style: barStyle,
  772. class: '__bar-is-' + vm.type,
  773. ref: 'thumb',
  774. on: {}
  775. };
  776. if (vm.type == 'vertical') {
  777. barWrapper.style.width = '100%';
  778. // Let bar to be on the center.
  779. bar.style.left = 0;
  780. bar.style.right = 0;
  781. } else {
  782. barWrapper.style.height = '100%';
  783. bar.style.top = 0;
  784. bar.style.bottom = 0;
  785. }
  786. /* istanbul ignore next */
  787. {
  788. var _touchObj = this.touchManager.getTouchObject();
  789. bar.on[_touchObj.touchstart] = this.createBarEvent();
  790. barWrapper.on[_touchObj.touchstart] = this.createTrackEvent();
  791. }
  792. return h(
  793. 'div',
  794. rail,
  795. [this.createScrollbarButton(h, 'start'), this.hideBar ? null : h(
  796. 'div',
  797. barWrapper,
  798. [h('div', bar)]
  799. ), this.createScrollbarButton(h, 'end')]
  800. );
  801. },
  802. data: function data() {
  803. return {
  804. isBarDragging: false
  805. };
  806. },
  807. methods: {
  808. setRailHover: function setRailHover() {
  809. var parent = getRealParent(this);
  810. var state = parent.vuescroll.state;
  811. if (!state.isRailHover) {
  812. state.isRailHover = true;
  813. parent.showBar();
  814. }
  815. },
  816. setRailLeave: function setRailLeave() {
  817. var parent = getRealParent(this);
  818. var state = parent.vuescroll.state;
  819. state.isRailHover = false;
  820. parent.hideBar();
  821. },
  822. setBarDrag: function setBarDrag(val) /* istanbul ignore next */{
  823. this.$emit('setBarDrag', this.isBarDragging = val);
  824. var parent = getRealParent(this);
  825. // set class hook
  826. parent.setClassHook(this.type == 'vertical' ? 'vBarDragging' : 'hBarDragging', !!val);
  827. },
  828. createBarEvent: function createBarEvent() {
  829. var ctx = this;
  830. var parent = getRealParent(ctx);
  831. var touchObj = ctx.touchManager.getTouchObject();
  832. function mousedown(e) /* istanbul ignore next */{
  833. var event = ctx.touchManager.getEventObject(e);
  834. if (!event) return;
  835. e.stopImmediatePropagation();
  836. e.preventDefault();
  837. event = event[0];
  838. document.onselectstart = function () {
  839. return false;
  840. };
  841. ctx.axisStartPos = event[ctx.bar.client] - ctx.$refs['thumb'].getBoundingClientRect()[ctx.bar.posName];
  842. // Tell parent that the mouse has been down.
  843. ctx.setBarDrag(true);
  844. eventCenter(document, touchObj.touchmove, mousemove);
  845. eventCenter(document, touchObj.touchend, mouseup);
  846. }
  847. function mousemove(e) /* istanbul ignore next */{
  848. if (!ctx.axisStartPos) {
  849. return;
  850. }
  851. var event = ctx.touchManager.getEventObject(e);
  852. if (!event) return;
  853. event = event[0];
  854. var thubmParent = ctx.$refs.thumb.parentNode;
  855. var delta = event[ctx.bar.client] - thubmParent.getBoundingClientRect()[ctx.bar.posName];
  856. delta = delta / ctx.barRatio;
  857. var percent = (delta - ctx.axisStartPos) / thubmParent[ctx.bar.offset];
  858. parent.scrollTo(defineProperty({}, ctx.bar.axis.toLowerCase(), parent.scrollPanelElm[ctx.bar.scrollSize] * percent), false);
  859. }
  860. function mouseup() /* istanbul ignore next */{
  861. ctx.setBarDrag(false);
  862. parent.hideBar();
  863. document.onselectstart = null;
  864. ctx.axisStartPos = 0;
  865. eventCenter(document, touchObj.touchmove, mousemove, false, 'off');
  866. eventCenter(document, touchObj.touchend, mouseup, false, 'off');
  867. }
  868. return mousedown;
  869. },
  870. createTrackEvent: function createTrackEvent() {
  871. var ctx = this;
  872. return function handleClickTrack(e) {
  873. var parent = getRealParent(ctx);
  874. var _ctx$bar = ctx.bar,
  875. client = _ctx$bar.client,
  876. offset = _ctx$bar.offset,
  877. posName = _ctx$bar.posName,
  878. axis = _ctx$bar.axis;
  879. var thumb = ctx.$refs['thumb'];
  880. e.preventDefault();
  881. e.stopImmediatePropagation();
  882. /* istanbul ignore if */
  883. if (!thumb) return;
  884. var barOffset = thumb[offset];
  885. var event = ctx.touchManager.getEventObject(e)[0];
  886. var percent = (event[client] - e.currentTarget.getBoundingClientRect()[posName] - barOffset / 2) / (e.currentTarget[offset] - barOffset);
  887. parent.scrollTo(defineProperty({}, axis.toLowerCase(), percent * 100 + '%'));
  888. };
  889. },
  890. // Scrollbuton relative things...
  891. createScrollbarButton: function createScrollbarButton(h, type /* start or end */) {
  892. var _style3;
  893. var barContext = this;
  894. if (!barContext.ops.scrollButton.enable) {
  895. return null;
  896. }
  897. var size = barContext.ops.rail.size;
  898. var _barContext$ops$scrol = barContext.ops.scrollButton,
  899. opacity = _barContext$ops$scrol.opacity,
  900. background = _barContext$ops$scrol.background;
  901. var borderColor = getRgbAColor(background, opacity);
  902. var wrapperProps = {
  903. class: ['__bar-button', '__bar-button-is-' + barContext.type + '-' + type],
  904. style: (_style3 = {}, defineProperty(_style3, barContext.bar.scrollButton[type], 0), defineProperty(_style3, 'width', size), defineProperty(_style3, 'height', size), defineProperty(_style3, 'position', 'absolute'), defineProperty(_style3, 'cursor', 'pointer'), defineProperty(_style3, 'display', 'table'), _style3),
  905. ref: type
  906. };
  907. var innerProps = {
  908. class: '__bar-button-inner',
  909. style: {
  910. border: 'calc(' + size + ' / 2.5) solid transparent',
  911. width: '0',
  912. height: '0',
  913. margin: 'auto',
  914. position: 'absolute',
  915. top: '0',
  916. bottom: '0',
  917. right: '0',
  918. left: '0'
  919. },
  920. on: {}
  921. };
  922. if (barContext.type == 'vertical') {
  923. if (type == 'start') {
  924. innerProps.style['border-bottom-color'] = borderColor;
  925. innerProps.style['transform'] = 'translateY(-25%)';
  926. } else {
  927. innerProps.style['border-top-color'] = borderColor;
  928. innerProps.style['transform'] = 'translateY(25%)';
  929. }
  930. } else {
  931. if (type == 'start') {
  932. innerProps.style['border-right-color'] = borderColor;
  933. innerProps.style['transform'] = 'translateX(-25%)';
  934. } else {
  935. innerProps.style['border-left-color'] = borderColor;
  936. innerProps.style['transform'] = 'translateX(25%)';
  937. }
  938. }
  939. /* istanbul ignore next */
  940. {
  941. var touchObj = this.touchManager.getTouchObject();
  942. innerProps.on[touchObj.touchstart] = this.createScrollButtonEvent(type, touchObj);
  943. }
  944. return h(
  945. 'div',
  946. wrapperProps,
  947. [h('div', innerProps)]
  948. );
  949. },
  950. createScrollButtonEvent: function createScrollButtonEvent(type, touchObj) {
  951. var ctx = this;
  952. var parent = getRealParent(ctx);
  953. var _ctx$ops$scrollButton = ctx.ops.scrollButton,
  954. step = _ctx$ops$scrollButton.step,
  955. mousedownStep = _ctx$ops$scrollButton.mousedownStep;
  956. var stepWithDirection = type == 'start' ? -step : step;
  957. var mousedownStepWithDirection = type == 'start' ? -mousedownStep : mousedownStep;
  958. var ref = requestAnimationFrame(window);
  959. // bar props: type
  960. var barType = ctx.type;
  961. var isMouseDown = false;
  962. var isMouseout = true;
  963. var timeoutId = void 0;
  964. function start(e) {
  965. /* istanbul ignore if */
  966. if (3 == e.which) {
  967. return;
  968. }
  969. // set class hook
  970. parent.setClassHook('cliking' + barType + type + 'Button', true);
  971. e.stopImmediatePropagation();
  972. e.preventDefault();
  973. isMouseout = false;
  974. parent.scrollBy(defineProperty({}, 'd' + ctx.bar.axis.toLowerCase(), stepWithDirection));
  975. eventCenter(document, touchObj.touchend, endPress, false);
  976. if (touchObj.touchstart == 'mousedown') {
  977. var elm = ctx.$refs[type];
  978. eventCenter(elm, 'mouseenter', enter, false);
  979. eventCenter(elm, 'mouseleave', leave, false);
  980. }
  981. clearTimeout(timeoutId);
  982. timeoutId = setTimeout(function () /* istanbul ignore next */{
  983. isMouseDown = true;
  984. ref(pressing, window);
  985. }, 500);
  986. }
  987. function pressing() /* istanbul ignore next */{
  988. if (isMouseDown && !isMouseout) {
  989. parent.scrollBy(defineProperty({}, 'd' + ctx.bar.axis.toLowerCase(), mousedownStepWithDirection), false);
  990. ref(pressing, window);
  991. }
  992. }
  993. function endPress() {
  994. clearTimeout(timeoutId);
  995. isMouseDown = false;
  996. eventCenter(document, touchObj.touchend, endPress, false, 'off');
  997. if (touchObj.touchstart == 'mousedown') {
  998. var elm = ctx.$refs[type];
  999. eventCenter(elm, 'mouseenter', enter, false, 'off');
  1000. eventCenter(elm, 'mouseleave', leave, false, 'off');
  1001. }
  1002. parent.setClassHook('cliking' + barType + type + 'Button', false);
  1003. }
  1004. function enter() /* istanbul ignore next */{
  1005. isMouseout = false;
  1006. pressing();
  1007. }
  1008. function leave() /* istanbul ignore next */{
  1009. isMouseout = true;
  1010. }
  1011. return start;
  1012. }
  1013. }
  1014. };
  1015. function getBarData(vm, type) {
  1016. var axis = scrollMap[type].axis;
  1017. /** type.charAt(0) = vBar/hBar */
  1018. var barType = type.charAt(0) + 'Bar';
  1019. var hideBar = !vm.bar[barType].state.size || !vm.mergedOptions.scrollPanel['scrolling' + axis] || vm.refreshLoad && type !== 'vertical' || vm.mergedOptions.bar.disable;
  1020. var keepShowRail = vm.mergedOptions.rail.keepShow;
  1021. if (hideBar && !keepShowRail) {
  1022. return null;
  1023. }
  1024. return {
  1025. hideBar: hideBar,
  1026. props: {
  1027. type: type,
  1028. ops: {
  1029. bar: vm.mergedOptions.bar,
  1030. rail: vm.mergedOptions.rail,
  1031. scrollButton: vm.mergedOptions.scrollButton
  1032. },
  1033. state: vm.bar[barType].state,
  1034. hideBar: hideBar
  1035. },
  1036. on: {
  1037. setBarDrag: vm.setBarDrag
  1038. },
  1039. ref: type + 'Bar',
  1040. key: type
  1041. };
  1042. }
  1043. /**
  1044. * create bars
  1045. *
  1046. * @param {any} size
  1047. * @param {any} type
  1048. */
  1049. function createBar(h, vm) {
  1050. var verticalBarProps = getBarData(vm, 'vertical');
  1051. var horizontalBarProps = getBarData(vm, 'horizontal');
  1052. // set class hooks
  1053. vm.setClassHook('hasVBar', !!(verticalBarProps && !verticalBarProps.hideBar));
  1054. vm.setClassHook('hasHBar', !!(horizontalBarProps && !horizontalBarProps.hideBar));
  1055. return [verticalBarProps ? h('bar', _extends({}, verticalBarProps, {
  1056. props: _extends({ otherBarHide: !horizontalBarProps }, verticalBarProps.props)
  1057. })) : null, horizontalBarProps ? h('bar', _extends({}, horizontalBarProps, {
  1058. props: _extends({ otherBarHide: !verticalBarProps }, horizontalBarProps.props)
  1059. })) : null];
  1060. }
  1061. /**
  1062. * This is like a HOC, It extracts the common parts of the
  1063. * native-mode, slide-mode and mix-mode.
  1064. * Each mode must implement the following methods:
  1065. * 1. refreshInternalStatus : use to refresh the component
  1066. * 2. destroy : Destroy some registryed events before component destroy.
  1067. * 3. updateBarStateAndEmitEvent: use to update bar states and emit events.
  1068. */
  1069. var createComponent = function createComponent(_ref) {
  1070. var _render = _ref.render,
  1071. components = _ref.components,
  1072. mixins = _ref.mixins;
  1073. return {
  1074. name: 'vueScroll',
  1075. props: {
  1076. ops: { type: Object }
  1077. },
  1078. components: components,
  1079. mixins: [api].concat(toConsumableArray([].concat(mixins))),
  1080. created: function created() {
  1081. var _this = this;
  1082. /**
  1083. * Begin to merge options
  1084. */
  1085. var _gfc = mergeObject(this.$vuescrollConfig || {}, {});
  1086. var ops = mergeObject(baseConfig, _gfc);
  1087. this.$options.propsData.ops = this.$options.propsData.ops || {};
  1088. Object.keys(this.$options.propsData.ops).forEach(function (key) {
  1089. {
  1090. defineReactive(_this.mergedOptions, key, _this.$options.propsData.ops);
  1091. }
  1092. });
  1093. // from ops to mergedOptions
  1094. mergeObject(ops, this.mergedOptions);
  1095. this._isVuescrollRoot = true;
  1096. this.renderError = validateOps(this.mergedOptions);
  1097. },
  1098. render: function render(h) {
  1099. var vm = this;
  1100. if (vm.renderError) {
  1101. return h('div', [[vm.$slots['default']]]);
  1102. }
  1103. if (!vm.touchManager) vm.touchManager = new touchManager();
  1104. // vuescroll data
  1105. var data = {
  1106. style: {
  1107. height: vm.vuescroll.state.height,
  1108. width: vm.vuescroll.state.width,
  1109. padding: 0,
  1110. position: 'relative',
  1111. overflow: 'hidden'
  1112. },
  1113. class: _extends({ __vuescroll: true }, vm.classHooks)
  1114. };
  1115. var touchObj = vm.touchManager.getTouchObject();
  1116. if (touchObj) {
  1117. var _data$on;
  1118. data.on = (_data$on = {}, defineProperty(_data$on, touchObj.touchenter, function () {
  1119. vm.vuescroll.state.pointerLeave = false;
  1120. vm.updateBarStateAndEmitEvent();
  1121. vm.setClassHook('mouseEnter', true);
  1122. }), defineProperty(_data$on, touchObj.touchleave, function () {
  1123. vm.vuescroll.state.pointerLeave = true;
  1124. vm.hideBar();
  1125. vm.setClassHook('mouseEnter', false);
  1126. }), defineProperty(_data$on, touchObj.touchmove, function () /* istanbul ignore next */{
  1127. vm.vuescroll.state.pointerLeave = false;
  1128. vm.updateBarStateAndEmitEvent();
  1129. }), _data$on);
  1130. }
  1131. var ch = [_render(h, vm)].concat(toConsumableArray(createBar(h, vm)));
  1132. var _customContainer = this.$slots['scroll-container'];
  1133. if (_customContainer) {
  1134. return insertChildrenIntoSlot(h, _customContainer, ch, data);
  1135. }
  1136. return h(
  1137. 'div',
  1138. data,
  1139. [ch]
  1140. );
  1141. },
  1142. mounted: function mounted() {
  1143. var _this2 = this;
  1144. if (!this.renderError) {
  1145. this.initVariables();
  1146. this.initWatchOpsChange();
  1147. // Call external merged Api
  1148. this.refreshInternalStatus();
  1149. this.updatedCbs.push(function () {
  1150. _this2.scrollToAnchor();
  1151. // need to reflow to deal with the
  1152. // latest thing.
  1153. _this2.updateBarStateAndEmitEvent();
  1154. });
  1155. }
  1156. },
  1157. updated: function updated() {
  1158. var _this3 = this;
  1159. this.updatedCbs.forEach(function (cb) {
  1160. cb.call(_this3);
  1161. });
  1162. // Clear
  1163. this.updatedCbs = [];
  1164. },
  1165. beforeDestroy: function beforeDestroy() {
  1166. if (this.destroy) {
  1167. this.destroy();
  1168. }
  1169. },
  1170. /** ------------------------------- Computed ----------------------------- */
  1171. computed: {
  1172. scrollPanelElm: function scrollPanelElm() {
  1173. return this.$refs['scrollPanel']._isVue ? this.$refs['scrollPanel'].$el : this.$refs['scrollPanel'];
  1174. }
  1175. },
  1176. data: function data() {
  1177. return {
  1178. vuescroll: {
  1179. state: {
  1180. isDragging: false,
  1181. pointerLeave: true,
  1182. isRailHover: false,
  1183. /** Default sizeStrategies */
  1184. height: '100%',
  1185. width: '100%',
  1186. // current size strategy
  1187. currentSizeStrategy: 'percent',
  1188. currentScrollState: null,
  1189. currentScrollInfo: null
  1190. }
  1191. },
  1192. bar: {
  1193. vBar: {
  1194. state: {
  1195. posValue: 0,
  1196. size: 0,
  1197. opacity: 0
  1198. }
  1199. },
  1200. hBar: {
  1201. state: {
  1202. posValue: 0,
  1203. size: 0,
  1204. opacity: 0
  1205. }
  1206. }
  1207. },
  1208. mergedOptions: {
  1209. vuescroll: {},
  1210. scrollPanel: {},
  1211. scrollContent: {},
  1212. rail: {},
  1213. bar: {}
  1214. },
  1215. updatedCbs: [],
  1216. renderError: false,
  1217. classHooks: {
  1218. hasVBar: false,
  1219. hasHBar: false,
  1220. vBarVisible: false,
  1221. hBarVisible: false,
  1222. vBarDragging: false,
  1223. hBarDragging: false,
  1224. clikingVerticalStartButton: false,
  1225. clikingVerticalEndButton: false,
  1226. clikingHorizontalStartButton: false,
  1227. clikingHorizontalEndButton: false,
  1228. mouseEnter: false
  1229. }
  1230. };
  1231. },
  1232. /** ------------------------------- Methods -------------------------------- */
  1233. methods: {
  1234. /** ------------------------ Handlers --------------------------- */
  1235. scrollingComplete: function scrollingComplete() {
  1236. this.updateBarStateAndEmitEvent('handle-scroll-complete');
  1237. },
  1238. setBarDrag: function setBarDrag(val) {
  1239. /* istanbul ignore next */
  1240. this.vuescroll.state.isDragging = val;
  1241. },
  1242. setClassHook: function setClassHook(name, value) {
  1243. this.classHooks[name] = value;
  1244. },
  1245. /** ------------------------ Some Helpers --------------------------- */
  1246. /*
  1247. * To have a good ux, instead of hiding bar immediately, we hide bar
  1248. * after some seconds by using this simple debounce-hidebar method.
  1249. */
  1250. showAndDefferedHideBar: function showAndDefferedHideBar(forceHideBar) {
  1251. var _this4 = this;
  1252. this.showBar();
  1253. if (this.timeoutId) {
  1254. clearTimeout(this.timeoutId);
  1255. this.timeoutId = 0;
  1256. }
  1257. this.timeoutId = setTimeout(function () {
  1258. _this4.timeoutId = 0;
  1259. _this4.hideBar(forceHideBar);
  1260. }, this.mergedOptions.bar.showDelay);
  1261. },
  1262. showBar: function showBar() {
  1263. var opacity = this.mergedOptions.bar.opacity;
  1264. this.bar.vBar.state.opacity = opacity;
  1265. this.bar.hBar.state.opacity = opacity;
  1266. },
  1267. hideBar: function hideBar(forceHideBar) {
  1268. var _vuescroll$state = this.vuescroll.state,
  1269. isDragging = _vuescroll$state.isDragging,
  1270. isRailHover = _vuescroll$state.isRailHover;
  1271. /* istanbul ignore next */
  1272. if (isDragging || isRailHover) {
  1273. return;
  1274. }
  1275. if (forceHideBar && !this.mergedOptions.bar.keepShow) {
  1276. this.bar.hBar.state.opacity = 0;
  1277. this.bar.vBar.state.opacity = 0;
  1278. }
  1279. // add isDragging condition
  1280. // to prevent from hiding bar while dragging the bar
  1281. if (!this.mergedOptions.bar.keepShow && !this.vuescroll.state.isDragging) {
  1282. this.bar.vBar.state.opacity = 0;
  1283. this.bar.hBar.state.opacity = 0;
  1284. }
  1285. },
  1286. useNumbericSize: function useNumbericSize() {
  1287. this.vuescroll.state.currentSizeStrategy = 'number';
  1288. var _mergedOptions$scroll = this.mergedOptions.scrollPanel,
  1289. maxHeight = _mergedOptions$scroll.maxHeight,
  1290. maxWidth = _mergedOptions$scroll.maxWidth;
  1291. var _$el$parentNode = this.$el.parentNode,
  1292. parentClientHeight = _$el$parentNode.clientHeight,
  1293. parentClientWidth = _$el$parentNode.clientWidth;
  1294. var _scrollPanelElm = this.scrollPanelElm,
  1295. scrollHeight = _scrollPanelElm.scrollHeight,
  1296. scrollWidth = _scrollPanelElm.scrollWidth;
  1297. var width = void 0;
  1298. var height = void 0;
  1299. if (maxHeight || maxWidth) {
  1300. height = scrollHeight <= maxHeight ? undefined : maxHeight;
  1301. width = scrollWidth <= maxWidth ? undefined : maxWidth;
  1302. } else {
  1303. height = parentClientHeight;
  1304. width = parentClientWidth;
  1305. }
  1306. this.vuescroll.state.height = height ? height + 'px' : undefined;
  1307. this.vuescroll.state.width = width ? width + 'px' : undefined;
  1308. },
  1309. usePercentSize: function usePercentSize() {
  1310. this.vuescroll.state.currentSizeStrategy = 'percent';
  1311. this.vuescroll.state.height = '100%';
  1312. this.vuescroll.state.width = '100%';
  1313. },
  1314. // Set its size to be equal to its parentNode
  1315. setVsSize: function setVsSize() {
  1316. var sizeStrategy = this.mergedOptions.vuescroll.sizeStrategy;
  1317. var _mergedOptions$scroll2 = this.mergedOptions.scrollPanel,
  1318. maxHeight = _mergedOptions$scroll2.maxHeight,
  1319. maxWidth = _mergedOptions$scroll2.maxWidth;
  1320. var _scrollPanelElm2 = this.scrollPanelElm,
  1321. clientHeight = _scrollPanelElm2.clientHeight,
  1322. clientWidth = _scrollPanelElm2.clientWidth;
  1323. if (sizeStrategy == 'number' || maxHeight && clientHeight > maxHeight || maxWidth && clientWidth > maxWidth) {
  1324. this.useNumbericSize();
  1325. } else if (sizeStrategy == 'percent' && clientHeight != maxHeight && clientWidth != maxWidth) {
  1326. this.usePercentSize();
  1327. }
  1328. },
  1329. /** ------------------------ Init --------------------------- */
  1330. initWatchOpsChange: function initWatchOpsChange() {
  1331. var _this5 = this;
  1332. var watchOpts = {
  1333. deep: true,
  1334. sync: true
  1335. };
  1336. this.$watch('mergedOptions', function () {
  1337. setTimeout(function () {
  1338. if (_this5.isSmallChangeThisTick) {
  1339. _this5.isSmallChangeThisTick = false;
  1340. _this5.updateBarStateAndEmitEvent('options-change');
  1341. return;
  1342. }
  1343. _this5.refreshInternalStatus();
  1344. }, 0);
  1345. }, watchOpts);
  1346. /**
  1347. * We also watch `small` changes, and when small changes happen, we send
  1348. * a signal to vuescroll, to tell it:
  1349. * 1. we don't need to registry resize
  1350. * 2. we don't need to registry scroller.
  1351. */
  1352. smallChangeArray.forEach(function (opts) {
  1353. _this5.$watch(opts, function () {
  1354. _this5.isSmallChangeThisTick = true;
  1355. }, watchOpts);
  1356. });
  1357. },
  1358. // scrollTo hash-anchor while mounted component have mounted.
  1359. scrollToAnchor: function scrollToAnchor() /* istanbul ignore next */{
  1360. var validateHashSelector = function validateHashSelector(hash) {
  1361. return (/^#[a-zA-Z_]\d*$/.test(hash)
  1362. );
  1363. };
  1364. var hash = window.location.hash;
  1365. if (!hash || (hash = hash.slice(hash.lastIndexOf('#'))) && !validateHashSelector(hash)) {
  1366. return;
  1367. }
  1368. var elm = document.querySelector(hash);
  1369. if (!isChildInParent(elm, this.$el) || this.mergedOptions.scrollPanel.initialScrollY || this.mergedOptions.scrollPanel.initialScrollX) {
  1370. return;
  1371. }
  1372. this.scrollIntoView(elm);
  1373. }
  1374. /** ------------------------ Registry Resize --------------------------- */
  1375. }
  1376. };
  1377. };
  1378. // begin importing
  1379. var scrollPanel = {
  1380. name: 'scrollPanel',
  1381. props: { ops: { type: Object, required: true } },
  1382. methods: {
  1383. // trigger scrollPanel options initialScrollX,
  1384. // initialScrollY
  1385. updateInitialScroll: function updateInitialScroll() {
  1386. var x = 0;
  1387. var y = 0;
  1388. var parent = getRealParent(this);
  1389. if (this.ops.initialScrollX) {
  1390. x = this.ops.initialScrollX;
  1391. }
  1392. if (this.ops.initialScrollY) {
  1393. y = this.ops.initialScrollY;
  1394. }
  1395. if (x || y) {
  1396. parent.scrollTo({ x: x, y: y });
  1397. }
  1398. }
  1399. },
  1400. mounted: function mounted() {
  1401. var _this = this;
  1402. setTimeout(function () {
  1403. if (!_this._isDestroyed) {
  1404. _this.updateInitialScroll();
  1405. }
  1406. }, 0);
  1407. },
  1408. render: function render(h) {
  1409. // eslint-disable-line
  1410. var data = {
  1411. class: ['__panel'],
  1412. style: {
  1413. position: 'relative',
  1414. boxSizing: 'border-box'
  1415. }
  1416. };
  1417. var parent = getRealParent(this);
  1418. var _customPanel = parent.$slots['scroll-panel'];
  1419. if (_customPanel) {
  1420. return insertChildrenIntoSlot(h, _customPanel, this.$slots.default, data);
  1421. }
  1422. return h(
  1423. 'div',
  1424. data,
  1425. [[this.$slots.default]]
  1426. );
  1427. }
  1428. };
  1429. /**
  1430. * Init following things
  1431. * 1. Component
  1432. * 2. Render
  1433. * 3. Config
  1434. */
  1435. function _install(core, render) {
  1436. var _components;
  1437. var extraConfigs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  1438. var extraValidators = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
  1439. var components = (_components = {}, defineProperty(_components, scrollPanel.name, scrollPanel), defineProperty(_components, bar.name, bar), _components);
  1440. var opts = {};
  1441. opts.components = components;
  1442. opts.render = render;
  1443. opts.mixins = core;
  1444. var comp = createComponent(opts);
  1445. // Init Config
  1446. extendOpts(extraConfigs, extraValidators);
  1447. return comp;
  1448. }
  1449. /**
  1450. * Get the children of parent those are in viewport
  1451. */
  1452. function getCurrentViewportDom(parent, container) {
  1453. var children = parent.children;
  1454. var domFragment = [];
  1455. var isCurrentview = function isCurrentview(dom) {
  1456. var _dom$getBoundingClien = dom.getBoundingClientRect(),
  1457. left = _dom$getBoundingClien.left,
  1458. top = _dom$getBoundingClien.top,
  1459. width = _dom$getBoundingClien.width,
  1460. height = _dom$getBoundingClien.height;
  1461. var _container$getBoundin = container.getBoundingClientRect(),
  1462. parentLeft = _container$getBoundin.left,
  1463. parentTop = _container$getBoundin.top,
  1464. parentHeight = _container$getBoundin.height,
  1465. parentWidth = _container$getBoundin.width;
  1466. if (left - parentLeft + width > 0 && left - parentLeft < parentWidth && top - parentTop + height > 0 && top - parentTop < parentHeight) {
  1467. return true;
  1468. }
  1469. return false;
  1470. };
  1471. for (var i = 0; i < children.length; i++) {
  1472. var dom = children.item(i);
  1473. if (isCurrentview(dom) && !dom.isResizeElm) {
  1474. domFragment.push(dom);
  1475. }
  1476. }
  1477. return domFragment;
  1478. }
  1479. /**
  1480. * Compatible to scroller's animation function
  1481. */
  1482. function createEasingFunction(easing, easingPattern) {
  1483. return function (time) {
  1484. return easingPattern(easing, time);
  1485. };
  1486. }
  1487. /**
  1488. * Calculate the easing pattern
  1489. * @link https://github.com/cferdinandi/smooth-scroll/blob/master/src/js/smooth-scroll.js
  1490. * modified by wangyi7099
  1491. * @param {String} type Easing pattern
  1492. * @param {Number} time Time animation should take to complete
  1493. * @returns {Number}
  1494. */
  1495. function easingPattern(easing, time) {
  1496. var pattern = null;
  1497. /* istanbul ignore next */
  1498. {
  1499. // Default Easing Patterns
  1500. if (easing === 'easeInQuad') pattern = time * time; // accelerating from zero velocity
  1501. if (easing === 'easeOutQuad') pattern = time * (2 - time); // decelerating to zero velocity
  1502. if (easing === 'easeInOutQuad') pattern = time < 0.5 ? 2 * time * time : -1 + (4 - 2 * time) * time; // acceleration until halfway, then deceleration
  1503. if (easing === 'easeInCubic') pattern = time * time * time; // accelerating from zero velocity
  1504. if (easing === 'easeOutCubic') pattern = --time * time * time + 1; // decelerating to zero velocity
  1505. if (easing === 'easeInOutCubic') pattern = time < 0.5 ? 4 * time * time * time : (time - 1) * (2 * time - 2) * (2 * time - 2) + 1; // acceleration until halfway, then deceleration
  1506. if (easing === 'easeInQuart') pattern = time * time * time * time; // accelerating from zero velocity
  1507. if (easing === 'easeOutQuart') pattern = 1 - --time * time * time * time; // decelerating to zero velocity
  1508. if (easing === 'easeInOutQuart') pattern = time < 0.5 ? 8 * time * time * time * time : 1 - 8 * --time * time * time * time; // acceleration until halfway, then deceleration
  1509. if (easing === 'easeInQuint') pattern = time * time * time * time * time; // accelerating from zero velocity
  1510. if (easing === 'easeOutQuint') pattern = 1 + --time * time * time * time * time; // decelerating to zero velocity
  1511. if (easing === 'easeInOutQuint') pattern = time < 0.5 ? 16 * time * time * time * time * time : 1 + 16 * --time * time * time * time * time; // acceleration until halfway, then deceleration
  1512. }
  1513. return pattern || time; // no easing, no acceleration
  1514. }
  1515. function noop() {
  1516. return true;
  1517. }
  1518. /* istanbul ignore next */
  1519. var now = Date.now || function () {
  1520. return new Date().getTime();
  1521. };
  1522. var ScrollControl = function () {
  1523. function ScrollControl() {
  1524. classCallCheck(this, ScrollControl);
  1525. this.init();
  1526. this.isRunning = false;
  1527. }
  1528. createClass(ScrollControl, [{
  1529. key: 'pause',
  1530. value: function pause() {
  1531. /* istanbul ignore if */
  1532. if (!this.isRunning) return;
  1533. this.isPaused = true;
  1534. }
  1535. }, {
  1536. key: 'stop',
  1537. value: function stop() {
  1538. this.isStopped = true;
  1539. }
  1540. }, {
  1541. key: 'continue',
  1542. value: function _continue() {
  1543. /* istanbul ignore if */
  1544. if (!this.isPaused) return;
  1545. this.isPaused = false;
  1546. this.ts = now() - this.percent * this.spd;
  1547. this.execScroll();
  1548. }
  1549. }, {
  1550. key: 'startScroll',
  1551. value: function startScroll(st, ed, spd) {
  1552. var stepCb = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : noop;
  1553. var completeCb = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : noop;
  1554. var vertifyCb = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : noop;
  1555. var easingMethod = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : noop;
  1556. var df = ed - st;
  1557. var dir = df > 0 ? -1 : 1;
  1558. var nt = now();
  1559. if (!this.isRunning) {
  1560. this.init();
  1561. }
  1562. if (dir != this.dir || nt - this.ts > 200) {
  1563. this.ts = nt;
  1564. this.dir = dir;
  1565. this.st = st;
  1566. this.ed = ed;
  1567. this.df = df;
  1568. } /* istanbul ignore next */else {
  1569. this.df += df;
  1570. }
  1571. this.spd = spd;
  1572. this.completeCb = completeCb;
  1573. this.vertifyCb = vertifyCb;
  1574. this.stepCb = stepCb;
  1575. this.easingMethod = easingMethod;
  1576. if (!this.isRunning) this.execScroll();
  1577. }
  1578. }, {
  1579. key: 'execScroll',
  1580. value: function execScroll() {
  1581. var _this = this;
  1582. if (!this.df) return;
  1583. var percent = this.percent || 0;
  1584. this.percent = 0;
  1585. this.isRunning = true;
  1586. var loop = function loop() {
  1587. /* istanbul ignore if */
  1588. if (!_this.isRunning || !_this.vertifyCb(percent) || _this.isStopped) {
  1589. _this.isRunning = false;
  1590. return;
  1591. }
  1592. percent = (now() - _this.ts) / _this.spd;
  1593. if (_this.isPaused) {
  1594. _this.percent = percent;
  1595. _this.isRunning = false;
  1596. return;
  1597. }
  1598. if (percent < 1) {
  1599. var value = _this.st + _this.df * _this.easingMethod(percent);
  1600. _this.stepCb(value);
  1601. _this.ref(loop);
  1602. } else {
  1603. // trigger complete
  1604. _this.stepCb(_this.st + _this.df);
  1605. _this.completeCb();
  1606. _this.isRunning = false;
  1607. }
  1608. };
  1609. this.ref(loop);
  1610. }
  1611. }, {
  1612. key: 'init',
  1613. value: function init() {
  1614. this.st = 0;
  1615. this.ed = 0;
  1616. this.df = 0;
  1617. this.spd = 0;
  1618. this.ts = 0;
  1619. this.dir = 0;
  1620. this.ref = requestAnimationFrame(window);
  1621. this.isPaused = false;
  1622. this.isStopped = false;
  1623. }
  1624. }]);
  1625. return ScrollControl;
  1626. }();
  1627. function scrollTo(elm, x, y) {
  1628. var speed = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 300;
  1629. var easing = arguments[4];
  1630. var scrollingComplete = arguments[5];
  1631. var scrollLeft = void 0,
  1632. scrollTop = void 0,
  1633. scrollHeight = void 0,
  1634. scrollWidth = void 0,
  1635. clientWidth = void 0,
  1636. clientHeight = void 0;
  1637. var _elm = elm,
  1638. nodeType = _elm.nodeType;
  1639. var scrollX = new ScrollControl();
  1640. var scrollY = new ScrollControl();
  1641. if (!nodeType) {
  1642. warn('You must pass a dom for the first param, ' + 'for window scrolling, ' + 'you can pass document as the first param.');
  1643. return;
  1644. }
  1645. if (nodeType == 9) {
  1646. // document
  1647. elm = elm.scrollingElement;
  1648. }
  1649. var _elm2 = elm;
  1650. scrollLeft = _elm2.scrollLeft;
  1651. scrollTop = _elm2.scrollTop;
  1652. scrollHeight = _elm2.scrollHeight;
  1653. scrollWidth = _elm2.scrollWidth;
  1654. clientWidth = _elm2.clientWidth;
  1655. clientHeight = _elm2.clientHeight;
  1656. if (typeof x === 'undefined') {
  1657. x = scrollLeft;
  1658. } else {
  1659. x = getNumericValue(x, scrollWidth - clientWidth);
  1660. }
  1661. if (typeof y === 'undefined') {
  1662. y = scrollTop;
  1663. } else {
  1664. y = getNumericValue(y, scrollHeight - clientHeight);
  1665. }
  1666. var easingMethod = createEasingFunction(easing, easingPattern);
  1667. scrollX.startScroll(scrollLeft, x, speed, function (dx) {
  1668. elm.scrollLeft = dx;
  1669. }, scrollingComplete, undefined, easingMethod);
  1670. scrollY.startScroll(scrollTop, y, speed, function (dy) {
  1671. elm.scrollTop = dy;
  1672. }, scrollingComplete, undefined, easingMethod);
  1673. }
  1674. var api$1 = {
  1675. mounted: function mounted() {
  1676. // registry scroll
  1677. this.scrollX = new ScrollControl();
  1678. this.scrollY = new ScrollControl();
  1679. },
  1680. methods: {
  1681. nativeStop: function nativeStop() {
  1682. this.scrollX.stop();
  1683. this.scrollY.stop();
  1684. },
  1685. nativePause: function nativePause() {
  1686. this.scrollX.pause();
  1687. this.scrollY.pause();
  1688. },
  1689. nativeContinue: function nativeContinue() {
  1690. this.scrollX.continue();
  1691. this.scrollY.continue();
  1692. },
  1693. nativeScrollTo: function nativeScrollTo(x, y, speed, easing) {
  1694. if (speed === false) {
  1695. } else if (typeof speed === 'undefined') {
  1696. speed = this.mergedOptions.scrollPanel.speed;
  1697. }
  1698. var elm = this.scrollPanelElm;
  1699. var scrollTop = elm.scrollTop,
  1700. scrollLeft = elm.scrollLeft,
  1701. scrollWidth = elm.scrollWidth,
  1702. clientWidth = elm.clientWidth,
  1703. scrollHeight = elm.scrollHeight,
  1704. clientHeight = elm.clientHeight;
  1705. if (typeof x === 'undefined') {
  1706. x = scrollLeft;
  1707. } else {
  1708. x = getNumericValue(x, scrollWidth - clientWidth);
  1709. }
  1710. if (typeof y === 'undefined') {
  1711. y = scrollTop;
  1712. } else {
  1713. y = getNumericValue(y, scrollHeight - clientHeight);
  1714. }
  1715. if (speed) {
  1716. easing = easing || this.mergedOptions.scrollPanel.easing;
  1717. var easingMethod = createEasingFunction(easing, easingPattern);
  1718. if (x != scrollLeft) {
  1719. this.scrollX.startScroll(scrollLeft, x, speed, function (x) {
  1720. elm.scrollLeft = x;
  1721. }, this.scrollingComplete.bind(this), undefined, easingMethod);
  1722. }
  1723. if (y != scrollTop) {
  1724. this.scrollY.startScroll(scrollTop, y, speed, function (y) {
  1725. elm.scrollTop = y;
  1726. }, this.scrollingComplete.bind(this), undefined, easingMethod);
  1727. }
  1728. } else {
  1729. elm.scrollTop = y;
  1730. elm.scrollLeft = x;
  1731. }
  1732. },
  1733. getCurrentviewDomNative: function getCurrentviewDomNative() {
  1734. var parent = this.scrollContentElm;
  1735. var domFragment = getCurrentViewportDom(parent, this.$el);
  1736. return domFragment;
  1737. }
  1738. }
  1739. };
  1740. function getPanelData(context) {
  1741. // scrollPanel data start
  1742. var data = {
  1743. ref: 'scrollPanel',
  1744. style: {
  1745. height: '100%',
  1746. overflowY: 'scroll',
  1747. overflowX: 'scroll'
  1748. },
  1749. class: [],
  1750. nativeOn: {
  1751. '&scroll': context.handleScroll
  1752. },
  1753. props: {
  1754. ops: context.mergedOptions.scrollPanel
  1755. }
  1756. };
  1757. context.scrollYEnable = true;
  1758. context.scrollXEnable = true;
  1759. data.nativeOn.DOMMouseScroll = data.nativeOn.mousewheel = context.onMouseWheel;
  1760. var _context$mergedOption = context.mergedOptions.scrollPanel,
  1761. scrollingY = _context$mergedOption.scrollingY,
  1762. scrollingX = _context$mergedOption.scrollingX;
  1763. if (!context.bar.hBar.state.size || !scrollingX) {
  1764. context.scrollXEnable = false;
  1765. data.style.overflowX = 'hidden';
  1766. }
  1767. if (!context.bar.vBar.state.size || !scrollingY) {
  1768. context.scrollYEnable = false;
  1769. data.style.overflowY = 'hidden';
  1770. }
  1771. var gutter = getGutter();
  1772. /* istanbul ignore if */
  1773. if (!gutter) {
  1774. createHideBarStyle();
  1775. data.class.push('__hidebar');
  1776. if (isIos()) {
  1777. data.style['-webkit-overflow-scrolling'] = 'touch';
  1778. }
  1779. } else {
  1780. // hide system bar by use a negative value px
  1781. // gutter should be 0 when manually disable scrollingX #14
  1782. if (context.bar.vBar.state.size && context.mergedOptions.scrollPanel.scrollingY) {
  1783. if (context.mergedOptions.scrollPanel.verticalNativeBarPos == 'right') {
  1784. data.style.marginRight = '-' + gutter + 'px';
  1785. } /* istanbul ignore next */else {
  1786. data.style.marginLeft = '-' + gutter + 'px';
  1787. }
  1788. }
  1789. if (context.bar.hBar.state.size && context.mergedOptions.scrollPanel.scrollingX) {
  1790. data.style.height = 'calc(100% + ' + gutter + 'px)';
  1791. }
  1792. }
  1793. // clear legency styles of slide mode...
  1794. data.style.transformOrigin = '';
  1795. data.style.transform = '';
  1796. return data;
  1797. }
  1798. /**
  1799. * create a scrollPanel
  1800. *
  1801. * @param {any} size
  1802. * @param {any} context
  1803. * @returns
  1804. */
  1805. function createPanel(h, context) {
  1806. var data = {};
  1807. data = getPanelData(context);
  1808. return h(
  1809. 'scrollPanel',
  1810. data,
  1811. [getPanelChildren(h, context)]
  1812. );
  1813. }
  1814. function getPanelChildren(h, context) {
  1815. var viewStyle = {
  1816. position: 'relative',
  1817. 'box-sizing': 'border-box',
  1818. 'min-width': '100%',
  1819. 'min-height': '100%'
  1820. };
  1821. var data = {
  1822. style: viewStyle,
  1823. ref: 'scrollContent',
  1824. class: '__view'
  1825. };
  1826. var _customContent = context.$slots['scroll-content'];
  1827. if (context.mergedOptions.scrollPanel.scrollingX) {
  1828. viewStyle.width = getComplitableStyle('width', 'fit-content');
  1829. } else {
  1830. data.style['width'] = '100%';
  1831. }
  1832. if (context.mergedOptions.scrollPanel.padding) {
  1833. data.style.paddingRight = context.mergedOptions.rail.size;
  1834. }
  1835. if (_customContent) {
  1836. return insertChildrenIntoSlot(h, _customContent, context.$slots.default, data);
  1837. }
  1838. return h(
  1839. 'div',
  1840. data,
  1841. [context.$slots.default]
  1842. );
  1843. }
  1844. // detect content size change
  1845. function installResizeDetection(element, callback) {
  1846. return injectObject(element, callback);
  1847. }
  1848. function injectObject(element, callback) {
  1849. if (element.hasResized) {
  1850. return;
  1851. }
  1852. var OBJECT_STYLE = 'display: block; position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: none; padding: 0; margin: 0; opacity: 0; z-index: -1000; pointer-events: none;';
  1853. // define a wrap due to ie's zIndex bug
  1854. var objWrap = document.createElement('div');
  1855. objWrap.style.cssText = OBJECT_STYLE;
  1856. var object = document.createElement('object');
  1857. object.style.cssText = OBJECT_STYLE;
  1858. object.type = 'text/html';
  1859. object.tabIndex = -1;
  1860. object.onload = function () {
  1861. eventCenter(object.contentDocument.defaultView, 'resize', callback);
  1862. };
  1863. // https://github.com/wnr/element-resize-detector/blob/aafe9f7ea11d1eebdab722c7c5b86634e734b9b8/src/detection-strategy/object.js#L159
  1864. if (!isIE()) {
  1865. object.data = 'about:blank';
  1866. }
  1867. objWrap.isResizeElm = true;
  1868. objWrap.appendChild(object);
  1869. element.appendChild(objWrap);
  1870. if (isIE()) {
  1871. object.data = 'about:blank';
  1872. }
  1873. return function destroy() {
  1874. if (object.contentDocument) {
  1875. eventCenter(object.contentDocument.defaultView, 'resize', callback, 'off');
  1876. }
  1877. element.removeChild(objWrap);
  1878. element.hasResized = false;
  1879. };
  1880. }
  1881. /**
  1882. * These mixes is exclusive for native mode
  1883. */
  1884. var update = {
  1885. methods: {
  1886. updateNativeModeBarState: function updateNativeModeBarState() {
  1887. var container = this.scrollPanelElm;
  1888. var isPercent = this.vuescroll.state.currentSizeStrategy == 'percent';
  1889. var _vuescroll$state = this.vuescroll.state,
  1890. width = _vuescroll$state.width,
  1891. height = _vuescroll$state.height;
  1892. var clientWidth = isPercent || !width ? container.clientWidth : width.slice(0, -2); // xxxpx ==> xxx
  1893. var clientHeight = isPercent || !height ? container.clientHeight : height.slice(0, -2);
  1894. var heightPercentage = clientHeight / container.scrollHeight;
  1895. var widthPercentage = clientWidth / container.scrollWidth;
  1896. this.bar.vBar.state.posValue = container.scrollTop * 100 / clientHeight;
  1897. this.bar.hBar.state.posValue = container.scrollLeft * 100 / clientWidth;
  1898. this.bar.vBar.state.size = heightPercentage < 1 ? heightPercentage : 0;
  1899. this.bar.hBar.state.size = widthPercentage < 1 ? widthPercentage : 0;
  1900. },
  1901. getNativePosition: function getNativePosition() {
  1902. return {
  1903. scrollTop: this.scrollPanelElm.scrollTop,
  1904. scrollLeft: this.scrollPanelElm.scrollLeft
  1905. };
  1906. },
  1907. css: function css(dom, style) /* istanbul ignore next */{
  1908. return window.getComputedStyle(dom)[style];
  1909. },
  1910. checkScrollable: function checkScrollable(e, dir, delta) /* istanbul ignore next */{
  1911. var scrollable = false;
  1912. // check mouse point scrollable.
  1913. var dom = e.target ? e.target : e;
  1914. while (dom && dom.nodeType == 1 && dom !== this.scrollPanelElm.parentNode && !/^BODY|HTML/.test(dom.nodeName)) {
  1915. var ov = (dir == 'dy' ? this.css(dom, 'overflowY') : this.css(dom, 'overflowX')) || this.css(dom, 'overflow') || '';
  1916. if (/scroll|auto/.test(ov)) {
  1917. var _getScrollProcess = this.getScrollProcess(dom),
  1918. v = _getScrollProcess.v,
  1919. h = _getScrollProcess.h;
  1920. if (dir == 'dx' && (delta < 0 && h > 0 || delta > 0 && h < 1) || dir == 'dy' && (delta < 0 && v > 0 || delta > 0 && v < 1)) {
  1921. scrollable = dom == this.scrollPanelElm;
  1922. break;
  1923. }
  1924. }
  1925. dom = dom.parentNode ? dom.parentNode : false;
  1926. }
  1927. return scrollable;
  1928. },
  1929. onMouseWheel: function onMouseWheel(event) /* istanbul ignore next */{
  1930. var duration = this.mergedOptions.vuescroll.wheelScrollDuration;
  1931. var isReverse = this.mergedOptions.vuescroll.wheelDirectionReverse;
  1932. var delta = 0;
  1933. var dir = void 0;
  1934. if (event.wheelDelta) {
  1935. if (event.deltaY) {
  1936. dir = 'dy';
  1937. delta = event.deltaY;
  1938. } else if (event.deltaX) {
  1939. delta = event.deltaX;
  1940. dir = 'dx';
  1941. } else {
  1942. delta = -1 * event.wheelDelta / 2;
  1943. }
  1944. } else if (event.detail) {
  1945. // horizontal scroll
  1946. if (event.axis == 1) {
  1947. dir = 'dx';
  1948. } else if (event.axis == 2) {
  1949. // vertical scroll
  1950. dir = 'dy';
  1951. }
  1952. delta = event.detail * 16;
  1953. }
  1954. if (event.shiftKey) {
  1955. dir = 'dx';
  1956. } else {
  1957. dir = 'dy';
  1958. }
  1959. if (isReverse) {
  1960. dir = dir == 'dx' ? 'dy' : 'dx';
  1961. }
  1962. if (this.checkScrollable(event, dir, delta)) {
  1963. event.stopPropagation();
  1964. event.preventDefault();
  1965. this.scrollBy(defineProperty({}, dir, delta), duration);
  1966. }
  1967. }
  1968. },
  1969. computed: {
  1970. scrollContentElm: function scrollContentElm() {
  1971. return this.$refs['scrollContent']._isVue ? this.$refs['scrollContent'].$el : this.$refs['scrollContent'];
  1972. }
  1973. }
  1974. };
  1975. var mixins = [api$1, update];
  1976. var core = {
  1977. mixins: mixins,
  1978. methods: {
  1979. destroy: function destroy() {
  1980. /* istanbul ignore next */
  1981. if (this.destroyResize) {
  1982. this.destroyResize();
  1983. }
  1984. },
  1985. getCurrentviewDom: function getCurrentviewDom() {
  1986. return this.getCurrentviewDomNative();
  1987. },
  1988. internalScrollTo: function internalScrollTo(destX, destY, animate, easing) {
  1989. this.nativeScrollTo(destX, destY, animate, easing);
  1990. },
  1991. internalStop: function internalStop() {
  1992. this.nativeStop();
  1993. },
  1994. internalPause: function internalPause() {
  1995. this.nativePause();
  1996. },
  1997. internalContinue: function internalContinue() {
  1998. this.nativeContinue();
  1999. },
  2000. handleScroll: function handleScroll(nativeEvent) {
  2001. this.updateBarStateAndEmitEvent('handle-scroll', nativeEvent);
  2002. },
  2003. updateBarStateAndEmitEvent: function updateBarStateAndEmitEvent(eventType) {
  2004. var nativeEvent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  2005. this.updateNativeModeBarState();
  2006. if (eventType) {
  2007. this.emitEvent(eventType, nativeEvent);
  2008. }
  2009. if (this.mergedOptions.bar.onlyShowBarOnScroll) {
  2010. if (eventType == 'handle-scroll' || eventType == 'handle-resize' || eventType == 'refresh-status' || eventType == 'window-resize' || eventType == 'options-change') {
  2011. this.showAndDefferedHideBar(true /* forceHideBar: true */);
  2012. }
  2013. } else {
  2014. this.showAndDefferedHideBar();
  2015. }
  2016. },
  2017. getScrollProcess: function getScrollProcess(elm) {
  2018. var _ref = elm || this.scrollPanelElm,
  2019. scrollHeight = _ref.scrollHeight,
  2020. scrollWidth = _ref.scrollWidth,
  2021. clientHeight = _ref.clientHeight,
  2022. clientWidth = _ref.clientWidth,
  2023. scrollTop = _ref.scrollTop,
  2024. scrollLeft = _ref.scrollLeft;
  2025. var v = Math.min(scrollTop / (scrollHeight - clientHeight || 1), 1);
  2026. var h = Math.min(scrollLeft / (scrollWidth - clientWidth || 1), 1);
  2027. return {
  2028. v: v,
  2029. h: h
  2030. };
  2031. },
  2032. emitEvent: function emitEvent(eventType) {
  2033. var nativeEvent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  2034. var _scrollPanelElm = this.scrollPanelElm,
  2035. scrollTop = _scrollPanelElm.scrollTop,
  2036. scrollLeft = _scrollPanelElm.scrollLeft;
  2037. var vertical = {
  2038. type: 'vertical'
  2039. };
  2040. var horizontal = {
  2041. type: 'horizontal'
  2042. };
  2043. var _getScrollProcess = this.getScrollProcess(),
  2044. v = _getScrollProcess.v,
  2045. h = _getScrollProcess.h;
  2046. vertical.process = v;
  2047. horizontal.process = h;
  2048. vertical['barSize'] = this.bar.vBar.state.size;
  2049. horizontal['barSize'] = this.bar.hBar.state.size;
  2050. vertical['scrollTop'] = scrollTop;
  2051. horizontal['scrollLeft'] = scrollLeft;
  2052. this.$emit(eventType, vertical, horizontal, nativeEvent);
  2053. },
  2054. initVariables: function initVariables() {
  2055. this.$el._isVuescroll = true;
  2056. },
  2057. refreshInternalStatus: function refreshInternalStatus() {
  2058. // 1.set vuescroll height or width according to
  2059. // sizeStrategy
  2060. this.setVsSize();
  2061. // 2. registry resize event
  2062. this.registryResize();
  2063. // 3. update scrollbar's height/width
  2064. this.updateBarStateAndEmitEvent('refresh-status');
  2065. },
  2066. registryResize: function registryResize() {
  2067. var _this = this;
  2068. var resizeEnable = this.mergedOptions.vuescroll.detectResize;
  2069. /* istanbul ignore next */
  2070. if (this.destroyResize && resizeEnable) {
  2071. return;
  2072. }
  2073. if (this.destroyResize) {
  2074. this.destroyResize();
  2075. }
  2076. if (!resizeEnable) {
  2077. return;
  2078. }
  2079. var contentElm = this.scrollContentElm;
  2080. var vm = this;
  2081. var handleWindowResize = function handleWindowResize() /* istanbul ignore next */{
  2082. vm.updateBarStateAndEmitEvent('window-resize');
  2083. };
  2084. var handleDomResize = function handleDomResize() {
  2085. var currentSize = {};
  2086. currentSize['width'] = _this.scrollPanelElm.scrollWidth;
  2087. currentSize['height'] = _this.scrollPanelElm.scrollHeight;
  2088. _this.updateBarStateAndEmitEvent('handle-resize', currentSize);
  2089. // Since content sie changes, we should tell parent to set
  2090. // correct size to fit content's size
  2091. _this.setVsSize();
  2092. };
  2093. window.addEventListener('resize', handleWindowResize, false);
  2094. var destroyDomResize = installResizeDetection(contentElm, handleDomResize);
  2095. var destroyWindowResize = function destroyWindowResize() {
  2096. window.removeEventListener('resize', handleWindowResize, false);
  2097. };
  2098. this.destroyResize = function () {
  2099. destroyWindowResize();
  2100. destroyDomResize();
  2101. _this.destroyResize = null;
  2102. };
  2103. },
  2104. getPosition: function getPosition() {
  2105. return this.getNativePosition();
  2106. }
  2107. }
  2108. };
  2109. var config = {
  2110. vuescroll: {
  2111. wheelScrollDuration: 0,
  2112. wheelDirectionReverse: false
  2113. }
  2114. };
  2115. var component = _install(core, createPanel, [config]);
  2116. function install(Vue$$1) {
  2117. var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2118. Vue$$1.component(opts.name || component.name, component);
  2119. Vue$$1.prototype.$vuescrollConfig = opts.ops || {};
  2120. }
  2121. var Vuescroll = _extends({
  2122. install: install,
  2123. version: '4.16.1',
  2124. refreshAll: refreshAll,
  2125. scrollTo: scrollTo
  2126. }, component);
  2127. /* istanbul ignore if */
  2128. if (typeof window !== 'undefined' && window.Vue) {
  2129. window.Vue.use(Vuescroll);
  2130. }
  2131. return Vuescroll;
  2132. })));