1
+ /*global $, Option */
2
+
3
+ /**
4
+ * @class RadioOption
5
+ *
6
+ * A radio option for the live example.
7
+ */
8
+ class RadioOption extends Option {
9
+
10
+ /**
11
+ * @cfg {Array} options
12
+ *
13
+ * An array of options for the radio selection. Each string is both the
14
+ * displayed name and value.
15
+ *
16
+ * Any data type may be passed as the elements, and {@link #getValue} will
17
+ * return that value/type.
18
+ */
19
+
20
+ /**
21
+ * @cfg {Boolean} [defaultValue=false]
22
+ *
23
+ * `true` to check the checkbox by default.
24
+ */
25
+
26
+
27
+ /**
28
+ * @constructor
29
+ * @param {Object } cfg The configuration options for this class, specified
30
+ * in an Object (map).
31
+ */
32
+ constructor ( cfg ) {
33
+ super ( cfg ) ;
34
+
35
+ this . options = [ ] . concat ( cfg . options ) ;
36
+ this . defaultValue = cfg . defaultValue || false ;
37
+
38
+ this . $containerEl . html ( this . generateHtml ( ) ) ;
39
+ this . $valueDisplayEl = this . $containerEl . find ( '#' + this . containerId + '-value' ) ;
40
+
41
+ this . $containerEl
42
+ . find ( ':radio' ) . on ( 'change' , this . updateDisplayEl . bind ( this ) ) ;
43
+ }
44
+
45
+
46
+ /**
47
+ * @private
48
+ * @return {string }
49
+ */
50
+ generateHtml ( ) {
51
+ var containerId = this . containerId ,
52
+ optionName = this . optionName ,
53
+ optionDescription = this . optionDescription ,
54
+ defaultValue = this . defaultValue ,
55
+ radiosHtml = this . createRadiosHtml ( this . options , defaultValue ) ;
56
+
57
+ return `
58
+ <label>
59
+ ${ optionDescription } : (<code>${ optionName } : <span id="${ containerId } -value">${ this . formatValueForDisplay ( defaultValue ) } </span></code>)
60
+ </label>
61
+ <div class="pl10">${ radiosHtml . join ( ' ' ) } </div>
62
+ ` ;
63
+ }
64
+
65
+
66
+ /**
67
+ * Creates an array of '<input type="radio">' HTML tags.
68
+ *
69
+ * @private
70
+ * @param {Array } options
71
+ * @param {* } defaultValue
72
+ * @return {String[] }
73
+ */
74
+ createRadiosHtml ( options , defaultValue ) {
75
+ return options . map ( ( option , idx ) => {
76
+ return `
77
+ <input type="radio" id="${ this . containerId } -radio-${ option } " name="${ this . containerId } -radio" data-option-idx="${ idx } " ${ option === this . defaultValue ? 'checked' : '' } >
78
+ <label for="${ this . containerId } -radio-${ option } ">${ option } </label>
79
+ ` ;
80
+ } ) ;
81
+ }
82
+
83
+
84
+ /**
85
+ * @private
86
+ */
87
+ updateDisplayEl ( ) {
88
+ var displayValue = this . formatValueForDisplay ( this . getValue ( ) ) ;
89
+
90
+ this . $valueDisplayEl . html ( displayValue ) ;
91
+ this . fireChange ( ) ;
92
+ }
93
+
94
+
95
+ /**
96
+ * @return {Boolean }
97
+ */
98
+ getValue ( ) {
99
+ var optionIdx = this . $containerEl . find ( ':radio:checked' ) . data ( 'option-idx' ) ;
100
+
101
+ return this . options [ optionIdx ] ;
102
+ }
103
+
104
+
105
+ /**
106
+ * Formats an option value for display.
107
+ *
108
+ * Strings are surrounded with quotes, booleans and numbers are returned
109
+ * as strings as-is.
110
+ *
111
+ * @param {* } value
112
+ */
113
+ formatValueForDisplay ( value ) {
114
+ return ( typeof value === 'string' ) ? `'${ value } '` : ( value + '' ) ;
115
+ }
116
+
117
+ }
0 commit comments