(function() {
this.createCalendar = function(selector, option) {
var $this, add_eventhandler, add_schedule_data, breakpoint, class_name, create_table, define_table, extensions, extensions_name, id, labels, mode, month, update_daily_calendar, update_table, year;
if (option == null) {
option = {};
}
id = option.id != null ? option.id : option.id = 'calendar';
breakpoint = option.breakpoint != null ? option.breakpoint : option.breakpoint = 768;
mode = option.mode != null ? option.mode : option.mode = 'responsive';
extensions = option.extensions != null ? option.extensions : option.extensions = {};
extensions_name = extensions.join(' ');
labels = option.labels != null ? option.labels : option.labels = [];
class_name = 'basil-calendar';
// start written by GrooowthInc
year = moment(window.date).format('YYYY');
month = moment(window.date).format('MM');
// end written by GrooowthInc
$this = $(selector);
(create_table = function() {
var calendar_html;
calendar_html = "
SUN | MON | TUE | WED | THU | FRI | SAT |
";
return $this.append(calendar_html);
})();
(define_table = function() {
var $center, $left, $right, center, left, right;
$left = $("#" + id + " .controller .left");
$center = $("#" + id + " .controller .center");
$right = $("#" + id + " .controller .right");
if (extensions.indexOf('show-only-3-months')) {
left = moment(year + month, 'YYYYMM');
center = moment(year + month, 'YYYYMM').add(1, 'months');
right = moment(year + month, 'YYYYMM').add(2, 'months');
if (labels.month) {
$left.attr({
'data-label-url': labels.month[left.format('M') - 1].path.replace(/(.+)(\.png)$/, '$1-wh$2'),
'data-label-url-selected': labels.month[left.format('M') - 1].path.replace(/(.+)(\.png)$/, '$1-bk$2'),
'data-year-month': left.format('YYYYMM'),
'class': 'selected'
});
$left.css({
'background-image': "url(" + ($left.data('label-url-selected')) + ")"
});
$center.attr({
'data-label-url': labels.month[center.format('M') - 1].path.replace(/(.+)(\.png)$/, '$1-wh$2'),
'data-label-url-selected': labels.month[center.format('M') - 1].path.replace(/(.+)(\.png)$/, '$1-bk$2'),
'data-year-month': center.format('YYYYMM')
});
$center.css({
'background-image': "url(" + ($center.data('label-url')) + ")"
});
$right.attr({
'data-label-url': labels.month[right.format('M') - 1].path.replace(/(.+)(\.png)$/, '$1-wh$2'),
'data-label-url-selected': labels.month[right.format('M') - 1].path.replace(/(.+)(\.png)$/, '$1-bk$2'),
'data-year-month': right.format('YYYYMM')
});
return $right.css({
'background-image': "url(" + ($right.data('label-url')) + ")"
});
}
}
})();
(update_table = function() {
var $center, $left, $right, body_html, center, day, firstday_of_week, i, j, k, lastday, left, ref, right, today, year_month_day;
$left = $("#" + id + " .controller .left");
$center = $("#" + id + " .controller .center");
$right = $("#" + id + " .controller .right");
if (!extensions.indexOf('show-only-3-months')) {
left = year;
center = moment(month, 'MM').format('M');
right = moment(month, 'MM').locale('en').format('MMM').toLowerCase();
$left.html(left);
$center.html(center);
$right.html(right);
}
firstday_of_week = Number(moment("" + year + month, 'YYYYM').startOf('month').format('d'));
lastday = Number(moment("" + year + month, 'YYYYM').endOf('month').format('D')) + firstday_of_week;
body_html = '';
i = 0;
j = firstday_of_week;
while (i < firstday_of_week) {
i++;
body_html += ' | ';
}
while (i < lastday) {
if (j === 0 && i > 1) {
body_html += '
';
}
for (j = k = ref = j; ref <= 6 ? k <= 6 : k >= 6; j = ref <= 6 ? ++k : --k) {
i++;
day = i - firstday_of_week;
year_month_day = moment("" + year + month + day, 'YYYYMMD').format('YYYYMMDD');
if (JapaneseHolidays.isHoliday(moment(year_month_day, 'YYYYMMDD').toDate())) {
body_html += "";
} else {
body_html += ' | ';
}
if (i <= lastday) {
body_html += " " + day + " ";
}
body_html += ' | ';
}
body_html += '
';
j = 0;
}
$this.find("." + class_name + " table.calendar.monthly tbody").html(body_html);
if (moment().format('YYYYMM') === ("" + year + month)) {
today = moment().format('YYYYMMDD');
return $("#" + id + " table.calendar.monthly p.day[data-date='" + today + "']").parent().addClass('today');
}
})();
(add_eventhandler = function() {
var on_resize;
if (extensions.indexOf('show-only-3-months')) {
$(document).on('click', "#" + id + " .controller .items > div:not('.selected')", function() {
$(this).parent().children().removeClass('selected');
$(this).parent().children().each(function() {
return $(this).css({
'background-image': "url(" + ($(this).data('label-url')) + ")"
});
});
$(this).addClass('selected').css({
'background-image': "url(" + ($(this).data('label-url-selected')) + ")"
});
month = moment($(this).data('year-month'), 'YYYYMM').format('MM');
year = moment($(this).data('year-month'), 'YYYYMM').format('YYYY');
update_table();
add_schedule_data();
return update_daily_calendar();
});
} else {
$(document).on('click', "#" + id + " .controller .button", function() {
var date;
if ($(this).hasClass('last-month')) {
date = moment("" + year + month, 'YYYYMM').subtract(1, 'months');
} else if ($(this).hasClass('next-month')) {
date = moment("" + year + month, 'YYYYMM').add(1, 'months');
}
month = date.format('MM');
year = date.format('YYYY');
update_table();
add_schedule_data();
return update_daily_calendar();
});
}
if (mode.match(/^responsive$/)) {
(on_resize = function() {
if ($(window).width() >= breakpoint) {
return $this.find("." + class_name).addClass('pc-calendar').removeClass('mobile-calendar');
} else {
return $this.find("." + class_name).addClass('mobile-calendar').removeClass('pc-calendar');
}
})();
return $(window).on('resize', function() {
return on_resize();
});
} else if (mode.match(/^pc$/)) {
return $this.find("." + class_name).addClass('pc-calendar').removeClass('mobile-calendar');
} else if (mode.match(/^mobile$/)) {
return $this.find("." + class_name).addClass('mobile-calendar').removeClass('pc-calendar');
}
})();
(add_schedule_data = function() {
return $.ajax({
// start written by GrooowthInc
url: '/api/v1/terumaru-schedules',
// end written by GrooowthInc
type: 'GET',
dataType: 'HTML'
}).done(function(html) {
$('#main-container').append(html);
$this.prepend($('#schedule-data style'));
return $('#schedule-data .schedule-data').each(function() {
var $eventday, date, schedule;
schedule = $(this).data();
date = schedule.date;
$eventday = $("#" + id + " .calendar.monthly p.day[data-date='" + date + "']").parent();
$eventday.addClass('event-day');
if (moment(date, 'YYYYMMDD') < moment()) {
$eventday.addClass('past');
}
if (labels.eventday) {
$eventday.attr({
style: "background-image: url(" + labels.eventday[moment(month, 'MM').format('M') - 1].path + ")"
});
}
return $eventday.append($(this));
});
});
})();
return (update_daily_calendar = function() {
var $event_days, html;
html = "";
$event_days = $("#" + id + " .calendar.monthly td.event-day");
$event_days.each(function() {
var $event_day, date;
$event_day = $(this).clone();
date = moment($event_day.find('p.day').data('date'), 'YYYYMMDD').format('M月D日(ddd)');
$event_day.find('p.day').text(date);
return html += " " + ($event_day.prop('outerHTML')) + "
";
});
return $("#" + id + " .calendar.daily tbody").html(html);
})();
};
}).call(this);