@@ -4,21 +4,34 @@ document.addEventListener("DOMContentLoaded", function () {
4
4
document . getElementById ( "judge0-year" ) . innerText = new Date ( ) . getFullYear ( ) ;
5
5
} ) ;
6
6
7
+ /**
8
+ * Dropdown component consists of the following elements:
9
+ * 1. A wrapper div with class "judge0-dropdown".
10
+ * 2. A button with class "judge0-dropdown-btn".
11
+ * 3. A span with class "judge0-dropdown-value".
12
+ * 4. A div with class "judge0-dropdown-menu" that contains the dropdown options.
13
+ * 5. A list of options with class "judge0-dropdown-option".
14
+ *
15
+ * If the dropdown is not select dropdown then classes (3) and (5) are not required.
16
+ */
7
17
document . body . addEventListener ( "click" , function ( event ) {
8
18
const dropdown = event . target . closest ( ".judge0-dropdown" ) ;
9
19
const dropdownBtn = event . target . closest ( ".judge0-dropdown-btn" ) ;
10
20
11
21
if ( event . target && dropdownBtn && dropdownBtn . contains ( event . target ) ) {
12
22
dropdown . querySelector ( ".judge0-dropdown-menu" ) . classList . toggle ( "hidden" ) ;
13
23
} else if ( event . target && event . target . classList . contains ( "judge0-dropdown-option" ) ) {
14
- const span = dropdown . querySelector ( "span" ) ;
24
+ const span = dropdown . querySelector ( "span.judge0-dropdown-value " ) ;
15
25
span . innerText = event . target . innerText ;
16
26
dropdown . querySelector ( ".judge0-dropdown-menu" ) . classList . toggle ( "hidden" ) ;
17
27
}
18
28
19
- // For each dropdown menu check if it needs to be hidden.
20
- // If the click is outside of the dropdown menu and if that dropdown menu is not
21
- // the dropdown menu of the just clicked dropdown button, hide it.
29
+ /**
30
+ * For each dropdown menu check if it needs to be hidden.
31
+ * Hide the dropdown menu if all applies:
32
+ * 1. The click is outside of the dropdown menu.
33
+ * 2. The dropdown menu is not the dropdown menu of the just clicked dropdown button.
34
+ */
22
35
document . querySelectorAll ( ".judge0-dropdown-menu" ) . forEach ( function ( dropdownMenu ) {
23
36
if ( ! dropdownMenu . contains ( event . target ) && dropdown !== dropdownMenu . closest ( ".judge0-dropdown" ) ) {
24
37
dropdownMenu . classList . add ( "hidden" ) ;
0 commit comments