Skip to content

Commit 47974b2

Browse files
authored
Merge pull request #243 from EmptyWork/master
Event delegation
2 parents 980660e + 23c6aec commit 47974b2

File tree

15 files changed

+244
-242
lines changed

15 files changed

+244
-242
lines changed

2-ui/2-events/03-event-delegation/1-hide-message-delegate/solution.view/index.html

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010

1111
<div id="container">
1212
<div class="pane">
13-
<h3>Horse</h3>
14-
<p>The horse is one of two extant subspecies of Equus ferus. It is an odd-toed ungulate mammal belonging to the taxonomic family Equidae. The horse has evolved over the past 45 to 55 million years from a small multi-toed creature, Eohippus, into the large, single-toed animal of today.</p>
13+
<h3>Kuda</h3>
14+
<p>Kuda adalah salah satu dari dua subspesies Equus ferus yang masih ada. Ini adalah mamalia berkuku aneh milik keluarga taksonomi Equidae. Kuda telah berevolusi selama 45 hingga 55 juta tahun terakhir dari makhluk kecil berjari banyak, Eohippus, menjadi hewan besar berjari satu saat ini.</p>
1515
<button class="remove-button">[x]</button>
1616
</div>
1717
<div class="pane">
18-
<h3>Donkey</h3>
19-
<p>The donkey or ass (Equus africanus asinus) is a domesticated member of the horse family, Equidae. The wild ancestor of the donkey is the African wild ass, E. africanus. The donkey has been used as a working animal for at least 5000 years.</p>
18+
<h3>Keledai</h3>
19+
<p>Keledai (Equus africanus asinus) adalah anggota keluarga kuda yang dijinakkan, Equidae. Nenek moyang keledai liar adalah keledai liar Afrika, E. africanus. Keledai telah digunakan sebagai hewan pekerja setidaknya selama 5000 tahun.</p>
2020
<button class="remove-button">[x]</button>
2121
</div>
2222
<div class="pane">
23-
<h3>Cat</h3>
24-
<p>The domestic cat (Latin: Felis catus) is a small, typically furry, carnivorous mammal. They are often called house cats when kept as indoor pets or simply cats when there is no need to distinguish them from other felids and felines. Cats are often valued by humans for companionship and for their ability to hunt vermin.
23+
<h3>Kucing</h3>
24+
<p>Kucing domestik (bahasa Latin: Felis catus) adalah mamalia karnivora kecil yang biasanya berbulu. Mereka sering disebut kucing rumah ketika dipelihara sebagai hewan peliharaan dalam ruangan atau hanya kucing ketika tidak perlu membedakan mereka dari felids dan kucing lainnya. Kucing sering dihargai oleh manusia karena persahabatan dan kemampuan mereka untuk berburu hama.
2525
</p>
2626
<button class="remove-button">[x]</button>
2727
</div>

2-ui/2-events/03-event-delegation/1-hide-message-delegate/source.view/index.html

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@
1010

