index.js 11 KB

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