@@ -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