Last active
August 29, 2015 14:05
-
-
Save colintoh/ec21f6d792ee770c690c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Predefined Settings | |
*/ | |
var model = { | |
daysObj: { | |
0: [0,1,2,3,4,5,6], | |
1: [1,2,3,4,5,6,0], | |
2: [2,3,4,5,6,0,1], | |
3: [3,4,5,6,0,1,2], | |
4: [4,5,6,0,1,2,3], | |
5: [5,6,0,1,2,3,4], | |
6: [6,0,1,2,3,4,5] | |
}, | |
daysName: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'], | |
monthName: ['January','February','March', 'April', 'May', 'June', 'July','August', 'September', 'October', 'November', 'December'] | |
}; | |
/* | |
* Util Modules | |
*/ | |
var util = { | |
generateDate: function(year,month, outDay,cb){ | |
return function(day){ | |
day = day || outDay; | |
var date = new Date(year, month, day); | |
return cb(date); | |
} | |
}, | |
render: function(month,year,daysArr,monthArr){ | |
console.log(" "+model.monthName[month]+" "+year); | |
var subheader = daysArr.map(function(item){ | |
return model.daysName[item]; | |
}).join(" "); | |
console.log(subheader); | |
monthArr.forEach(function(arr){ | |
console.log(arr.join(" ")); | |
}); | |
}, | |
bufferArr: function(arr, startingIndex){ // Fill up the buffer space in arr | |
for(var j=0; j < startingIndex; j++){ | |
arr[j] = " "; | |
} | |
return arr; | |
}, | |
buildRest: function(arr, startingIndex, total){ // Build up the rest of the arr | |
for(var i = 1; i <= total; i++){ | |
if(i < 10){ | |
arr[startingIndex] = i+" "; | |
} else { | |
arr[startingIndex] = i; | |
} | |
startingIndex++; | |
} | |
return arr; | |
}, | |
buildNaiveArr: function(startingIndex,total){ //Build the entire naive array | |
return this.buildRest(this.bufferArr([],startingIndex),startingIndex,total); | |
} | |
}; | |
/* | |
* Calender | |
*/ | |
function calendar(year,month,day){ | |
day = day || 0; | |
var totalDays = util.generateDate(year,month,0,function(date){ | |
return date.getDate(); | |
}); | |
var today = util.generateDate(year,month-1,null,function(date){ | |
return date.getDay(); | |
}); | |
var daysArr = model.daysObj[day], | |
startingIndex = daysArr.indexOf(today(1)); | |
var arr = util.buildNaiveArr(startingIndex,totalDays()), | |
monthArr = []; | |
//Split naiveArray into weeks | |
while(arr.length>0){ | |
monthArr.push(arr.splice(0,7)); | |
} | |
util.render(month-1,year,daysArr,monthArr); | |
} | |
calendar(2014,2,4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment