diff options
(checklist) Replaced import-export to URL with IndexedDB
URL has length limit which could easily be hit
1 files changed, 48 insertions, 15 deletions
| -rw-r--r-- | checklist.html | 63 |
diff --git a/checklist.html b/checklist.html index dcb8f95..d93db49 100644 --- a/checklist.html +++ b/checklist.html @@ -16,7 +16,7 @@ <style> #lists { position: relative; - padding-left: 6em; + padding-left: 7em; margin-top: 0.5em; } @@ -83,6 +83,7 @@ input { <input type="button" value="Import" id="import-btn"> <input type="button" value="Export" id="export-btn"> <input type="text" id="exp-imp-text"> + <select id="db-lists"></select> </div> <br> <input type="button" value="Uncheck all" id="uncheck-all"> @@ -96,7 +97,6 @@ input { const stylesheetLocked = document.getElementById('stylesheet-locked'); stylesheetLocked.disabled = true; const expImpText = document.getElementById('exp-imp-text'); - const url = new URL(location.href); /* Item functionality */ @@ -223,8 +223,23 @@ input { document.getElementById('check-all').onclick = function () { changeCheckLists(true); } - + </script> + <script> /* Import-export */ + + const dropdown = document.getElementById('db-lists'); + const dbRequest = window.indexedDB.open('checklist', 1); + var db = '', store = ''; + + dbRequest.onupgradeneeded = function() { + db = dbRequest.result; + store = db.createObjectStore('CheckLists', { keyPath: 'name' }); + store.createIndex('name', 'name', { unique: true }); + store.createIndex('data', 'data'); + } + dbRequest.onerror = function() { + console.log('Error ', dbRequest.error); + } function stringify(current) { var str = ""; @@ -294,20 +309,38 @@ input { } } - document.getElementById('import-btn').onclick = function () { - if (expImpText.value != "") - parse(expImpText.value, lists); - else - parse(url.searchParams.get('list'), lists); - } + // while (db == '') {} - document.getElementById('export-btn').onclick = function () { - expImpText.value = stringify().substring(1); - url.searchParams.set('list', expImpText.value); - history.pushState(null, '', url); - } + dbRequest.onsuccess = function() { + db = dbRequest.result; - document.getElementById('import-btn').click(); + var store = db.transaction('CheckLists', 'readwrite').objectStore('CheckLists'); + var all = store.getAll(); + all.onsuccess = function() { + all.result.forEach(function (l) { + var nop = document.createElement('option'); + nop.value = l.name; + nop.innerText = l.name; + dropdown.appendChild(nop); + }); + } + + document.getElementById('import-btn').onclick = function () { + const name = dropdown.options[dropdown.selectedIndex].text; + var store = db.transaction('CheckLists', 'readwrite').objectStore('CheckLists'); + var val = store.get(name); + val.onsuccess = function() { + parse(val.result.value, lists); + } + } + + document.getElementById('export-btn').onclick = function () { + expImpText.value = stringify().substring(1); + + var store = db.transaction('CheckLists', 'readwrite').objectStore('CheckLists'); + store.put({ name: 'ListTest', value: expImpText.value }); + } + } </script> </body> </html> |