|
33 | 33 |
|
34 | 34 | <script> |
35 | 35 | import theme from '../../core/components/mdTheme/mixin'; |
| 36 | + import throttle from '../../core/utils/throttle'; |
36 | 37 |
|
37 | 38 | export default { |
38 | 39 | props: { |
|
56 | 57 | hasIcons: false, |
57 | 58 | hasLabel: false, |
58 | 59 | transitionControl: null, |
| 60 | + transitionOff: false, |
59 | 61 | contentHeight: '0px', |
60 | 62 | contentWidth: '0px' |
61 | 63 | }), |
|
120 | 122 | } |
121 | 123 | }, |
122 | 124 | observeElementChanges() { |
123 | | - this.contentObserver = new MutationObserver(this.calculateOnWatch); |
124 | | - this.navigationObserver = new MutationObserver(this.calculateOnWatch); |
125 | | - this.contentObserver.observe(this.$refs.tabContent, { |
| 125 | + this.parentObserver = new MutationObserver(throttle(this.calculateOnWatch, 50)); |
| 126 | + this.parentObserver.observe(this.$refs.tabContent, { |
126 | 127 | childList: true, |
127 | 128 | attributes: true, |
128 | | - characterData: true, |
129 | | - subtree: true, |
130 | | - attributeOldValue: true, |
131 | | - characterDataOldValue: true |
132 | | - }); |
133 | | - this.navigationObserver.observe(this.$refs.tabNavigation.$el, { |
134 | | - attributes: true |
| 129 | + subtree: true |
135 | 130 | }); |
136 | 131 | }, |
137 | 132 | getTabIndex(id) { |
138 | | - let idList = Object.keys(this.tabList); |
| 133 | + const idList = Object.keys(this.tabList); |
139 | 134 |
|
140 | 135 | return idList.indexOf(id); |
141 | 136 | }, |
142 | 137 | calculateIndicatorPos() { |
143 | 138 | if (this.$refs.tabHeader && this.$refs.tabHeader[this.activeTabNumber]) { |
144 | | - let tabsWidth = this.$el.offsetWidth; |
145 | | - let activeTab = this.$refs.tabHeader[this.activeTabNumber]; |
146 | | - let left = activeTab.offsetLeft; |
147 | | - let right = tabsWidth - left - activeTab.offsetWidth; |
| 139 | + const tabsWidth = this.$el.offsetWidth; |
| 140 | + const activeTab = this.$refs.tabHeader[this.activeTabNumber]; |
| 141 | + const left = activeTab.offsetLeft; |
| 142 | + const right = tabsWidth - left - activeTab.offsetWidth; |
148 | 143 |
|
149 | 144 | this.$refs.indicator.style.left = left + 'px'; |
150 | 145 | this.$refs.indicator.style.right = right + 'px'; |
|
157 | 152 | this.contentWidth = width * this.activeTabNumber + 'px'; |
158 | 153 |
|
159 | 154 | for (const tabId in this.tabList) { |
160 | | - let tab = this.tabList[tabId]; |
| 155 | + const tab = this.tabList[tabId]; |
161 | 156 |
|
162 | 157 | tab.ref.width = width + 'px'; |
163 | 158 | tab.ref.left = width * index + 'px'; |
|
188 | 183 | }, 200); |
189 | 184 | }, |
190 | 185 | calculateOnWatch() { |
191 | | - this.transitionOff = true; |
192 | 186 | this.calculatePosition(); |
193 | 187 | this.debounceTransition(); |
194 | 188 | }, |
| 189 | + calculateOnResize() { |
| 190 | + this.transitionOff = true; |
| 191 | + this.calculateOnWatch(); |
| 192 | + }, |
195 | 193 | setActiveTab(tabData) { |
196 | 194 | this.hasIcons = !!tabData.icon; |
197 | 195 | this.hasLabel = !!tabData.label; |
|
204 | 202 | mounted() { |
205 | 203 | this.$nextTick(() => { |
206 | 204 | this.observeElementChanges(); |
207 | | - window.addEventListener('resize', this.calculateOnWatch); |
| 205 | + window.addEventListener('resize', this.calculateOnResize); |
208 | 206 |
|
209 | 207 | if (Object.keys(this.tabList).length && !this.activeTab) { |
210 | 208 | let firstTab = Object.keys(this.tabList)[0]; |
|
214 | 212 | }); |
215 | 213 | }, |
216 | 214 | beforeDestroy() { |
217 | | - if (this.contentObserver) { |
218 | | - this.contentObserver.disconnect(); |
219 | | - } |
220 | | -
|
221 | | - if (this.navigationObserver) { |
222 | | - this.navigationObserver.disconnect(); |
| 215 | + if (this.parentObserver) { |
| 216 | + this.parentObserver.disconnect(); |
223 | 217 | } |
224 | 218 |
|
225 | | - window.removeEventListener('resize', this.calculateOnWatch); |
| 219 | + window.removeEventListener('resize', this.calculateOnResize); |
226 | 220 | } |
227 | 221 | }; |
228 | 222 | </script> |
0 commit comments