/
x-option.js
74 lines (66 loc) · 1.98 KB
/
x-option.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
import Ember from 'ember';
import XSelectComponent from './x-select';
var isArray = Ember.isArray;
/**
* Used to wrap a native `<option>` tag and associate an object with
* it that can be bound. It can only be used in conjuction with a
* containing `x-select` component
*
* @class Ember.XOptionComponent
* @extends Ember.Component
*/
export default Ember.Component.extend({
tagName: 'option',
attributeBindings: ['selected', 'name', 'disabled', 'value', 'title'],
classNameBindings: [':x-option'],
/**
* The value associated with this option. When this option is
* selected, the `x-select` will fire its action with this
* value.
*
* @property value
* @type Object
* @default null
*/
value: null,
/**
* Property bound to the `selected` attribute of the native
* `<option>` element. It is aware of the containing `x-select`'s
* value and will mark itself if it is the same.
*
* @private
* @property selected
* @type Boolean
*/
selected: Ember.computed('value', 'select.value', 'select.multiple', function() {
if (this.get('select.multiple') && isArray(this.get('select.value'))) {
let selectValue = Ember.A(this.get('select.value'));
return selectValue.contains(this.get('value'));
} else {
return this.get('value') === this.get('select.value');
}
}),
/**
* Register this x-option with the containing `x-select`
*
* @override
*/
didInsertElement: function() {
this._super.apply(this, arguments);
Ember.run.scheduleOnce('afterRender', () => {
var select = this.nearestOfType(XSelectComponent);
Ember.assert("x-option component declared without enclosing x-select", !!select);
this.set('select', select);
select.registerOption(this);
});
},
/**
* Unregister this x-option with its containing x-select.
*
* @override
*/
willDestroyElement: function() {
this._super.apply(this, arguments);
this.get('select').unregisterOption(this);
}
});