-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar-data-structure.js
executable file
·136 lines (116 loc) · 4.92 KB
/
calendar-data-structure.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
133
134
135
136
/*
@Author: Joel De La Torriente
@Email: [email protected]
@WebSite: www.jjdltc.com
@Version: 0.3.0
@License: MIT License (MIT)
*/
if(module && module.hasOwnProperty("exports")){
module.exports = CalendarStructure;
}
function CalendarStructure(options){
var options = options || {};
this.languageData = options.languageData || {
days : ["Dom","Lun","Mar","Mie","Jue","Vie","Sab"],
daysFullText : ["Domingo","Lunes","Martes","Miercoles","Jueves","Viernes","Sabado"],
months : ["Ene","Feb","Mar","Abr","May","Jun","Jul","Ago","Sep","Oct","Nov","Dic"],
monthsFullText : ["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"]
};
this.options = {
sixWeeksPerMonth : options.sixWeeksPerMonth || false,
weekStartDay : (options.weekStartDay && options.weekStartDay < 7)?options.weekStartDay:0,
actualDate : options.actualDate || new Date()
};
this.actualDate = this.options.actualDate;
this.utils = this.utils();
}
CalendarStructure.prototype.getWeeksArr = function(){
var date = new Date(this.actualDate.getTime()),
monthWeeks = [],
self = this;
date.setDate(1);
for(var i = 1; i<=6; i++){
monthWeeks.push(self.getDaysArr(date));
date.setDate(date.getDate()+7);
}
if(!self.options.sixWeeksPerMonth && (monthWeeks[1][0].monthNumber != monthWeeks[5][0].monthNumber)){
monthWeeks.pop();
}
return monthWeeks;
}
CalendarStructure.prototype.getDaysArr = function(date){
var weekDate = (date && (date.constructor == new Date().constructor))?new Date(date.getTime()):new Date(this.actualDate.getTime()),
daysToWeekStart = this.utils.daysToWeekStart(weekDate),
weekArr = [];
weekDate.setHours(24*daysToWeekStart)
for(var i = 0; i<7; i++){
weekArr[i] = this.utils.buildDayObj(weekDate);
weekDate.setHours(+24);
}
return weekArr;
}
CalendarStructure.prototype.utils = function(){
var self = this,
daysToWeekStart = function(weekDate){
var options = self.options,
diff = options.weekStartDay - weekDate.getDay(),
result = (options.weekStartDay > weekDate.getDay())?(diff-7):diff;
return result;
},
buildDayObj = function(date){
return {
dayText : self.languageData.days[date.getDay()],
dayNumber : date.getDate(),
monthText : self.languageData.months[date.getMonth()],
monthNumber : date.getMonth(),
year : date.getFullYear()
};
},
updateDate = function(difference, type){
var difference = difference || 0,
isInt = difference % 1 === 0,
types = {
day : {set:"setDate", get:"getDate", multiplier:1},
week : {set:"setDate", get:"getDate", multiplier:7},
month : {set:"setMonth", get:"getMonth", multiplier:1},
year : {set:"setFullYear", get:"getFullYear", multiplier:1}
},
type = types[type] || false;
if(difference!=0 && isInt && type){
self.actualDate[type.set](self.actualDate[type.get]()+(difference*type.multiplier));
}
};
return {
daysToWeekStart : daysToWeekStart,
buildDayObj : buildDayObj,
updateDate : updateDate
};
}
/*
Set the actual date to another week
@params: difference {Integer 1...n} difference in days to set (could be negative)
*/
CalendarStructure.prototype.setOtherDay = function(difference){
this.utils.updateDate(difference, "day");
}
/*
Set the actual date to another week
@params: difference {Integer 1...n} difference in weeks to set (could be negative)
*/
CalendarStructure.prototype.setOtherWeek = function(difference){
this.utils.updateDate(difference, "week");
}
/*
Set the actual date to another month
@params: difference {Integer 1...n} difference in months to set (could be negative)
*/
CalendarStructure.prototype.setOtherMonth = function(difference){
this.utils.updateDate(difference, "month");
}
/*
Set the actual date to another year
@params: difference {Integer 1...n} difference in years to set (could be negative)
*/
CalendarStructure.prototype.setOtherYear = function(difference){
this.utils.updateDate(difference, "year");
}