UNPKG

@copoko/space

Version:
166 lines 4.94 kB
<script> function NotifyCard(it) { return `<div id="${it.id}" class="card collapsed-card card-${it.type}"> <div class="card-header"> <h3 class="card-title">${it.title}</h3> <div class="card-tools"> <button type="button" class="btn btn-tool" data-card-widget="maximize"> <i class="fas fa-expand"></i> </button> <button type="button" class="btn btn-tool" data-card-widget="collapse"> <i class="fas fa-plus"></i> </button> <button action="${it.id}" onclick="removeNotifyCard(this)" type="button" class="btn btn-tool" data-card-widget="remove"> <i class="fas fa-times"></i> </button> </div> </div> <div class="card-body"> ${it.content} </div> </div>` } fetch("/space/api/notify").then(function (response) { return response.json(); }).then(function (data) { console.log(data); if (data.length > 0) { data.reverse() var notify = document.getElementById("notify"); notify.innerHTML = ""; for (var i = 0; i < data.length; i++) { var div = document.createElement("div"); div.innerHTML = NotifyCard(data[i]); notify.appendChild(div); } } }); function removeNotifyCard(it) { const id = it.getAttribute("action"); fetch("/space/api/notify", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ id: id, action: "delete" }) }).then(function (response) { return response.json(); }).then(function (data) { console.log(data); if (data.length > 0) { data.reverse() var notify = document.getElementById("notify"); notify.innerHTML = ""; for (var i = 0; i < data.length; i++) { var div = document.createElement("div"); div.innerHTML = NotifyCard(data[i]); notify.appendChild(div); } } }); } </script> <script> Space.KV.Get("CalendarEventSource").then(e => { if (e.sucess) { const data = JSON.parse(e.value) window.toDoEvents = [] let HTML = "" document.getElementById("todo-list").innerHTML = "" data.forEach(item => { let date; if (item.end) { date = new Date(item.end).getTime() } else { date = new Date(item.start).getTime() } toDoEvents.push({ title: item.title, id: btoa(encodeURIComponent(item.title + date)), date: date, checked: false, }) }) if (toDoEvents.length) { toDoEvents.sort((a, b) => { return a.date - b.date }) Space.KV.Get("CalendarToDoEvent").then(e => { if (e.sucess) { const data = JSON.parse(e.value) toDoEvents.forEach(item => { data.forEach(item2 => { if (item.id === item2.id) { item.checked = item2.checked } }) }) toDoEvents = toDoEvents.filter(item => { return item.date > new Date().getTime() }) toDoEvents.forEach(item => { HTML += getToDoItem(item) }) if (toDoEvents.length) { document.getElementById("todo-list").innerHTML = HTML document.getElementById("todo").style.display = "block" saveToDoEvents() } } }) } } }) function saveToDoEvents() { toDoEvents = toDoEvents.filter(item => { return item.date > new Date().getTime() }) Space.KV.Put("CalendarToDoEvent", JSON.stringify(toDoEvents)) } function getToDoItem(it) { const time = Space.friendlyFeatureTime(it.date) let type; if (time) { if (time.includes("month")) { type = "secondary" } if (time.includes("week")) { type = "info" } if (time.includes("day")) { type = "primary" } if (time.includes("days")) { type = "success" } if (time.includes("hour")) { type = "warning" } if (time.includes("min")) { type = "danger" } return `<li ${it.checked ? 'class="done"' : ''}> <div class="icheck-primary d-inline ml-2" id="${it.id}"> <input type="checkbox" value="" name="todo" onclick="onClickToDo(this)" id="todoCheck-${it.id}" ${it.checked ? "checked" : ""}> <label for="todoCheck-${it.id}"></label> </div> <span class="text">${it.title}</span> <small class="badge badge-${type}"><i class="far fa-clock"></i>${time}</small> </li>` } return "" } function onClickToDo(it) { const id = it.id.split("todoCheck-")[1] console.log(id); toDoEvents.forEach(item => { if (item.id == id) { item.checked = !item.checked } }) saveToDoEvents() } </script>