index.js 12 KB

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