-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHolWeekCalendar.js
328 lines (248 loc) · 9.46 KB
/
HolWeekCalendar.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
var app = angular.module("myApp", []);
app.service("GetData", function($q){
this.GetStartingDate = function(){
return $q.when(new Date(2018, 0, 5));
}
});
// test controller
app.controller("myController", function(GetData) {
var vm = this;
vm.selectedDate = "";
// testing date
GetData.GetStartingDate()
.then(function(data){
vm.myDate = data;
});
vm.dateChange = function(dt) {
vm.selectedDate = dt;
console.log("Date Model: " + vm.selectedDate);
}
vm.updateTest = function(){
vm.myDate = new Date(2018, 10, 4);
}
});
// our calendar component
app.directive('holCalendar', function() {
return {
restrict: 'E',
controller: ['$scope', HolidayCalendar],
templateUrl: 'HolidayCalendarTemplate.html',
bindToController: true,
controllerAs: 'vm',
scope: {
returnedDay: '@',
onChange: '&',
initialDate: '='
}
};
});
function HolidayCalendar($scope) {
var vm = this;
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"];
vm.cal = createArray(6, 7);
vm.weeks = createArray(6);
// init the calendar to start with
init();
// ===================
// class methods below
// ===================
function createArray(length) {
var arr = new Array(length || 0),
i = length;
arr.fill("");
if (arguments.length > 1) {
var args = Array.prototype.slice.call(arguments, 1);
while (i--) arr[length - 1 - i] = createArray.apply(this, args);
}
return arr;
}
function getWeekNbr(year, month, day) {
function serial(days) {
return 86400000 * days;
}
function dateserial(year, month, day) {
return (new Date(year, month - 1, day).valueOf());
}
function weekday(date) {
return (new Date(date)).getDay() + 1;
}
function yearserial(date) {
return (new Date(date)).getFullYear();
}
var date = year instanceof Date ? year.valueOf() : typeof year === "string" ? new Date(year).valueOf() : dateserial(year, month, day),
date2 = dateserial(yearserial(date - serial(weekday(date - serial(1))) + serial(4)), 1, 3);
return ~~((date - date2 + serial(weekday(date2) + 5)) / serial(7));
}
function daysInMonth(month, year) {
return new Date(year, month, 0).getDate();
}
function getDateOfWeek(weekNumber, year) {
//Create a date object starting january first of chosen year, plus the number of days in a week multiplied by the week number to get the right date.
return new Date(year, 0, 1 + ((weekNumber - 1) * 7));
}
function addDays(date, days) {
var result = new Date(date);
result.setDate(result.getDate() + days);
return result;
}
function clearCal() {
for (var week = 0; week < 6; week++) {
for (var day = 0; day < 7; day++) {
vm.cal[week][day] = "";
}
}
}
function clearWeeks() {
for (var week = 0; week < 6; week++) {
vm.weeks[week] = "";
}
}
function fillOutCurrentMonth(year, month) {
var date = new Date(year, month, 1);
clearCal();
clearWeeks();
// get the first day and the total number of days in this month
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1).getDay();
var totalDaysInMonth = daysInMonth(vm.currentMonthNbr + 1, vm.currentYear);
// adjust for starting on monday vs sunday (js starts the week on sunday)
if (firstDay === 0)
firstDay = 6;
else
firstDay--;
var dayCount = 1;
var weekIndex = 0;
// 1st week is special and we need to know what day of the week the 1st is on
for (var day = 0; day < 7; day++) {
if (day >= firstDay) {
vm.cal[0][day] = dayCount;
// handle filling in week nbr for the first week if it starts on a monday for this month/year
if (day === 0) { // 0 = Monday
// even though we are checking for monday we need to add 1 day to it to make it a Tue in order for the getWeekNbr() to correctly handle DST
vm.weeks[weekIndex] = getWeekNbr(vm.currentYear, vm.currentMonthNbr + 1, dayCount + 1);
}
dayCount++;
}
}
weekIndex++;
// fill in the rest of the weeks
for (var week = 1; week < 6; week++) {
for (day = 0; day < 7; day++) {
vm.cal[week][day] = dayCount;
// handle filling the week number
if (day === 0) { // 0 = Monday
// even though we are checking for monday we need to add 1 day to it to make it a Tue in order for the getWeekNbr() to correctly handle DST
vm.weeks[weekIndex] = getWeekNbr(vm.currentYear, vm.currentMonthNbr + 1, dayCount + 1);
weekIndex++;
}
dayCount++;
if (dayCount > totalDaysInMonth)
return;
}
}
}
function init() {
// init to todays month/year
var date = new Date();
// if there is an initial date then fill it out
if (vm.initialDate !== undefined){
date = vm.initialDate;
}
vm.selectedWeekIndex = -1;
// when there is a highlighted week already but the user is navigating to other month/years
vm.savedWeekIndex = -1;
vm.savedWeekMonth = "";
vm.savedWeekYear = 0;
vm.showCal = false;
var weekNbr = getWeekNbr(date.getFullYear(), date.getMonth() + 1, ((date.getDay() === 0) ? date.getDate() : date.getDate() + 1));
var monday = getDateOfWeek(weekNbr, date.getFullYear());
vm.currentMonthNbr = monday.getMonth();
vm.currentMonth = monthNames[vm.currentMonthNbr];
vm.currentYear = monday.getFullYear();
fillOutCurrentMonth(vm.currentYear, vm.currentMonthNbr);
// vm.weeks array is filled in now with each week number. get the index of the matching number and that is our seleceted index
vm.selectedWeekIndex = vm.weeks.indexOf(weekNbr);
// set the text of the textbox
vm.week = "Week " + weekNbr + ", " + vm.currentYear;
//console.log("Week Nbr From " + (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear());
//console.log("Week Nbr =" + weekNbr);
//console.log("Monday " + monday);
}
// when the initialValue variable changes we need to update our calendar
$scope.$watch(function(){ return vm.initialDate; }, function(oldValue, newValue) {
if (oldValue === newValue)
return;
init();
})
vm.onClick = function(event) {
// when we click the input box save off the results
vm.savedWeekIndex = vm.selectedWeekIndex;
vm.savedWeekMonth = vm.currentMonth;
vm.savedWeekYear = vm.currentYear;
vm.showCal = true;
};
vm.close = function() {
vm.showCal = false;
}
// clicking the week is the driving force of this control
vm.weekClick = function(index) {
vm.selectedWeekIndex = index; // this is for highlighting the week in the calendar html
vm.selectedWeek = vm.weeks[vm.selectedWeekIndex]; // this sets the week number
// calculate the monday of this week
var mondayOfWeek = getDateOfWeek(vm.selectedWeek, vm.currentYear);
// get the day the user said they wanted in this week
var finalDate = addDays(mondayOfWeek, days.indexOf(vm.returnedDay.toLowerCase()));
vm.week = "Week " + vm.selectedWeek + ", " + vm.currentYear;
vm.showCal = false;
// raise our event
vm.onChange({
dt: finalDate
});
};
vm.lowerMonth = function() {
vm.currentMonthNbr--;
if (vm.currentMonthNbr < 0)
vm.currentMonthNbr = 11;
vm.currentMonth = monthNames[vm.currentMonthNbr];
// restore the selected week as we're navigating between month/years
if (vm.currentMonth === vm.savedWeekMonth && vm.savedWeekYear === vm.currentYear)
vm.selectedWeekIndex = vm.savedWeekIndex;
else
vm.selectedWeekIndex = -1;
fillOutCurrentMonth(vm.currentYear, vm.currentMonthNbr);
};
vm.higherMonth = function() {
vm.currentMonthNbr++;
if (vm.currentMonthNbr > 11)
vm.currentMonthNbr = 0;
vm.currentMonth = monthNames[vm.currentMonthNbr];
// restore the selected week as we're navigating between month/years
if (vm.currentMonth === vm.savedWeekMonth && vm.savedWeekYear === vm.currentYear)
vm.selectedWeekIndex = vm.savedWeekIndex;
else
vm.selectedWeekIndex = -1;
fillOutCurrentMonth(vm.currentYear, vm.currentMonthNbr);
};
vm.lowerYear = function() {
vm.currentYear--;
vm.currentMonth = monthNames[vm.currentMonthNbr];
// restore the selected week as we're navigating between years
if (vm.currentMonth === vm.savedWeekMonth && vm.savedWeekYear === vm.currentYear)
vm.selectedWeekIndex = vm.savedWeekIndex;
else
vm.selectedWeekIndex = -1;
fillOutCurrentMonth(vm.currentYear, vm.currentMonthNbr);
};
vm.higherYear = function() {
vm.currentYear++;
vm.currentMonth = monthNames[vm.currentMonthNbr];
// restore the selected week as we're navigating between years
if (vm.currentMonth === vm.savedWeekMonth && vm.savedWeekYear === vm.currentYear)
vm.selectedWeekIndex = vm.savedWeekIndex;
else
vm.selectedWeekIndex = -1;
fillOutCurrentMonth(vm.currentYear, vm.currentMonthNbr);
};
}