index.js 11 KB

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