mirror of
https://github.com/tim-krehan/shopping-list.git
synced 2024-11-24 06:40:01 +01:00
124 lines
4.3 KiB
JavaScript
124 lines
4.3 KiB
JavaScript
$(document).ready(function () {
|
|
$(".password-input").on("input", checkPasswordStrings);
|
|
$("#passwordSaveButton").click(savePassword);
|
|
$("#export-recipe-button").click(exportRecipe);
|
|
$("#export-list-button").click(exportList);
|
|
$("#changeThemePrepend").change(themeChange)
|
|
$("#themeSaveButton").click(changeTheme);
|
|
|
|
$("#import-button").click(function () {
|
|
$('<input type="file" accept=".json">').on('change', function () {
|
|
var file = this.files[0];
|
|
var reader = new FileReader();
|
|
reader.onload = function () {
|
|
var content = JSON.parse(reader.result);
|
|
if (content.sites != null) {
|
|
$.post("/api/recipes/import",
|
|
{
|
|
content: reader.result
|
|
},
|
|
function (data) {
|
|
if (data == 0) {
|
|
$("#toast-recipe-import-success").toast("show");
|
|
}
|
|
else {
|
|
$("#toast-recipe-import-warning").toast("show");
|
|
downloadObjectAsJson(JSON.parse(data), "failed_recipe_import");
|
|
}
|
|
}
|
|
);
|
|
}
|
|
else if (content.list != null) {
|
|
$.post("/api/list/import",
|
|
{
|
|
content: reader.result
|
|
},
|
|
function (data) {
|
|
if (data == 0) {
|
|
$("#toast-list-import-success").toast("show");
|
|
}
|
|
}
|
|
);
|
|
}
|
|
};
|
|
reader.readAsText(file);
|
|
}).click();
|
|
});
|
|
});
|
|
|
|
function themeChange(){
|
|
$("#themeSaveButton").removeClass("disabled");
|
|
$("#themeSaveButton").attr("disabled", false);
|
|
}
|
|
|
|
function changeTheme() {
|
|
$.post(
|
|
"/api/user/change-theme",
|
|
{
|
|
theme: $('#changeThemePrepend option:selected').val()
|
|
},
|
|
function(){
|
|
window.location.href = window.location.href;
|
|
}
|
|
);
|
|
}
|
|
|
|
function exportList() {
|
|
$.post("/api/list/export", {}, function (data) {
|
|
downloadObjectAsJson(JSON.parse(data), "list");
|
|
});
|
|
}
|
|
|
|
function exportRecipe() {
|
|
$.post("/api/recipes/export", {}, function (data) {
|
|
downloadObjectAsJson(JSON.parse(data), "recipes");
|
|
});
|
|
}
|
|
|
|
function savePassword() {
|
|
$.post("/api/user/change-pw", {
|
|
current: $("#old-password-input").val(),
|
|
new: $("#new-password-input").val()
|
|
}, function (data) {
|
|
if (data == 0) {
|
|
$("#old-password-input").val("");
|
|
$("#new-password-input").val("");
|
|
$("#check-password-input").val("");
|
|
$("#new-password-input").removeClass("is-valid");
|
|
$("#check-password-input").removeClass("is-valid");
|
|
$("#toast-pw-success").toast("show");
|
|
}
|
|
else {
|
|
$("#old-password-input").addClass("is-invalid");
|
|
}
|
|
});
|
|
}
|
|
|
|
function checkPasswordStrings() {
|
|
if ((($("#old-password-input").val()).length > 0) &&
|
|
(($("#new-password-input").val()).length > 0) &&
|
|
(($("#check-password-input").val()).length > 0) &&
|
|
($("#new-password-input").val() == $("#check-password-input").val())) {
|
|
|
|
$("#new-password-input").addClass("is-valid");
|
|
$("#check-password-input").addClass("is-valid");
|
|
$("#passwordSaveButton").prop("disabled", false);
|
|
$("#passwordSaveButton").removeClass("disabled");
|
|
}
|
|
else {
|
|
$("#passwordSaveButton").prop("disabled", true);
|
|
$("#passwordSaveButton").addClass("disabled");
|
|
$("#new-password-input").removeClass("is-valid");
|
|
$("#check-password-input").removeClass("is-valid");
|
|
}
|
|
}
|
|
|
|
function downloadObjectAsJson(exportObj, exportName) {
|
|
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj));
|
|
var downloadAnchorNode = document.createElement('a');
|
|
downloadAnchorNode.setAttribute("href", dataStr);
|
|
downloadAnchorNode.setAttribute("download", exportName + ".json");
|
|
document.body.appendChild(downloadAnchorNode); // required for firefox
|
|
downloadAnchorNode.click();
|
|
downloadAnchorNode.remove();
|
|
}
|