-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathmd-select.js
More file actions
91 lines (82 loc) · 2.58 KB
/
md-select.js
File metadata and controls
91 lines (82 loc) · 2.58 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { A } from '@ember/array';
import { isNone } from '@ember/utils';
import { later } from '@ember/runloop';
import { get, observer, computed } from '@ember/object';
import MaterializeInputField from './md-input-field';
import layout from '../templates/components/md-select';
import jQuery from 'jquery';
export default MaterializeInputField.extend({
layout,
classNames: ['md-select'],
optionLabelPath: 'content',
optionValuePath: 'content',
didRender() {
this._super(...arguments);
this._setupSelect();
},
willUpdate() {
this._teardownSelect();
},
willDestroyElement() {
this._teardownSelect();
},
_setupSelect() {
// jscs: disable
jQuery('select').material_select();
// jscs: enable
},
_parsedContent: computed('optionValuePath', 'optionLabelPath', 'content.[]', function() {
const contentRegex = /(content\.|^content$)/;
// keep backwards compatability for defining optionValuePath & as optionContentPath `content.{{attName}}`
const optionValuePath = (this.get('optionValuePath') || '').replace(contentRegex, '');
const optionLabelPath = (this.get('optionLabelPath') || '').replace(contentRegex, '');
return A(
(this.get('content') || []).map(option => {
return {
value: optionValuePath ? get(option, optionValuePath) : option,
label: optionLabelPath ? get(option, optionLabelPath) : option
};
})
);
}),
_teardownSelect() {
// jscs: disable
jQuery('select').material_select('destroy');
// jscs: enable
},
actions: {
optionSelected(e) {
const target = e.target;
if (!this.get('isDisabled')) {
if (this.get('action')) {
this.sendAction('action', target.value);
}
else {
this.set('value', target.value)
}
}
}
},
// TODO: this could be converted to a computed property, returning a string
// that is bound to the class attribute of the inputSelector
errorsDidChange: observer('errors', function() {
const inputSelector = jQuery('input');
// monitor the select's validity and copy the appropriate validation class to the materialize input element.
if (!isNone(inputSelector)) {
later(
this,
function() {
const isValid = jQuery('select').hasClass('valid');
if (isValid) {
inputSelector.removeClass('invalid');
inputSelector.addClass('valid');
} else {
inputSelector.removeClass('valid');
inputSelector.addClass('invalid');
}
},
150
);
}
})
});