UNPKG

can

Version:

MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.

148 lines (127 loc) 4.69 kB
<html> <head> <style> [type=date] { border: none; } </style> </head> <body> <todo-list></todo-list> <script src="../../node_modules/steal/steal.js" dev-bundle main="@empty" id="demo-source"> import { fixture, ObservableArray, ObservableObject, restModel, StacheElement, type } from "can"; class Todo extends ObservableObject { static props = { // `id` uniquely identifies instances of this type. id: { type: type.maybe(Number), identity: true }, complete: { type: Boolean, default: false }, dueDate: type.maybeConvert(Date), name: type.maybe(String) }; } class TodoListModel extends ObservableArray { static items = Todo; } const todoConnection = restModel({ Map: Todo, List: TodoListModel, url: "/api/todos/{id}" }); let terms = ["can you","please","","","",""], verbs = ["clean","walk","do","vaccum","organize","fold","wash","dust","pay","cook","get","take out"], subjects = ["dog","laundry","diapers","clothes","car","windows","carpet","taxes","food","gas","trash"]; let dayInMS = 24*60*60*1000; let lastWeek = new Date() - (7*dayInMS); let fourWeeks = new Date().getTime() + (4*7*dayInMS); let todoStore = fixture.store(20, function(){ return { complete: fixture.rand([true, false],1)[0], dueDate: new Date( fixture.rand(lastWeek, fourWeeks) ).toString(), name: (fixture.rand(terms,1)[0]+" "+fixture.rand(verbs,1)[0]+" "+fixture.rand(subjects,1)[0]).trim() } }, Todo); fixture("/api/todos/{id}", todoStore); class TodoList extends StacheElement { static view = ` Sort By: <select value:bind="sort"> <option value="">none</option> <option value="name">name</option> <option value="dueDate">dueDate</option> </select> Show: <select value:bind="completeFilter"> <option value="">All</option> <option value="complete">Complete</option> <option value="incomplete">Incomplete</option> </select> Due: <select value:bind="dueFilter"> <option value="">Anytime</option> <option value="today">Today</option> <option value="week">This Week</option> </select> Results <select value:bind="count"> <option value="">All</option> <option value="10">10</option> <option value="20">20</option> </select> <ul> {{# if(this.todosPromise.isResolved) }} {{# for(todo of this.todosPromise.value) }} <li> <input type='checkbox' checked:bind='todo.complete' disabled/> <label>{{ todo.name }}</label> <input type='date' valueAsDate:bind='todo.dueDate' disabled/> </li> {{/ todo }} {{/ if }} {{# if(this.todosPromise.isPending) }} <li>Loading</li> {{/ if }} </ul> `; static props = { sort: type.maybe(String), completeFilter: type.maybe(String), dueFilter: type.maybe(String), count: {type: String, default: "10"}, todosPromise: { get(){ let query = {filter: { }}; if(this.sort) { query.sort = this.sort; } if(this.completeFilter) { query.filter.complete = this.completeFilter === "complete"; } if(this.dueFilter) { let day = 24*60*60*1000; let now = new Date(); let today = new Date(now.getFullYear(), now.getMonth(), now.getDate() ); if(this.dueFilter === "today") { query.filter.dueDate = { $gte: now.toString(), $lt: new Date(now.getTime() + day).toString() } } if(this.dueFilter === "week") { let start = today.getTime() - (today.getDay() * day); query.filter.dueDate = { $gte: new Date(start).toString(), $lt: new Date(start + 7*day).toString() }; } } if(this.count) { query.page = { start: 0, end: (+this.count)-1 }; } return Todo.getList(query); } } }; } customElements.define("todo-list", TodoList); </script> </body> </html>