Skip to content

Commit 8e232ca

Browse files
author
Phil Strong
committed
Late and Today
1 parent a59cad6 commit 8e232ca

File tree

4 files changed

+179
-9
lines changed

4 files changed

+179
-9
lines changed

app/css/app.css

+10
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,13 @@
2828
content: "";
2929
padding: 0;
3030
}
31+
32+
.hdr {
33+
border-bottom: solid 1px;
34+
}
35+
36+
.hdr.late {
37+
border-color: #cc0000;
38+
39+
color: red;
40+
}

app/index.html

+20-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,30 @@
77
</head>
88
<body>
99
<ul class="menu">
10-
<li><a href="#/view1">view1</a></li>
10+
<li><a href="#/list">list</a></li>
1111
<li><a href="#/view2">view2</a></li>
1212
</ul>
1313

14-
<div ng-view></div>
14+
<div ng-controller="TodoCtrl">
15+
16+
<h2>Total Todos: {{remaining()}}</h2>
17+
<h2 class="hdr late">Late</h2>
18+
<ul>
19+
<li ng-repeat="todo in late()">
20+
<h3>{{todo.title}}</h3>
21+
<div>{{timeSince(todo.due)}}</div>
22+
</li>
23+
</ul>
24+
25+
<h2 class="hdr today">Today</h2>
26+
<ul>
27+
<li ng-repeat="todo in today()">
28+
<h3>{{todo.title}}</h3>
29+
<div>{{timeSince(todo.due)}}</div>
30+
</li>
31+
</ul>
32+
</div>
1533

16-
<div>Angular seed app: v<span app-version></span></div>
1734

1835
<!-- In production use:
1936
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script>

app/js/app.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
// Declare app level module which depends on filters, and services
55
angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives']).
66
config(['$routeProvider', function($routeProvider) {
7-
$routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: MyCtrl1});
8-
$routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: MyCtrl2});
7+
$routeProvider.when('/list', {templateUrl: 'partials/partial1.html', controller: TodoCtrl});
8+
$routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: TodoCtrl});
99
$routeProvider.otherwise({redirectTo: '/view1'});
1010
}]);
11+

app/js/controllers.js

+146-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,152 @@
33
/* Controllers */
44

55

6-
function MyCtrl1() {}
7-
MyCtrl1.$inject = [];
6+
function TodoCtrl($scope) {
7+
$scope.totalTodos = 22;
88

9+
$scope.remaining = function() {
10+
return $scope.todos.length;
11+
};
912

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+
];
11152
}
12-
MyCtrl2.$inject = [];
153+
154+
// TodoItemCtrl.$inject = [];

0 commit comments

Comments
 (0)