refactor(web): move util js functions into a separate file
This commit is contained in:
parent
eeb4b52192
commit
3bfc54e407
@ -13,42 +13,6 @@ if (sorting == null) {
|
||||
sorting = "-datetime";
|
||||
}
|
||||
|
||||
function datetimeToLocalISO(datetime) {
|
||||
var options = {
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
timeZoneName: "longOffset",
|
||||
};
|
||||
|
||||
var formatter = new Intl.DateTimeFormat("sv-SE", options);
|
||||
var date = new Date(datetime);
|
||||
|
||||
return formatter
|
||||
.formatToParts(date)
|
||||
.map(({ type, value }) => {
|
||||
if (type === "timeZoneName") {
|
||||
return value.slice(3);
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
})
|
||||
.join("")
|
||||
.replace(" ", "T")
|
||||
.replace(" ", "");
|
||||
}
|
||||
|
||||
function escapedString(str) {
|
||||
return str
|
||||
.replace("&", "&")
|
||||
.replace("<", "<")
|
||||
.replace(">", ">")
|
||||
.replace("\n", "<br>");
|
||||
}
|
||||
|
||||
function renderBlockQuote(quote) {
|
||||
return `
|
||||
<div class="border rounded-lg p-6 hover:shadow-md transition-shadow">
|
||||
@ -168,11 +132,7 @@ $(document).on("click", "#btn-add-close", function (e) {
|
||||
|
||||
$(document).on("submit", "#quote-create", function (e) {
|
||||
e.preventDefault();
|
||||
formdata = $("#quote-create").serializeArray();
|
||||
data = {};
|
||||
$(formdata).each(function (index, obj) {
|
||||
data[obj.name] = obj.value;
|
||||
});
|
||||
data = formToJSON($("#quote-create"));
|
||||
data.datetime = datetimeToLocalISO(data.datetime);
|
||||
$.ajax({
|
||||
url: "/api/quotes",
|
||||
|
||||
@ -31,12 +31,7 @@ $(document).on("click", "#btn-logout", function (e) {
|
||||
|
||||
$(document).on("submit", "#user-update", function (e) {
|
||||
e.preventDefault();
|
||||
formdata = $("#user-update").serializeArray();
|
||||
data = {};
|
||||
$(formdata).each(function (index, obj) {
|
||||
data[obj.name] = obj.value;
|
||||
});
|
||||
console.log(data);
|
||||
data = formToJSON($("#user-update"));
|
||||
$.ajax({
|
||||
url: "/api/auth",
|
||||
type: "PATCH",
|
||||
|
||||
42
web/static/js/utils.js
Normal file
42
web/static/js/utils.js
Normal file
@ -0,0 +1,42 @@
|
||||
function datetimeToLocalISO(datetime) {
|
||||
var options = {
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
timeZoneName: "longOffset",
|
||||
};
|
||||
var formatter = new Intl.DateTimeFormat("sv-SE", options);
|
||||
var date = new Date(datetime);
|
||||
return formatter
|
||||
.formatToParts(date)
|
||||
.map(({ type, value }) => {
|
||||
if (type === "timeZoneName") {
|
||||
return value.slice(3);
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
})
|
||||
.join("")
|
||||
.replace(" ", "T")
|
||||
.replace(" ", "");
|
||||
}
|
||||
|
||||
function escapedString(str) {
|
||||
return str
|
||||
.replace("&", "&")
|
||||
.replace("<", "<")
|
||||
.replace(">", ">")
|
||||
.replace("\n", "<br>");
|
||||
}
|
||||
|
||||
function formToJSON(form) {
|
||||
formdata = form.serializeArray();
|
||||
data = {};
|
||||
$(formdata).each(function (index, obj) {
|
||||
data[obj.name] = obj.value;
|
||||
});
|
||||
return data;
|
||||
}
|
||||
@ -102,6 +102,7 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script src="/static/js/utils.js"></script>
|
||||
<script src="/static/js/quotes.js"></script>
|
||||
</body>
|
||||
|
||||
|
||||
@ -94,6 +94,7 @@
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<script src="/static/js/utils.js"></script>
|
||||
<script src="/static/js/settings.js"></script>
|
||||
</body>
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user