app/calendar-view/calendar-view.component.js
//Create a calendar JSON Object from a specific start date. Ideally, the start date would reflect the data.
Create a calendar JSON Object from a specific start date. Ideally, the start date would reflect the data.
Create a calendar JSON Object from a specific start date . Ideally the start date would reflect the data .
function createCalendarObjectStartingFrom(startDate, data){
						var calendar = {years:[]};
						var thisDate = new Date();
						var thisYear, thisMonth;
						var currentMonth = thisDate.getMonth();
						while(thisDate.getMonth() === currentMonth){
							thisDate.setDate(thisDate.getDate() + 1);
						}
						thisDate.setDate(thisDate.getDate() - 1);
						while(thisDate >= startDate){
							var yearObj, monthObj, dayObj;
							if(thisYear !== thisDate.getFullYear()){
								yearObj = getYearObj(thisDate);
								calendar.years.push(yearObj);
								thisYear = thisDate.getFullYear();
							}
							if(thisMonth !== thisDate.getMonth()){
								monthObj = getMonthObj(thisDate);
								yearObj.months.push(monthObj);
								thisMonth = thisDate.getMonth();
							}
							var dayObj = getDayObj(thisDate);
							if(typeof data[dayObj.localeDate] !== "undefined"){
								dayObj.coffees = data[dayObj.localeDate];
							}
							monthObj.days.push(dayObj);
							if(thisDate.getDate() === 1 && thisDate.getDay() !== 0){
								var weekDate = new Date(thisDate);
								weekDate.setDate(weekDate.getDate()-1);
								while(weekDate.getDay() !== 6){
									monthObj.days.push({iso:(weekDate.getDay()*-1),name:''}); 
									weekDate.setDate(weekDate.getDate()-1);
								}
							}
							thisDate.setDate(thisDate.getDate() - 1);
						}	
						return calendar;
					}
function createCalendarObjectStartingFrom ( startDate , data ) { var calendar = { years : [ ] } ; var thisDate = new Date ( ) ; var thisYear , thisMonth ; var currentMonth = thisDate . getMonth ( ) ; while ( thisDate . getMonth ( ) === currentMonth ) { thisDate . setDate ( thisDate . getDate ( ) + 1 ) ; } thisDate . setDate ( thisDate . getDate ( ) - 1 ) ; while ( thisDate >= startDate ) { var yearObj , monthObj , dayObj ; if ( thisYear !== thisDate . getFullYear ( ) ) { yearObj = getYearObj ( thisDate ) ; calendar . years . push ( yearObj ) ; thisYear = thisDate . getFullYear ( ) ; } if ( thisMonth !== thisDate . getMonth ( ) ) { monthObj = getMonthObj ( thisDate ) ; yearObj . months . push ( monthObj ) ; thisMonth = thisDate . getMonth ( ) ; } var dayObj = getDayObj ( thisDate ) ; if ( typeof data [ dayObj . localeDate ] !== "undefined" ) { dayObj . coffees = data [ dayObj . localeDate ] ; } monthObj . days . push ( dayObj ) ; if ( thisDate . getDate ( ) === 1 && thisDate . getDay ( ) !== 0 ) { var weekDate = new Date ( thisDate ) ; weekDate . setDate ( weekDate . getDate ( ) - 1 ) ; while ( weekDate . getDay ( ) !== 6 ) { monthObj . days . push ( { iso : ( weekDate . getDay ( ) * - 1 ) , name : '' } ) ; weekDate . setDate ( weekDate . getDate ( ) - 1 ) ; } } thisDate . setDate ( thisDate . getDate ( ) - 1 ) ; } return calendar ; }
Create a calendar JSON Object from a specific start date.
Create a calendar JSON Object from a specific start date .
//Loop through days to complete the month. //Once the next month is detected, the month is complete and we need to a walk back a day. //Loop backwards until we reach the the start date. //If the stored year does not match up with the dates year, create new year object, push that year object to the calendar, and update the stored year. //If the stored month does not match up with the dates month, create new month object, push that month object to the year object, and update the stored month. //Create new day object. //Check coffee data to see if an entry exists for this date. If so, add coffees to the day object. //Push the day object to the month object //Because calendars align columns to days of the week (e.g. Sunday, Monday, etc...) and also because months start on different days of the week, days need to be added to pad out the month/ //If this is the first day of the month and this isn't Sunday, loop backward adding zero/negative days to this month until Sunday is reached. // Add negative date for extraneous days. Extraneous days also don't have names. //Continue calendar loop.