forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-FINISHED.html
163 lines (136 loc) · 3.69 KB
/
index-FINISHED.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hold Shift to Check Multiple Checkboxes</title>
</head>
<body>
<style>
html {
font-family: sans-serif;
background: #ffc600;
}
.inbox {
max-width: 400px;
margin: 50px auto;
background: white;
border-radius: 5px;
box-shadow: 10px 10px 0 rgba(0,0,0,0.1);
}
.item {
display: flex;
align-items: center;
border-bottom: 1px solid #F1F1F1;
}
.item:last-child {
border-bottom: 0;
}
input:checked + p {
background: #F9F9F9;
text-decoration: line-through;
}
input[type="checkbox"] {
margin: 20px;
}
p {
margin: 0;
padding: 20px;
transition: background 0.2s;
flex: 1;
font-family:'helvetica neue';
font-size: 20px;
font-weight: 200;
border-left: 1px solid #D1E2FF;
}
</style>
<!--
The following is a common layout you would see in an email client.
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.
-->
<div class="inbox">
<div class="item">
<input type="checkbox">
<p>This is an inbox layout.</p>
</div>
<div class="item">
<input type="checkbox">
<p>Check one item</p>
</div>
<div class="item">
<input type="checkbox">
<p>Hold down your Shift key</p>
</div>
<div class="item">
<input type="checkbox">
<p>Check a lower item</p>
</div>
<div class="item">
<input type="checkbox">
<p>Everything in between should also be set to checked</p>
</div>
<div class="item">
<input type="checkbox">
<p>Try to do it without any libraries</p>
</div>
<div class="item">
<input type="checkbox">
<p>Just regular JavaScript</p>
</div>
<div class="item">
<input type="checkbox">
<p>Good Luck!</p>
</div>
<div class="item">
<input type="checkbox">
<p>Don't forget to tweet your result!</p>
</div>
</div>
<script>
class listSelect {
constructor () {
this.checkboxes = document.querySelectorAll('input[type="checkbox"]');
this.proto = Array.prototype;
this.lastIndex;
}
findIndex = node => this.proto.findIndex.call(this.checkboxes, box => node === box )
firstLast ( node, currentIndex ) {
const indexArr = this.proto.reduce.call(this.checkboxes, (arr, box) => {
if( box.checked ){
arr.push( this.findIndex(box) );
}
return arr;
}, []),
firstLastArr = ( (arr, unchecked, current) => {
if ( ! unchecked ) {
return arr;
}
arr[0] = current > arr[0] && current > this.lastIndex ? current +1 : arr[0];
arr[1] = current < arr[1] && current < this.lastIndex ? current -1 : arr[1];
return arr;
})([indexArr[0], indexArr[indexArr.length -1]], false == node.checked, currentIndex);
return firstLastArr;
}
update (node, currentIndex) {
const firstLast = this.firstLast(node, currentIndex);
this.checkboxes.forEach( (box, index) => {
if( index >= firstLast[0] && index <= firstLast[1] ) {
box.checked = true;
} else {
box.checked = false;
}
})
}
}
const lS = new listSelect();
function change(e) {
const currentIndex = lS.findIndex(this);
if ( ! e.shiftKey ) {
return;
}
lS.update(this, currentIndex);
lS.lastIndex = currentIndex;
}
lS.checkboxes.forEach( box => box.addEventListener('click', change ) )
</script>
</body>
</html>