Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
amethystnetwork-dev
GitHub Repository: amethystnetwork-dev/Incognito
Path: blob/main/static/script/selection.js
917 views
1
/**
2
* Incognito
3
*
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 3 of the License, or
7
* (at your option) any later version.
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16
*/
17
18
/*
19
_____ _ _ _
20
| __ \ | | | | | |
21
| |__) | ___ _ __ | |_ ___ __| | | |__ _ _
22
| ___/ / _ \ | '__| | __| / _ \ / _` | | '_ \ | | | |
23
| | | (_) | | | | |_ | __/ | (_| | | |_) | | |_| |
24
|_| \___/ |_| \__| \___| \__,_| |_.__/ \__, |
25
__/ |
26
|___/
27
_ _ _ _ _ _ _
28
/\ | | | | | | | \ | | | | | |
29
/ \ _ __ ___ ___ | |_ | |__ _ _ ___ | |_ | \| | ___ | |_ __ __ ___ _ __ | | __
30
/ /\ \ | '_ ` _ \ / _ \ | __| | '_ \ | | | | / __| | __| | . ` | / _ \ | __| \ \ /\ / / / _ \ | '__| | |/ /
31
/ ____ \ | | | | | | | __/ | |_ | | | | | |_| | \__ \ | |_ | |\ | | __/ | |_ \ V V / | (_) | | | | <
32
/_/ \_\ |_| |_| |_| \___| \__| |_| |_| \__, | |___/ \__| |_| \_| \___| \__| \_/\_/ \___/ |_| |_|\_\
33
__/ |
34
|___/
35
*/
36
import EventEmitter from "./events.js";
37
38
class Selection extends EventEmitter {
39
constructor(app) {
40
super();
41
this.app = app;
42
this.createElement = app.createElement;
43
this.selectors = {};
44
this.element = app.createElement('div', [], {
45
class: 'selector-wrapper'
46
});
47
};
48
switchSelector(id) {
49
if (!(id in this.selectors)) return false;
50
this.unselectAll();
51
this.emit('select', id);
52
this.selectors[id].setAttribute('data-selected', '');
53
return this.selectors[id];
54
};
55
createSelector(id, element) {
56
this.selectors[id] = element;
57
58
element.addEventListener('click', () =>
59
this.switchSelector(id)
60
);
61
62
this.element.append(element);
63
return element;
64
};
65
unselectAll() {
66
for (const key in this.selectors) {
67
if (this.selectors[key].hasAttribute('data-selected')) {
68
this.selectors[key].removeAttribute('data-selected')
69
};
70
};
71
return true;
72
};
73
};
74
75
export { Selection };
76