|
105 | 105 |
|
106 | 106 | <script> |
107 | 107 |
|
108 | | -let clickIndex = null; |
109 | | -const inputs = document.querySelectorAll('input'); |
110 | | - |
111 | | -Array.from(inputs).forEach((input) => { |
112 | | - input.addEventListener('click', (e) => { |
113 | | - // console.log(e, 'click'); |
114 | | - if(e.target.checked === true) { |
115 | | - if(e.shiftKey === true) { |
116 | | - toggleShiftInputs(); |
117 | | - } else { |
118 | | - findFocus(); |
| 108 | +let lastChecked; |
| 109 | +const checkboxes = document.querySelectorAll('.inbox input[type="checkbox"]'); |
| 110 | + |
| 111 | +function handleCheck(e) { |
| 112 | + console.log(e); |
| 113 | + //do they have shiftkey down? |
| 114 | + let inBetween = false; //looking between 2 indexes |
| 115 | + if(e.shiftKey && this.checked) { |
| 116 | + //loop through every checkbox |
| 117 | + checkboxes.forEach(checkbox => { |
| 118 | + // console.log(checkbox); |
| 119 | + if(checkbox === this || checkbox === lastChecked) { |
| 120 | + inBetween = !inBetween; //toggle inbetween at beginning or end of loop (handles reverse order) |
119 | 121 | } |
120 | | - } else { |
121 | | - clickIndex = null; |
122 | | - } |
123 | | - }); |
124 | | -}); |
125 | | - |
126 | | -function findFocus() { |
127 | | - Array.from(inputs).forEach((input, index, array) => { |
128 | | - if(input === document.activeElement) { |
129 | | - clickIndex = index; |
130 | | - } |
131 | | - }); |
132 | | -} |
133 | | - |
134 | | - |
135 | | -function toggleShiftInputs() { |
136 | | - let alpha = clickIndex; |
137 | | - findFocus(); |
138 | | - let omega = clickIndex; |
139 | | - |
140 | | - if(alpha !== null && omega !== null) { |
141 | | - if(alpha > omega) { //5, 1 |
142 | | - //step through descending |
143 | | - Array.from(inputs).forEach((input, index) => { |
144 | | - if(index <= alpha && index >= omega ) { |
145 | | - input.checked = true; |
146 | | - } |
147 | | - }); |
148 | | - } else { |
149 | | - // step through ascending |
150 | | - Array.from(inputs).forEach((input, index) => { |
151 | | - if(index >= alpha && index <= omega ) { |
152 | | - input.checked = true; |
153 | | - } |
154 | | - }); |
155 | | - } |
| 122 | + if (inBetween) { |
| 123 | + checkbox.checked = true; |
| 124 | + } |
| 125 | + }); |
156 | 126 | } |
| 127 | + lastChecked = this; |
157 | 128 | } |
158 | 129 |
|
| 130 | +checkboxes.forEach(checkbox => checkbox.addEventListener('click', handleCheck)); |
| 131 | + |
| 132 | + |
| 133 | +// let clickIndex = null; |
| 134 | +// const inputs = document.querySelectorAll('input'); |
| 135 | + |
| 136 | +// Array.from(inputs).forEach((input) => { |
| 137 | +// input.addEventListener('click', (e) => { |
| 138 | +// // console.log(e, 'click'); |
| 139 | +// if(e.target.checked === true) { |
| 140 | +// if(e.shiftKey === true) { |
| 141 | +// toggleShiftInputs(); |
| 142 | +// } else { |
| 143 | +// findFocus(); |
| 144 | +// } |
| 145 | +// } else { |
| 146 | +// clickIndex = null; |
| 147 | +// } |
| 148 | +// }); |
| 149 | +// }); |
| 150 | + |
| 151 | +// function findFocus() { |
| 152 | +// Array.from(inputs).forEach((input, index, array) => { |
| 153 | +// if(input === document.activeElement) { |
| 154 | +// clickIndex = index; |
| 155 | +// } |
| 156 | +// }); |
| 157 | +// } |
| 158 | + |
| 159 | + |
| 160 | +// function toggleShiftInputs() { |
| 161 | +// let alpha = clickIndex; |
| 162 | +// findFocus(); |
| 163 | +// let omega = clickIndex; |
| 164 | + |
| 165 | +// if(alpha !== null && omega !== null) { |
| 166 | +// if(alpha > omega) { //5, 1 |
| 167 | +// //step through descending |
| 168 | +// Array.from(inputs).forEach((input, index) => { |
| 169 | +// if(index <= alpha && index >= omega ) { |
| 170 | +// input.checked = true; |
| 171 | +// } |
| 172 | +// }); |
| 173 | +// } else { |
| 174 | +// // step through ascending |
| 175 | +// Array.from(inputs).forEach((input, index) => { |
| 176 | +// if(index >= alpha && index <= omega ) { |
| 177 | +// input.checked = true; |
| 178 | +// } |
| 179 | +// }); |
| 180 | +// } |
| 181 | +// } |
| 182 | +// } |
| 183 | + |
159 | 184 | </script> |
160 | 185 | </body> |
161 | 186 | </html> |
0 commit comments