|
3 | 3 | /* Controllers */
|
4 | 4 |
|
5 | 5 |
|
6 |
| -function MyCtrl1() {} |
7 |
| -MyCtrl1.$inject = []; |
| 6 | +function TodoCtrl($scope) { |
| 7 | + $scope.totalTodos = 22; |
8 | 8 |
|
| 9 | + $scope.remaining = function() { |
| 10 | + return $scope.todos.length; |
| 11 | + }; |
9 | 12 |
|
10 |
| -function MyCtrl2() { |
| 13 | + $scope.late = function() { |
| 14 | + var late = [], |
| 15 | + todos = $scope.todos, |
| 16 | + today = Date.now(), |
| 17 | + dt; |
| 18 | + |
| 19 | + |
| 20 | + for (var i = 0; i < todos.length; i++) { |
| 21 | + dt = new Date(todos[i].due); |
| 22 | + |
| 23 | + if (dt < today && !$scope.isToday(dt)) { |
| 24 | + late.push(todos[i]); |
| 25 | + } |
| 26 | + } |
| 27 | + return late; |
| 28 | + }; |
| 29 | + |
| 30 | + $scope.isToday = function(dt) { |
| 31 | + if (typeof dt.getMonth !== 'function') { |
| 32 | + dt = new Date(dt); |
| 33 | + } |
| 34 | + |
| 35 | + var today = new Date(); |
| 36 | + return dt.getYear() === today.getYear() && dt.getMonth() === today.getMonth() && dt.getDay() === today.getDay(); |
| 37 | + }; |
| 38 | + |
| 39 | + $scope.today = function() { |
| 40 | + var late = [], |
| 41 | + todos = $scope.todos, |
| 42 | + today = Date.now(), |
| 43 | + dt, seconds, interval; |
| 44 | + |
| 45 | + |
| 46 | + for (var i = 0; i < todos.length; i++) { |
| 47 | + dt = new Date(todos[i].due); |
| 48 | + |
| 49 | + if ($scope.isToday(dt)) { |
| 50 | + late.push(todos[i]); |
| 51 | + } |
| 52 | + } |
| 53 | + return late; |
| 54 | + }; |
| 55 | + |
| 56 | + $scope.soon = function() { |
| 57 | + var late = [], |
| 58 | + todos = $scope.todos, |
| 59 | + today = Date.now(), |
| 60 | + dt; |
| 61 | + |
| 62 | + |
| 63 | + for (var i = 0; i < todos.length; i++) { |
| 64 | + dt = new Date(todos[i].due); |
| 65 | + if (dt < today) { |
| 66 | + late.push(todos[i]); |
| 67 | + } |
| 68 | + } |
| 69 | + return late; |
| 70 | + }; |
| 71 | + |
| 72 | + $scope.someday = function() { |
| 73 | + var late = [], |
| 74 | + todos = $scope.todos, |
| 75 | + today = Date.now(), |
| 76 | + dt; |
| 77 | + |
| 78 | + |
| 79 | + for (var i = 0; i < todos.length; i++) { |
| 80 | + dt = new Date(todos[i].due); |
| 81 | + if (dt < today) { |
| 82 | + late.push(todos[i]); |
| 83 | + } |
| 84 | + } |
| 85 | + return late; |
| 86 | + }; |
| 87 | + |
| 88 | + $scope.timeSince = function(date) { |
| 89 | + var now = new Date(), |
| 90 | + seconds, |
| 91 | + interval; |
| 92 | + // today, tommorow |
| 93 | + date = new Date(date); |
| 94 | + |
| 95 | + seconds = Math.floor((now - date) / 1000); |
| 96 | + if (date < now) { |
| 97 | + // late |
| 98 | + interval = Math.floor(seconds / 31536000); |
| 99 | + |
| 100 | + if (interval > 1) { |
| 101 | + return interval + " years ago"; |
| 102 | + } |
| 103 | + interval = Math.floor(seconds / 2592000); |
| 104 | + if (interval > 1) { |
| 105 | + return interval + " months ago"; |
| 106 | + } |
| 107 | + interval = Math.floor(seconds / 86400); |
| 108 | + if (interval > 1) { |
| 109 | + return interval + " days ago"; |
| 110 | + } |
| 111 | + interval = Math.floor(seconds / 3600); |
| 112 | + if (interval > 1) { |
| 113 | + return "today"; |
| 114 | + } |
| 115 | + interval = Math.floor(seconds / 60); |
| 116 | + if (interval > 1) { |
| 117 | + return "today"; |
| 118 | + } |
| 119 | + return Math.floor(seconds) + " seconds"; |
| 120 | + } else { |
| 121 | + interval = Math.floor(seconds / 31536000); |
| 122 | + if (interval > 1) { |
| 123 | + return "in " + interval + " years"; |
| 124 | + } |
| 125 | + interval = Math.floor(seconds / 2592000); |
| 126 | + if (interval > 1) { |
| 127 | + return "in " + interval + " months"; |
| 128 | + } |
| 129 | + interval = Math.floor(seconds / 86400); |
| 130 | + if (interval > 1) { |
| 131 | + return "in " + interval + " days"; |
| 132 | + } |
| 133 | + interval = Math.floor(seconds / 3600); |
| 134 | + if (interval > 1) { |
| 135 | + return "today"; |
| 136 | + } |
| 137 | + interval = Math.floor(seconds / 60); |
| 138 | + if (interval > 1) { |
| 139 | + return "today"; |
| 140 | + } |
| 141 | + return Math.floor(seconds) + " seconds"; |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + }; |
| 146 | + |
| 147 | + $scope.todos = [ |
| 148 | + {"title":"Pay electric bill","duration":600,"due":"2013-03-01T08:00:00.000Z","created":"2013-03-06T00:57:47.000Z","updated":"2013-03-06T00:57:47.000Z","completed":null,"deleted":false,"id":"b1-68691467-45"}, |
| 149 | + {"title":"Walk","duration":600,"due":"2013-03-06T08:00:00.000Z","created":"2013-03-06T00:57:47.000Z","updated":"2013-03-06T00:57:47.000Z","completed":null,"deleted":false,"id":"b1-68691467-45"}, |
| 150 | + {"title":"Walk","duration":600,"due":"2013-03-05T08:00:00.000Z","created":"2013-03-06T00:57:47.000Z","updated":"2013-03-06T00:57:47.000Z","completed":null,"deleted":false,"id":"b1-68691467-45"} |
| 151 | + ]; |
11 | 152 | }
|
12 |
| -MyCtrl2.$inject = []; |
| 153 | + |
| 154 | +// TodoItemCtrl.$inject = []; |
0 commit comments