1 line
1.9 KiB
JavaScript
1 line
1.9 KiB
JavaScript
|
window.VT=window.VT||{},VT.TodoItem=function(a){function b(){var b=k.value.trim();return""===b?void requestAnimationFrame(function(){a.dispatchEvent(new CustomEvent("deleteItem",{detail:f.item,bubbles:!0}))}):void(a.dispatchEvent(new CustomEvent("saveItem",{detail:{item:f.item,label:b},bubbles:!0})),d({editing:!1}))}function c(){h=!1,d({editing:!1})}function d(b){Object.assign(f,b),a.classList.toggle("-done",f.item.done),i.querySelector("input").checked=f.item.done,j.innerText=f.item.label,a.classList.toggle("-editing",f.editing),a.classList.toggle("_nodrag",f.editing),f.editing&&g&&(k.value=f.item.label,k.focus(),k.select(),g=!1)}var f={item:null,editing:!1},g=!1,h=!0;a.innerHTML="<div class=\"checkbox\">\n <input type=\"checkbox\">\n</div>\n<p class=\"label\"></p>\n<p class=\"form\">\n <input type=\"text\" class=\"input use-focus-other\">\n <button class=\"app-button save\"><i class=\"app-icon\" data-id=\"check-16\"></i></button>\n</p>";var i=a.querySelector(".checkbox"),j=a.querySelector(".label"),k=a.querySelector(".input"),l=a.querySelector(".save");VT.AppDraggable(a,{dropSelector:".todo-list > .items"}),a.querySelectorAll(".app-icon").forEach(VT.AppIcon),i.addEventListener("touchstart",function(){h=!1}),i.addEventListener("mousedown",function(){h=!1}),i.addEventListener("click",function(){f.editing&&b(),a.dispatchEvent(new CustomEvent("checkItem",{detail:{item:f.item,done:!f.item.done},bubbles:!0}))}),j.addEventListener("click",function(){g=!0,d({editing:!0})}),k.addEventListener("keyup",function(a){switch(a.keyCode){case 13:b();break;case 27:c();}}),k.addEventListener("blur",function(){h&&b(),h=!0}),k.addEventListener("focusOther",function(){f.editing&&b()}),l.addEventListener("mousedown",function(){h=!1}),l.addEventListener("click",b),a.addEventListener("draggableStart",function(a){a.detail.data.item=f.item,a.detail.data.key=f.item.id}),a.todoItem={update:d}};
|