|
110 | 110 | const checkboxes = document.querySelectorAll('.inbox input[type="checkbox"]'); |
111 | 111 |
|
112 | 112 | let lastChecked; |
113 | | -let atEdge = false; |
114 | | -let filling = false; |
115 | | -let shiftClick = false; |
116 | 113 |
|
117 | 114 | function handleCheck(e) { |
118 | | - if(lastChecked && shiftClick) { |
119 | | - for( i = 0; i < checkboxes.length; i++ ) { |
120 | | - atEdge = (checkboxes[i] == lastChecked || checkboxes[i] == e.currentTarget); |
121 | | - filling = atEdge ? !filling : filling; |
122 | | - if ( !atEdge ) { |
123 | | - checkboxes[i].checked = filling; |
| 115 | + // Check if they had the shift key down |
| 116 | + // AND check that they are checking it |
| 117 | + let inBetween = false; |
| 118 | + if(e.shiftKey && this.checked) { |
| 119 | + //go ahead and do what we please |
| 120 | + //loop over every single checkbox |
| 121 | + checkboxes.forEach(checkbox => { |
| 122 | + if(checkbox === this || checkbox === lastChecked) { |
| 123 | + inBetween = !inBetween; |
| 124 | + } |
| 125 | + |
| 126 | + if(inBetween) { |
| 127 | + checkbox.checked = true; |
| 128 | + } |
| 129 | + }) |
| 130 | + } |
| 131 | + |
| 132 | + if(e.shiftKey && lastChecked) { |
| 133 | + checkboxes.forEach(checkbox => { |
| 134 | + if (checkbox === this || checkbox === lastChecked) { |
| 135 | + inBetween = !inBetween; |
| 136 | + } |
| 137 | + |
| 138 | + if(inBetween) { |
| 139 | + checkbox.checked = true; |
124 | 140 | } |
125 | | - } |
| 141 | + }); |
126 | 142 | } |
127 | | - atEdge = false; |
128 | | - filling = false; |
129 | | - lastChecked = e.currentTarget; |
| 143 | + lastChecked = this; |
130 | 144 | } |
131 | 145 |
|
132 | 146 | //add a click event listener on each item |
133 | 147 | checkboxes.forEach(checkbox => checkbox.addEventListener('click', handleCheck)); |
134 | 148 |
|
135 | | -//figure out how to index the items |
136 | | - |
137 | | -//Get the index of the last clicked item |
138 | | - |
139 | | -//Get the index of the current clicked item |
140 | | - |
141 | | -//Check all boxes in between last clicked and current click index if shift key down + click |
142 | | - |
143 | | - |
144 | | -//Day 01 listening for keyboard events |
145 | | -//Figure out how to tell if it's a shift+click |
146 | | -function keydown(e) { |
147 | | - if(e.keyCode == "16") { |
148 | | - shiftClick = true; |
149 | | - } |
150 | | -} |
151 | | - |
152 | | -function keyup(e) { |
153 | | - if(e.keyCode == "16") { |
154 | | - shiftClick = false; |
155 | | - } |
156 | | -} |
157 | | - |
158 | | -window.addEventListener('keydown', keydown); |
159 | | -window.addEventListener('keyup', keyup); |
160 | 149 | </script> |
161 | 150 | </body> |
162 | 151 | </html> |
0 commit comments