1111
<div id="container">
1212
<div class="pane">
13-
<h3>Horse</h3>
14-
<p>The horse is one of two extant subspecies of Equus ferus. It is an odd-toed ungulate mammal belonging to the taxonomic family Equidae. The horse has evolved over the past 45 to 55 million years from a small multi-toed creature, Eohippus, into the large, single-toed animal of today.</p>
13+
<h3>Kuda</h3>
14+
<p>Kuda adalah salah satu dari dua subspesies Equus ferus yang masih ada. Ini adalah mamalia berkuku aneh milik keluarga taksonomi Equidae. Kuda telah berevolusi selama 45 hingga 55 juta tahun terakhir dari makhluk kecil berjari banyak, Eohippus, menjadi hewan besar berjari satu saat ini.</p>
1515
<button class="remove-button">[x]</button>
1616
</div>
1717
<div class="pane">
18-
<h3>Donkey</h3>
19-
<p>The donkey or ass (Equus africanus asinus) is a domesticated member of the horse family, Equidae. The wild ancestor of the donkey is the African wild ass, E. africanus. The donkey has been used as a working animal for at least 5000 years.</p>
18+
<h3>Keledai</h3>
19+
<p>Keledai (Equus africanus asinus) adalah anggota keluarga kuda yang dijinakkan, Equidae. Nenek moyang keledai liar adalah keledai liar Afrika, E. africanus. Keledai telah digunakan sebagai hewan pekerja setidaknya selama 5000 tahun.</p>
2020
<button class="remove-button">[x]</button>
2121
</div>
2222
<div class="pane">
23-
<h3>Cat</h3>
24-
<p>The domestic cat (Latin: Felis catus) is a small, typically furry, carnivorous mammal. They are often called house cats when kept as indoor pets or simply cats when there is no need to distinguish them from other felids and felines. Cats are often valued by humans for companionship and for their ability to hunt vermin.
23+
<h3>Kucing</h3>
24+
<p>Kucing domestik (bahasa Latin: Felis catus) adalah mamalia karnivora kecil yang biasanya berbulu. Mereka sering disebut kucing rumah ketika dipelihara sebagai hewan peliharaan dalam ruangan atau hanya kucing ketika tidak perlu membedakan mereka dari felids dan kucing lainnya. Kucing sering dihargai oleh manusia karena persahabatan dan kemampuan mereka untuk berburu hama.
2525
</p>
2626
<button class="remove-button">[x]</button>
2727
</div>
2828
</div>
2929

3030
<script>
31-
// ...your code...
31+
// ...kode kamu...
3232
</script>
3333

3434
</body>

2-ui/2-events/03-event-delegation/1-hide-message-delegate/task.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ importance: 5
22

33
---
44

5-
# Hide messages with delegation
5+
# Menyembunyikan pesan menggunakan delegasi
66

7-
There's a list of messages with removal buttons `[x]`. Make the buttons work.
7+
Ada sebuah daftar dengan tombol `[x]`. Buat tombol itu berfungsi.
88

9-
Like this:
9+
Seperti ini:
1010

1111
[iframe src="solution" height=420]
1212

13-
P.S. Should be only one event listener on the container, use event delegation.
13+
Tambahan: Gunakan 1 event listener pada container, gunakan delegasi peristiwa (_event delegation_).
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The solution has two parts.
1+
Solusi terbagi atas 2 bagian.
22

3-
1. Wrap every tree node title into `<span>`. Then we can CSS-style them on `:hover` and handle clicks exactly on text, because `<span>` width is exactly the text width (unlike without it).
4-
2. Set a handler to the `tree` root node and handle clicks on that `<span>` titles.
3+
1. Bungkus setiap node pada pohon kedalam `<span>`. Kemudian kita bisa menambahkan `:hover` dengan CSS-style dan menanggani klik tepat pada teks, karena lebar `<span>` sama dengan lebar tulisan (lebar tidak sama jika tidak menggunakan `<span>`);
4+
2. Atur sebuah penangan (_handler_) ke `tree` akar dari node dan tanggani setiap klik pada judul `<span>`.

2-ui/2-events/03-event-delegation/2-sliding-tree/solution.view/index.html

+21-21
Original file line numberDiff line numberDiff line change
@@ -17,60 +17,60 @@
1717
<body>
1818

