-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.js
151 lines (123 loc) · 4.58 KB
/
tools.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
let toolsCont = document.querySelector(".tools-cont");
let optionsCont = document.querySelector(".hamburger-menu");
let optionsFlag = true;
let pencilToolCont = document.querySelector(".pencil-tool-cont");
let eraserToolCont = document.querySelector(".eraser-tool-cont");
let pencil = document.querySelector(".pencil");
let eraser = document.querySelector(".eraser");
let pencilFlag = false;
let eraserFlag = false;
let stickyicon = document.querySelector(".sticky-note");
let upload = document.querySelector(".upload");
// true -> to show the tools, false-> hide tools
optionsCont.addEventListener("click",(e)=>{
optionsFlag = !optionsFlag;
optionsFlag == true ? openTools() : closeTools();
});
function openTools(){
let iconElem = optionsCont.children[0];
iconElem.classList.remove("fa-times");
iconElem.classList.add("fa-bars");
toolsCont.style.display = "flex";
}
function closeTools(){
let iconElem = optionsCont.children[0];
iconElem.classList.remove("fa-bars");
iconElem.classList.add("fa-times");
toolsCont.style.display = "none";
pencilToolCont.style.display = "none";
eraserToolCont.style.display = "none";
}
pencil.addEventListener("click", (e) => {
//true -> show pencil tool, false -> hide pencil tool
pencilFlag = !pencilFlag;
if (pencilFlag) pencilToolCont.style.display = "block"
else pencilToolCont.style.display = "none";
});
eraser.addEventListener("click", (e) => {
//true -> show eraser tool, false -> hide eraser tool
eraserFlag = !eraserFlag;
if (eraserFlag) eraserToolCont.style.display = "flex"
else eraserToolCont.style.display = "none";
});
upload.addEventListener("click", (e) => {
// Open file explorer
let input = document.createElement("input");
input.setAttribute("type","file");
input.click();
input.addEventListener("change", (e) => {
let file = input.files[0];
let url = URL.createObjectURL(file);
let stickyTemplateHTML = `
<div class="header-cont">
<div class="minimise"></div>
<div class="remove"></div>
</div>
<div class="note-cont">
<img src="${url}"/>
</div>
`;
createSticky(stickyTemplateHTML);
})
})
stickyicon.addEventListener("click", (e) => {
let stickyTemplateHTML = `
<div class="header-cont">
<div class="minimise"></div>
<div class="remove"></div>
</div>
<div class="note-cont">
<textarea spellcheck="false"></textarea>
</div>
`;
createSticky(stickyTemplateHTML);
})
function createSticky(stickyTemplateHTML){
let stickyCont = document.createElement("div");
stickyCont.setAttribute("class","sticky-cont");
stickyCont.innerHTML = stickyTemplateHTML;
document.body.appendChild(stickyCont);
let minimise = stickyCont.querySelector(".minimise");
let remove = stickyCont.querySelector(".remove");
noteAction(minimise,remove,stickyCont);
stickyCont.onmousedown = function(event) {
dragnDrop(stickyCont,event);
};
stickyCont.ondragstart = function() {
return false;
};
}
function noteAction(minimise, remove, stickyCont){
remove.addEventListener("click", (e) => {
stickyCont.remove();
})
minimise.addEventListener("click", (e) => {
let noteCont = stickyCont.querySelector(".note-cont");
let display = getComputedStyle(noteCont).getPropertyValue("display");
if (display === "none") noteCont.style.display = "block"
else noteCont.style.display = "none";
})
}
function dragnDrop(element,event){
let shiftX = event.clientX - element.getBoundingClientRect().left;
let shiftY = event.clientY - element.getBoundingClientRect().top;
element.style.position = 'absolute';
element.style.zIndex = 1000;
moveAt(event.pageX, event.pageY);
// moves the ball at (pageX, pageY) coordinates
// taking initial shifts into account
function moveAt(pageX, pageY) {
element.style.left = pageX - shiftX + 'px';
element.style.top = pageY - shiftY + 'px';
}
function onMouseMove(event) {
moveAt(event.pageX, event.pageY);
}
// move the ball on mousemove
document.addEventListener('mousemove', onMouseMove);
// drop the ball, remove unneeded handlers
element.onmouseup = function() {
document.removeEventListener('mousemove', onMouseMove);
element.onmouseup = null;
};
}