-
Notifications
You must be signed in to change notification settings - Fork 0
/
new-note.js
160 lines (127 loc) · 4.91 KB
/
new-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
//load new note box small
function new_note_small() {
const new_note_small = `
<!-- new note box small-->
<div id="new_note_small" class="card-body row py-2">
<p class="my-0">Take a note...</p>
<i class="material-icons-outlined">check_box</i>
<i class="material-icons-outlined">image</i>
</div>`
$("#new_note").html(new_note_small).css("background-color","White");
};
new_note_small();
// new note expanded box
function new_note_expanded() {
const new_note_expanded = `
<!-- new note box expanded -->
<form id="new_note_expanded">
<div class= "card-header py-1">
<!-- The following two <div> MUST BE EMPTY to display placeholder -->
<!-- new note title input -->
<div id="note_title_input" 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="note_content_input" 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>`
$("#new_note").html(new_note_expanded);
$("#note_content_input").focus();
// color pickers at footer
$("#new_note .dot").each(function(index) {
$(this).css("background-color", $(this).attr("value"));
$(this).click(function() {
$(this).addClass("selected_dot")
.siblings().removeClass("selected_dot");
$("#new_note").css("background-color",$(this).attr("value"));
})
if(index === 0) {
$(this).addClass("selected_dot");
}
});
// set max character limit on text boxes
$("#new_note").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
$("#note_title_input").keydown(function(e){
if(e.keyCode == 13) {
e.preventDefault();
$("#note_content_input").focus();
}
});
// discard note on delete button click
$("#new_note .delete").click(new_note_small);
};
// expand new note card on click
$("#new_note").on('click', '#new_note_small > p',new_note_expanded);
/* now time to post new note */
// variable to hold request
var request;
$("#new_note").on('submit','#new_note_expanded',function(event){
event.preventDefault();
// bind form contents to hidden elements
$("input[name='title']",this).val($("#note_title_input").text());
$("input[name='content']",this).val($("#note_content_input").html());
$("input[name='color']",this).val($(".selected_dot",this).attr("value"));
$("input[name='pinned']",this).val("false");
// abort any pending request
if(request) {
request.abort();
}
// select form
$form = $("#new_note_expanded");
// 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/create.php
request = $.ajax({
url: "php/create.php",
type: "POST",
data: serializedData
});
// callback handler on success
request.done(function(response, textStatus, jqXHR) {
// show small bar again
new_note_small();
// add new note to the DOM
$(response).hide().prependTo("#existing_notes").fadeIn();
// $("#existing_notes").prepend(response).hide().fadeIn();
});
// handle failure
request.fail(function(jqXHR, textStatus, errorThrown) {
console.log("Error - Something wrong");
});
request.always(function() {
// re-enable inpurs
$inputs.prop("disabled",false);
})
});