|
| 1 | + |
| 2 | + |
| 3 | +Component({ |
| 4 | + options: { |
| 5 | + addGlobalClass: true, |
| 6 | + pureDataPattern: /^_/, |
| 7 | + multipleSlots: true |
| 8 | + }, |
| 9 | + properties: { |
| 10 | + vtabs: { type: Array, value: [] }, |
| 11 | + tabBarClass: { type: String, value: '' }, |
| 12 | + activeClass: { type: String, value: '' }, |
| 13 | + tabBarLineColor: { type: String, value: '#ff0000' }, |
| 14 | + tabBarInactiveTextColor: { type: String, value: '#000000' }, |
| 15 | + tabBarActiveTextColor: { type: String, value: '#ff0000' }, |
| 16 | + tabBarInactiveBgColor: { type: String, value: '#eeeeee' }, |
| 17 | + tabBarActiveBgColor: { type: String, value: '#ffffff' }, |
| 18 | + activeTab: { type: Number, value: 0 }, |
| 19 | + animation: { type: Boolean, value: true } |
| 20 | + }, |
| 21 | + data: { |
| 22 | + currentView: 0, |
| 23 | + contentScrollTop: 0, |
| 24 | + _heightRecords: [], |
| 25 | + _contentHeight: {} |
| 26 | + }, |
| 27 | + observers: { |
| 28 | + activeTab: function activeTab(_activeTab) { |
| 29 | + this.scrollTabBar(_activeTab); |
| 30 | + } |
| 31 | + }, |
| 32 | + relations: { |
| 33 | + '../vtabs-content/vtabs-content': { |
| 34 | + type: 'child', |
| 35 | + linked: function linked(target) { |
| 36 | + var _this = this; |
| 37 | + |
| 38 | + target.calcHeight(function (rect) { |
| 39 | + _this.data._contentHeight[target.data.tabIndex] = rect.height; |
| 40 | + if (_this._calcHeightTimer) { |
| 41 | + clearTimeout(_this._calcHeightTimer); |
| 42 | + } |
| 43 | + _this._calcHeightTimer = setTimeout(function () { |
| 44 | + _this.calcHeight(); |
| 45 | + }, 100); |
| 46 | + }); |
| 47 | + }, |
| 48 | + unlinked: function unlinked(target) { |
| 49 | + delete this.data._contentHeight[target.data.tabIndex]; |
| 50 | + } |
| 51 | + } |
| 52 | + }, |
| 53 | + lifetimes: { |
| 54 | + attached: function attached() {} |
| 55 | + }, |
| 56 | + methods: { |
| 57 | + calcHeight: function calcHeight() { |
| 58 | + var length = this.data.vtabs.length; |
| 59 | + var _contentHeight = this.data._contentHeight; |
| 60 | + var _heightRecords = []; |
| 61 | + var temp = 0; |
| 62 | + for (var i = 0; i < length; i++) { |
| 63 | + _heightRecords[i] = temp + (_contentHeight[i] || 0); |
| 64 | + temp = _heightRecords[i]; |
| 65 | + } |
| 66 | + this.data._heightRecords = _heightRecords; |
| 67 | + }, |
| 68 | + scrollTabBar: function scrollTabBar(index) { |
| 69 | + var len = this.data.vtabs.length; |
| 70 | + if (len === 0) return; |
| 71 | + var currentView = index < 6 ? 0 : index - 5; |
| 72 | + if (currentView >= len) currentView = len - 1; |
| 73 | + this.setData({ currentView: currentView }); |
| 74 | + }, |
| 75 | + handleTabClick: function handleTabClick(e) { |
| 76 | + var _heightRecords = this.data._heightRecords; |
| 77 | + var index = e.currentTarget.dataset.index; |
| 78 | + var contentScrollTop = _heightRecords[index - 1] || 0; |
| 79 | + this.triggerEvent('tabclick', { index: index }); |
| 80 | + this.setData({ |
| 81 | + activeTab: index, |
| 82 | + contentScrollTop: contentScrollTop |
| 83 | + }); |
| 84 | + }, |
| 85 | + handleContentScroll: function handleContentScroll(e) { |
| 86 | + var _heightRecords = this.data._heightRecords; |
| 87 | + if (_heightRecords.length === 0) return; |
| 88 | + var length = this.data.vtabs.length; |
| 89 | + var scrollTop = e.detail.scrollTop; |
| 90 | + var index = 0; |
| 91 | + if (scrollTop >= _heightRecords[0]) { |
| 92 | + for (var i = 1; i < length; i++) { |
| 93 | + if (scrollTop >= _heightRecords[i - 1] && scrollTop < _heightRecords[i]) { |
| 94 | + index = i; |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + if (index !== this.data.activeTab) { |
| 100 | + this.triggerEvent('change', { index: index }); |
| 101 | + this.setData({ activeTab: index }); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +}); |
0 commit comments