|
var lib = { |
|
// (A) PROPERTIES |
|
// (A1) INDEXED DB |
|
iDB : null, iTX : null, iName : "MyLib", |
|
|
|
// (A2) HTML ELEMENTS |
|
hForm : null, hID : null, hCode : null, |
|
hTitle : null, hAuthor : null, hLoc : null, hList : null, |
|
|
|
// (B) INIT |
|
init : () => { |
|
// (B1) REQUIREMENTS CHECK - INDEXED DB |
|
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; |
|
if (!window.indexedDB) { |
|
alert("Your browser does not support indexed database."); |
|
return; |
|
} |
|
|
|
// (B2) REQUIREMENTS CHECK - STORAGE CACHE |
|
if (!"caches" in window) { |
|
alert("Your browser does not support cache storage."); |
|
return; |
|
} |
|
|
|
// (B3) OPEN IDB |
|
let req = window.indexedDB.open(lib.iName, 1); |
|
|
|
// (B4) IDB OPEN ERROR |
|
req.onerror = evt => { |
|
alert("Indexed DB init error - " + evt.message); |
|
console.error(evt); |
|
}; |
|
|
|
// (B5) IDB UPGRADE NEEDED |
|
req.onupgradeneeded = evt => { |
|
lib.iDB = evt.target.result; |
|
|
|
// (B5-1) IDB UPGRADE ERROR |
|
lib.iDB.onerror = evt => { |
|
alert("Indexed DB upgrade error - " + evt.message); |
|
console.error(evt); |
|
}; |
|
|
|
// (B5-2) IDB VERSION 1 |
|
if (evt.oldVersion < 1) { |
|
let store = lib.iDB.createObjectStore(lib.iName, { |
|
keyPath: "id", |
|
autoIncrement: true |
|
}); |
|
store.createIndex("code", "code"); |
|
store.createIndex("title", "title"); |
|
store.createIndex("author", "author"); |
|
} |
|
}; |
|
|
|
// (B6) IDB OPEN OK |
|
req.onsuccess = evt => { |
|
// (B6-1) IDB OBJECTS |
|
lib.iDB = evt.target.result; |
|
lib.iTX = () => { |
|
return lib.iDB |
|
.transaction(lib.iName, "readwrite") |
|
.objectStore(lib.iName); |
|
}; |
|
|
|
// (B6-2) GET HTML ELEMENTS |
|
lib.hForm = document.getElementById("libFormWrap"); |
|
lib.hID = document.getElementById("formID"); |
|
lib.hCode = document.getElementById("formCode"); |
|
lib.hTitle = document.getElementById("formTitle"); |
|
lib.hAuthor = document.getElementById("formAuthor"); |
|
lib.hLoc = document.getElementById("formLoc"); |
|
lib.hList = document.getElementById("libList"); |
|
|
|
// (B6-3) DRAW BOOKS LIST |
|
lib.draw(); |
|
}; |
|
}, |
|
|
|
// (C) TOGGLE ADD/EDIT MEDIA FORM |
|
toggle : id => { |
|
// (C1) HIDE FORM |
|
if (id == false) { |
|
lib.hID.value = ""; |
|
lib.hCode.value = ""; |
|
lib.hTitle.value = ""; |
|
lib.hAuthor.value = ""; |
|
lib.hLoc.value = ""; |
|
lib.hForm.classList.remove("show"); |
|
} |
|
|
|
// (C2) SHOW FORM |
|
else { |
|
// (C2-1) "EDIT" MODE |
|
if (Number.isInteger(id)) { |
|
let req = lib.iTX().get(id); |
|
req.onsuccess = evt => { |
|
lib.hID.value = id; |
|
lib.hCode.value = req.result.code; |
|
lib.hTitle.value = req.result.title; |
|
lib.hAuthor.value = req.result.author; |
|
lib.hLoc.value = req.result.location; |
|
}; |
|
} |
|
|
|
// (C2-2) SHOW FORM |
|
lib.hForm.classList.add("show"); |
|
} |
|
}, |
|
|
|
// (D) ADD A NEW BOOK |
|
add : () => { |
|
// (D1) DATA TO SAVE |
|
let data = { |
|
id : lib.hID.value, |
|
code : lib.hCode.value, |
|
title : lib.hTitle.value, |
|
author : lib.hAuthor.value, |
|
location : lib.hLoc.value |
|
}; |
|
|
|
// (D2) SAVE OR UPDATE |
|
if (data.id == "") { |
|
delete data.id; |
|
lib.iTX().add(data); |
|
} else { |
|
data.id = parseInt(data.id); |
|
lib.iTX().put(data); |
|
} |
|
|
|
// (D3) DONE |
|
lib.toggle(false); |
|
lib.draw(); |
|
return false; |
|
}, |
|
|
|
// (E) DELETE ENTRY |
|
del : id => { if (confirm(`Delete ${id}?`)) { |
|
// (E1) DELETE BOOK |
|
lib.iTX().delete(id); |
|
|
|
// (E2) REDRAW LIST |
|
lib.draw(); |
|
}}, |
|
|
|
// (F) DRAW BOOKS LIST |
|
draw : () => { |
|
lib.hList.innerHTML = ""; |
|
lib.iTX().getAll().onsuccess = evt => { for (let book of evt.target.result) { |
|
let row = document.createElement("div"); |
|
row.className = "row"; |
|
row.innerHTML = `<div class="binfo"> |
|
<div> |
|
<i class="mi">numbers</i> ${book.id} |
|
<i class="mi">qr_code_2</i> ${book.code} |
|
</div> |
|
<div> |
|
<i class="mi">menu_book</i> ${book.title} |
|
<i class="mi">person_outline</i> ${book.author} |
|
</div> |
|
<i class="mi">room</i> ${book.location} |
|
</div> |
|
<input type="button" class="mi" value="delete" onclick="lib.del(${book.id})"> |
|
<input type="button" class="mi" value="edit" onclick="lib.toggle(${book.id})">`; |
|
lib.hList.appendChild(row); |
|
}}; |
|
} |
|
}; |
|
window.addEventListener("DOMContentLoaded", lib.init); |