index.js 10 KB

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