2019-05-19 18:37:00 +02:00
|
|
|
$(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);
|
2018-10-29 22:14:56 +01:00
|
|
|
}
|
2018-11-09 15:16:36 +01:00
|
|
|
|
2019-05-19 18:37:00 +02:00
|
|
|
function changeTheme() {
|
|
|
|
$.post(
|
|
|
|
"/api/user/change-theme",
|
|
|
|
{
|
|
|
|
theme: $('#changeThemePrepend option:selected').val()
|
|
|
|
},
|
|
|
|
function(){
|
|
|
|
window.location.href = window.location.href;
|
2018-11-09 15:16:36 +01:00
|
|
|
}
|
|
|
|
);
|
2019-05-19 18:37:00 +02:00
|
|
|
}
|
2018-11-09 15:16:36 +01:00
|
|
|
|
2019-05-19 18:37:00 +02:00
|
|
|
function exportList() {
|
|
|
|
$.post("/api/list/export", {}, function (data) {
|
|
|
|
downloadObjectAsJson(JSON.parse(data), "list");
|
2018-10-29 22:23:51 +01:00
|
|
|
});
|
2019-05-19 18:37:00 +02:00
|
|
|
}
|
2018-11-14 16:08:36 +01:00
|
|
|
|
2019-05-19 18:37:00 +02:00
|
|
|
function exportRecipe() {
|
|
|
|
$.post("/api/recipes/export", {}, function (data) {
|
|
|
|
downloadObjectAsJson(JSON.parse(data), "recipes");
|
2018-10-29 22:14:56 +01:00
|
|
|
});
|
2019-05-19 18:37:00 +02:00
|
|
|
}
|
2018-11-14 16:08:36 +01:00
|
|
|
|
2019-05-19 18:37:00 +02:00
|
|
|
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");
|
2018-11-14 16:08:36 +01:00
|
|
|
}
|
2019-05-19 18:37:00 +02:00
|
|
|
else {
|
|
|
|
$("#old-password-input").addClass("is-invalid");
|
2018-11-14 16:08:36 +01:00
|
|
|
}
|
2019-05-19 18:37:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|