index.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var component_1 = require('../common/component');
  4. var touch_1 = require('../mixins/touch');
  5. var utils_1 = require('../common/utils');
  6. var validator_1 = require('../common/validator');
  7. var relation_1 = require('../common/relation');
  8. component_1.VantComponent({
  9. mixins: [touch_1.touch],
  10. classes: ['nav-class', 'tab-class', 'tab-active-class', 'line-class'],
  11. relation: relation_1.useChildren('tab', function () {
  12. this.updateTabs();
  13. }),
  14. props: {
  15. sticky: Boolean,
  16. border: Boolean,
  17. swipeable: Boolean,
  18. titleActiveColor: String,
  19. titleInactiveColor: String,
  20. color: String,
  21. animated: {
  22. type: Boolean,
  23. observer: function () {
  24. var _this = this;
  25. this.children.forEach(function (child, index) {
  26. return child.updateRender(index === _this.data.currentIndex, _this);
  27. });
  28. },
  29. },
  30. lineWidth: {
  31. type: null,
  32. value: 40,
  33. observer: 'resize',
  34. },
  35. lineHeight: {
  36. type: null,
  37. value: -1,
  38. },
  39. active: {
  40. type: null,
  41. value: 0,
  42. observer: function (name) {
  43. if (name !== this.getCurrentName()) {
  44. this.setCurrentIndexByName(name);
  45. }
  46. },
  47. },
  48. type: {
  49. type: String,
  50. value: 'line',
  51. },
  52. ellipsis: {
  53. type: Boolean,
  54. value: true,
  55. },
  56. duration: {
  57. type: Number,
  58. value: 0.3,
  59. },
  60. zIndex: {
  61. type: Number,
  62. value: 1,
  63. },
  64. swipeThreshold: {
  65. type: Number,
  66. value: 5,
  67. observer: function (value) {
  68. this.setData({
  69. scrollable: this.children.length > value || !this.data.ellipsis,
  70. });
  71. },
  72. },
  73. offsetTop: {
  74. type: Number,
  75. value: 0,
  76. },
  77. lazyRender: {
  78. type: Boolean,
  79. value: true,
  80. },
  81. },
  82. data: {
  83. tabs: [],
  84. scrollLeft: 0,
  85. scrollable: false,
  86. currentIndex: 0,
  87. container: null,
  88. skipTransition: true,
  89. lineOffsetLeft: 0,
  90. },
  91. mounted: function () {
  92. var _this = this;
  93. utils_1.requestAnimationFrame(function () {
  94. _this.setData({
  95. container: function () {
  96. return _this.createSelectorQuery().select('.van-tabs');
  97. },
  98. });
  99. _this.resize(true);
  100. _this.scrollIntoView();
  101. });
  102. },
  103. methods: {
  104. updateTabs: function () {
  105. var _a = this,
  106. _b = _a.children,
  107. children = _b === void 0 ? [] : _b,
  108. data = _a.data;
  109. this.setData({
  110. tabs: children.map(function (child) {
  111. return child.data;
  112. }),
  113. scrollable:
  114. this.children.length > data.swipeThreshold || !data.ellipsis,
  115. });
  116. this.setCurrentIndexByName(data.active || this.getCurrentName());
  117. },
  118. trigger: function (eventName, child) {
  119. var currentIndex = this.data.currentIndex;
  120. var currentChild = child || this.children[currentIndex];
  121. if (!validator_1.isDef(currentChild)) {
  122. return;
  123. }
  124. this.$emit(eventName, {
  125. index: currentChild.index,
  126. name: currentChild.getComputedName(),
  127. title: currentChild.data.title,
  128. });
  129. },
  130. onTap: function (event) {
  131. var _this = this;
  132. var index = event.currentTarget.dataset.index;
  133. var child = this.children[index];
  134. if (child.data.disabled) {
  135. this.trigger('disabled', child);
  136. } else {
  137. this.setCurrentIndex(index);
  138. utils_1.nextTick(function () {
  139. _this.trigger('click');
  140. });
  141. }
  142. },
  143. // correct the index of active tab
  144. setCurrentIndexByName: function (name) {
  145. var _a = this.children,
  146. children = _a === void 0 ? [] : _a;
  147. var matched = children.filter(function (child) {
  148. return child.getComputedName() === name;
  149. });
  150. if (matched.length) {
  151. this.setCurrentIndex(matched[0].index);
  152. }
  153. },
  154. setCurrentIndex: function (currentIndex) {
  155. var _this = this;
  156. var _a = this,
  157. data = _a.data,
  158. _b = _a.children,
  159. children = _b === void 0 ? [] : _b;
  160. if (
  161. !validator_1.isDef(currentIndex) ||
  162. currentIndex >= children.length ||
  163. currentIndex < 0
  164. ) {
  165. return;
  166. }
  167. utils_1.groupSetData(this, function () {
  168. children.forEach(function (item, index) {
  169. var active = index === currentIndex;
  170. if (active !== item.data.active || !item.inited) {
  171. item.updateRender(active, _this);
  172. }
  173. });
  174. });
  175. if (currentIndex === data.currentIndex) {
  176. return;
  177. }
  178. var shouldEmitChange = data.currentIndex !== null;
  179. this.setData({ currentIndex: currentIndex });
  180. utils_1.nextTick(function () {
  181. _this.resize();
  182. _this.scrollIntoView();
  183. _this.trigger('input');
  184. if (shouldEmitChange) {
  185. _this.trigger('change');
  186. }
  187. });
  188. },
  189. getCurrentName: function () {
  190. var activeTab = this.children[this.data.currentIndex];
  191. if (activeTab) {
  192. return activeTab.getComputedName();
  193. }
  194. },
  195. resize: function (skipTransition) {
  196. var _this = this;
  197. if (skipTransition === void 0) {
  198. skipTransition = false;
  199. }
  200. if (this.data.type !== 'line') {
  201. return;
  202. }
  203. var _a = this.data,
  204. currentIndex = _a.currentIndex,
  205. ellipsis = _a.ellipsis;
  206. Promise.all([
  207. utils_1.getAllRect(this, '.van-tab'),
  208. utils_1.getRect(this, '.van-tabs__line'),
  209. ]).then(function (_a) {
  210. var _b = _a[0],
  211. rects = _b === void 0 ? [] : _b,
  212. lineRect = _a[1];
  213. var rect = rects[currentIndex];
  214. if (rect == null) {
  215. return;
  216. }
  217. var lineOffsetLeft = rects
  218. .slice(0, currentIndex)
  219. .reduce(function (prev, curr) {
  220. return prev + curr.width;
  221. }, 0);
  222. lineOffsetLeft +=
  223. (rect.width - lineRect.width) / 2 + (ellipsis ? 0 : 8);
  224. _this.setData({
  225. lineOffsetLeft: lineOffsetLeft,
  226. skipTransition: skipTransition,
  227. });
  228. });
  229. },
  230. // scroll active tab into view
  231. scrollIntoView: function () {
  232. var _this = this;
  233. var _a = this.data,
  234. currentIndex = _a.currentIndex,
  235. scrollable = _a.scrollable;
  236. if (!scrollable) {
  237. return;
  238. }
  239. Promise.all([
  240. utils_1.getAllRect(this, '.van-tab'),
  241. utils_1.getRect(this, '.van-tabs__nav'),
  242. ]).then(function (_a) {
  243. var tabRects = _a[0],
  244. navRect = _a[1];
  245. var tabRect = tabRects[currentIndex];
  246. var offsetLeft = tabRects
  247. .slice(0, currentIndex)
  248. .reduce(function (prev, curr) {
  249. return prev + curr.width;
  250. }, 0);
  251. _this.setData({
  252. scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2,
  253. });
  254. });
  255. },
  256. onTouchScroll: function (event) {
  257. this.$emit('scroll', event.detail);
  258. },
  259. onTouchStart: function (event) {
  260. if (!this.data.swipeable) return;
  261. this.touchStart(event);
  262. },
  263. onTouchMove: function (event) {
  264. if (!this.data.swipeable) return;
  265. this.touchMove(event);
  266. },
  267. // watch swipe touch end
  268. onTouchEnd: function () {
  269. if (!this.data.swipeable) return;
  270. var _a = this,
  271. direction = _a.direction,
  272. deltaX = _a.deltaX,
  273. offsetX = _a.offsetX;
  274. var minSwipeDistance = 50;
  275. if (direction === 'horizontal' && offsetX >= minSwipeDistance) {
  276. var index = this.getAvaiableTab(deltaX);
  277. if (index !== -1) {
  278. this.setCurrentIndex(index);
  279. }
  280. }
  281. },
  282. getAvaiableTab: function (direction) {
  283. var _a = this.data,
  284. tabs = _a.tabs,
  285. currentIndex = _a.currentIndex;
  286. var step = direction > 0 ? -1 : 1;
  287. for (
  288. var i = step;
  289. currentIndex + i < tabs.length && currentIndex + i >= 0;
  290. i += step
  291. ) {
  292. var index = currentIndex + i;
  293. if (
  294. index >= 0 &&
  295. index < tabs.length &&
  296. tabs[index] &&
  297. !tabs[index].disabled
  298. ) {
  299. return index;
  300. }
  301. }
  302. return -1;
  303. },
  304. },
  305. });