-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalenday.ts
225 lines (177 loc) · 6.26 KB
/
calenday.ts
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
//---------------------
// MAIN \
////////////////////////
function calenday(format: string, id: string, date: string= today()): number{
// Check the inputs passed arguments
if (formats.indexOf(format) == -1) {
return 1;
}
if (typeof date != 'string' || date.length > 10) {
return 2;
}
if (typeof id != 'string') {
return 3
}
//Select the wrapper from the HTML
const wrapper: HTMLElement = document.getElementById(id)
// Create a JS date and set to GMT x
let d: Date = new Date(`${date} GMT${timeDiff}`)
// Gets the required elements for following loop
let firstDstr: string = getFirstDay(d, format);
let lastDstr: string = getLastDay(d,format)
let currentD: Date = new Date(firstDstr)
wrapper.append(getMonthDiv(d.getMonth()))
wrapper.append(getLabels(format))
// Create a div for each day
do {
const w: HTMLElement = crtDiv()
w.classList.add('cl_week')
if (format === 'week') {w.append(getHoursLabels(hours[0], hours[1]))}
for (let i = 0; i < 7; i++) {
let singleWeek = (format === 'week') ? true : false;
let fillUp = currentD.getMonth() === d.getMonth() ? false : true;
let weekend = currentD.getDay() == 0 || currentD.getDay() == 6 ? true : false;
let div = getDayDiv(currentD, singleWeek, fillUp, weekend)
w.append(div)
currentD.setDate(currentD.getDate() + 1)
}
wrapper.append(w)
} while (currentD <= new Date(lastDstr));
return 0;
}
//---------------------------------------
// 2 - LIBRARY
//---------------------------------------
// 2.1 - Variables and const
//-----------------------------
const timeDiff: string = '00:00';
const hours: Array<number> = [8, 17];
const formats: Array<string> = ['week', 'month'];
const labels: Array<string> = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
const months: Array<string> = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'November', 'October', 'December'];
//-----------------------------
// 2.2 - Shorthands
//-----------------------------
const crtDiv = (): HTMLElement => document.createElement('div');
const dateTS = (date: Date, gmt: boolean = true): string => {
let month = date.getMonth() + 1;
let dateTS = `${month}/${date.getDate().toString()}/${date.getFullYear().toString()}`;
if (gmt) dateTS += ` GMT${timeDiff}`
return dateTS;
}
const today = ():string => {
let date = new Date();
let tmp = dateTS(date);
return tmp.slice(0, -8)
}
//-----------------------------
// 2.3 - Funtions
//-----------------------------
// Create the labels from the array of labels
function getLabels(format: string): HTMLElement {
const ls: HTMLElement = crtDiv();
ls.classList.add('cl_labels')
if (format == 'week') {
const s: HTMLElement = crtDiv();
s.classList.add('cl_H-label', 'cl_H-labels_shift');
ls.append(s);
}
labels.forEach(label => {
const l: HTMLElement = crtDiv();
l.classList.add('cl_label');
l.textContent = label;
ls.append(l);
})
return ls;
}
function getHoursLabels(start: number, end: number): HTMLElement {
const hs: HTMLElement = crtDiv();
const th: HTMLElement = crtDiv();
hs.classList.add('cl_H-labels');
th.classList.add('cl_H-header');
th.textContent = 'Hours';
hs.append(th)
for (let i = start; i < end; i++) {
const h: HTMLElement = crtDiv();
h.classList.add('cl_H-label');
h.textContent = `${i}:00`
hs.append(h)
}
return hs;
}
// Create the div from th current month
function getMonthDiv(month: number): HTMLElement {
const m: HTMLElement = crtDiv();
m.classList.add('cl_month', month.toString())
m.textContent = months[month]
return m;
}
// Create a the div from one day
function getDayDiv(date: Date, singleWeek: boolean, fillUp: boolean, weekend: boolean): HTMLElement {
const day = date.getDate()
const dataDate = dateTS(date);
const d: HTMLElement = crtDiv();
const t: HTMLElement = crtDiv();
const c: HTMLElement = crtDiv();
d.classList.add('cl_day');
t.classList.add('cl_day-header');
c.classList.add('cl_day-content');
d.dataset.date = dataDate.slice(0, -8);
// Special classes
if (fillUp) d.classList.add('cl_day__dif')
if (weekend) d.classList.add('cl_day__weekend')
t.textContent = day.toString();
if (singleWeek) { // Generates the different hours
for (let i = hours[0]; i < hours[1]; i++) {
const h: HTMLElement = crtDiv();
h.classList.add('cl_hour');
c.append(h);
}
}
d.append(t, c);
return d;
}
// Calculate the first day of the first week from the current month if format == 'month' otherwise the first day of the week
function getFirstDay(date: Date, format: string): string {
let tmp = date;
if (format === 'month') {
let firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
tmp = firstDay
}
let diff = tmp.getDate() - tmp.getDay() + (tmp.getDay() === 0 ? -6 : 1);
let result = new Date(dateTS(date));
result.setDate(diff)
return dateTS(result);
}
// Calculate the last day of the last week from the current month if format == 'month' otherwise the last day of the week
function getLastDay(date: Date, format: string) {
let tmp = date
if (format === 'month') {
let lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
tmp = new Date(dateTS(lastDay))
}
let result = new Date(getFirstDay(tmp, 'week'))
result.setDate(result.getDate() + 6)
return dateTS(result);
}
const updateMonth = (initial: Date, side: string): string => {
let month: number = initial.getMonth() + 1;
let year: number = initial.getFullYear()
if (side === 'up'){
if (month === 12){
month = 1;
year += 1;
} else {
month += 1;
}
}
if (side === 'down'){
if (month === 1){
month = 12;
year -= 1;
} else {
month -= 1;
}
}
return `${month}/1/${year}`;
}