-
Notifications
You must be signed in to change notification settings - Fork 0
/
displayHelpers.js
259 lines (187 loc) · 7.01 KB
/
displayHelpers.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
(function(root) {
var NoteAdder = document.querySelector('#note-adder');
var NoteTitle = NoteAdder.querySelector('#note-title');
var NoteButtons = NoteAdder.querySelector('.note-button-bar');
var NoteBody = NoteAdder.querySelector('#note-body');
var MenuButton = document.querySelector('#menu-button');
var Menu = document.querySelector('#menu');
var HelpButton = document.querySelector('#help-button');
var Help = document.querySelector('#help')
// Help ---------------------------------------------
function toggleHelp() {
if (Help.style.zIndex === '100') {
Help.style.zIndex = -100;
Help.style.background = 'rgba(229, 229, 229, 0)';
Help.style.opacity = '0';
} else {
Help.style.zIndex = 100;
Help.style.background = 'rgba(229, 229, 229, 0.7)';
Help.style.opacity = '1';
};
};
HelpButton.addEventListener('click', toggleHelp);
Help.addEventListener('click', function(e) {
if (e.target === this) toggleHelp();
})
root.toggleHelp = toggleHelp;
// MenuButton ----------------------------------------
function toggleMenu() {
if (Menu.style.left === '0px') {
Menu.style.left = '-312px';
} else {
Menu.style.left = '0px';
};
};
MenuButton.addEventListener('click', toggleMenu);
// NoteAdder - show and hide Stuff -------------------
function toggleFullNoteAdder(e) {
var shouldHide = true;
NoteAdder.childNodes.forEach(function(child) {
if (child === e.target) {
shouldHide = false;
NoteTitle.style.display = 'block';
NoteButtons.style.display = 'block';
};
});
NoteButtons.childNodes.forEach(function(child) {
if (child === e.target) {
shouldHide = false;
NoteTitle.style.display = 'block';
NoteButtons.style.display = 'block';
};
child.childNodes.forEach(function(child) {
if (child === e.target) {
shouldHide = false;
NoteTitle.style.display = 'block';
NoteButtons.style.display = 'block';
};
})
});
if (shouldHide) {
NoteTitle.style.display = 'none';
NoteButtons.style.display = 'none';
};
};
window.addEventListener('click', toggleFullNoteAdder);
// Note - show and hide stuff --------------------
var Notes = document.querySelectorAll('.note');
var showFullNote = function(e) { //TODO - get the index and the place where the mouse enters to mimic the Google Keep magical little wave on entry
this.querySelectorAll('.note-button-bar li').forEach(function(li) {
li.style.opacity = '1';
});
};
var hideFullNote = function(e) {
this.querySelectorAll('.note-button-bar li').forEach(function(li) {
li.style.opacity = '0';
});
};
Notes.forEach(function(note) {
note.addEventListener('mouseover', showFullNote);
note.addEventListener('mouseout', hideFullNote);
});
root.showFullNote = showFullNote;
// Note Updater - show and hide and update data -------------------
var NoteUpdaterWrapper = document.querySelector('#note-updater-wrapper');
var NoteUpdater = NoteUpdaterWrapper.querySelector('#note-updater');
var Title = NoteUpdater.querySelector('.title');
var Body = NoteUpdater.querySelector('.body');
function showNoteUpdater(e) {
// hide the note we clicked
this.style.opacity = 0;
// get the note's data
var id = getNoteId(this);
var title = getNoteTitle(this);
var body = getNoteBody(this);
// fill in the NoteUpdater
Title.innerHTML = title;
Body.innerHTML = body;
// allow editing
Title.setAttribute('contenteditable', 'true');
Body.setAttribute('contenteditable', 'true');
// add placeholders if needed
if (title === '') {
Title.setAttribute('class', 'title title-placeholder');
};
if (body == '') {
Body.setAttribute('class', 'body body-placeholder');
};
// remove placeholders when there is content
// show NoteUpdater
NoteUpdaterWrapper.style.zIndex = 1001;
NoteUpdaterWrapper.style.background = 'rgba(229, 229, 229, 0.7)';
NoteUpdater.style.opacity = '1';
// track which note we're editing
NoteUpdater.setAttribute('data-editing', id);
// Move the cursor into the box
NoteUpdater.querySelector('.body').focus();
};
root.showNoteUpdater = showNoteUpdater;
function hideNoteUpdater() {
// make content not editable
NoteUpdater.querySelector('.title').setAttribute('contenteditable', 'false');
NoteUpdater.querySelector('.body').setAttribute('contenteditable', 'false');
// show the note again
var id = NoteUpdater.getAttribute('data-editing');
var note = document.querySelector('#note'+id);
note.style.opacity = 1;
// hide NoteUpdater
NoteUpdaterWrapper.style.zIndex = -100;
NoteUpdaterWrapper.style.background = 'rgba(229, 229, 229, 0)';
NoteUpdater.style.opacity = '0';
};
root.hideNoteUpdater = hideNoteUpdater;
NoteUpdater.addEventListener('keyup', function(e) {
if (e.code === 'Enter' && e.ctrlKey) hideNoteUpdater();
});
NoteUpdater.querySelector('.done').addEventListener('click', hideNoteUpdater);
NoteUpdaterWrapper.addEventListener('mousedown', function(e) {
if (e.target === NoteUpdaterWrapper) hideNoteUpdater();
});
var handlePlaceholderFactory = function(field) {
return function(e) {
if (this.innerHTML === '') {
var classString = field + ' ' + field + '-placeholder';
} else {
var classString = field;
};
this.setAttribute('class', classString);
};
};
var handleTitlePlaceholder = handlePlaceholderFactory('title');
var handleBodyPlaceholder = handlePlaceholderFactory('body');
Title.addEventListener('keyup', handleTitlePlaceholder);
Body.addEventListener('keyup', handleBodyPlaceholder);
function getNoteId(note) {
return note.id.slice(4);
};
window.getNoteId = getNoteId;
function getNoteTitle(note) {
return getNoteField.call(null, note, 'title');
};
function getNoteBody(note) {
return getNoteField.call(null, note, 'body');
};
function getNoteField(note, field) {
var classForField = '.' + field;
var value = '';
try {
value = note.querySelector(classForField).innerHTML;
} catch(e) {
// console.warn(e);
};
return value;
};
root.showNoteUpdater = showNoteUpdater;
Notes.forEach(function(note) {
note.addEventListener('click', function(e) {
if(e.target.parentElement === note) showNoteUpdater.bind(this)(e);
});
});
function toggleFullScreen() {
NoteUpdater.className === 'fullscreen' ? NoteUpdater.className = '' : NoteUpdater.className = 'fullscreen';
};
var expandButtons = document.querySelectorAll('.expand-button');
expandButtons.forEach(function(button) {
button.addEventListener('click', toggleFullScreen);
});
}(window));