index.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. (0, component_1.VantComponent)({
  9. mixins: [touch_1.touch],
  10. classes: ['nav-class', 'tab-class', 'tab-active-class', 'line-class'],
  11. relation: (0, 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. scrollWithAnimation: false,
  90. lineOffsetLeft: 0,
  91. },
  92. mounted: function () {
  93. var _this = this;
  94. (0, utils_1.requestAnimationFrame)(function () {
  95. _this.setData({
  96. container: function () { return _this.createSelectorQuery().select('.van-tabs'); },
  97. });
  98. _this.resize();
  99. _this.scrollIntoView();
  100. });
  101. },
  102. methods: {
  103. updateTabs: function () {
  104. var _a = this, _b = _a.children, children = _b === void 0 ? [] : _b, data = _a.data;
  105. this.setData({
  106. tabs: children.map(function (child) { return child.data; }),
  107. scrollable: this.children.length > data.swipeThreshold || !data.ellipsis,
  108. });
  109. this.setCurrentIndexByName(data.active || this.getCurrentName());
  110. },
  111. trigger: function (eventName, child) {
  112. var currentIndex = this.data.currentIndex;
  113. var currentChild = child || this.children[currentIndex];
  114. if (!(0, validator_1.isDef)(currentChild)) {
  115. return;
  116. }
  117. this.$emit(eventName, {
  118. index: currentChild.index,
  119. name: currentChild.getComputedName(),
  120. title: currentChild.data.title,
  121. });
  122. },
  123. onTap: function (event) {
  124. var _this = this;
  125. var index = event.currentTarget.dataset.index;
  126. var child = this.children[index];
  127. if (child.data.disabled) {
  128. this.trigger('disabled', child);
  129. }
  130. else {
  131. this.setCurrentIndex(index);
  132. (0, utils_1.nextTick)(function () {
  133. _this.trigger('click');
  134. });
  135. }
  136. },
  137. // correct the index of active tab
  138. setCurrentIndexByName: function (name) {
  139. var _a = this.children, children = _a === void 0 ? [] : _a;
  140. var matched = children.filter(function (child) { return child.getComputedName() === name; });
  141. if (matched.length) {
  142. this.setCurrentIndex(matched[0].index);
  143. }
  144. },
  145. setCurrentIndex: function (currentIndex) {
  146. var _this = this;
  147. var _a = this, data = _a.data, _b = _a.children, children = _b === void 0 ? [] : _b;
  148. if (!(0, validator_1.isDef)(currentIndex) ||
  149. currentIndex >= children.length ||
  150. currentIndex < 0) {
  151. return;
  152. }
  153. (0, utils_1.groupSetData)(this, function () {
  154. children.forEach(function (item, index) {
  155. var active = index === currentIndex;
  156. if (active !== item.data.active || !item.inited) {
  157. item.updateRender(active, _this);
  158. }
  159. });
  160. });
  161. if (currentIndex === data.currentIndex) {
  162. return;
  163. }
  164. var shouldEmitChange = data.currentIndex !== null;
  165. this.setData({ currentIndex: currentIndex });
  166. (0, utils_1.requestAnimationFrame)(function () {
  167. _this.resize();
  168. _this.scrollIntoView();
  169. });
  170. (0, utils_1.nextTick)(function () {
  171. _this.trigger('input');
  172. if (shouldEmitChange) {
  173. _this.trigger('change');
  174. }
  175. });
  176. },
  177. getCurrentName: function () {
  178. var activeTab = this.children[this.data.currentIndex];
  179. if (activeTab) {
  180. return activeTab.getComputedName();
  181. }
  182. },
  183. resize: function () {
  184. var _this = this;
  185. if (this.data.type !== 'line') {
  186. return;
  187. }
  188. var _a = this.data, currentIndex = _a.currentIndex, ellipsis = _a.ellipsis, skipTransition = _a.skipTransition;
  189. Promise.all([
  190. (0, utils_1.getAllRect)(this, '.van-tab'),
  191. (0, utils_1.getRect)(this, '.van-tabs__line'),
  192. ]).then(function (_a) {
  193. var _b = _a[0], rects = _b === void 0 ? [] : _b, lineRect = _a[1];
  194. var rect = rects[currentIndex];
  195. if (rect == null) {
  196. return;
  197. }
  198. var lineOffsetLeft = rects
  199. .slice(0, currentIndex)
  200. .reduce(function (prev, curr) { return prev + curr.width; }, 0);
  201. lineOffsetLeft +=
  202. (rect.width - lineRect.width) / 2 + (ellipsis ? 0 : 8);
  203. _this.setData({ lineOffsetLeft: lineOffsetLeft });
  204. if (skipTransition) {
  205. (0, utils_1.nextTick)(function () {
  206. _this.setData({ skipTransition: false });
  207. });
  208. }
  209. });
  210. },
  211. // scroll active tab into view
  212. scrollIntoView: function () {
  213. var _this = this;
  214. var _a = this.data, currentIndex = _a.currentIndex, scrollable = _a.scrollable, scrollWithAnimation = _a.scrollWithAnimation;
  215. if (!scrollable) {
  216. return;
  217. }
  218. Promise.all([
  219. (0, utils_1.getAllRect)(this, '.van-tab'),
  220. (0, utils_1.getRect)(this, '.van-tabs__nav'),
  221. ]).then(function (_a) {
  222. var tabRects = _a[0], navRect = _a[1];
  223. var tabRect = tabRects[currentIndex];
  224. var offsetLeft = tabRects
  225. .slice(0, currentIndex)
  226. .reduce(function (prev, curr) { return prev + curr.width; }, 0);
  227. _this.setData({
  228. scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2,
  229. });
  230. if (!scrollWithAnimation) {
  231. (0, utils_1.nextTick)(function () {
  232. _this.setData({ scrollWithAnimation: true });
  233. });
  234. }
  235. });
  236. },
  237. onTouchScroll: function (event) {
  238. this.$emit('scroll', event.detail);
  239. },
  240. onTouchStart: function (event) {
  241. if (!this.data.swipeable)
  242. return;
  243. this.touchStart(event);
  244. },
  245. onTouchMove: function (event) {
  246. if (!this.data.swipeable)
  247. return;
  248. this.touchMove(event);
  249. },
  250. // watch swipe touch end
  251. onTouchEnd: function () {
  252. if (!this.data.swipeable)
  253. return;
  254. var _a = this, direction = _a.direction, deltaX = _a.deltaX, offsetX = _a.offsetX;
  255. var minSwipeDistance = 50;
  256. if (direction === 'horizontal' && offsetX >= minSwipeDistance) {
  257. var index = this.getAvaiableTab(deltaX);
  258. if (index !== -1) {
  259. this.setCurrentIndex(index);
  260. }
  261. }
  262. },
  263. getAvaiableTab: function (direction) {
  264. var _a = this.data, tabs = _a.tabs, currentIndex = _a.currentIndex;
  265. var step = direction > 0 ? -1 : 1;
  266. for (var i = step; currentIndex + i < tabs.length && currentIndex + i >= 0; i += step) {
  267. var index = currentIndex + i;
  268. if (index >= 0 &&
  269. index < tabs.length &&
  270. tabs[index] &&
  271. !tabs[index].disabled) {
  272. return index;
  273. }
  274. }
  275. return -1;
  276. },
  277. },
  278. });