| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  | var filters = { | 
					
						
							|  |  |  |     all: function () { return true }, | 
					
						
							|  |  |  |     active: function (todo) { return !todo.completed }, | 
					
						
							|  |  |  |     completed: function (todo) { return todo.completed } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 02:34:51 +08:00
										 |  |  | window.addEventListener('hashchange', function () { | 
					
						
							|  |  |  |     Seed.broadcast('filterchange') | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  | Seed.controller('todos', { | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     // initializer, reserved
 | 
					
						
							|  |  |  |     init: function () { | 
					
						
							| 
									
										
										
										
											2013-08-15 04:47:21 +08:00
										 |  |  |         window.app = this | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |         // listen for hashtag change
 | 
					
						
							|  |  |  |         this.updateFilter() | 
					
						
							|  |  |  |         this.$on('filterchange', this.updateFilter.bind(this)) | 
					
						
							|  |  |  |         // instance properties
 | 
					
						
							|  |  |  |         this.todos = todoStorage.fetch() | 
					
						
							|  |  |  |         this.remaining = this.todos.filter(filters.active).length | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // computed properties ----------------------------------------------------
 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     total: {get: function () { | 
					
						
							|  |  |  |         return this.todos.length | 
					
						
							|  |  |  |     }}, | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     completed: {get: function () { | 
					
						
							|  |  |  |         return this.total - this.remaining | 
					
						
							|  |  |  |     }}, | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-15 04:47:21 +08:00
										 |  |  |     // dynamic context computed property using info from target viewmodel
 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     todoFiltered: {get: function (ctx) { | 
					
						
							| 
									
										
										
										
											2013-08-15 04:47:21 +08:00
										 |  |  |         return filters[this.filter]({ completed: ctx.vm.completed }) | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     }}, | 
					
						
							| 
									
										
										
										
											2013-08-11 13:50:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-11 23:30:53 +08:00
										 |  |  |     // dynamic context computed property using info from target element
 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     filterSelected: {get: function (ctx) { | 
					
						
							|  |  |  |         return this.filter === ctx.el.textContent.toLowerCase() | 
					
						
							|  |  |  |     }}, | 
					
						
							| 
									
										
										
										
											2013-08-11 13:50:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // two-way computed property with both getter and setter
 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     allDone: { | 
					
						
							| 
									
										
										
										
											2013-08-10 00:18:34 +08:00
										 |  |  |         get: function () { | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |             return this.remaining === 0 | 
					
						
							| 
									
										
										
										
											2013-08-10 00:18:34 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |         set: function (value) { | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |             this.todos.forEach(function (todo) { | 
					
						
							| 
									
										
										
										
											2013-08-10 19:56:59 +08:00
										 |  |  |                 todo.completed = value | 
					
						
							| 
									
										
										
										
											2013-08-10 00:18:34 +08:00
										 |  |  |             }) | 
					
						
							| 
									
										
										
										
											2013-08-15 04:47:21 +08:00
										 |  |  |             this.remaining = value ? 0 : this.total | 
					
						
							|  |  |  |             todoStorage.save(this.todos) | 
					
						
							| 
									
										
										
										
											2013-08-10 00:18:34 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2013-08-09 04:24:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  |     // event handlers ---------------------------------------------------------
 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     addTodo: function () { | 
					
						
							|  |  |  |         var value = this.newTodo && this.newTodo.trim() | 
					
						
							| 
									
										
										
										
											2013-08-11 23:30:53 +08:00
										 |  |  |         if (value) { | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |             this.todos.unshift({ title: value, completed: false }) | 
					
						
							|  |  |  |             this.newTodo = '' | 
					
						
							|  |  |  |             this.remaining++ | 
					
						
							|  |  |  |             todoStorage.save(this.todos) | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     removeTodo: function (e) { | 
					
						
							| 
									
										
										
										
											2013-08-15 04:47:21 +08:00
										 |  |  |         this.todos.remove(e.vm) | 
					
						
							|  |  |  |         this.remaining -= e.vm.completed ? 0 : 1 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |         todoStorage.save(this.todos) | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     toggleTodo: function (e) { | 
					
						
							| 
									
										
										
										
											2013-08-15 04:47:21 +08:00
										 |  |  |         this.remaining += e.vm.completed ? -1 : 1 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |         todoStorage.save(this.todos) | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     editTodo: function (e) { | 
					
						
							| 
									
										
										
										
											2013-08-15 04:47:21 +08:00
										 |  |  |         this.beforeEditCache = e.vm.title | 
					
						
							|  |  |  |         e.vm.editing = true | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     doneEdit: function (e) { | 
					
						
							| 
									
										
										
										
											2013-08-15 04:47:21 +08:00
										 |  |  |         if (!e.vm.editing) return | 
					
						
							|  |  |  |         e.vm.editing = false | 
					
						
							|  |  |  |         e.vm.title = e.vm.title.trim() | 
					
						
							|  |  |  |         if (!e.vm.title) this.removeTodo(e) | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |         todoStorage.save(this.todos) | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2013-08-11 13:50:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     cancelEdit: function (e) { | 
					
						
							| 
									
										
										
										
											2013-08-15 04:47:21 +08:00
										 |  |  |         e.vm.editing = false | 
					
						
							|  |  |  |         e.vm.title = this.beforeEditCache | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     removeCompleted: function () { | 
					
						
							|  |  |  |         this.todos = this.todos.filter(filters.active) | 
					
						
							|  |  |  |         todoStorage.save(this.todos) | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:53:29 +08:00
										 |  |  |     updateFilter: function () { | 
					
						
							|  |  |  |         var filter = location.hash.slice(2) | 
					
						
							|  |  |  |         this.filter = (filter in filters) ? filter : 'all' | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-13 03:04:02 +08:00
										 |  |  | Seed.bootstrap() |