-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathmd-tabs.js
More file actions
75 lines (66 loc) · 2.16 KB
/
md-tabs.js
File metadata and controls
75 lines (66 loc) · 2.16 KB
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
import Component from '@ember/component';
import { alias } from '@ember/object/computed';
import { debounce } from '@ember/runloop';
import { A } from '@ember/array';
import { observer, computed, get } from '@ember/object';
import ParentComponentSupport from 'ember-composability/mixins/parent-component-support';
import layout from '../templates/components/md-tabs';
import jQuery from 'jquery';
export default Component.extend(ParentComponentSupport, {
layout,
classNames: ['materialize-tabs', 'row'],
composableChildrenDebounceTime: 1,
content: null,
numTabs: alias('composableChildren.length'),
optionValuePath: 'id',
optionLabelPath: 'title',
colWidth: 2,
selected: null,
didInsertElement() {
this._super(...arguments);
this._updateIndicatorPosition(false);
},
_indicatorUpdater: observer('selected', 'content.[]', 'composableChildren.[]', function() {
debounce(this, this._updateIndicatorPosition, 100);
}),
tabComponents() {
return A(this.get('composableChildren')) || A();
},
_updateIndicatorPosition(animate = true) {
if (!this.element) {
return;
}
const [tabComponent] = (this.get('composableChildren') || []).filter(
item => get(item, 'value') === this.get('selected')
);
const tabSetRect = this.element.getBoundingClientRect();
if (tabComponent) {
const tabRect = tabComponent.element.getBoundingClientRect();
const cssParams = {
left: tabRect.left - tabSetRect.left,
right: tabSetRect.right - tabRect.right
};
if (!animate) {
jQuery('.indicator').css(cssParams);
} else {
jQuery('.indicator1').velocity(cssParams, {
duration: 150
});
jQuery('.indicator2').velocity(cssParams, {
duration: 150,
delay: 40
});
}
}
},
_content: computed('content.[]', 'optionLabelPath', 'optionValuePath', function() {
const labelPath = this.get('optionLabelPath');
const valuePath = this.get('optionValuePath');
return new A(
(this.get('content') || []).map(contentItem => ({
id: contentItem[valuePath],
title: contentItem[labelPath]
}))
);
})
});