index.js 8.3 KB

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