-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch_countries.js
46 lines (44 loc) · 1.6 KB
/
switch_countries.js
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
/* global document,window */
class SwitchBetweenCountries {
constructor(container, selector) {
this.container = container;
this.selectLeft = container.querySelector(selector.selectLeftSelector);
this.selectRight = container.querySelector(selector.selectRightSelector);
this.addButton = container.querySelector(selector.addButtonSelector);
this.remButton = container.querySelector(selector.remButtonSelector);
}
init() {
this.remButton.addEventListener("click", () => {
while (this.selectRight.selectedIndex !== -1) {
this.removeCountry(this.selectLeft, this.selectRight);
}
});
this.addButton.addEventListener("click", () => {
while (this.selectLeft.selectedIndex !== -1) {
this.addCountry(this.selectLeft, this.selectRight);
}
});
}
addCountry(selectLeft, selectRight) {
var selectedIndexToAdd = selectLeft.selectedIndex;
selectRight.appendChild(selectLeft[selectedIndexToAdd]);
}
removeCountry(selectLeft, selectRight) {
var selectedIndexToRem = selectRight.selectedIndex;
selectLeft.appendChild(selectRight[selectedIndexToRem]);
}
}
window.onload = () => {
var selector = {
container: "[data-type=container]",
selectLeftSelector: "[data-move=select-left]",
selectRightSelector: "[data-move=select-right]",
addButtonSelector: "[data-move=add-button]",
remButtonSelector: "[data-move=rem-button]"
};
var container = document.querySelectorAll(selector.container);
for (var i = 0; i < container.length; i++) {
var obj = new SwitchBetweenCountries(container[i], selector);
obj.init();
}
};