-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.ts
111 lines (90 loc) · 3.59 KB
/
setup.ts
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
import { DEFAULTS, Selector, SelectionMode } from "../src";
import { generateRandomElements } from "./randomElements";
//==============================================================================
const selectectedClass = "selected";
function handleSelected(selectedElements: HTMLElement[], selectionMode: SelectionMode) {
selectedElements.forEach((selectedElement) => {
if (selectionMode == SelectionMode.ADD) {
selectedElement.classList.add(selectectedClass);
} else {
selectedElement.classList.remove(selectectedClass);
}
});
}
function onClick(element: HTMLElement) {
if (element.classList.contains(selectectedClass)) {
element.classList.remove(selectectedClass);
} else {
element.classList.add(selectectedClass);
}
}
//==============================================================================
function setupArea(areaId: string) {
const areaSelector = "#" + areaId;
const selectablesRootSelector = areaSelector + " div.selectables-root";
const selectablesRoot = document.querySelector(selectablesRootSelector) as HTMLElement;
generateRandomElements(selectablesRoot, 6, 400);
const selectableElementsSelector = "div.selectable";
const selector = new Selector(
selectableElementsSelector,
handleSelected,
{
selectablesRootSelector,
selectorClass: [
DEFAULTS.SELECTOR_CLASS,
areaId
],
onClick
}
);
selector.mount();
const clearSelectedButton = document.querySelector(areaSelector + " .clear-selected") as HTMLElement;
function clearSelected() {
document
.querySelectorAll(selectableElementsSelector)
.forEach((selectedElement) => {
selectedElement.classList.remove(selectectedClass);
});
}
clearSelectedButton.addEventListener("click", clearSelected);
const selectionModeButton = document.querySelector(areaSelector + " .change-selection-mode") as HTMLElement;
setSelectionModeButtonText();
function setSelectionModeButtonText() {
const selectionMode = selector.getSelectionMode();
selectionModeButton.textContent = computeSelectionModeButtonText(selectionMode);
selectionModeButton.classList.remove(
computeSelectionModeButtonClass(SelectionMode.ADD),
computeSelectionModeButtonClass(SelectionMode.REMOVE)
);
selectionModeButton.classList.add(
computeSelectionModeButtonClass(selectionMode)
);
}
function computeSelectionModeButtonText(selectionMode: SelectionMode): string {
return (selectionMode == SelectionMode.ADD)
? "Mode: Add"
: "Mode: Remove";
}
function computeSelectionModeButtonClass(selectionMode: SelectionMode): string {
return (selectionMode == SelectionMode.ADD)
? "add"
: "remove";
}
function toggleSelectionMode() {
selector.setSelectionMode(
selector.getSelectionMode() == SelectionMode.ADD
? SelectionMode.REMOVE
: SelectionMode.ADD
);
setSelectionModeButtonText();
}
selectionModeButton.addEventListener("click", toggleSelectionMode);
}
//==============================================================================
function onLoad() {
setupArea("area-1");
setupArea("area-2");
}
//==============================================================================
onLoad();
//==============================================================================