Skip to content
This repository was archived by the owner on Mar 4, 2023. It is now read-only.

Commit 372bb71

Browse files
author
evombau
committed
zwischenstand
1 parent e768bcb commit 372bb71

File tree

2 files changed

+97
-92
lines changed

2 files changed

+97
-92
lines changed

projects/angular-calendar/src/lib/calendar-panels/calendar-panels.component.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,13 @@ interface Config {
88
panelColor?: string;
99
holidayBgColor?: string;
1010
holidayColor?: string;
11-
}
12-
interface Display {
13-
year: number;
14-
month: number;
15-
before?: number;
16-
after?: number;
1711
displayYear: boolean;
1812
switches: boolean;
1913
}
2014

2115
@Component({
2216
selector: 'calendar-panels',
23-
inputs: ['placeholderDay', 'config', 'display'],
17+
inputs: ['mode', 'placeholderDay', 'config', 'year', 'month', 'monthsBefore', 'monthsAfter'],
2418
templateUrl: './calendar-panels.component.html',
2519
styleUrls: ['./calendar-panels.component.scss']
2620
})
@@ -32,14 +26,18 @@ export class CalendarPanelsComponent implements OnInit {
3226
panelBgColor: '#00677f',
3327
textColor: 'white',
3428
holidayBgColor: 'rgb(0, 103, 127)',
35-
holidayColor: 'rgb(0, 103, 127)'
36-
}
37-
display: Display = {
38-
year: 2020,
39-
month: 3,
40-
displayYear: true,
41-
switches: true
29+
holidayColor: 'rgb(0, 103, 127)',
30+
displayYear: false,
31+
switches: false
4232
}
33+
mode = 'monthly' // monthly | annual
34+
35+
year = new Date().getFullYear()
36+
month = new Date().getUTCMonth()
37+
monthsBefore = 0
38+
monthsAfter = 0
39+
calendar = null
40+
weekdayNames = ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So']
4341

4442

