-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar_display.js
109 lines (94 loc) · 3.37 KB
/
calendar_display.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Generated by CoffeeScript 1.6.2
(function() {
var CalendarDisplay, Cell, moment;
moment = require('moment');
Cell = function(day, isActive) {
/*
# Each Cell represents a square in a calendar. This is a class for the purpose
# of providing lots of assistance to the view.
*/
this.day = day;
this.active = isActive;
this._activities = [];
return this;
};
Cell.prototype["class"] = function() {
var classes;
classes = this.active ? ['active'] : ['inactive'];
if (this._activities.length > 0) {
classes.push('hasActivities');
}
return classes.join(' ');
};
Cell.prototype.addActivity = function(activity) {
return this._activities.push(activity);
};
Cell.prototype.activities = function() {
return this._activities;
};
Cell.prototype.toString = function() {
return "Day: " + this.day + " Classes: " + (this["class"]()) + " Activities: " + (this.activities());
};
CalendarDisplay = exports.CalendarDisplay = function(activities) {
/*
# Handles the partitioning of a raw list of FitnessActivities, provided by
# RunKeeper, into days by month and readying the data to be rendered by the
# frontend.
*/
this._activities = activities.reverse();
return this;
};
CalendarDisplay.prototype.activitiesByMonth = function() {
var act, actByMonth, monthKey, _i, _len, _ref, _ref1;
actByMonth = {};
_ref = this._activities;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
act = _ref[_i];
monthKey = moment(act['start_time']).format('MMMM YYYY');
if ((_ref1 = actByMonth[monthKey]) == null) {
actByMonth[monthKey] = [];
}
actByMonth[monthKey].push(act);
}
return actByMonth;
};
CalendarDisplay.prototype._fillMonth = function(monthStr, activities) {
var act, d, day, missingNumDays, month, monthCells, monthOffset, prevMoDays, prevMoStart, _i, _j, _k, _l, _len, _ref, _ref1;
month = moment(monthStr);
monthOffset = month.day();
monthCells = [];
if (monthOffset > 0) {
month.subtract('months', 1);
prevMoDays = month.daysInMonth();
prevMoStart = prevMoDays - (monthOffset - 1);
for (day = _i = prevMoStart; prevMoStart <= prevMoDays ? _i <= prevMoDays : _i >= prevMoDays; day = prevMoStart <= prevMoDays ? ++_i : --_i) {
monthCells.push(new Cell(day, false));
}
month.add('months', 1);
}
for (day = _j = 1, _ref = month.daysInMonth(); 1 <= _ref ? _j <= _ref : _j >= _ref; day = 1 <= _ref ? ++_j : --_j) {
monthCells.push(new Cell(day, true));
}
missingNumDays = monthCells.length % 7;
if (missingNumDays > 0) {
for (day = _k = 1, _ref1 = 7 - missingNumDays; 1 <= _ref1 ? _k <= _ref1 : _k >= _ref1; day = 1 <= _ref1 ? ++_k : --_k) {
monthCells.push(new Cell(day, false));
}
}
for (_l = 0, _len = activities.length; _l < _len; _l++) {
act = activities[_l];
d = moment(act['start_time']);
monthCells[(monthOffset - 1) + d.date()].addActivity(act);
}
return monthCells;
};
CalendarDisplay.prototype.getElts = function() {
var actByMonth, activities, monthName;
actByMonth = this.activitiesByMonth();
for (monthName in actByMonth) {
activities = actByMonth[monthName];
actByMonth[monthName] = this._fillMonth(monthName, activities);
}
return actByMonth;
};
}).call(this);