-
Notifications
You must be signed in to change notification settings - Fork 332
/
paper-tabs.js
205 lines (167 loc) · 6.52 KB
/
paper-tabs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { inject as service } from '@ember/service';
import { gt } from '@ember/object/computed';
import { computed, observer } from '@ember/object';
import Component from '@ember/component';
import { htmlSafe } from '@ember/string';
import { scheduleOnce, next } from '@ember/runloop';
import layout from '../templates/components/paper-tabs';
import { ParentMixin } from 'ember-composability-tools';
import ColorMixin from 'ember-paper/mixins/color-mixin';
import { invokeAction } from 'ember-invoke-action';
export default Component.extend(ParentMixin, ColorMixin, {
layout,
tagName: 'md-tabs',
classNames: ['md-no-tab-content', 'md-default-theme'],
attributeBindings: ['borderBottom:md-border-bottom'],
constants: service(),
selected: 0, // select first tab by default
_selectedTab: computed('childComponents.@each.value', 'selected', function() {
return this.get('childComponents').findBy('value', this.get('selected'));
}),
_selectedTabDidChange: observer('_selectedTab', function() {
let selectedTab = this.get('_selectedTab');
let previousSelectedTab = this.get('_previousSelectedTab');
if (selectedTab === previousSelectedTab) {
return;
}
this.setMovingRight();
this.fixOffsetIfNeeded();
this.set('_previousSelectedTab', selectedTab);
}),
noInkBar: false,
noInk: false,
ariaLabel: null,
previousInkBarPosition: 0,
stretch: 'sm',
inkBarLeft: computed('_selectedTab.left', function() {
return this.get('_selectedTab.left') || 0;
}),
inkBarRight: computed('wrapperWidth', '_selectedTab.currentWidth', 'inkBarLeft', function() {
return this.get('wrapperWidth') - this.get('inkBarLeft') - (this.get('_selectedTab.currentWidth') || 0);
}),
tabsWidth: computed('childComponents.@each.width', function() {
return this.get('childComponents').reduce((prev, t) => prev + t.get('width'), 0);
}),
shouldPaginate: false,
shouldCenter: computed('shouldPaginate', 'center', function() {
return !this.get('shouldPaginate') && this.get('center');
}),
shouldStretch: computed('shouldPaginate', 'currentStretch', function() {
return !this.get('shouldPaginate') && this.get('currentStretch');
}),
didInsertElement() {
this._super(...arguments);
let updateCanvasWidth = () => {
this.updateDimensions();
this.updateStretchTabs();
this.fixOffsetIfNeeded();
};
window.addEventListener('resize', updateCanvasWidth);
window.addEventListener('orientationchange', updateCanvasWidth);
this.updateCanvasWidth = updateCanvasWidth;
// trigger updateDimensions to calculate shouldPaginate early on
this.updateDimensions();
scheduleOnce('afterRender', () => {
next(() => {
// here the previous and next buttons should already be renderd
// and hence the offsets are correctly calculated
this.updateDimensions();
this.fixOffsetIfNeeded();
});
});
},
didRender() {
this._super(...arguments);
// this makes sure that the tabs react to stretch and center changes
this.updateCanvasWidth();
},
willDestroyElement() {
this._super(...arguments);
window.removeEventListener('resize', this.updateCanvasWidth);
window.removeEventListener('orientationchange', this.updateCanvasWidth);
},
registerChild(childComponent) {
this._super(...arguments);
// automatically set value if not manually set
if (childComponent.get('value') === undefined) {
let length = this.childComponents.get('length');
childComponent.set('value', length - 1);
}
},
setMovingRight() {
let movingRight = this.get('_previousSelectedTab.left') < this.get('_selectedTab.left');
this.set('movingRight', movingRight);
},
fixOffsetIfNeeded() {
let canvasWidth = this.get('canvasWidth');
let currentOffset = this.get('currentOffset');
let tabRight = this.get('_selectedTab.left') + this.get('_selectedTab.width');
if (tabRight - currentOffset > canvasWidth) {
let newOffset = tabRight - canvasWidth;
this.set('currentOffset', newOffset);
this.set('paginationStyle', htmlSafe(`transform: translate3d(-${newOffset}px, 0px, 0px);`));
}
if (this.get('_selectedTab.left') < currentOffset) {
let newOffset = this.get('_selectedTab.left');
this.set('currentOffset', newOffset);
this.set('paginationStyle', htmlSafe(`transform: translate3d(-${newOffset}px, 0px, 0px);`));
}
},
updateDimensions() {
let canvasWidth = this.element.querySelector('md-tabs-canvas').offsetWidth;
let wrapperWidth = this.element.querySelector('md-pagination-wrapper').offsetWidth;
this.get('childComponents').invoke('updateDimensions');
this.set('canvasWidth', canvasWidth);
this.set('wrapperWidth', wrapperWidth);
if (wrapperWidth > canvasWidth) {
this.set('shouldPaginate', true);
}
},
updateStretchTabs() {
let stretch = this.get('stretch');
let currentStretch;
// if `true` or `false` is specified, always/never "stretch tabs"
// otherwise proceed with normal matchMedia test
if (typeof stretch === 'boolean') {
currentStretch = stretch;
} else {
let mediaQuery = this.get('constants').MEDIA[stretch] || stretch;
currentStretch = window.matchMedia(mediaQuery).matches;
}
this.set('currentStretch', currentStretch);
},
currentOffset: 0,
canPageBack: gt('currentOffset', 0),
canPageForward: computed('wrapperWidth', 'currentOffset', 'canvasWidth', function() {
return this.get('wrapperWidth') - this.get('currentOffset') > this.get('canvasWidth');
}),
actions: {
previousPage() {
let tab = this.get('childComponents').find((t) => {
return t.get('left') >= this.get('currentOffset');
});
if (tab) {
let left = Math.max(0, tab.get('left') - this.get('canvasWidth'));
this.set('currentOffset', left);
this.set('paginationStyle', htmlSafe(`transform: translate3d(-${left}px, 0px, 0px);`));
}
},
nextPage() {
let tab = this.get('childComponents').find((t) => {
return t.get('left') + t.get('width') - this.get('currentOffset') > this.get('canvasWidth');
});
if (tab) {
this.set('currentOffset', tab.get('left'));
this.set('paginationStyle', htmlSafe(`transform: translate3d(-${tab.get('left')}px, 0px, 0px);`));
}
},
onChange(selected) {
// support non DDAU scenario
if (this.get('onChange')) {
invokeAction(this, 'onChange', selected.get('value'));
} else {
this.set('selected', selected.get('value'));
}
}
}
});