index.js 8.5 KB

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