4543
@HostBinding("style.--panel-color")
@@ -56,15 +54,19 @@ export class CalendarPanelsComponent implements OnInit {
5654
private panelColorHoliday = this.config.holidayColor;
5755

5856
constructor(private calendarService: CalendarService) {
59-
this.panelBgColor = this.config.panelBgColor
57+
this.panelBgColor = this.config.panelBgColor;
58+
console.log(this.mode)
59+
if (this.mode === 'annual') {
60+
this.calendar = this.calendarService.generateMatrix(null, this.year)
61+
} else if (this.mode === 'monthly') {
62+
console.log(this.month)
63+
this.calendar = this.calendarService.generateMatrix(null, this.year, this.month, this.monthsBefore, this.monthsAfter)
64+
}
6065
}
6166

62-
calendar = null
63-
weekdayNames = ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So']
64-
6567
ngOnInit() {
6668
console.log(this.placeholderDay)
67-
this.calendar = this.calendarService.generateMatrix(2020, this.placeholderDay)
69+
this.calendar = this.calendarService.generateMatrix(null, 2020)
6870
console.log(this.calendar)
6971
this.isLoading = false
7072
}

projects/angular-calendar/src/lib/service/calendar.service.ts

Lines changed: 76 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -40,58 +40,33 @@ export class CalendarService {
4040
// weekdayNames = this.weekdaysShort.push(this.weekdaysShort.shift());
4141
monthNames = this.momentLoc.monthsShort()
4242

43-
makeWJObjekt(day: Day, type: string, dateBack?: number) {
44-
let newDay = null
45-
if (dateBack) {
46-
newDay = moment(day.date).subtract(dateBack, 'days')
47-
} else {
48-
newDay = moment(day.date)
49-
}
50-
let result
51-
switch (type) {
52-
case 'kw':
53-
result = {
54-
kw: day.kw,
55-
type: 'kw'
56-
}
57-
break;
58-
case 'leer':
59-
result = {
60-
kw: newDay.week(),
61-
type: 'leer',
62-
day: newDay.date(),
63-
isWeekendDay: business.isWeekendDay(moment(newDay)),
64-
isHoliday: moment(newDay).isHoliday([])['allStates']
65-
}
66-
break;
67-
case 'placeholderDay':
68-
result = {
69-
kw: newDay.week(),
70-
type: 'placeholderDay',
71-
day: newDay.date(),
72-
isWeekendDay: business.isWeekendDay(moment(newDay)),
73-
isHoliday: moment(newDay).isHoliday([])['allStates']
74-
}
75-
break;
76-
}
77-
return result
78-
}
7943

8044
/**
81-
* @param {Calendar} calendar Monat von 1 bis 12
45+
* @param {Calendar} calendar Custom data, (optinal)
46+
* @param {Number} year Gerarate calender for one year, (optinal)
47+
* @param {Number} currMonth current selected month, (optinal)
48+
* @param {Number} monthsBefore months before the selected month, (optinal) default 0
49+
* @param {Number} monthsAfter months after the selected month, (optinal) default 0
8250
*/
83-
generateMatrix(year: number, placeholderDay?: boolean, calendar?: Calendar) {
84-
console.log(placeholderDay)
85-
let emptyOrPlaceholder = 'placeholderDay'
86-
if (placeholderDay === true) {
87-
emptyOrPlaceholder = 'placeholderDay'
88-
}
89-
console.log(emptyOrPlaceholder)
90-
51+
generateMatrix(calendar?: Calendar, year?: number, currMonth?: number, monthsBefore?: number, monthsAfter?: number) {
52+
// is a current month selected?
9153
let cal = calendar;
92-
if (!calendar) {
93-
cal = this.generateCal(year)
54+
if (currMonth > 0) {
55+
const months: Month[] = []
56+
months.push(this.generateMonth(currMonth, year))
57+
if (!calendar) {
58+
cal = {
59+
months: months,
60+
year: null
61+
}
62+
}
63+
} else {
64+
// Custom calendar data?
65+
if (!calendar) {
66+
cal = this.generateCal(year)
67+
}
9468
}
69+
console.log(cal)
9570

9671
cal.months.forEach((month, index) => {
9772
month.days.forEach(day => {
@@ -111,11 +86,11 @@ export class CalendarService {
11186
}
11287
// Vormonat
11388
for (let i = 0; i < dayOfWeek - 1; i++) {
114-
render.splice(i, 0, this.makeWJObjekt(firstMonthDay, emptyOrPlaceholder, (dayOfWeek - 1) - i));
89+
render.splice(i, 0, this.makeWJObjekt(firstMonthDay, 'placeholderDay', (dayOfWeek - 1) - i));
11590
}
11691
// Nachmonat
11792
for (let i = 0; render.length < 42; i--) {
118-
render.splice(render.length + 1, 0, this.makeWJObjekt(nextMonthDay, emptyOrPlaceholder, i));
93+
render.splice(render.length + 1, 0, this.makeWJObjekt(nextMonthDay, 'placeholderDay', i));
11994
}
12095
// Kalenderwochen
12196
render.splice(0, 0, this.makeWJObjekt(render[0], "kw"));
@@ -131,39 +106,67 @@ export class CalendarService {
131106
return cal;
132107
}
133108

109+
makeWJObjekt(day: Day, type: string, dateBack?: number) {
110+
let newDay = null
111+
if (dateBack) {
112+
newDay = moment(day.date).subtract(dateBack, 'days')
113+
} else {
114+
newDay = moment(day.date)
115+
}
116+
let result
117+
switch (type) {
118+
case 'kw':
119+
result = {
120+
kw: day.kw,
121+
type: 'kw'
122+
}
123+
break;
124+
case 'placeholderDay':
125+
result = {
126+
kw: newDay.week(),
127+
type: 'placeholderDay',
128+
day: newDay.date(),
129+
isWeekendDay: business.isWeekendDay(moment(newDay)),
130+
isHoliday: moment(newDay).isHoliday([])['allStates']
131+
}
132+
break;
133+
}
134+
return result
135+
}
136+
134137
generateCal(year: number): Calendar {
138+
//if ()
139+
const months = []
140+
for (let index = 0; index < this.monthNames.length; index++) {
141+
months.push(this.generateMonth(index, year))
142+
}
135143
return {
136144
year: year,
137-
months: this.generateMonth(year)
145+
months: months
138146
}
139147
}
140148

141-
generateMonth(year) {
142-
console.log(this.momentLoc)
143-
const calendar: Month[] = []
144-
for (let index = 0; index < this.monthNames.length; index++) {
145-
calendar.push({
146-
name: this.monthNames[index],
147-
days: this.generateDay(index, year)
148-
})
149+
generateMonth(monthNumber, year) {
150+
const daysInMonth = moment(`${year}-${monthNumber + 1}`, "YYYY-MM").daysInMonth()
151+
const days: Day[] = [];
152+
for (let index = 0; index < daysInMonth; index++) {
153+
const currentDay = new Date(year, monthNumber, (index + 1))
154+
days.push(this.generateDay(currentDay))
155+
}
156+
return {
157+
name: this.monthNames[monthNumber],
158+
year: year,
159+
days: days
149160
}
150-
151-
return calendar
152161
}
153162

154-
generateDay(monthNumber: number, yearNumber: number) {
155-
const daysInMonth = moment(`${yearNumber}-${monthNumber + 1}`, "YYYY-MM").daysInMonth()
156-
const month: Day[] = [];
157-
for (let index = 0; index < daysInMonth; index++) {
158-
const curDay = new Date(yearNumber, monthNumber, (index + 1))
159-
month.push({
160-
kw: moment(curDay).week(),
161-
day: curDay.getDate(),
162-
date: curDay,
163-
isWeekendDay: business.isWeekendDay(moment(curDay)),
164-
isHoliday: moment(curDay).isHoliday([])['allStates']
165-
})
163+
generateDay(currentDay): Day {
164+
return {
165+
kw: moment(currentDay).week(),
166+
day: currentDay.getDate(),
167+
date: currentDay,
168+
isWeekendDay: business.isWeekendDay(moment(currentDay)),
169+
isHoliday: moment(currentDay).isHoliday([])['allStates']
166170
}
167-
return month
168171
}
169172
}

0 commit comments

Comments
 (0)