index.js 9.9KB

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