/
calendardata.js
132 lines (122 loc) · 3.47 KB
/
calendardata.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* handle data of calendar
*/
/**
* Calendar data
* @class CalendarData
*/
var CalendarData = function(){
var sApiKey = "PUT_YOUR_API_KEY_HERE";
//var sCalId = "ja.japanese%23holiday@group.v.calendar.google.com";
var sCalId = "outid3el0qkcrsuf89fltf7a4qbacgt9@import.calendar.google.com";
var sGCalUrl = "https://www.googleapis.com/calendar/v3/calendars/"
+ sCalId
+ "/events?orderBy=startTime&singleEvents=true&maxResults=1000"
+ "&key=" + sApiKey;
// &timeMin=2011-01-01T00%3A00%3A00Z&timeMax=2012-01-01T00%3A00%3A00Z;
/*
Example data
oHolidays = {
"2011-1":[true, null, null, ....],
"2011-2":[null, null, null, ... true, null,...]
}
oMarkDates = {
"2011-1":["red", null, null, "yellow"],
"2011-2":[null, null, "green"]
}
*/
var oHolidays = {};
var oMarkDates = {}; // date(s) that are marked by a user
if (localStorage) {
var oHData = $.parseJSON(localStorage.getItem("holidays"));
var oMData = $.parseJSON(localStorage.getItem("markdates"));
oHolidays = oHData ? oHData : oHolidays;
oMarkDates = oMData ? oMData : oMarkDates;
}
$(document).ready(function(){
var oDate = new Date();
var nYear = oDate.getFullYear();
var nMonth = oDate.getMonth() + 1;
var sMonth = nMonth < 10 ? "0"+nMonth : ""+nMonth;
if (oHolidays && $.isArray(oHolidays[(nYear+1) + "-" + nMonth])) {
return;
}
var sUrl = sGCalUrl
+ "&timeMin=" + (nYear-1) + "-" + sMonth + "-01T00%3A00%3A00Z"
+ "&timeMax=" + (nYear+2) + "-" + sMonth + "-01T00%3A00%3A00Z"
+ "&callback=?";
$.getJSON(sUrl, CalendarData.recordHolidays);
});
return {
/**
* returns holiday data for specified month
* @method getHolidays
* @public
* @param {number} Year
* @param {number} Month
* @return {array} Holidays
*/
getHolidays: function(nYear, nMonth){
var aHolidays = oHolidays[nYear+"-"+nMonth];
return aHolidays ? aHolidays : [];
},
/**
* returns marked date for specified month
* @method getMarkDates
* @public
* @param {number} Year
* @param {number} Month
* @return {array} MarkDates
*/
getMarkDates: function(nYear, nMonth){
var aMarkDates = oMarkDates[nYear+"-"+nMonth];
return aMarkDates ? aMarkDates : [];
},
/**
* record holiday data that is returned from google calenda API
* to localStorage
* @method recordHolidays
* @public
* @param {object} Json
* @return {void}
*/
recordHolidays: function(oJson){
if (!oJson || !$.isArray(oJson.items)) {
return;
}
$.each(oJson.items, function(i, oData){
var sDate = oData.start.date;
var aYMD = sDate.split("-");
var sYearMonth = aYMD[0] + "-" + parseInt(aYMD[1], 10);
var nDate = parseInt(aYMD[2], 10);
if (!oHolidays[sYearMonth]) {
oHolidays[sYearMonth] = [];
}
oHolidays[sYearMonth][nDate] = true;
});
if (localStorage) {
localStorage.setItem("holidays", JSON.stringify(oHolidays));
}
},
/**
* record a marked date to localStorage
* @method recordMarkDate
* @public
* @param {number} Year
* @param {number} Month
* @param {number} Date
* @param {string} Color
* @return {void}
*/
recordMarkDate: function(nYear, nMonth, nDate, sColor){
var sYearMonth = nYear + "-" + nMonth;
if (!oMarkDates[sYearMonth]) {
oMarkDates[sYearMonth] = [];
}
oMarkDates[sYearMonth][nDate] = sColor;
if (localStorage) {
localStorage.setItem("markdates", JSON.stringify(oMarkDates));
}
}
};
}();