| 
									
										
										
										
											2013-08-14 02:34:51 +08:00
										 |  |  | window.addEventListener('hashchange', function () { | 
					
						
							|  |  |  |     Seed.broadcast('filterchange') | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-13 03:04:02 +08:00
										 |  |  | Seed.controller('todos', function (scope) { | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 02:34:51 +08:00
										 |  |  |     // filters ----------------------------------------------------------------
 | 
					
						
							|  |  |  |     var filters = { | 
					
						
							|  |  |  |         all: function () { return true }, | 
					
						
							|  |  |  |         active: function (todo) { return !todo.completed }, | 
					
						
							|  |  |  |         completed: function (todo) { return todo.completed } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     var updateFilter = function () { | 
					
						
							|  |  |  |         var filter = location.hash.slice(2) | 
					
						
							|  |  |  |         scope.filter = (filter in filters) ? filter : 'all' | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     updateFilter() | 
					
						
							|  |  |  |     scope.$on('filterchange', updateFilter) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  |     // regular properties -----------------------------------------------------
 | 
					
						
							| 
									
										
										
										
											2013-08-13 03:04:02 +08:00
										 |  |  |     scope.todos = todoStorage.fetch() | 
					
						
							| 
									
										
										
										
											2013-08-14 02:34:51 +08:00
										 |  |  |     scope.remaining = scope.todos.filter(filters.active).length | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // computed properties ----------------------------------------------------
 | 
					
						
							| 
									
										
										
										
											2013-08-08 04:11:52 +08:00
										 |  |  |     scope.total = {get: function () { | 
					
						
							|  |  |  |         return scope.todos.length | 
					
						
							|  |  |  |     }} | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 04:11:52 +08:00
										 |  |  |     scope.completed = {get: function () { | 
					
						
							| 
									
										
										
										
											2013-08-08 06:24:21 +08:00
										 |  |  |         return scope.total - scope.remaining | 
					
						
							| 
									
										
										
										
											2013-08-08 04:11:52 +08:00
										 |  |  |     }} | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-11 23:30:53 +08:00
										 |  |  |     // dynamic context computed property using info from target scope
 | 
					
						
							| 
									
										
										
										
											2013-08-14 01:20:49 +08:00
										 |  |  |     scope.todoFiltered = {get: function (ctx) { | 
					
						
							| 
									
										
										
										
											2013-08-14 02:34:51 +08:00
										 |  |  |         return filters[scope.filter](ctx.scope) | 
					
						
							| 
									
										
										
										
											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 01:20:49 +08:00
										 |  |  |     scope.filterSelected = {get: function (ctx) { | 
					
						
							| 
									
										
										
										
											2013-08-13 12:06:30 +08:00
										 |  |  |         return scope.filter === ctx.el.textContent.toLowerCase() | 
					
						
							| 
									
										
										
										
											2013-08-11 13:50:32 +08:00
										 |  |  |     }} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // two-way computed property with both getter and setter
 | 
					
						
							| 
									
										
										
										
											2013-08-10 00:18:34 +08:00
										 |  |  |     scope.allDone = { | 
					
						
							|  |  |  |         get: function () { | 
					
						
							|  |  |  |             return scope.remaining === 0 | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         set: function (value) { | 
					
						
							| 
									
										
										
										
											2013-08-13 12:06:30 +08:00
										 |  |  |             scope.remaining = value ? 0 : scope.total | 
					
						
							| 
									
										
										
										
											2013-08-10 00:18:34 +08:00
										 |  |  |             scope.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-09 04:24:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  |     // event handlers ---------------------------------------------------------
 | 
					
						
							| 
									
										
										
										
											2013-08-11 13:50:32 +08:00
										 |  |  |     scope.addTodo = function () { | 
					
						
							| 
									
										
										
										
											2013-08-12 10:35:06 +08:00
										 |  |  |         var value = scope.newTodo && scope.newTodo.trim() | 
					
						
							| 
									
										
										
										
											2013-08-11 23:30:53 +08:00
										 |  |  |         if (value) { | 
					
						
							|  |  |  |             scope.todos.unshift({ title: value, completed: false }) | 
					
						
							| 
									
										
										
										
											2013-08-11 13:50:32 +08:00
										 |  |  |             scope.newTodo = '' | 
					
						
							| 
									
										
										
										
											2013-08-09 10:36:52 +08:00
										 |  |  |             scope.remaining++ | 
					
						
							| 
									
										
										
										
											2013-08-13 03:04:02 +08:00
										 |  |  |             todoStorage.save(scope.todos) | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     scope.removeTodo = function (e) { | 
					
						
							|  |  |  |         scope.todos.remove(e.scope) | 
					
						
							| 
									
										
										
										
											2013-08-10 19:56:59 +08:00
										 |  |  |         scope.remaining -= e.scope.completed ? 0 : 1 | 
					
						
							| 
									
										
										
										
											2013-08-13 03:04:02 +08:00
										 |  |  |         todoStorage.save(scope.todos) | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-13 12:06:30 +08:00
										 |  |  |     scope.toggleTodo = function (e) { | 
					
						
							| 
									
										
										
										
											2013-08-10 19:56:59 +08:00
										 |  |  |         scope.remaining += e.scope.completed ? -1 : 1 | 
					
						
							| 
									
										
										
										
											2013-08-13 03:04:02 +08:00
										 |  |  |         todoStorage.save(scope.todos) | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-11 13:50:32 +08:00
										 |  |  |     var beforeEditCache | 
					
						
							| 
									
										
										
										
											2013-08-13 03:04:02 +08:00
										 |  |  |     scope.editTodo = function (e) { | 
					
						
							| 
									
										
										
										
											2013-08-11 13:50:32 +08:00
										 |  |  |         beforeEditCache = e.scope.title | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  |         e.scope.editing = true | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-11 13:50:32 +08:00
										 |  |  |     scope.doneEdit = function (e) { | 
					
						
							|  |  |  |         if (!e.scope.editing) return | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  |         e.scope.editing = false | 
					
						
							| 
									
										
										
										
											2013-08-11 13:50:32 +08:00
										 |  |  |         e.scope.title = e.scope.title.trim() | 
					
						
							|  |  |  |         if (!e.scope.title) scope.removeTodo(e) | 
					
						
							| 
									
										
										
										
											2013-08-13 03:04:02 +08:00
										 |  |  |         todoStorage.save(scope.todos) | 
					
						
							| 
									
										
										
										
											2013-08-11 13:50:32 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     scope.cancelEdit = function (e) { | 
					
						
							|  |  |  |         e.scope.editing = false | 
					
						
							| 
									
										
										
										
											2013-08-13 12:06:30 +08:00
										 |  |  |         e.scope.title = beforeEditCache | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     scope.removeCompleted = function () { | 
					
						
							| 
									
										
										
										
											2013-08-14 02:34:51 +08:00
										 |  |  |         scope.todos = scope.todos.filter(filters.active) | 
					
						
							| 
									
										
										
										
											2013-08-13 03:04:02 +08:00
										 |  |  |         todoStorage.save(scope.todos) | 
					
						
							| 
									
										
										
										
											2013-08-07 14:03:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-13 03:04:02 +08:00
										 |  |  | Seed.bootstrap() |