/
Tool.js
47 lines (42 loc) · 1.2 KB
/
Tool.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
47
function Tool(o) {
this.id = o.id || uniqueId();
this.name = o.name;
if(typeof o.image == 'string') {
this.image = new Image();
this.image.src = o.image;
} else {
this.image = o.image;
}
this.preactivate = o.preactivate;
this.activate = o.activate;
this.deactivate = o.deactivate;
if(this.image) {
this.tag = this.image;
} else {
this.tag = document.createElement('div');
this.tag.textContent = this.name.substr(0,3);
}
this.tag.classList.add('tool');
this.tag.addEventListener('click', () => {
Tool.selectedTool && typeof Tool.selectedTool.deactivate == 'function' && Tool.selectedTool.deactivate();
this.tag.style.outline = '3px solid green';
this.tag.style.outlineOffset = '-4px';
Tool.tools.forEach((t) => {
if(t.id != this.id) {
t.tag.style.outline = '';
}
})
typeof this.preactivate == 'function' && this.preactivate();
Tool.selectedTool = this;
}, false);
Tool.tools.push(this);
}
Tool.container = document.getElementById('tools');
Tool.tools = [];
Tool.selectedTool;
Tool.render = function() {
this.tools.forEach((t) => t.render());
}
Tool.prototype.render = function() {
Tool.container.appendChild(this.tag);
}