index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.default = void 0;
  5. var _utils = require("../utils");
  6. var _utils2 = require("./utils");
  7. var _router = require("../utils/router");
  8. var _style = require("../utils/dom/style");
  9. var _event = require("../utils/dom/event");
  10. var _unit = require("../utils/format/unit");
  11. var _constant = require("../utils/constant");
  12. var _interceptor = require("../utils/interceptor");
  13. var _scroll = require("../utils/dom/scroll");
  14. var _relation = require("../mixins/relation");
  15. var _bindEvent = require("../mixins/bind-event");
  16. var _Title = _interopRequireDefault(require("./Title"));
  17. var _sticky = _interopRequireDefault(require("../sticky"));
  18. var _Content = _interopRequireDefault(require("./Content"));
  19. // Utils
  20. // Mixins
  21. // Components
  22. var _createNamespace = (0, _utils.createNamespace)('tabs'),
  23. createComponent = _createNamespace[0],
  24. bem = _createNamespace[1];
  25. var _default2 = createComponent({
  26. mixins: [(0, _relation.ParentMixin)('vanTabs'), (0, _bindEvent.BindEventMixin)(function (bind) {
  27. if (!this.scroller) {
  28. this.scroller = (0, _scroll.getScroller)(this.$el);
  29. }
  30. bind(window, 'resize', this.resize, true);
  31. if (this.scrollspy) {
  32. bind(this.scroller, 'scroll', this.onScroll, true);
  33. }
  34. })],
  35. model: {
  36. prop: 'active'
  37. },
  38. props: {
  39. color: String,
  40. border: Boolean,
  41. sticky: Boolean,
  42. animated: Boolean,
  43. swipeable: Boolean,
  44. scrollspy: Boolean,
  45. background: String,
  46. lineWidth: [Number, String],
  47. lineHeight: [Number, String],
  48. beforeChange: Function,
  49. titleActiveColor: String,
  50. titleInactiveColor: String,
  51. type: {
  52. type: String,
  53. default: 'line'
  54. },
  55. active: {
  56. type: [Number, String],
  57. default: 0
  58. },
  59. ellipsis: {
  60. type: Boolean,
  61. default: true
  62. },
  63. duration: {
  64. type: [Number, String],
  65. default: 0.3
  66. },
  67. offsetTop: {
  68. type: [Number, String],
  69. default: 0
  70. },
  71. lazyRender: {
  72. type: Boolean,
  73. default: true
  74. },
  75. swipeThreshold: {
  76. type: [Number, String],
  77. default: 5
  78. }
  79. },
  80. data: function data() {
  81. return {
  82. position: '',
  83. currentIndex: null,
  84. lineStyle: {
  85. backgroundColor: this.color
  86. }
  87. };
  88. },
  89. computed: {
  90. // whether the nav is scrollable
  91. scrollable: function scrollable() {
  92. return this.children.length > this.swipeThreshold || !this.ellipsis;
  93. },
  94. navStyle: function navStyle() {
  95. return {
  96. borderColor: this.color,
  97. background: this.background
  98. };
  99. },
  100. currentName: function currentName() {
  101. var activeTab = this.children[this.currentIndex];
  102. if (activeTab) {
  103. return activeTab.computedName;
  104. }
  105. },
  106. offsetTopPx: function offsetTopPx() {
  107. return (0, _unit.unitToPx)(this.offsetTop);
  108. },
  109. scrollOffset: function scrollOffset() {
  110. if (this.sticky) {
  111. return this.offsetTopPx + this.tabHeight;
  112. }
  113. return 0;
  114. }
  115. },
  116. watch: {
  117. color: 'setLine',
  118. active: function active(name) {
  119. if (name !== this.currentName) {
  120. this.setCurrentIndexByName(name);
  121. }
  122. },
  123. children: function children() {
  124. var _this = this;
  125. this.setCurrentIndexByName(this.active);
  126. this.setLine();
  127. this.$nextTick(function () {
  128. _this.scrollIntoView(true);
  129. });
  130. },
  131. currentIndex: function currentIndex() {
  132. this.scrollIntoView();
  133. this.setLine(); // scroll to correct position
  134. if (this.stickyFixed && !this.scrollspy) {
  135. (0, _scroll.setRootScrollTop)(Math.ceil((0, _scroll.getElementTop)(this.$el) - this.offsetTopPx));
  136. }
  137. },
  138. scrollspy: function scrollspy(val) {
  139. if (val) {
  140. (0, _event.on)(this.scroller, 'scroll', this.onScroll, true);
  141. } else {
  142. (0, _event.off)(this.scroller, 'scroll', this.onScroll);
  143. }
  144. }
  145. },
  146. mounted: function mounted() {
  147. this.init();
  148. },
  149. activated: function activated() {
  150. this.init();
  151. this.setLine();
  152. },
  153. methods: {
  154. // @exposed-api
  155. resize: function resize() {
  156. this.setLine();
  157. },
  158. init: function init() {
  159. var _this2 = this;
  160. this.$nextTick(function () {
  161. _this2.inited = true;
  162. _this2.tabHeight = (0, _scroll.getVisibleHeight)(_this2.$refs.wrap);
  163. _this2.scrollIntoView(true);
  164. });
  165. },
  166. // update nav bar style
  167. setLine: function setLine() {
  168. var _this3 = this;
  169. var shouldAnimate = this.inited;
  170. this.$nextTick(function () {
  171. var titles = _this3.$refs.titles;
  172. if (!titles || !titles[_this3.currentIndex] || _this3.type !== 'line' || (0, _style.isHidden)(_this3.$el)) {
  173. return;
  174. }
  175. var title = titles[_this3.currentIndex].$el;
  176. var lineWidth = _this3.lineWidth,
  177. lineHeight = _this3.lineHeight;
  178. var left = title.offsetLeft + title.offsetWidth / 2;
  179. var lineStyle = {
  180. width: (0, _utils.addUnit)(lineWidth),
  181. backgroundColor: _this3.color,
  182. transform: "translateX(" + left + "px) translateX(-50%)"
  183. };
  184. if (shouldAnimate) {
  185. lineStyle.transitionDuration = _this3.duration + "s";
  186. }
  187. if ((0, _utils.isDef)(lineHeight)) {
  188. var height = (0, _utils.addUnit)(lineHeight);
  189. lineStyle.height = height;
  190. lineStyle.borderRadius = height;
  191. }
  192. _this3.lineStyle = lineStyle;
  193. });
  194. },
  195. // correct the index of active tab
  196. setCurrentIndexByName: function setCurrentIndexByName(name) {
  197. var matched = this.children.filter(function (tab) {
  198. return tab.computedName === name;
  199. });
  200. var defaultIndex = (this.children[0] || {}).index || 0;
  201. this.setCurrentIndex(matched.length ? matched[0].index : defaultIndex);
  202. },
  203. setCurrentIndex: function setCurrentIndex(currentIndex) {
  204. var newIndex = this.findAvailableTab(currentIndex);
  205. if (!(0, _utils.isDef)(newIndex)) {
  206. return;
  207. }
  208. var newTab = this.children[newIndex];
  209. var newName = newTab.computedName;
  210. var shouldEmitChange = this.currentIndex !== null;
  211. this.currentIndex = newIndex;
  212. if (newName !== this.active) {
  213. this.$emit('input', newName);
  214. if (shouldEmitChange) {
  215. this.$emit('change', newName, newTab.title);
  216. }
  217. }
  218. },
  219. findAvailableTab: function findAvailableTab(index) {
  220. var diff = index < this.currentIndex ? -1 : 1;
  221. while (index >= 0 && index < this.children.length) {
  222. if (!this.children[index].disabled) {
  223. return index;
  224. }
  225. index += diff;
  226. }
  227. },
  228. // emit event when clicked
  229. onClick: function onClick(item, index) {
  230. var _this4 = this;
  231. var _this$children$index = this.children[index],
  232. title = _this$children$index.title,
  233. disabled = _this$children$index.disabled,
  234. computedName = _this$children$index.computedName;
  235. if (disabled) {
  236. this.$emit('disabled', computedName, title);
  237. } else {
  238. (0, _interceptor.callInterceptor)({
  239. interceptor: this.beforeChange,
  240. args: [computedName],
  241. done: function done() {
  242. _this4.setCurrentIndex(index);
  243. _this4.scrollToCurrentContent();
  244. }
  245. });
  246. this.$emit('click', computedName, title);
  247. (0, _router.route)(item.$router, item);
  248. }
  249. },
  250. // scroll active tab into view
  251. scrollIntoView: function scrollIntoView(immediate) {
  252. var titles = this.$refs.titles;
  253. if (!this.scrollable || !titles || !titles[this.currentIndex]) {
  254. return;
  255. }
  256. var nav = this.$refs.nav;
  257. var title = titles[this.currentIndex].$el;
  258. var to = title.offsetLeft - (nav.offsetWidth - title.offsetWidth) / 2;
  259. (0, _utils2.scrollLeftTo)(nav, to, immediate ? 0 : +this.duration);
  260. },
  261. onSticktScroll: function onSticktScroll(params) {
  262. this.stickyFixed = params.isFixed;
  263. this.$emit('scroll', params);
  264. },
  265. // @exposed-api
  266. scrollTo: function scrollTo(name) {
  267. var _this5 = this;
  268. this.$nextTick(function () {
  269. _this5.setCurrentIndexByName(name);
  270. _this5.scrollToCurrentContent(true);
  271. });
  272. },
  273. scrollToCurrentContent: function scrollToCurrentContent(immediate) {
  274. var _this6 = this;
  275. if (immediate === void 0) {
  276. immediate = false;
  277. }
  278. if (this.scrollspy) {
  279. var target = this.children[this.currentIndex];
  280. var el = target == null ? void 0 : target.$el;
  281. if (el) {
  282. var to = (0, _scroll.getElementTop)(el, this.scroller) - this.scrollOffset;
  283. this.lockScroll = true;
  284. (0, _utils2.scrollTopTo)(this.scroller, to, immediate ? 0 : +this.duration, function () {
  285. _this6.lockScroll = false;
  286. });
  287. }
  288. }
  289. },
  290. onScroll: function onScroll() {
  291. if (this.scrollspy && !this.lockScroll) {
  292. var index = this.getCurrentIndexOnScroll();
  293. this.setCurrentIndex(index);
  294. }
  295. },
  296. getCurrentIndexOnScroll: function getCurrentIndexOnScroll() {
  297. var children = this.children;
  298. for (var index = 0; index < children.length; index++) {
  299. var top = (0, _scroll.getVisibleTop)(children[index].$el);
  300. if (top > this.scrollOffset) {
  301. return index === 0 ? 0 : index - 1;
  302. }
  303. }
  304. return children.length - 1;
  305. }
  306. },
  307. render: function render() {
  308. var _this7 = this,
  309. _ref;
  310. var h = arguments[0];
  311. var type = this.type,
  312. animated = this.animated,
  313. scrollable = this.scrollable;
  314. var Nav = this.children.map(function (item, index) {
  315. var _item$badge;
  316. return h(_Title.default, {
  317. "ref": "titles",
  318. "refInFor": true,
  319. "attrs": {
  320. "type": type,
  321. "dot": item.dot,
  322. "info": (_item$badge = item.badge) != null ? _item$badge : item.info,
  323. "title": item.title,
  324. "color": _this7.color,
  325. "isActive": index === _this7.currentIndex,
  326. "disabled": item.disabled,
  327. "scrollable": scrollable,
  328. "activeColor": _this7.titleActiveColor,
  329. "inactiveColor": _this7.titleInactiveColor
  330. },
  331. "style": item.titleStyle,
  332. "class": item.titleClass,
  333. "scopedSlots": {
  334. default: function _default() {
  335. return item.slots('title');
  336. }
  337. },
  338. "on": {
  339. "click": function click() {
  340. _this7.onClick(item, index);
  341. }
  342. }
  343. });
  344. });
  345. var Wrap = h("div", {
  346. "ref": "wrap",
  347. "class": [bem('wrap', {
  348. scrollable: scrollable
  349. }), (_ref = {}, _ref[_constant.BORDER_TOP_BOTTOM] = type === 'line' && this.border, _ref)]
  350. }, [h("div", {
  351. "ref": "nav",
  352. "attrs": {
  353. "role": "tablist"
  354. },
  355. "class": bem('nav', [type, {
  356. complete: this.scrollable
  357. }]),
  358. "style": this.navStyle
  359. }, [this.slots('nav-left'), Nav, type === 'line' && h("div", {
  360. "class": bem('line'),
  361. "style": this.lineStyle
  362. }), this.slots('nav-right')])]);
  363. return h("div", {
  364. "class": bem([type])
  365. }, [this.sticky ? h(_sticky.default, {
  366. "attrs": {
  367. "container": this.$el,
  368. "offsetTop": this.offsetTop
  369. },
  370. "on": {
  371. "scroll": this.onSticktScroll
  372. }
  373. }, [Wrap]) : Wrap, h(_Content.default, {
  374. "attrs": {
  375. "count": this.children.length,
  376. "animated": animated,
  377. "duration": this.duration,
  378. "swipeable": this.swipeable,
  379. "currentIndex": this.currentIndex
  380. },
  381. "on": {
  382. "change": this.setCurrentIndex
  383. }
  384. }, [this.slots()])]);
  385. }
  386. });
  387. exports.default = _default2;