-
Notifications
You must be signed in to change notification settings - Fork 1
/
calendar.js
82 lines (73 loc) · 2.17 KB
/
calendar.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
var querystring = require('querystring')
var moment = require('moment')
, Calendar = require('calendar').Calendar
var settings = require('./settings')
var URL_ROOT = '/calendar/'
, URL_FMT = 'YYYY/M'
module.exports = function(kwargs) {
var calendarYear = kwargs.year
, calendarMonth = kwargs.month
, filters = querystring.stringify(kwargs.filters)
if (filters) {
filters = '?' + filters
}
var today = new Date()
, firstDate = null
, lastDate = null
, cellLookup = {}
var calendar = new Calendar(settings.weekStartsMonday ? 1 : 0)
, headings = moment.weekdays.slice()
if (settings.weekStartsMonday) {
headings.push(headings.shift())
}
var rows = calendar.monthDates(calendarYear, calendarMonth, function(d) {
if (firstDate === null) {
firstDate = d
}
else {
lastDate = d
}
var cell = {
date: d.getDate()
, month: d.getMonth()
, year: d.getFullYear()
, classes: []
, tasks: []
}
// Add special cell classes
if (d.getMonth() !== calendarMonth) {
cell.classes.push('other-month')
}
if (d.getDate() == today.getDate() &&
d.getMonth() == today.getMonth() &&
d.getFullYear() == today.getFullYear()) {
cell.classes.push('today')
}
// Add cell to the month/date lookup
if (typeof cellLookup[d.getMonth()] == 'undefined') {
cellLookup[d.getMonth()] = {}
}
cellLookup[cell.month][cell.date] = cell
// Return the cell to add it to a row
return cell
})
var month = moment([calendarYear, calendarMonth])
return {
// Tasks
fromTime : moment(firstDate).startOf('day').valueOf()
, toTime : moment(lastDate).endOf('day').valueOf()
, month : month.format('MMMM YYYY')
, headings : headings
, rows : rows
// URLs
, currentURL : URL_ROOT + month.format(URL_FMT)
, previousURL : URL_ROOT + moment(month).subtract('M', 1).format(URL_FMT) + filters
, nextURL : URL_ROOT + moment(month).add('M', 1).format(URL_FMT) + filters
// Functions
, addTasks: function(tasks) {
tasks.forEach(function(task) {
cellLookup[task.due.getMonth()][task.due.getDate()].tasks.push(task)
})
}
}
}