|
1 |
| -import {Router} from 'aurelia-router'; |
2 |
| -import {Todos} from './todos'; |
| 1 | +var KNOWN_ROUTES = Object.assign(Object.create(null), { |
| 2 | + active: 'active', |
| 3 | + completed: 'completed' |
| 4 | +}); |
| 5 | +const ENTER_KEY = 13; |
| 6 | +const ESC_KEY = 27; |
| 7 | +const STORAGE_NAME = 'todomvc-aurelia'; |
3 | 8 |
|
4 |
| -export class App { |
5 |
| - static inject() { return [Router]; } |
| 9 | +/** |
| 10 | + * Simple ES5 style factory fn |
| 11 | + */ |
| 12 | +function App() { |
6 | 13 |
|
7 |
| - constructor(router) { |
8 |
| - this.router = router; |
9 |
| - this.router.configure(this.configureRoutes); |
10 |
| - } |
| 14 | + var app = { |
| 15 | + init: function() { |
| 16 | + this.load(); |
| 17 | + this.filter = ''; |
| 18 | + this.filterVersion = 0; // This is observed by the filterTodos value converter to avoid dirty checking |
| 19 | + this.configureRouting(); |
| 20 | + }, |
| 21 | + |
| 22 | + // Simplest form of routing, for the todomvc app. Can also be done with `aurelia-router` (see https://aurelia.io/docs/routing/) for apps that use setRoot |
| 23 | + configureRouting: function() { |
| 24 | + var handleHashChange = function() { |
| 25 | + var fragment = location.hash; |
| 26 | + var filter = fragment.replace(/^#?\/?/, ''); |
| 27 | + app.filter = KNOWN_ROUTES[filter] || ''; |
| 28 | + if (!(filter in KNOWN_ROUTES)) { |
| 29 | + location.hash = '#/'; |
| 30 | + } |
| 31 | + }; |
| 32 | + handleHashChange(); |
| 33 | + window.onhashchange = handleHashChange; |
| 34 | + }, |
| 35 | + |
| 36 | + addNewTodo: function(title) { |
| 37 | + if (title === undefined) { |
| 38 | + title = this.newTodoTitle; |
| 39 | + } |
| 40 | + title = title && title.trim(); |
| 41 | + if (!title) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + this.todos.push({ title: title, isCompleted: false }); |
| 46 | + |
| 47 | + this.newTodoTitle = ''; |
| 48 | + this.save(); |
| 49 | + }, |
| 50 | + |
| 51 | + beginEdit: function(todo) { |
| 52 | + todo.isEditing = true; |
| 53 | + todo.tempTitle = todo.title; |
| 54 | + }, |
| 55 | + |
| 56 | + commitEdit: function(todo) { |
| 57 | + todo.title = todo.tempTitle.trim(); |
| 58 | + todo.isEditing = false; |
| 59 | + this.save(); |
| 60 | + }, |
| 61 | + |
| 62 | + cancelEdit: function(todo) { |
| 63 | + todo.tempTitle = todo.title; |
| 64 | + todo.isEditing = false; |
| 65 | + }, |
| 66 | + |
| 67 | + deleteTodo: function(todo) { |
| 68 | + this.todos.splice(this.todos.indexOf(todo), 1); |
| 69 | + this.save(); |
| 70 | + }, |
| 71 | + |
| 72 | + toggleTodo: function(todo) { |
| 73 | + todo.isCompleted = !todo.isCompleted; |
| 74 | + this.filterVersion++; |
| 75 | + this.save(); |
| 76 | + }, |
| 77 | + |
| 78 | + toggleAll: function() { |
| 79 | + const allCompleted = this.todos.every(function(todo) { return todo.isCompleted; }); |
| 80 | + this.todos.forEach(function(todo) { |
| 81 | + return todo.isCompleted = !allCompleted; |
| 82 | + }); |
| 83 | + this.filterVersion++; |
| 84 | + }, |
| 85 | + |
| 86 | + clearCompletedTodos: function() { |
| 87 | + this.todos = this.todos.filter(function(todo) { return !todo.isCompleted; }); |
| 88 | + this.save(); |
| 89 | + }, |
| 90 | + |
| 91 | + handleKeyup: function(todo, ev) { |
| 92 | + if (ev.keyCode === ENTER_KEY) { |
| 93 | + this.commitEdit(todo); |
| 94 | + } else if (ev.keyCode === ESC_KEY) { |
| 95 | + ev.target.blur(); |
| 96 | + } |
| 97 | + }, |
| 98 | + |
| 99 | + load: function() { |
| 100 | + const storageContent = localStorage.getItem(STORAGE_NAME); |
| 101 | + this.todos = storageContent && JSON.parse(storageContent) || []; |
| 102 | + }, |
| 103 | + |
| 104 | + save: function() { |
| 105 | + localStorage.setItem(STORAGE_NAME, JSON.stringify(this.todos)); |
| 106 | + } |
| 107 | + }; |
| 108 | + |
| 109 | + app.init(); |
| 110 | + return app; |
| 111 | +} |
11 | 112 |
|
12 |
| - configureRoutes(cfg) { |
13 |
| - cfg.title = 'TodoMVC'; |
14 |
| - cfg.map([ |
15 |
| - { route: ['', ':filter'], moduleId: 'todos' } |
16 |
| - ]); |
| 113 | +// A value converter for filtering todos based supplied arguments (see https://aurelia.io/docs/binding/value-converters#introduction) |
| 114 | +function FilterTodoValueConverter() { |
| 115 | + return { |
| 116 | + toView: function(todos, filter) { |
| 117 | + if (!Array.isArray(todos)) return []; |
| 118 | + if (filter === 'all' || !filter) return todos; |
| 119 | + |
| 120 | + const isCompleted = filter !== 'active'; |
| 121 | + return todos.filter(function(todo) { return todo.isCompleted === isCompleted; }); |
| 122 | + } |
17 | 123 | }
|
18 | 124 | }
|
0 commit comments