-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-note.js
161 lines (129 loc) · 5.32 KB
/
update-note.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
function update_box() {
const update_box = `
<!-- new note box expanded -->
<form id="update_note">
<input type="hidden" name="id">
<div class= "card-header py-1">
<!-- The following two <div> MUST BE EMPTY to display placeholder -->
<!-- new note title input -->
<div id="update_title" contenteditable="true"
max="100" data-placeholder="Title"></div>
<input type="hidden" name="title">
<i class="icon-pushpin" style="margin-top: 5px" value="false"></i>
<input type="hidden" name="pinned">
</div>
<!-- new note content -->
<div id="update_content" class="card-body" contenteditable="true"
max="1000" data-placeholder="Take a note..." autofocus></div>
<input type="hidden" name="content">
<!-- new note box footer -->
<div class="card-footer">
<div class="icons">
<i class="dot" value="White"></i>
<i class="dot" value="peachpuff"></i>
<i class="dot" value="thistle"></i>
<i class="dot" value="khaki"></i>
<i class="dot" value="aquamarine"></i>
<i class="dot" value="lightgreen"></i>
<i class="material-icons-outlined">check_box</i>
<i class="material-icons-outlined">image</i>
<i class="material-icons-outlined delete">delete</i>
</div>
<input type="hidden" name="color">
<button>Save</button>
</div>
</form>`;
$("#update_note_box").html(update_box);
$("#update_content").focus();
// color pickers at footer
$("#update_note .dot").each(function (index) {
$(this).css("background-color", $(this).attr("value"));
$(this).click(function () {
$(this).addClass("selected_dot")
.siblings().removeClass("selected_dot");
$("#update_note_box").css("background-color", $(this).attr("value"));
})
if (index === 0) {
$(this).addClass("selected_dot");
}
});
// set max character limit on text boxes
$("#update_note_box").on("keypress paste", '[contenteditable]', function (e) {
if (this.innerHTML.length >= this.getAttribute("max")) {
e.preventDefault();
return false;
}
});
// move focus to note content on enter key press
$("#update_title").keydown(function (e) {
if (e.keyCode == 13) {
e.preventDefault();
$("#update_content").focus();
}
});
}
update_box();
// show update box modal on click of note
$("#existing_notes").on('click', '.note_title, .note_content', function () {
$modal = $("#update_modal");
$modal.modal("show");
// select the note
$note = $(this).parent();
$id = $note.attr("id");
$title = $note.children(".note_title").text();
$content = $note.children(".note_content").text();
$color = $note.attr("data-color");
// fill value to update modal
$("#update_title").text($title);
$("#update_content").text($content);
$("#update_note input[name='id']").val($id);
// change color accordingly
$modal.find(".dot").each(function () {
if ($(this).attr("value") == $color) {
$(this).addClass("selected_dot")
.siblings().removeClass("selected_dot");
$("#update_note_box").css("background-color", $(this).attr("value"));
}
});
// update on save button click
$modal.on('submit', "#update_note", function (event) {
event.preventDefault();
// bind form contents to hidden elements
// $("input[name='id']", this).val($id);
$("input[name='title']", this).val($("#update_title").html());
$("input[name='content']", this).val($("#update_content").html());
$("input[name='color']", this).val($(".selected_dot", this).attr("value"));
$("input[name='pinned']", this).val("false");
// select form
$form = $("#update_note");
// cache inputs
$inputs = $form.find("input");
// serialize the data
var serializedData = $form.serialize();
// console.log(serializedData);
// disable form elements for the duration of ajax request
// disabled elements do not be serialized so serialize first and disable
$inputs.prop("disabled", true);
// fire Ajax request to php/update.php
var request = $.ajax({
url: "php/update.php",
type: "POST",
data: serializedData
});
// callback handler on success
request.done(function (response, textStatus, jqXHR) {
response = JSON.parse(response);
$modal.modal("hide");
// update note on DOM
$note.children(".note_title").html(response['title']);
$note.children(".note_content").html(response['content']);
$note.attr("data-color",response['color']);
$note.attr("data-lastModified",response['last_modified']);
$note.attr("style","background-color:"+response['color']);
$note.hide().prependTo("#existing_notes").fadeIn(1000);
});
request.always(function() {
$inputs.prop("disabled",false);
});
});
});