1919
<ul class="tree" id="tree">
20-
<li>Animals
20+
<li>Hewan
2121
<ul>
22-
<li>Mammals
22+
<li>Mamalia
2323
<ul>
24-
<li>Cows</li>
25-
<li>Donkeys</li>
26-
<li>Dogs</li>
27-
<li>Tigers</li>
24+
<li>Sapi</li>
25+
<li>Keledai</li>
26+
<li>Anjing</li>
27+
<li>Harimau</li>
2828
</ul>
2929
</li>
30-
<li>Other
30+
<li>Lain
3131
<ul>
32-
<li>Snakes</li>
33-
<li>Birds</li>
34-
<li>Lizards</li>
32+
<li>Ular</li>
33+
<li>Burung</li>
34+
<li>Kadal</li>
3535
</ul>
3636
</li>
3737
</ul>
3838
</li>
39-
<li>Fishes
39+
<li>Ikan
4040
<ul>
41-
<li>Aquarium
41+
<li>Akuarium
4242
<ul>
43-
<li>Guppy</li>
44-
<li>Angelfish</li>
43+
<li>Gupi (ikan seribu)</li>
44+
<li>Ikan bidadari</li>
4545
</ul>
4646
</li>
47-
<li>Sea
47+
<li>Laut
4848
<ul>
49-
<li>Sea trout</li>
49+
<li>Trout laut</li>
5050
</ul>
5151
</li>
5252
</ul>
5353
</li>
5454
</ul>
5555

5656
<script>
57-
// move all text into <span>
58-
// they occupy exactly the place necessary for the text,
57+
// pindahkan semua teks kedalam <span>
58+
// mereka menempati tempat yang tepat diperlukan untuk teks,
5959
for (let li of tree.querySelectorAll('li')) {
6060
let span = document.createElement('span');
6161
li.prepend(span);
62-
span.append(span.nextSibling); // move the text node into span
62+
span.append(span.nextSibling); // memindahkan node teks ke dalam span
6363
}
6464

65-
// catch clicks on whole tree
65+
// tangkap klik pada keseluruhan pohon (tree)
6666
tree.onclick = function(event) {
6767

6868
if (event.target.tagName != 'SPAN') {
6969
return;
7070
}
7171

7272
let childrenContainer = event.target.parentNode.querySelector('ul');
73-
if (!childrenContainer) return; // no children
73+
if (!childrenContainer) return; // tidak ada elemen bawah
7474

7575
childrenContainer.hidden = !childrenContainer.hidden;
7676
}

2-ui/2-events/03-event-delegation/2-sliding-tree/source.view/index.html

+16-16
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,36 @@
66
<body>
77

88
<ul class="tree" id="tree">
9-
<li>Animals
9+
<li>Hewan
1010
<ul>
11-
<li>Mammals
11+
<li>Mamalia
1212
<ul>
13-
<li>Cows</li>
14-
<li>Donkeys</li>
15-
<li>Dogs</li>
16-
<li>Tigers</li>
13+
<li>Sapi</li>
14+
<li>Keledai</li>
15+
<li>Anjing</li>
16+
<li>Harimau</li>
1717
</ul>
1818
</li>
19-
<li>Other
19+
<li>Lain
2020
<ul>
21-
<li>Snakes</li>
22-
<li>Birds</li>
23-
<li>Lizards</li>
21+
<li>Ular</li>
22+
<li>Burung</li>
23+
<li>Kadal</li>
2424
</ul>
2525
</li>
2626
</ul>
2727
</li>
28-
<li>Fishes
28+
<li>Ikan
2929
<ul>
30-
<li>Aquarium
30+
<li>Akuarium
3131
<ul>
32-
<li>Guppy</li>
33-
<li>Angelfish</li>
32+
<li>Gupi (ikan seribu)</li>
33+
<li>Ikan bidadari</li>
3434
</ul>
3535
</li>
36-
<li>Sea
36+
<li>Laut
3737
<ul>
38-
<li>Sea trout</li>
38+
<li>Trout laut</li>
3939
</ul>
4040
</li>
4141
</ul>

2-ui/2-events/03-event-delegation/2-sliding-tree/task.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ importance: 5
22

33
---
44

5-
# Tree menu
5+
# Menu pohon (_tree menu_)
66

7-
Create a tree that shows/hides node children on click:
7+
Buat sebuah pohon yang akan menampilkan/menyembunyikan elemen bawahan pada saat di klik:
88

99
[iframe border=1 src="solution"]
1010

11-
Requirements:
11+
Syarat:
1212

