Skip to content

Instantly share code, notes, and snippets.

@skamenetskiy
Last active August 29, 2015 14:12
Show Gist options
  • Save skamenetskiy/c9f828e06613cdeb25fb to your computer and use it in GitHub Desktop.
Save skamenetskiy/c9f828e06613cdeb25fb to your computer and use it in GitHub Desktop.
(function (Window, Document, DateObject, ArrayObject, append, d28, d29, d30, d31, undefined) {
var _date = new DateObject(),
_daysInMonth = [d31, d28, d31, d30, d31, d30, d31, d31, d30, d31, d30, d31];
/**
* HTMLElement factory
* @param tag
* @param [attributes]
* @returns {HTMLElement}
* @private
*/
function _element(tag, attributes) {
var element = Document.createElement(tag);
if ('object' === typeof(attributes)) {
for (var i in attributes) {
if (attributes.hasOwnProperty(i)) {
element[i] = attributes[i]
}
}
}
return element
}
/**
* DOM append helper function
* @private
*/
function _append() {
var i, where = arguments[0],
what = ArrayObject.prototype.splice(arguments, 1);
for (i = 0; i < what.length; i++) {
where[append](what[i])
}
}
/**
* Main calendar class
* @param month
* @param year
* @constructor
*/
function Calendar(month, year) {
var _month = undefined !== month ? month : _date.getMonth(),
_year = undefined !== year ? year : _date.getFullYear(),
_class = 'calendar',
_days = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс'],
_months = ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'],
_firstDayOfWeek = 0;
/**
* Sets the month
* @param month
* @returns {Calendar}
* @expose
*/
this.setMonth = function (month) {
_month = month;
return this
};
/**
* Sets months names
* @param months
* @returns {Calendar}
*/
this.setMoths = function (months) {
if (months instanceof ArrayObject && months.length == 12) {
_months = months
}
return this
};
/**
* Sets days names
* @param days
* @returns {Calendar}
*/
this.setDays = function (days) {
if (days instanceof ArrayObject && days.length == 7) {
_days = days
}
return this
};
/**
* Sets the year
* @param year
* @returns {Calendar}
* @expose
*/
this.setYear = function (year) {
_year = year;
return this;
};
/**
* Sets the first day of week
* @param day
* @returns {Calendar}
*/
this.setFirstWeekDay = function (day) {
_firstDayOfWeek = day;
return this
};
/**
* Sets table class name
* @param className
* @returns {Calendar}
*/
this.setClass = function (className) {
_class = className;
return this
};
/**
* Renders the calendar table
* @returns {HTMLElement}
*/
this.render = function () {
var table = _element('table', {className: _class}),
firstDay = new DateObject(_year, _month, _firstDayOfWeek),
startingDay = firstDay.getDay(),
monthLength = _daysInMonth[_month],
monthName = _months[_month];
if (_month == 1) {
if ((_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0) {
monthLength = d29
}
}
var thead = _element('thead'),
tbody = _element('tbody');
table[append](thead);
table[append](tbody);
(function (_tr, _th, _td) {
(function () {
var tr1 = _element(_tr), tr2 = _element(_tr);
tr1[append](_element(_th, {colSpan: '7', innerHTML: monthName + ' ' + _year}));
thead[append](tr1);
thead[append](tr2);
for (var i = 0; i <= 6; i++) {
tr2[append](_element(_th, {innerHTML: _days[i]}))
}
})();
(function () {
var day = 1,
tr = _element(_tr);
for (var i = 0; i < 9; i++) {
for (var j = 0; j <= 6; j++) {
var td = _element(_td);
if (day <= monthLength && (i > 0 || j >= startingDay)) {
td.innerHTML = day;
day++
}
tr[append](td)
}
if (day > monthLength) {
tbody[append](tr);
break;
} else {
tbody[append](tr);
tr = _element(_tr)
}
}
})()
})('tr', 'th', 'td');
return table
};
}
/**
* Global Calendar factory
* @param month
* @param year
* @returns {Calendar}
* @constructor
*/
Window.Calendar = function (month, year) {
return new Calendar(month, year)
};
/**
* Extending global Date object to allow direct calls. Returns Calendar object.
* @returns {Calendar}
* @expose
*/
DateObject.prototype.getCalendar = function () {
return new Calendar(this.getMonth(), this.getFullYear())
};
/**
* Extending global Date object to allow direct calls. Returns calendar table.
* @returns {HTMLElement}
* @expose
*/
DateObject.prototype.getCalendarTable = function () {
return this.getCalendar().render()
};
})(window, document, Date, Array, 'appendChild', 28, 29, 30, 31);
(function(u,v,h,k,c,w,x,f,b,p){function g(g,c){var d=v.createElement(g);if("object"===typeof c)for(var e in c)c.hasOwnProperty(e)&&(d[e]=c[e]);return d}function l(b,f){var d=p!==b?b:q.getMonth(),e=p!==f?f:q.getFullYear(),l="calendar",r="\u041f\u043d \u0412\u0442 \u0421\u0440 \u0427\u0442 \u041f\u0442 \u0421\u0431 \u0412\u0441".split(" "),s="\u042f\u043d\u0432\u0430\u0440\u044c \u0424\u0435\u0432\u0440\u0430\u043b\u044c \u041c\u0430\u0440\u0442 \u0410\u043f\u0440\u0435\u043b\u044c \u041c\u0430\u0439 \u0418\u044e\u043d\u044c \u0418\u044e\u043b\u044c \u0410\u0432\u0433\u0443\u0441\u0442 \u0421\u0435\u043d\u0442\u044f\u0431\u0440\u044c \u041e\u043a\u0442\u044f\u0431\u0440\u044c \u041d\u043e\u044f\u0431\u0440\u044c \u0414\u0435\u043a\u0430\u0431\u0440\u044c".split(" "),
t=0;this.setMonth=function(a){d=a;return this};this.setMoths=function(a){a instanceof k&&12==a.length&&(s=a);return this};this.setDays=function(a){a instanceof k&&7==a.length&&(r=a);return this};this.setYear=function(a){e=a;return this};this.setFirstWeekDay=function(a){t=a;return this};this.setClass=function(a){l=a;return this};this.render=function(){var a=g("table",{className:l}),b=(new h(e,d,t)).getDay(),f=y[d],k=s[d];1==d&&(0==e%4&&0!=e%100||0==e%400)&&(f=x);var m=g("thead"),n=g("tbody");a[c](m);
a[c](n);(function(a,d,l){(function(){var b=g(a),f=g(a);b[c](g(d,{colSpan:"7",innerHTML:k+" "+e}));m[c](b);m[c](f);for(b=0;6>=b;b++)f[c](g(d,{innerHTML:r[b]}))})();(function(){for(var d=1,e=g(a),h=0;9>h;h++){for(var k=0;6>=k;k++){var m=g(l);d<=f&&(0<h||k>=b)&&(m.innerHTML=d,d++);e[c](m)}if(d>f){n[c](e);break}else n[c](e),e=g(a)}})()})("tr","th","td");return a}}var q=new h,y=[b,w,b,f,b,f,b,b,f,b,f,b];u.Calendar=function(b,c){return new l(b,c)};h.prototype.getCalendar=function(){return new l(this.getMonth(),
this.getFullYear())};h.prototype.getCalendarTable=function(){return this.getCalendar().render()}})(window,document,Date,Array,"appendChild",28,29,30,31);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment