-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathindex.html
156 lines (133 loc) · 4.28 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<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;
}
.details {
text-align: center;
font-size: 15px;
}
</style>
</head>
<body>
<!--
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" id="0">
<p>This is an inbox layout.</p>
</div>
<div class="item">
<input type="checkbox" id="1">
<p>Check one item</p>
</div>
<div class="item">
<input type="checkbox" id="2">
<p>Hold down your Shift key</p>
</div>
<div class="item">
<input type="checkbox" id="3">
<p>Check a lower item</p>
</div>
<div class="item">
<input type="checkbox" id="4">
<p>Everything inbetween should also be set to checked</p>
</div>
<div class="item">
<input type="checkbox" id="5">
<p>Try do it with out any libraries</p>
</div>
<div class="item">
<input type="checkbox" id="6">
<p>Just regular JavaScript</p>
</div>
<div class="item">
<input type="checkbox" id="7">
<p>Good Luck!</p>
</div>
<div class="item">
<input type="checkbox" id="8">
<p>Don't forget to tweet your result!</p>
</div>
</div>
<script>
// Wrap JS code in an IIFE, creating a closure so as to not pollute the global namespace
(() => {
// Declare variable that will be defined later as the input that the user
// most recently selected
let lastSelected;
// Declare & define constant as a reference to all inputs of type checkbox
// that are children of the 'inbox' class
const checkBoxes = document.querySelectorAll('.inbox input[type="checkbox"]');
// Event Handler function
const multiCheck = (el, e) => {
// If the lastSelected variable is defined and the shift key was pressed
// when this event triggered...
if (lastSelected && e.shiftKey) {
// Set checked property of checkbox to be the same as the
// checked property of the last selected input
el.checked = lastSelected.checked;
// If the ID of the checkbox is greater than the ID of the last selected input...
const [startIdx, endIdx] = el.id > lastSelected.id ?
// ...define variable startIdx as last selected input ID and endIdx as checkbox ID
[lastSelected.id, el.id] :
// ...if not, switch variable definitions
[el.id, lastSelected.id];
// Declare & define constant as an array of inputs between the last selected input
// and the checkbox input
const middleInputs = [...checkBoxes].slice(parseInt(startIdx) + 1, endIdx);
// Set checked property of each input to match checked property of checkbox input
middleInputs.forEach(checkbox => checkbox.checked = el.checked);
}
// If no lastSelected or shiftKey was not pressed, entire if statement is skipped
// Define lastSelected as the input that was the checked which triggered the event handler
lastSelected = el;
}
// Add event listener to each input that will call multiCheck function on the 'click' event
checkBoxes.forEach(checkBox => {
checkBox.addEventListener('click', multiCheck.bind(null, checkBox));
});
})();
</script>
</body>
</html>