-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
156 lines (123 loc) · 3.34 KB
/
main.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
152
153
154
155
156
function arrangeNotes(collection) {
var notes = collection || getElementsAsStack();
var count = notes.length;
for(var i=0; i < count; i++) {
cascadeElement(notes.pop(), i);
}
}
function setupListeners() {
setupNoteSpecificListeners();
setupGenericListeners();
setupHoverAnimations();
}
function setupNoteSpecificListeners() {
var approve = document.getElementById('note-approve');
var remove = document.getElementById('note-remove');
approve.addEventListener('click', event => {
var active = getActiveNote();
removeNote(active);
});
remove.addEventListener('click', event => {
var active = getActiveNote();
removeNote(active);
});
}
function setupGenericListeners() {
var add = document.getElementById('note-add');
var show = document.getElementById('note-show');
var addAlternate = document.getElementById('note-add-alternate');
add.addEventListener('click', event => {
addNote();
});
addAlternate.addEventListener('click', event => {
addNote();
});
show.addEventListener('click', event => {
var mainContainer = getMainContainer();
var showGrid = !mainContainer.classList.contains('arrange');
toggleClass(getMainContainer(), 'arrange');
toggleClass(show, 'active');
if(showGrid) {
arrangeNotesAsGrid();
} else {
arrangeNotes();
}
});
}
function setupHoverAnimations() {
var mainContainer = getMainContainer();
var toolbar = document.getElementById('toolbar');
mainContainer.addEventListener('mouseover', event => {
addClass(toolbar, 'active');
});
mainContainer.addEventListener('mouseout', event => {
removeClass(toolbar, 'active');
});
}
function removeNote(activeNote) {
var notes = getElementsAsStack();
hideNote(activeNote);
notes.pop();
if(!notes.length) {
addClass(getMainContainer(), 'empty');
} else {
addClass(getLast(notes), 'note-active');
arrangeNotes(notes);
}
}
function addNote() {
var notes = getElementsAsStack();
if(!notes.length) {
removeClass(getMainContainer(), 'empty');
} else {
removeClass(notes, 'note-active')
}
notes.push(createNote());
arrangeNotes(notes);
}
function createNote() {
var note = document.createElement("div");
addClass(note, "note");
addClass(note, "note-active");
note.appendChild(document.createElement("textarea"));
prepend(getMainContainer(), note);
return note;
}
function ready() {
arrangeNotes();
setupListeners();
}
window.onload = ready;
// Utility Functions Specific to this app
function getElementsAsStack(collection) {
var elements = collection || document.getElementsByClassName('note');
var stack = [];
for(var i = elements.length - 1; i >= 0; i--) {
stack.push(elements[i]);
}
return stack;
}
function getMainContainer() {
return document.getElementsByClassName('main-container')[0];
}
function getActiveNote() {
return document.querySelectorAll('.note-active')[0];
}
function hideNote(note) {
addClass(note, 'hide');
note.style.left = note.style.top = -50;
setTimeout(() => { removeElement(note); }, 500);
}
function arrangeNotesAsGrid() {
var notes = getElementsAsStack();
var row = 1;
var colSize = ((window.outerWidth * .75) / 3) - 10;
var count = notes.length;
for(var i = 0; i < count; i++) {
var column = (i % 3) + 1;
var current = notes.pop();
current.style.top = (row * 305) - 305;
current.style.left = (column * (colSize + 5)) - (colSize + 5);
if(column === 3) row++;
}
}