whisper.cat/weboasis/todo/scripts/TodoStore.min.js
2023-10-05 23:28:32 +11:00

1 line
2.6 KiB
JavaScript

window.VT=window.VT||{},VT.TodoStore=function(a){function b(b){Object.assign(f,b),c(),a.dispatchEvent(new CustomEvent("todoData",{detail:f,bubbles:!1}))}function c(){clearTimeout(d),d=setTimeout(function(){try{localStorage.todo=JSON.stringify(f)}catch(a){console.warn(a)}},100)}var d,f={items:[],customLists:[],at:VT.formatDateId(new Date),customAt:0};a.addEventListener("addItem",function(a){var c=0;f.items.forEach(function(b){b.listId===a.detail.listId&&(c=Math.max(c,b.index+1))}),f.items.push({id:VT.uuid(),listId:a.detail.listId,index:c,label:a.detail.label,done:!1}),b({items:f.items})}),a.addEventListener("checkItem",function(a){a.detail.item.done===a.detail.done||(a.detail.item.done=a.detail.done,b({items:f.items}))}),a.addEventListener("saveItem",function(a){a.detail.item.label===a.detail.label||(a.detail.item.label=a.detail.label,b({items:f.items}))}),a.addEventListener("moveItem",function(a){var c=f.items.find(function(b){return b.id===a.detail.item.id}),d=f.items.filter(function(b){return b.listId===a.detail.listId&&b!==c});d.sort(function(c,a){return c.index-a.index}),c.listId=a.detail.listId,d.splice(a.detail.index,0,c),d.forEach(function(a,b){a.index=b}),b({items:f.items})}),a.addEventListener("deleteItem",function(a){b({items:f.items.filter(function(b){return b.id!==a.detail.id})})}),a.addEventListener("addList",function(a){var c=0;f.customLists.forEach(function(a){c=Math.max(c,a.index+1)}),f.customLists.push({id:VT.uuid(),index:c,title:a.detail.title||""}),b({customLists:f.customLists})}),a.addEventListener("saveList",function(a){var c=f.customLists.find(function(b){return b.id===a.detail.list.id});c.title===a.detail.title||(c.title=a.detail.title,b({customLists:f.customLists}))}),a.addEventListener("moveList",function(a){var c=f.customLists.findIndex(function(b){return b.id===a.detail.list.id}),d=f.customLists[c];f.customLists.splice(c,1),f.customLists.sort(function(c,a){return c.index-a.index}),f.customLists.splice(a.detail.index,0,d),f.customLists.forEach(function(a,b){a.index=b}),b({customLists:f.customLists})}),a.addEventListener("deleteList",function(a){b({customLists:f.customLists.filter(function(b){return b.id!==a.detail.id})})}),a.addEventListener("seek",function(a){var c=new Date(f.at+" 00:00:00");c.setDate(c.getDate()+a.detail),b({at:VT.formatDateId(c)})}),a.addEventListener("seekHome",function(){b({at:VT.formatDateId(new Date)})}),a.addEventListener("customSeek",function(a){b({customAt:Math.max(0,Math.min(f.customLists.length-1,f.customAt+a.detail))})}),a.todoStore={dispatch:b,load:function(){if(!localStorage||!localStorage.todo)return void b(f);try{b(JSON.parse(localStorage.todo))}catch(a){console.warn(a)}}}};