-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjscn.js
201 lines (161 loc) · 4.77 KB
/
jscn.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
var debug = true;
var cookieExpirationAdd = "; expires=Fri, 31 Dec 9999 23:59:59 GMT";
var cookieExpirationDel = "; expires=Thu, 01 Jan 1970 00:00:01 GMT";
var allNotes = [];
var noteSelected = '';
$(document).ready(function(){
console.log("---- start");
restoreAll();
printAll();
$('#closeNewNoteOverlay').click(function(){
closeOverlay();
noteSelected = '';
});
$('#addNewNote').click(function(){
closeOverlay();
var value = document.getElementById("newNoteTextArea").value;
if(noteSelected != '') {
allNotes[noteSelected] = value;
} else {
allNotes.push(value);
}
noteSelected = '';
printAll();
saveAll();
});
$('#deleteNote').click(function(){
closeOverlay();
if(noteSelected != '') {
allNotes.splice(noteSelected, 1);
}
noteSelected = '';
printAll();
saveAll();
});
$('#openNewNoteOverlay').click(function(){
openOverlay(false);
document.getElementById("newNoteTextArea").value = '';
});
$('#openSettingsOverlay').click(function(){
$( '#settingsOverlay' ).height( '100%' );
$( '.openOverlay' ).hide();
});
$('#closeSettings').click(function(){
$( '#settingsOverlay' ).height( '0%' );
$( '.openOverlay' ).show();
});
$('#saveAndCloseSettings').click(function(){
console.log('#saveAndCloseSettings');
$( '#settingsOverlay' ).height( '0%' );
$( '.openOverlay' ).show();
});
$('#shAllCookiesDebugButton').click( function(){ showAllCookies(); });
$('#delAllCookiesDebugButton').click( function(){ deleteAllCookies(); });
$('#clearDebugMessagesButton').click( function(){ $('#debugMessages').html(''); });
});
// ---------- Page Controls ----------
function openOverlay(showTrashButton) {
if(showTrashButton) {
$( '.controlButton' ).width( '33%' );
$( '.addNoteButton' ).css( 'text-align', 'center' );
$( '.deleteNoteButton' ).show();
} else {
$( '.controlButton' ).width( '50%' );
$( '.addNoteButton' ).css( 'text-align', 'right' );
$( '.deleteNoteButton' ).hide();
}
$( '#newNoteOverlay' ).height( '100%' );
$( '.openOverlay' ).hide();
}
function closeOverlay() {
$( '#newNoteOverlay' ).height( '0%' );
$( '.openOverlay' ).show();
}
function selectNote(noteId) {
openOverlay(true);
document.getElementById('newNoteTextArea').value = document.getElementById(noteId).innerHTML;
noteSelected = noteId.substring('noteId'.length, noteId.length);
}
// <div class="note" id="noteId1" onclick="selectNote('noteId1');"></div>
function printAll() {
var nWrap = document.getElementById('notesWrapperDiv');
var allNotesHTML = '';
for(var i = 0; i < allNotes.length; i++) {
allNotesHTML += '<div class="note" id="noteId' + i + '" onclick="selectNote(\'noteId' + i + '\');">';
allNotesHTML += allNotes[i] + '</div>';
}
nWrap.innerHTML = allNotesHTML;
nWrap.style.display = 'block';
if(allNotes.length == 0) {
nWrap.style.display = 'none';
}
allNotesHTML = '';
}
// save all notes in allNotes array in a cookie
function saveAll() {
if(allNotes.length == 0) {
deleteAllCookies();
return;
}
var allNotesTemp = allNotes.slice();
for(var i = 0; i < allNotesTemp.length; i++) {
allNotesTemp[i] = encodeURIComponent(allNotesTemp[i]);
}
var saveString = btoa(allNotesTemp.toString());
allNotesTemp = null;
setCookie('jscn', saveString);
}
// restore all notes from cookie to allNotes array
function restoreAll() {
var saveString = getCookieValue('jscn');
if(saveString.length == 0) {
return;
}
allNotes = atob(saveString).split(',');
for(var i = 0; i < allNotes.length; i++) {
allNotes[i] = decodeURIComponent(allNotes[i]);
}
}
// ------------- Cookie functions -------------
function setCookie(cName, cValue) {
document.cookie = cName + "=" + cValue + cookieExpirationAdd;
}
function deleteCookie(cName) {
document.cookie = cName + "=" + cookieExpirationDel;
}
function getCookieValue(cName) {
var cArray = document.cookie.split(";");
var cNameTmp = '';
for(var i = 0; i < cArray.length; i++) {
cNameTmp = cArray[i].trim().split("=")[0];
if(cNameTmp === cName) {
return cArray[i].trim().split("=")[1];
}
}
return '';
}
function deleteAllCookies() {
var result = confirm('Delete all cookies from this web page?');
if(!result)
return;
if(document.cookie != '') {
var cArray = document.cookie.split(";");
for(var i = 0; i < cArray.length; i++) {
deleteCookie(cArray[i].trim().split("=")[0])
}
}
}
function showAllCookies() {
if(document.cookie != '') {
out(document.cookie + ' [' + document.cookie.length + ']');
} else {
out('nothing');
}
}
// ---------- DEBUG ----------
function out(msg) {
if(debug) {
//console.log(msg)
$('#debugMessages').append(msg + '<br>')
}
}