|
30 | 30 | border-bottom: 0; |
31 | 31 | } |
32 | 32 |
|
33 | | - |
34 | 33 | input:checked + p { |
35 | 34 | background: #F9F9F9; |
36 | 35 | text-decoration: line-through; |
|
45 | 44 | padding: 20px; |
46 | 45 | transition: background 0.2s; |
47 | 46 | flex: 1; |
48 | | - font-family: 'helvetica neue'; |
| 47 | + font-family:'helvetica neue'; |
49 | 48 | font-size: 20px; |
50 | 49 | font-weight: 200; |
51 | 50 | border-left: 1px solid #D1E2FF; |
|
54 | 53 | <!-- |
55 | 54 | The following is a common layout you would see in an email client. |
56 | 55 |
|
57 | | - When a user clicks a checkbox, holds Shift, and then clicks another checkbox a few rows down, all the checkboxes in-between those two checkboxes should be checked. |
| 56 | + When a user clicks a checkbox, holds Shift, and then clicks another checkbox a few rows down, all the checkboxes inbetween those two checkboxes should be checked. |
58 | 57 |
|
59 | 58 | --> |
60 | 59 | <div class="inbox"> |
|
80 | 79 | </div> |
81 | 80 | <div class="item"> |
82 | 81 | <input type="checkbox"> |
83 | | - <p>Try do it without any libraries</p> |
| 82 | + <p>Try to do it without any libraries</p> |
84 | 83 | </div> |
85 | 84 | <div class="item"> |
86 | 85 | <input type="checkbox"> |
|
97 | 96 | </div> |
98 | 97 |
|
99 | 98 | <script> |
100 | | -const checkboxes = document.querySelectorAll('.inbox input[type="checkbox"]'); |
101 | | - |
102 | | -let lastChecked; |
103 | | - |
104 | | -function handleCheck(e) { |
105 | | - // Check if they had the shift key down |
106 | | - // AND check that they are checking it |
107 | | - let inBetween = false; |
108 | | - if (e.shiftKey && this.checked) { |
109 | | - // go ahead and do what we please |
110 | | - // loop over every single checkbox |
111 | | - checkboxes.forEach(checkbox => { |
112 | | - console.log(checkbox); |
113 | | - if (checkbox === this || checkbox === lastChecked) { |
114 | | - inBetween = !inBetween; |
115 | | - console.log('Starting to check them in between!'); |
116 | | - } |
117 | | - |
118 | | - if (inBetween) { |
119 | | - checkbox.checked = true; |
120 | | - } |
121 | | - }); |
| 99 | + class listSelect { |
| 100 | + constructor () { |
| 101 | + this.checkboxes = document.querySelectorAll('input[type="checkbox"]'); |
| 102 | + this.proto = Array.prototype; |
| 103 | + this.lastIndex; |
| 104 | + } |
| 105 | + |
| 106 | + findIndex = node => this.proto.findIndex.call(this.checkboxes, box => node === box ) |
| 107 | + |
| 108 | + firstLast ( node, currentIndex ) { |
| 109 | + const indexArr = this.proto.reduce.call(this.checkboxes, (arr, box) => { |
| 110 | + if( box.checked ){ |
| 111 | + arr.push( this.findIndex(box) ); |
| 112 | + } |
| 113 | + return arr; |
| 114 | + }, []), |
| 115 | + firstLastArr = ( (arr, unchecked, current) => { |
| 116 | + if ( ! unchecked ) { |
| 117 | + return arr; |
| 118 | + } |
| 119 | + arr[0] = current > arr[0] && current > this.lastIndex ? current +1 : arr[0]; |
| 120 | + arr[1] = current < arr[1] && current < this.lastIndex ? current -1 : arr[1]; |
| 121 | + |
| 122 | + return arr; |
| 123 | + })([indexArr[0], indexArr[indexArr.length -1]], false == node.checked, currentIndex); |
| 124 | + |
| 125 | + return firstLastArr; |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | + update (node, currentIndex) { |
| 130 | + const firstLast = this.firstLast(node, currentIndex); |
| 131 | + |
| 132 | + this.checkboxes.forEach( (box, index) => { |
| 133 | + if( index >= firstLast[0] && index <= firstLast[1] ) { |
| 134 | + box.checked = true; |
| 135 | + } else { |
| 136 | + box.checked = false; |
| 137 | + } |
| 138 | + }) |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + const lS = new listSelect(); |
| 146 | + function change(e) { |
| 147 | + const currentIndex = lS.findIndex(this); |
| 148 | + |
| 149 | + if ( ! e.shiftKey ) { |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + lS.update(this, currentIndex); |
| 154 | + |
| 155 | + lS.lastIndex = currentIndex; |
| 156 | + |
122 | 157 | } |
123 | 158 |
|
124 | | - lastChecked = this; |
125 | | -} |
| 159 | + lS.checkboxes.forEach( box => box.addEventListener('click', change ) ) |
126 | 160 |
|
127 | | -checkboxes.forEach(checkbox => checkbox.addEventListener('click', handleCheck)); |
128 | 161 | </script> |
129 | 162 | </body> |
130 | 163 | </html> |
0 commit comments