-
Notifications
You must be signed in to change notification settings - Fork 332
/
paper-tab.js
72 lines (62 loc) · 2.1 KB
/
paper-tab.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
import { computed } from '@ember/object';
import Component from '@ember/component';
import { htmlSafe } from '@ember/string';
import layout from '../templates/components/paper-tab';
import { ChildMixin } from 'ember-composability-tools';
import RippleMixin from 'ember-paper/mixins/ripple-mixin';
import FocusableMixin from 'ember-paper/mixins/focusable-mixin';
import { invokeAction } from 'ember-invoke-action';
export default Component.extend(ChildMixin, RippleMixin, FocusableMixin, {
layout,
tagName: 'md-tab-item',
classNames: ['md-tab'],
classNameBindings: ['isSelected:md-active'],
attributeBindings: ['isSelected:aria-selected', 'style', 'maybeHref:href'],
rippleContainerSelector: null,
// <a> tags have browser styles or are usually styled by the user
// this makes sure that tab item still looks good with an anchor tag
style: computed('href', function() {
if (this.get('href')) {
return htmlSafe('text-decoration: none; border: none;');
}
}),
maybeHref: computed('href', 'disabled', function() {
if (this.get('href') && !this.get('disabled')) {
return this.get('href');
}
}),
isSelected: computed('selected', 'value', function() {
return this.get('selected') === this.get('value');
}),
init() {
this._super(...arguments);
if (this.get('href')) {
this.set('tagName', 'a');
}
},
didInsertElement() {
this._super(...arguments);
let width = this.element.offsetWidth;
// this is the initial tab width
// it is used to calculate if we need pagination or not
this.set('width', width);
},
didRender() {
this._super(...arguments);
this.updateDimensions();
},
// this method is also called by the parent
updateDimensions() {
let left = this.element.offsetLeft;
// this is the true current width
// it is used to calculate the ink bar position
let currentWidth = this.element.offsetWidth;
this.setProperties({ left, currentWidth });
},
click() {
if (!this.get('disabled')) {
invokeAction(this, 'onClick', ...arguments);
invokeAction(this, 'onSelect', this);
}
}
});