13-
- Only one event handler (use delegation)
14-
- A click outside the node title (on an empty space) should not do anything.
13+
- Hanya satu penangan peristiwa (_event handler_) gunakan delegasi peristiwa.
14+
- Klik di luar judul node (pada ruang kosong) tidak boleh melakukan apa-apa.

2-ui/2-events/03-event-delegation/3-sortable-table/solution.view/index.html

+8-8
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
<table id="grid">
2626
<thead>
2727
<tr>
28-
<th data-type="number">Age</th>
29-
<th data-type="string">Name</th>
28+
<th data-type="number">Umur</th>
29+
<th data-type="string">Nama</th>
3030
</tr>
3131
</thead>
3232
<tbody>
@@ -59,10 +59,10 @@
5959
if (e.target.tagName != 'TH') return;
6060

6161
let th = e.target;
62-
// if TH, then sort
63-
// cellIndex is the number of th:
64-
// 0 for the first column
65-
// 1 for the second column, etc
62+
// Jika TH, maka urutkan
63+
// cellIndex adalah nomor dari th:
64+
// 0 untuk kolom pertama
65+
// 1 untuk kolom kedua, dan seterusnya
6666
sortGrid(th.cellIndex, th.dataset.type);
6767
};
6868

@@ -71,7 +71,7 @@
7171

7272
let rowsArray = Array.from(tbody.rows);
7373

74-
// compare(a, b) compares two rows, need for sorting
74+
// bandingkan(a, b) bandikna kedua baris, untuk di urutkan
7575
let compare;
7676

7777
switch (type) {
@@ -87,7 +87,7 @@
8787
break;
8888
}
8989

90-
// sort
90+
// urutkan
9191
rowsArray.sort(compare);
9292

9393
tbody.append(...rowsArray);

2-ui/2-events/03-event-delegation/3-sortable-table/source.view/index.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
<table id="grid">
2626
<thead>
2727
<tr>
28-
<th data-type="number">Age</th>
29-
<th data-type="string">Name</th>
28+
<th data-type="number">Umur</th>
29+
<th data-type="string">Nama</th>
3030
</tr>
3131
</thead>
3232
<tbody>
@@ -54,7 +54,7 @@
5454
</table>
5555

5656
<script>
57-
// ...your code...
57+
// ...Kode kamu...
5858
</script>
5959

6060
</body>

2-ui/2-events/03-event-delegation/3-sortable-table/task.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ importance: 4
22

33
---
44

5-
# Sortable table
5+
# Tabel yang bisa diurutkan
66

7-
Make the table sortable: clicks on `<th>` elements should sort it by corresponding column.
7+
Buat sebuah table yang dapat diurutkan: klik pada elemen `<th>` harus mengurutkan kolom dibawahnya.
88

9-
Each `<th>` has the type in the attribute, like this:
9+
Setiap `<th>` memiliki tipe pada atribut, seperti ini:
1010

1111
```html
1212
<table id="grid">
1313
<thead>
1414
<tr>
1515
*!*
16-
<th data-type="number">Age</th>
17-
<th data-type="string">Name</th>
16+
<th data-type="number">Umur</th>
17+
<th data-type="string">Nama</th>
1818
*/!*
1919
</tr>
2020
</thead>
@@ -32,12 +32,12 @@ Each `<th>` has the type in the attribute, like this:
3232
</table>
3333
```
3434

35-
In the example above the first column has numbers, and the second one -- strings. The sorting function should handle sort according to the type.
35+
Pada contoh diatas kolom memiliki nomor, dan kolom kedua - string (teks). Fungsi pengurutan harus menanggani pengurutan berdasarkan tipenya.
3636

37-
Only `"string"` and `"number"` types should be supported.
37+
Hanya tipe `"string"` dan `"number"` yang bisa di urutkan.
3838

39-
The working example:
39+
Contoh yang sudah jadi:
4040

4141
[iframe border=1 src="solution" height=190]
4242

43-
P.S. The table can be big, with any number of rows and columns.
43+
Tambahan: Tabel bisa besar, dengan banyak baris dan kolom.

0 commit comments

Comments
 (0)