@@ -7,13 +7,15 @@ import "../../core/jquery-ext";
7
7
export const parser = new Parser ( "checklist" ) ;
8
8
parser . addArgument ( "select" , ".select-all" ) ;
9
9
parser . addArgument ( "deselect" , ".deselect-all" ) ;
10
+ parser . addArgument ( "toggle" , ".toggle-all" ) ;
10
11
11
12
export default Base . extend ( {
12
13
name : "checklist" ,
13
14
trigger : ".pat-checklist" ,
14
15
jquery_plugin : true ,
15
16
all_selects : [ ] ,
16
17
all_deselects : [ ] ,
18
+ all_toggles : [ ] ,
17
19
all_checkboxes : [ ] ,
18
20
all_radios : [ ] ,
19
21
@@ -24,7 +26,9 @@ export default Base.extend({
24
26
} ,
25
27
26
28
_init ( ) {
27
- this . all_checkboxes = this . el . querySelectorAll ( "input[type=checkbox]" ) ;
29
+ this . all_checkboxes = this . el . querySelectorAll (
30
+ `input[type=checkbox]:not(${ this . options . toggle } `
31
+ ) ;
28
32
this . all_radios = this . el . querySelectorAll ( "input[type=radio]" ) ;
29
33
30
34
this . all_selects = dom . find_scoped ( this . el , this . options . select ) ;
@@ -37,14 +41,19 @@ export default Base.extend({
37
41
btn . addEventListener ( "click" , this . deselect_all . bind ( this ) ) ;
38
42
}
39
43
44
+ this . all_toggles = dom . find_scoped ( this . el , this . options . toggle ) ;
45
+ for ( const btn of this . all_toggles ) {
46
+ btn . addEventListener ( "click" , this . toggle_all . bind ( this ) ) ;
47
+ }
48
+
40
49
// update select/deselect button status
41
50
this . el . addEventListener ( "change" , this . _handler_change . bind ( this ) ) ;
42
- this . change_buttons ( ) ;
51
+ this . change_buttons_and_toggles ( ) ;
43
52
this . change_checked ( ) ;
44
53
} ,
45
54
46
55
_handler_change ( ) {
47
- utils . debounce ( ( ) => this . change_buttons ( ) , 50 ) ( ) ;
56
+ utils . debounce ( ( ) => this . change_buttons_and_toggles ( ) , 50 ) ( ) ;
48
57
utils . debounce ( ( ) => this . change_checked ( ) , 50 ) ( ) ;
49
58
} ,
50
59
@@ -65,7 +74,7 @@ export default Base.extend({
65
74
let res ;
66
75
let parent = el . parentNode ;
67
76
while ( parent ) {
68
- res = parent . querySelectorAll ( sel ) ;
77
+ res = parent . querySelectorAll ( ` ${ sel } :not( ${ this . options . toggle } )` ) ;
69
78
if ( res . length || parent === this . el ) {
70
79
// return if results were found or we reached the pattern top
71
80
return res ;
@@ -84,7 +93,7 @@ export default Base.extend({
84
93
return chkbxs ;
85
94
} ,
86
95
87
- change_buttons ( ) {
96
+ change_buttons_and_toggles ( ) {
88
97
let chkbxs ;
89
98
for ( const btn of this . all_selects ) {
90
99
chkbxs = this . find_checkboxes ( btn , "input[type=checkbox]" ) ;
@@ -98,6 +107,12 @@ export default Base.extend({
98
107
. map ( ( el ) => el . matches ( ":checked" ) )
99
108
. every ( ( it ) => it === false ) ;
100
109
}
110
+ for ( const tgl of this . all_toggles ) {
111
+ chkbxs = this . find_checkboxes ( tgl , "input[type=checkbox]" ) ;
112
+ tgl . checked = [ ...chkbxs ]
113
+ . map ( ( el ) => el . matches ( ":checked" ) )
114
+ . every ( ( it ) => it === true ) ;
115
+ }
101
116
} ,
102
117
103
118
select_all ( e ) {
@@ -121,19 +136,28 @@ export default Base.extend({
121
136
}
122
137
} ,
123
138
139
+ toggle_all ( e ) {
140
+ e . preventDefault ( ) ;
141
+ const checked = e . target . checked ;
142
+ const chkbxs = this . find_checkboxes ( e . target , "input[type=checkbox]" ) ;
143
+ for ( const box of chkbxs ) {
144
+ box . checked = checked ;
145
+ box . dispatchEvent ( new Event ( "change" , { bubbles : true , cancelable : true } ) ) ;
146
+ }
147
+ } ,
148
+
124
149
change_checked ( ) {
125
- for ( const it of [ ...this . all_checkboxes ] . concat ( [ ... this . all_radios ] ) ) {
150
+ for ( const it of [ ...this . all_checkboxes , ... this . all_radios ] ) {
126
151
for ( const label of it . labels ) {
127
- label . classList . remove ( "unchecked" ) ;
128
- label . classList . remove ( "checked" ) ;
152
+ label . classList . remove ( "checked" , "unchecked" ) ;
129
153
label . classList . add ( it . checked ? "checked" : "unchecked" ) ;
130
154
}
131
155
}
132
156
133
157
for ( const fieldset of dom . querySelectorAllAndMe ( this . el , "fieldset" ) ) {
134
158
if (
135
159
fieldset . querySelectorAll (
136
- " input[type=checkbox]:checked, input[type=radio]:checked"
160
+ ` input[type=checkbox]:checked:not( ${ this . options . toggle } ) , input[type=radio]:checked`
137
161
) . length
138
162
) {
139
163
fieldset . classList . remove ( "unchecked" ) ;
0 commit comments