-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathradios.js
120 lines (108 loc) · 4.26 KB
/
radios.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'use strict';
import _ from 'underscore';
import FormElementGroup from './form-element-group';
/**
* A callback function that fires when one of the radio button elements are selected
* @callback Radios~onChange
* @param {string} value - The value of the input element that was changed
* @param {HTMLInputElement} input - The input element that was changed
* @param {HTMLElement} UIElement - The container of the input element that was changed
*/
/**
* Groups radio buttons elements.
* @class Radios
* @extends FormElement
*/
class Radios extends FormElementGroup {
/**
* Initialization.
* @param {object} options - Options passed into instance
* @param {Array|HTMLInputElement} options.inputs - The collection of radio input elements
* @param {Radios~onChange} [options.onChange] - A callback function that fires when one of the toggle elements are selected
* @param {string} [options.containerClass] - The css class that will be applied to each toggle item's container
* @param {string} [options.inputClass] - The css class that will be applied to each toggle item (input element)
* @param {string} [options.selectedClass] - The css class that will be applied to a radio button item (UI-version) when it is selected
* @param {string} [options.disabledClass] - The css class that will be applied to a radio button item (UI-version) when it is disabled
* @param {string|Array} [options.value] - The string matching the name attribute of the toggle button to have selected initially (or an array of such strings)
*/
constructor (options) {
options = _.extend({
inputs: [],
onChange: null,
containerClass: 'ui-radio',
inputClass: 'ui-radio-input',
selectedClass: 'ui-radio-selected',
disabledClass: 'ui-radio-disabled',
value: null
}, options);
super(options);
this.options = options;
}
/**
* When one of the radio input elements are clicked.
* @param {HTMLInputElement} formElement - The radio button element
* @param {HTMLElement} UIElement - The ui element
* @private
*/
_onFormElementClick (formElement, UIElement) {
if (this._lastRadioClicked !== formElement) {
this.triggerAll((formEl, UIEl) => {
if (!formEl.checked) {
UIEl.classList.remove(this.options.selectedClass);
} else {
UIEl.classList.add(this.options.selectedClass);
}
});
this._lastRadioClicked = formElement;
this.triggerChange(formElement, UIElement);
}
}
/**
* When one of the radio UI elements are clicked.
* @param {HTMLInputElement} formElement - The radio button element
* @param {HTMLElement} UIElement - The ui element
* @private
*/
_onUIElementClick (formElement, UIElement) {
if (this._lastRadioClicked !== formElement) {
this.triggerAll((formEl, UIEl) => {
if (formEl !== formElement) {
UIEl.classList.remove(this.options.selectedClass);
formEl.checked = false;
} else {
UIEl.classList.add(this.options.selectedClass);
formEl.checked = true;
}
});
this._lastRadioClicked = formElement;
this.triggerChange(formElement, UIElement);
}
}
/**
* Selects the toggle item.
* @param {Number} index - The index of the toggle item
*/
select (index) {
var input = this.getFormElement(index),
toggle = this.getUIElement(index);
if (!input.checked) {
input.checked = true;
toggle.classList.add(this.options.selectedClass);
this.triggerChange(input, toggle);
}
this.triggerAll(function (formElement, UIElement, idx) {
if (!formElement.checked) {
// deselect all other toggles if they are radio buttons
this.deselect(idx);
}
}.bind(this));
}
/**
* Gets the unique identifier for radio buttons.
* @returns {string}
*/
getElementKey () {
return 'radios';
}
}
module.exports = Radios;