-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmynotes.js
183 lines (145 loc) · 4.84 KB
/
mynotes.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
$(function(){
//define variables
var activeNote = 0;
var editMode=false;
//load notes on page load: ajax call to loadnotes.php
$.ajax({
url: "loadnotes.php",
success: function(data){
$("#notes").html(data);
clickonNote();
clickonDelete();
},
error: function(){
$('#alertContent').text("there is an error with ajax call.");
$('#alert').fadeIn();
}
});
//add a new notes: ajax call to createnote.php
$("#addNote").click(function(){
$.ajax({
url: "createnote.php",
success: function(data){
if(data=='error'){
$("#alertContent").text("there was an issue inserting the new note in the database");
alert("#alert").fadein();
}else{
//update activeNote to the id of the new note
activeNote = parseInt(data);
$("textarea").val("");
//show hide elements
showHide(["#notePad", "#allNotes"],["#notes","#addNote", "#edit", "#done"]);
$('#textarea').focus();
}
},
error: function(){
$('#alertContent').text("there is an error with ajax call.");
$('#alert').fadeIn();
}
});
});
//type note: ajax call to updatnote.php
$("textarea").keyup(function(){
// ajax call to update the task of id activenote
$.ajax({
url: "updatenote.php",
type: "POST",
//we need to send the current note content with its id to the php file
data: {note: $(this).val(), id: activeNote},
success: function(data){
$("#alertContent").html("tis is a hello");
if(data == 'error'){
$('#alertContent').text("thre was an issue updating the note in the database.");
$('#alert').fadeIn();
}
},
error: function(){
$('#alertContent').text("there was an error with ajax call.");
$('#alert').fadeIn();
}
});
});
//click on all notes button
$("#allNotes").click(function(){
$.ajax({
url: "loadnotes.php",
success: function(data){
$("#notes").html(data);
showHide(["#addNote","#edit","#notes"],["#allNotes","#notePad"]);
clickonNote();
clickonDelete();
},
error: function(){
$('#alertContent').text("there was an error with ajax call.");
$('#alert').fadeIn();
}
});
});
//click on done after editing: load notes again
$("#done").click(function(){
//switch to non edit mode
editMode = false;
//expand notes
$(".noteheader").removeClass("col-xs-7 col-sm-9");
// show hide elements
showHide(["#edit"],[this,".delete"]);
});
// click on edit: go to edit mode (show delete buttons, ...)
$('#edit').click(function(){
//switch to edit mode
editMode = true;
//reduce the width of notes
$(".noteheader").addClass("col-xs-7 col-sm-9");
//show hide elements
showHide(["#done",".delete"],[this]);
});
//functions
//click on a note
function clickonNote(){
$(".noteheader").click(function(){
if(!editMode){
//update activeNote variable to id of note
activeNote = $(this).attr("id");
//fill text area
$("textarea").val($(this).find('.text').text());
//show hide elements
showHide(["#notePad", "#allNotes"],["#notes","#addNote", "#edit", "#done"]);
$('#textarea').focus();
}
});
}
// click on delete
function clickonDelete(){
$(".delete").click(function(){
var deleteButton = $(this);
//send ajax call to delete notes
$.ajax({
url: "deletenote.php",
type: "POST",
//we need to send the id of the node to be deleted
data: {id: deleteButton.next().attr("id")},
success: function(data){
if(data == 'error'){
$('#alertContent').text("thre was an issue deleting the note from the database.");
$('#alert').fadeIn();
}else{
//remove containing div
deleteButton.parent().remove();
}
},
error: function(){
$('#alertContent').text("there was an error with ajax call.");
$('#alert').fadeIn();
}
});
});
}
function showHide(array1,array2){
for(i=0; i<array1.length; i++){
$(array1[i]).show();
}
for(i=0; i<array2.length; i++){
$(array2[i]).hide();
}
}
});