Skip to content

Commit 7296b55

Browse files
author
Mike North
committed
Checkbox, Radio, Switch, Range and Checkbox/Radio/Switch List components
1 parent 0cac9fd commit 7296b55

38 files changed

Lines changed: 925 additions & 54 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import SelectableItem from './materialize-selectable-item';
2+
import layout from '../templates/components/materialize-checkbox';
3+
4+
export default SelectableItem.extend({
5+
layout: layout,
6+
classNames: ['materialize-checkbox']
7+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import SelectableItemGroup from './materialize-selectable-item-group';
2+
import CheckboxComponent from './materialize-checkbox';
3+
import GroupSelectableItemMixin from '../mixins/group-selectable-item';
4+
5+
6+
var GroupCheckboxComponent = CheckboxComponent.extend(GroupSelectableItemMixin, { });
7+
8+
export default SelectableItemGroup.extend({
9+
selectableItemView: GroupCheckboxComponent,
10+
multiple: true
11+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Ember from 'ember';
2+
import SelectableItem from './materialize-selectable-item';
3+
import layout from '../templates/components/materialize-radio';
4+
5+
export default SelectableItem.extend({
6+
layout: layout,
7+
className: ['materialize-radio'],
8+
groupName: Ember.computed.alias('group.element.id'),
9+
10+
_inputId: Ember.computed('element.id', function () {
11+
return this.get('element.id') + 'input';
12+
}).readOnly(),
13+
14+
didInsertElement() {
15+
this._super(...arguments);
16+
this._checkboxInitialization();
17+
},
18+
19+
_checkboxInitialization() {
20+
/*
21+
Workaround for a limitation of the {{input}} helper, where
22+
the checked property doesn't two-way bind as expected when
23+
type="radio" is used
24+
*/
25+
this._radioChangeListener = jqEvt => this.set('isSelected', jqEvt.target.checked);
26+
27+
this.$('.selectable-item-input')
28+
.on('change', this._radioChangeListener);
29+
30+
// Potentially part of the same workaround. We need to
31+
// set the initial state of the radio button explicitly
32+
this.$('.selectable-item-input')
33+
.prop('checked', this.get('isSelected'));
34+
},
35+
36+
willDestroyElement() {
37+
this.$('.selectable-item-input')
38+
.off('change', this._radioChangeListener);
39+
}
40+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import SelectableItemGroup from './materialize-selectable-item-group';
2+
import RadioComponent from './materialize-radio';
3+
import GroupSelectableItemMixin from '../mixins/group-selectable-item';
4+
5+
var GroupRadioComponent = RadioComponent.extend(GroupSelectableItemMixin, { });
6+
7+
export default SelectableItemGroup.extend({
8+
selectableItemView: GroupRadioComponent
9+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Ember from 'ember';
2+
import layout from '../templates/components/materialize-range';
3+
4+
export default Ember.Component.extend({
5+
min: 0,
6+
max: 100,
7+
step: 5,
8+
layout: layout,
9+
classNames: ['range-field']
10+
});
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import Ember from 'ember';
2+
import layout from '../templates/components/materialize-selectable-item-group';
3+
4+
var get = Ember.get,
5+
map = Ember.EnumerableUtils.map,
6+
indexOf = Ember.EnumerableUtils.indexOf;
7+
8+
export default Ember.Component.extend({
9+
content: null,
10+
selection: null,
11+
layout: layout,
12+
optionValuePath: 'content',
13+
optionLabelPath: 'content',
14+
multiple: false,
15+
__materializeSelectableItemGroup: true,
16+
17+
init() {
18+
this._super(...arguments);
19+
if (this.get('selection') === null && !!this.get('multiple')) {
20+
this.set('selection', Ember.A());
21+
}
22+
},
23+
24+
isValueSelected(value) {
25+
if (this.get('multiple')) {
26+
return indexOf(this.get('selection'), value) >= 0;
27+
}
28+
else {
29+
return this.get('selection') === value;
30+
}
31+
},
32+
33+
setValueSelection(value, select) {
34+
if (select) {
35+
return this.addToSelection(value);
36+
}
37+
else {
38+
return this.removeFromSelection(value);
39+
}
40+
},
41+
42+
addToSelection(value) {
43+
if (this.get('multiple')) {
44+
this.get('selection').addObject(value);
45+
}
46+
else {
47+
this.set('selection', value);
48+
}
49+
},
50+
51+
removeFromSelection(value) {
52+
if (this.get('multiple')) {
53+
this.get('selection').removeObject(value);
54+
}
55+
else {
56+
throw new Error('removeFromSelection is not supported in single-selection mode');
57+
}
58+
},
59+
60+
_valuePath: Ember.computed('optionValuePath', function () {
61+
var optionValuePath = get(this, 'optionValuePath');
62+
return optionValuePath.replace(/^content\.?/, '');
63+
}),
64+
65+
_labelPath: Ember.computed('optionLabelPath', function () {
66+
var optionLabelPath = get(this, 'optionLabelPath');
67+
return optionLabelPath.replace(/^content\.?/, '');
68+
}),
69+
70+
_content: Ember.computed('content.[]', '_valuePath', '_labelPath',function () {
71+
var valuePath = get(this, '_valuePath');
72+
var labelPath = get(this, '_labelPath');
73+
var content = get(this, 'content') || Ember.A([]);
74+
75+
if (valuePath && labelPath) {
76+
return Ember.A(map(content, function (el) { return {value: get(el, valuePath), label: get(el, labelPath)}; }));
77+
} else {
78+
return Ember.A(map(content, function (el) { return {value: el, label: el}; }));
79+
}
80+
})
81+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import Ember from 'ember';
2+
3+
export default Ember.Component.extend({
4+
checked: null,
5+
classNames: ['materialize-selectable-item'],
6+
_checked: Ember.computed('group.selection', 'group.selection.[]', function (key, val) {
7+
var group = this.get('group');
8+
if (!group) {
9+
if (arguments.length <= 1) {
10+
return this.get('checked');
11+
}
12+
else {
13+
this.set('checked', val);
14+
return val;
15+
}
16+
}
17+
else {
18+
// Operate in the context of a group. The group owns
19+
// the selection state
20+
if (arguments.length <= 1) {
21+
// get
22+
return group.isValueSelected(this.get('value'));
23+
}
24+
else {
25+
//set
26+
group.setValueSelection(this.get('value'), val);
27+
return !!val;
28+
}
29+
}
30+
}),
31+
32+
isSelected: Ember.computed.alias('_checked'),
33+
_setupLabel() {
34+
var $input = this.$('.selectable-item-input')[0];
35+
36+
var inputId = $input ? $input.id : null;
37+
if (inputId) {
38+
this.$('.selectable-item-label').attr('for', inputId);
39+
}
40+
},
41+
42+
didInsertElement() {
43+
this._super(...arguments);
44+
this._setupLabel();
45+
},
46+
47+
group: Ember.computed(function () {
48+
return this.nearestWithProperty('__materializeSelectableItemGroup');
49+
})
50+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Ember from 'ember';
2+
import SelectableItem from './materialize-selectable-item';
3+
import layout from '../templates/components/materialize-switch';
4+
5+
export default SelectableItem.extend({
6+
layout: layout,
7+
offLabel: 'Off',
8+
onLabel: 'On',
9+
disabled: false,
10+
11+
classNames: ['switch', 'materialize-switch'],
12+
_labelClass: Ember.computed('name', function () {
13+
return this.get('name') ? 'right' : '';
14+
})
15+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import SelectableItemGroup from './materialize-selectable-item-group';
2+
import GroupSelectableItemMixin from '../mixins/group-selectable-item';
3+
import SwitchComponent from './materialize-switch';
4+
5+
var GroupSwitchComponent = SwitchComponent.extend(GroupSelectableItemMixin, { });
6+
7+
export default SelectableItemGroup.extend({
8+
selectableItemView: GroupSwitchComponent,
9+
multiple: true
10+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Ember from 'ember';
2+
3+
export default Ember.Mixin.create({
4+
name: Ember.computed.alias('content.label'),
5+
value: Ember.computed.alias('content.value'),
6+
disabled: Ember.computed.alias('group.disabled')
7+
});

0 commit comments

Comments
 (0)