Browse Source

save

master
Gregor Wüst 4 years ago
parent
commit
789469e89a
1 changed files with 161 additions and 90 deletions
  1. 161
    90
      scripts/notes_app.js

+ 161
- 90
scripts/notes_app.js View File

$(document).ready(function(){ $(document).ready(function(){


$("#addNoteDiv").on('click', function() { $("#addNoteDiv").on('click', function() {

// Listener für den Plus Button zum hinzufügen von Note Forms.

console.log("Add")

createForm(); createForm();
}) })


$('#allNotesDiv').on('click', 'button', async function() { $('#allNotesDiv').on('click', 'button', async function() {
//
// Listener für den Save Button zum speichern von Note Forms.

console.log("Save") console.log("Save")
console.log(this)
//


let buttonId = (this.id).split("_")[1]; let buttonId = (this.id).split("_")[1];
let type = (this.id).split("_")[0]; let type = (this.id).split("_")[0];
if (type == 'saveNoteButton'){
await saveNoteObject(buttonId);
} else if (type == 'editNoteButton'){
await saveNoteObject(buttonId, true);
try {
if (type == 'saveNoteButton'){
// Speichern einer neuen Note.
await saveNoteObject(buttonId);
} else if (type == 'editNoteButton'){
// Editieren einer gespeicherten Note.
await saveNoteObject(buttonId, true);
}
} catch(err){
alert(err);
} }

//
console.log(buttonId)
//
}) })


$('#noteStorage').on('click', 'button', async function() { $('#noteStorage').on('click', 'button', async function() {
//
console.log("Delete")
console.log(this)
//
// Listener für den Delete Button zum Löschen einer Note aus dem Note Browser und Local Storage.


console.log("Delete")
let buttonId = (this.id).split("_")[1]; let buttonId = (this.id).split("_")[1];

//
console.log(buttonId)
//
await removeBrowserItem(buttonId);
try {
await removeBrowserItem(buttonId);
} catch(err) {
alert(err);
}
}) })


$("#refreshButton").on('click', async function() { $("#refreshButton").on('click', async function() {
//
// Listener für den Refresh Button zum aktualisieren der gespeicherten Notes.

console.log("Refresh") console.log("Refresh")
//
createLoadingSign(id="", append="noteStorage"); createLoadingSign(id="", append="noteStorage");
enableLoadingSign(id=""); enableLoadingSign(id="");
await sleep(2000); await sleep(2000);
disableLoadingSign(id=""); disableLoadingSign(id="");
await refreshBrowser();
try {
await refreshBrowser();
} catch(err){
alert(err);
}
}) })
}) })


let id=1; let id=1;


function createForm(){ function createForm(){

// Erstellt eine NoteForm und initialisiert die OnClick-Listener auf den Buttons der Form

id++; id++;
//
console.log("createForm() "+id) console.log("createForm() "+id)
//
let wrapper = document.createElement("div"); let wrapper = document.createElement("div");
wrapper.setAttribute('id', "note"+id); wrapper.setAttribute('id', "note"+id);
wrapper.style.margin = '2.5px'; wrapper.style.margin = '2.5px';
let inputsWrapper = document.createElement("div"); let inputsWrapper = document.createElement("div");
inputsWrapper.setAttribute('class', "noteDiv"); inputsWrapper.setAttribute('class', "noteDiv");


let inputDiv = document.createElement("div");
let input = document.createElement("input"); let input = document.createElement("input");
let deleteButton = document.createElement('button');
input.setAttribute('type', "text"); input.setAttribute('type', "text");
input.setAttribute('name',"noteTitle"); input.setAttribute('name',"noteTitle");
input.setAttribute('id', "inputTitle"+id); input.setAttribute('id', "inputTitle"+id);
input.setAttribute('placeholder', "Enter title..."); input.setAttribute('placeholder', "Enter title...");
input.style.marginBottom='5px'; input.style.marginBottom='5px';

deleteButton.style.marginLeft = '2px';
deleteButton.innerHTML = "X";
deleteButton.setAttribute('id', "deleteForm_" + id);
deleteButton.setAttribute('onClick', "deleteForm(this.id)");
deleteButton.setAttribute('class', "right");
inputDiv.appendChild(input);
inputDiv.appendChild(deleteButton);
let textAreaDiv = document.createElement("div"); let textAreaDiv = document.createElement("div");
let textarea = document.createElement("textarea"); let textarea = document.createElement("textarea");
textarea.setAttribute('name',"noteText"); textarea.setAttribute('name',"noteText");
saveNoteButton.setAttribute('id',"saveNoteButton_"+id); saveNoteButton.setAttribute('id',"saveNoteButton_"+id);
saveNoteButton.innerHTML = 'Save'; saveNoteButton.innerHTML = 'Save';


inputsWrapper.appendChild(input);
inputsWrapper.appendChild(inputDiv);
inputsWrapper.appendChild(textAreaDiv); inputsWrapper.appendChild(textAreaDiv);
inputsWrapper.appendChild(saveNoteButton); inputsWrapper.appendChild(saveNoteButton);
wrapper.appendChild(inputsWrapper); wrapper.appendChild(inputsWrapper);
document.getElementById('allNotesDiv').appendChild(wrapper); document.getElementById('allNotesDiv').appendChild(wrapper);
} }


