-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditpost.js
More file actions
171 lines (146 loc) · 6.06 KB
/
editpost.js
File metadata and controls
171 lines (146 loc) · 6.06 KB
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
document.addEventListener('DOMContentLoaded', function() {
// Custom Functions
function HTMLDecode(html) {
var txt = document.createElement("textarea");
txt.innerHTML = html;
return txt.value;
}
function addTag(tag) {
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.name = "tag";
checkbox.checked = true;
checkbox.classList.add("form-check-input");
checkbox.value = tag;
const label = document.createElement("label");
label.classList.add("form-check-label");
label.appendChild(checkbox);
label.appendChild(document.createTextNode(tag));
tagList.appendChild(label);
}
const post_id = id;
const image = document.getElementById('image-input');
const preview = document.getElementById('preview');
const change = document.getElementById('change_status');
const remove_preview = document.getElementById('remove_preview');
const title = document.getElementById('title');
const contents = document.getElementById('contents');
const tagInput = document.getElementById("tagInput");
const tagList = document.getElementById("tagList");
const submit_button = document.getElementById("submit_button");
// Remove current image button
remove_preview.onclick = function() {
document.getElementById('preview').src = '';
remove_preview.style.display = 'none';
image.value = null;
change.value = "changed";
};
title.addEventListener('keyup', function() {
if (title.value.length == 70) {
document.getElementById("warning1").innerHTML = "Maximum characters reached!";
setTimeout(function() {
document.getElementById("warning1").innerHTML = "";
}, 3000);
} else {
document.getElementById("warning1").innerHTML = "";
}
});
contents.addEventListener('keyup', function() {
if (contents.value.length == 640) {
document.getElementById("warning2").innerHTML = "Maximum characters reached!";
setTimeout(function() {
document.getElementById("warning2").innerHTML = "";
}, 3000);
} else {
document.getElementById("warning2").innerHTML = "";
}
});
tagInput.addEventListener("keyup", function (event) {
if (event.key === " " && tagInput.value.trim() !== "") {
let bool1 = true;
let tag = tagInput.value.trim();
for (var i = 0; i < list.length; i++) {
list[i] = HTMLDecode(list[i]);
}
if (tag[0] == '#') {
tag = tag.slice(1);
let tag_proper = tag.charAt(0).toUpperCase() + tag.slice(1).toLowerCase();
let current_tags = document.getElementsByName('tag');
for (let current_tag of current_tags) {
if (current_tag.value == tag_proper) {
bool1 = false;
}
}
if (list.includes(tag_proper) && bool1) {
addTag(tag_proper);
document.getElementById('warning3').innerHTML = ""
} else {
document.getElementById('warning3').innerHTML = "Tag already included or does not exist!"
}
tagInput.value = "";
} else if (tagList.innerHTML == "") {
document.getElementById('warning3').innerHTML = "Include at least one tag with your post!";
}
else {
document.getElementById('warning3').innerHTML = "Tags must start with '#'!";
tagInput.value = "";
}
}
});
// Add preview if valid image is uploaded
image.onchange = function() {
if (!image.value || image.value == "") {
preview.src = "";
image.value = null;
remove_preview.style.display = 'none';
} else if (image.value.endsWith(".png") || image.value.endsWith(".jfif") || image.value.endsWith(".pjp") || image.value.endsWith(".jpg") || image.value.endsWith(".pjpeg") || image.value.endsWith(".jpeg")) {
var reader = new FileReader();
reader.onload = function() {
remove_preview.style.display = 'block';
preview.src = reader.result;
};
reader.readAsDataURL(image.files[0]);
} else {
preview.src = "";
remove_preview.style.display = 'none';
image.value = null;
document.getElementById('warning0').innerHTML = "Supported image formats are PNG/JPG!";
setTimeout(function() {
document.getElementById('warning0').innerHTML = "";
}, 3000);
}
change.value = "changed";
};
// Check tags
submit_button.addEventListener('click', function(event) {
event.preventDefault();
if (tagList.innerHTML == "") {
document.getElementById('warning3').innerHTML = "Include at least one tag with your post!";
return false;
}
var tags = document.getElementsByName("tag");
var check_tag = false;
for (tag of tags) {
if (tag.checked == true) {
check_tag = true;
break;
}
}
if (check_tag == false) {
document.getElementById('warning3').innerHTML = "Select at least one tag with your post!";
setTimeout(function() {
document.getElementById('warning3').innerHTML = "";
}, 3000);
return false;
}
submit_button.disabled = true;
submit_button.classList.add('btn-submit--loading');
// Send post_id to server
const postid_input = document.createElement('input');
postid_input.type = 'hidden';
postid_input.name = 'post_id';
postid_input.value = post_id;
document.querySelector('form').appendChild(postid_input);
document.querySelector('form').submit();
});
});