From 60797ffdb82ba5b8543cc138492f3445b8cabf42 Mon Sep 17 00:00:00 2001 From: maxpozdeev Date: Mon, 3 Oct 2022 12:12:32 +0300 Subject: [PATCH] + remember last opened list in browser storage (closes GH-39) --- src/content/mytinytodo.js | 54 ++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/src/content/mytinytodo.js b/src/content/mytinytodo.js index b491844..9af7be3 100644 --- a/src/content/mytinytodo.js +++ b/src/content/mytinytodo.js @@ -729,26 +729,28 @@ var mytinytodo = window.mytinytodo = _mtt = { { var ti = ''; var openListId = 0; - if(res && res.total && res.list) + + if (res && res.total && res.list) { - // open required or first non-hidden list - for(var i=0; i _mtt.options.openList == item.id ); + } + else { + const lastOpenList = getLocalStorageItem('lastList'); + if (lastOpenList) { + list = res.list.find( item => !item.hidden && lastOpenList == item.id ); } - else if(!res.list[i].hidden) { - openListId = res.list[i].id; - break; + if (!list) { + list = res.list.find( item => !item.hidden ); } } + if (list) { + openListId = list.id; + } - // open all tasks tab - if(_mtt.options.openList == -1) openListId = -1; - - $.each(res.list, function(i, item) { + res.list.forEach( (item) => { if ( item.id == -1 ) { tabLists._alltasks = item; ti += prepareListHtml(item); @@ -1524,6 +1526,7 @@ function tabSelect(elementOrId) var newTitle = curList.name + ' - ' + _mtt.options.title; var isFirstLoad = flag.firstLoad; updateHistoryState( { list:id }, _mtt.urlForList(curList), newTitle ); + setLocalStorageItem('lastList', ''+id); if (curList.hidden && flag.readOnly != true) { curList.hidden = false; @@ -2512,6 +2515,27 @@ function hideList(listId) } } +function getLocalStorageItem(key) +{ + try { + return localStorage.getItem(key); + } + catch (e) { + console.log(e); + } + return null; +} + +function setLocalStorageItem(key, value) +{ + try { + localStorage.setItem(key, value); + } + catch (e) { + console.log(e); + } +} + /* Errors and Info messages */