1
+ /*global $, Option */
2
+
3
+ namespace LiveExample {
4
+
5
+ interface CheckboxOptionCfg extends LiveExample . OptionCfg {
6
+ defaultValue ?: boolean ;
7
+ }
8
+
9
+
10
+ /**
11
+ * @class CheckboxOption
12
+ *
13
+ * A checkbox option for the live example.
14
+ */
15
+ export class CheckboxOption extends LiveExample . Option {
16
+
17
+ /**
18
+ * @cfg {Boolean} [defaultValue=false]
19
+ *
20
+ * `true` to check the checkbox by default.
21
+ */
22
+ private defaultValue : boolean = false ;
23
+
24
+ private $checkboxEl : JQuery ;
25
+ private $valueDisplayEl : JQuery ;
26
+
27
+
28
+ /**
29
+ * @constructor
30
+ * @param {CheckboxOptionCfg } cfg The configuration options for this
31
+ * class, specified in an Object (map).
32
+ */
33
+ constructor ( cfg : CheckboxOptionCfg ) {
34
+ super ( cfg ) ;
35
+
36
+ this . defaultValue = cfg . defaultValue || false ;
37
+
38
+ this . $containerEl . html ( this . generateHtml ( ) ) ;
39
+ this . $checkboxEl = this . $containerEl . find ( ':checkbox' ) . on ( 'change' , this . updateDisplayEl . bind ( this ) ) ;
40
+ this . $valueDisplayEl = this . $containerEl . find ( '#' + this . containerId + '-value' ) ;
41
+ }
42
+
43
+
44
+ /**
45
+ * @private
46
+ * @return {string }
47
+ */
48
+ private generateHtml ( ) {
49
+ var containerId = this . containerId ,
50
+ optionDescription = this . optionDescription ,
51
+ defaultValue = this . defaultValue ,
52
+ checkboxId = containerId + '-checkbox' ;
53
+
54
+ return `
55
+ <input type="checkbox" id="${ checkboxId } " ${ defaultValue ? 'checked' : '' } >
56
+ <label for="${ checkboxId } ">${ optionDescription } </label>
57
+ (<code>${ this . getApiDocAnchor ( ) } : <span id="${ containerId } -value">${ defaultValue } </span></code>)
58
+ ` ;
59
+ }
60
+
61
+
62
+ /**
63
+ * @private
64
+ */
65
+ private updateDisplayEl ( ) {
66
+ this . $valueDisplayEl . html ( this . getValue ( ) + '' ) ;
67
+ this . fireChange ( ) ;
68
+ }
69
+
70
+
71
+ /**
72
+ * @return {Boolean }
73
+ */
74
+ getValue ( ) {
75
+ return this . $checkboxEl . prop ( 'checked' ) ;
76
+ }
77
+
78
+ }
79
+
80
+ }
0 commit comments