function deleteForm(idName){

// Aktiviert durch OnClickListener auf dem X-Button innerhalb der Form zum Schließen der Form

console.log("deleteForm() " + id);

id = idName.split("_")[1];
document.getElementById("note"+id).remove();
}

function createFormWithSavedContent(item, id){ function createFormWithSavedContent(item, id){
//
console.log("createFormWithSavedContent() "+id)
console.log(item.title);
console.log(item.text);
//
// Holt eine gespeicherte Form aus dem Speicher und erstellt erneut eine NoteForm und initialisiert die OnClick-Listener auf den Buttons der Form

console.log("createFormWithSavedContent() "+ id)


let wrapper = document.createElement("div") let wrapper = document.createElement("div")
wrapper.setAttribute('id', "note"+id) wrapper.setAttribute('id', "note"+id)
let inputsWrapper = document.createElement("div") let inputsWrapper = document.createElement("div")
inputsWrapper.setAttribute('class', "noteDiv"); inputsWrapper.setAttribute('class', "noteDiv");


let inputDiv = document.createElement("div");
let input = document.createElement("input"); let input = document.createElement("input");
let deleteButton = document.createElement('button');
input.setAttribute('type', "text"); input.setAttribute('type', "text");
input.setAttribute('name',"noteTitle"); input.setAttribute('name',"noteTitle");
input.setAttribute('id', "inputTitle"+id); input.setAttribute('id', "inputTitle"+id);
input.setAttribute('value', item.title); input.setAttribute('value', item.title);
input.style.marginBottom='5px'; input.style.marginBottom='5px';

deleteButton.style.marginLeft = '2px';
deleteButton.innerHTML = "X";
deleteButton.setAttribute('id', "deleteForm_" + id);
deleteButton.setAttribute('onClick', "deleteForm(this.id)");
inputDiv.appendChild(input);
inputDiv.appendChild(deleteButton);
let textAreaDiv = document.createElement("div"); let textAreaDiv = document.createElement("div");
let textarea = document.createElement("textarea"); let textarea = document.createElement("textarea");
textarea.setAttribute('name',"noteText"); textarea.setAttribute('name',"noteText");
editNoteButton.setAttribute('id',"editNoteButton_"+id); editNoteButton.setAttribute('id',"editNoteButton_"+id);
editNoteButton.innerHTML = 'Save'; editNoteButton.innerHTML = 'Save';


inputsWrapper.appendChild(input);
inputsWrapper.appendChild(inputDiv);
inputsWrapper.appendChild(textAreaDiv); inputsWrapper.appendChild(textAreaDiv);
inputsWrapper.appendChild(editNoteButton); inputsWrapper.appendChild(editNoteButton);
wrapper.appendChild(inputsWrapper); wrapper.appendChild(inputsWrapper);
let ctr; let ctr;


async function sleep(ms) { async function sleep(ms) {

// Best Practice Variante zum pausieren von Code
// Soll hier Prozesse simulieren die bei synchronem Code zu I/O-Blocking führen würden

console.log("sleep() " + ms);

try { try {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }
} }


async function removeNote(id){ async function removeNote(id){
try {
return new Promise(resolve => {

// Löscht eine Note aus dem Note Browser

console.log("removeNote() " + id);

return new Promise((resolve, reject) => {
try {
localStorage.removeItem('noteObj' + id); localStorage.removeItem('noteObj' + id);
removeBrowserItem(id); removeBrowserItem(id);
resolve(); resolve();
});
}
catch (alert) {
return alert(alert);
}
} catch(err) {
reject(err);
}
});
} }


async function saveNote(id){ async function saveNote(id){
try {
return new Promise(resolve => {

console.log("saveNote() " + id);

return new Promise((resolve, reject) => {
try {
let noteTitle = document.getElementById('inputTitle' + id).value; let noteTitle = document.getElementById('inputTitle' + id).value;
let noteText = document.getElementById('noteText' + id).value; let noteText = document.getElementById('noteText' + id).value;
let noteObj = { let noteObj = {
let noteObj_serialized = JSON.stringify(noteObj); //stringify noteObj so it is displayed properly when set to local Storage let noteObj_serialized = JSON.stringify(noteObj); //stringify noteObj so it is displayed properly when set to local Storage
localStorage.setItem("noteObj" + id, noteObj_serialized); localStorage.setItem("noteObj" + id, noteObj_serialized);
resolve(noteObj); resolve(noteObj);
});
}
catch (alert) {
return alert(alert);
}
} catch(err) {
reject(err);
}
});
} }


async function refreshBrowser(){ async function refreshBrowser(){
try {
return new Promise(resolve => {

console.log("refreshBrowser()");

return new Promise((resolve, reject) => {
try {
let noteStorage = []; let noteStorage = [];
let keys = Object.keys(localStorage); let keys = Object.keys(localStorage);
let i = keys.length; let i = keys.length;
createBrowserItem(JSON.parse(element)); createBrowserItem(JSON.parse(element));
}); });
resolve(); resolve();
});
}
catch (alert) {
return alert(alert);
}
} catch(err) {
reject(err);
}
});
} }


async function saveNoteObject(id, overwrite=false){ async function saveNoteObject(id, overwrite=false){
//
console.log("saveNoteObject() "+id)
console.log(id)
//
console.log("saveNoteObject() " + id + " " + overwrite);
createLoadingSign(id, append="note") createLoadingSign(id, append="note")
enableLoadingSign(id); enableLoadingSign(id);
await sleep(10000) //sleep() simulates the delay that would occur when there is a remote server access or similar, await sleep(10000) //sleep() simulates the delay that would occur when there is a remote server access or similar,
//the leftover code of the async function is executed after the delay //the leftover code of the async function is executed after the delay
disableLoadingSign(id); disableLoadingSign(id);


if (overwrite == true){
await removeNote(id);
try {
if (overwrite == true){
await removeNote(id);
}
let obj = await saveNote(id);
createBrowserItem(obj);
} catch(err){
alert(err)
} }

let obj = await saveNote(id);
createBrowserItem(obj);
document.getElementById("note"+id).remove(); document.getElementById("note"+id).remove();
} }


function enableLoadingSign(id){ function enableLoadingSign(id){
console.log("id:"+id);

console.log("enableLoadingSign() " + id);

let target = 'loadSign'+id; let target = 'loadSign'+id;
console.log("enableLoadingSign() "+id)
document.getElementById(target).style.display = "block"; document.getElementById(target).style.display = "block";
} }


function disableLoadingSign(id){ function disableLoadingSign(id){
console.log("id:"+id);

console.log("disableLoadingSign() " + id);

let target = 'loadSign'+id; let target = 'loadSign'+id;
let sign = document.getElementById(target); let sign = document.getElementById(target);
sign.style.display = "none"; sign.style.display = "none";
} }


function createLoadingSign(id, append){ function createLoadingSign(id, append){
console.log("id:"+id);

console.log("createLoadingSign() " + id);

let target = 'loadSign'+id; let target = 'loadSign'+id;
let appendTo = append+id; let appendTo = append+id;
console.log(appendTo);
console.log("createLoadingSign() "+id)
let loadingSign = document.createElement('div'); let loadingSign = document.createElement('div');
let loadingSignWrapper = document.createElement('div'); let loadingSignWrapper = document.createElement('div');
loadingSign.setAttribute('class', 'loader') loadingSign.setAttribute('class', 'loader')
} }


function createBrowserItem(item){ function createBrowserItem(item){
console.log(item);

console.log("createBrowserItem() " + item);

let wrapperDiv = document.createElement('div'); let wrapperDiv = document.createElement('div');
let browserItem = document.createElement('div'); let browserItem = document.createElement('div');
let deleteButton = document.createElement('button'); let deleteButton = document.createElement('button');
} }


async function removeBrowserItem(id){ async function removeBrowserItem(id){
try {
return new Promise(resolve => {
console.log("removeBrowserItem() " + id);

console.log("removeBrowserItem() " + id);

return new Promise((resolve, reject) => {
try {
localStorage.removeItem('noteObj' + id); localStorage.removeItem('noteObj' + id);
document.getElementById("browserItem_" + id).remove(); document.getElementById("browserItem_" + id).remove();
document.getElementById("deleteItem_" + id).remove(); document.getElementById("deleteItem_" + id).remove();
resolve(); resolve();
});
}
catch (alert) {
return alert(alert);
}
} catch(err) {
reject(err);
}
});
} }


async function getItemFromStorage(idTmp){ async function getItemFromStorage(idTmp){
return new Promise(resolve => {
let item = JSON.parse(localStorage.getItem('noteObj'+idTmp));
console.log("in promise "+item);
resolve(item);

console.log("getItemFromStorage() " + idTmp);

return new Promise((resolve, reject) => {
try {
let item = JSON.parse(localStorage.getItem('noteObj'+idTmp));
resolve(item);
} catch (err) {
reject(err);
}
}); });
} }




async function getSavedNote(id){ async function getSavedNote(id){

console.log("getSavedNote() "+ id); console.log("getSavedNote() "+ id);

createLoadingSign("", "allNotesDiv"); createLoadingSign("", "allNotesDiv");
enableLoadingSign(""); enableLoadingSign("");
await sleep(5000); await sleep(5000);
disableLoadingSign(""); disableLoadingSign("");
console.log("id before split " + id);
let idTmp = id.split("_")[1]; let idTmp = id.split("_")[1];
console.log("idTmp " +idTmp);
let item = await getItemFromStorage(idTmp);
console.log(item);
createFormWithSavedContent(item, idTmp);
try {
let item = await getItemFromStorage(idTmp);
createFormWithSavedContent(item, idTmp);
} catch(err) {
alert(err)
}
} }

Loading…
Cancel
Save