/** * * jQuery calendar widget/large view plugin with Bootstrap compatibility * * * * **/ var oshimenId; var oshimenUrl; (function(factory) { if(typeof define === 'function' && define.amd) { define(['jquery'], factory); } else { factory(jQuery); } }(function($) { oshimenId = parseInt($("#oshimenTitleId").attr('data-oshimen_id')); oshimenUrl = $("#oshimenTitleId").attr('data-oshimen_image'); var defaults = { todayButton: false, // Show the button to reset to today's date? showInput: true, // Show input weekStart: 0, // Start day of the week. 0 is Sunday, 6 for Saturday, 1 for Monday (default) widget: true, cellRatio: 1, format: 'd/m/y', footer: true, dayHeader: true, mode: 'widget', // 'widget' (default), 'tiny', 'popup', 'page' animDuration: 200, transition: '', url: '', category: null, data: null, tableClasses: 'table table-condensed', hidden: true, setOnMonthChange: true, condensed: false }; var now = new Date(); var selectedDate, monthCursor; var category=null; var months = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']; var shortMonths = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; var shortDays = ['日', '月', '火', '水', '木', '金', '土']; var dayImage = ['day_sun', 'day_mon', 'day_tue', 'day_wed', 'day_thr', 'day_fri', 'day_sat']; var iconImage = ['icon_cd']; var monthEvent = null, monthEventArticle=null,oshimenMonthEvent=null; var iconSelectionCategory = 2; var response; var tableHeight; // Get the number of days in the current month (or next month if delta == 1, or prev month if delta == -1, etc) Date.prototype.daysInMonth = function(delta) { delta = delta === undefined ? 0 : delta; return new Date(this.getFullYear(), this.getMonth() + 1 + delta, 0).getDate(); } Date.prototype.isDay = function(day) { if(day === undefined) { day = new Date(); } return this.getFullYear()==day.getFullYear() && this.getMonth()==day.getMonth() && this.getDate()==day.getDate(); } Date.prototype.isValid = function() { return Object.prototype.toString.call(this) === "[object Date]" && !isNaN(this.getTime()); } $.fn.get_monthEvent = function(){ return monthEvent; }; $.fn.get_oshimenMonthEvent = function(){ return oshimenMonthEvent; }; $.fn.get_monthEventArticle = function(){ return monthEventArticle ; }; $.fn.main_calendar = function(method) { // Private methods var pMethods = { drawCalendar: function(selectedDateTemp, replace) { var options = $(this).data('options'); var inputDate; if(options['data']!=null && options['data']!=''){ inputDate = new Date(''+ options['data']); }else{ inputDate = now; } selectedDate = selectedDateTemp || inputDate; if(replace !== undefined && replace == true) { this.empty(); } pMethods.drawHeader(selectedDate, options).appendTo(this); var month = pMethods.drawMonth(selectedDate, options).addClass('current'); $('
').addClass('main_calendar-month').html(month).appendTo(this); $(this).data('main_calendar', true); $(this).data('date', selectedDate); $(this).data('element', this); return this; }, drawPopupCalendar: function(selectedDate, replace) { var options = $(this).data('options'); selectedDate = selectedDate || now; var container, calendar; if($(this).parent('.main_calendar-popup-wrapper').length) { container = $(this).parent(); calendar = $(this).parent().find('.main_calendar-popup'); calendar.empty(); } else { container = $('').addClass('main_calendar-popup-wrapper'); calendar = $('') .addClass('main_calendar main_calendar-popup') .width($(this).outerWidth(true)); $(this).wrap(container); } $(this).after(calendar); if(options.hidden) { calendar.hide(); } pMethods.drawHeader(selectedDate, options).appendTo(calendar); var month = pMethods .drawMonth(selectedDate, options) .addClass('current'); $('') .addClass('main_calendar-month') .html(month) .appendTo(calendar); calendar.data('main_calendar', true); calendar.data('date', selectedDate); calendar.data('element', this); calendar.data('options', options); $(this).parent().wrap($('').addClass('main_calendar-affix')); return this; }, drawHeader: function(date, options) { var header = $('').addClass('main_calendar-header'); var monthNames = options.shortMonths ? shortMonths : months; var lastMonth = date.getMonth()-1; var path= ''; if(lastMonth<0){ lastMonth =11; } var nextMonth = date.getMonth()+1; if(nextMonth>11){ nextMonth = 0; } if( getLocale() == 'en_US' ){ monthNames = shortMonths; path = '
';
}else {
path = '
';
}
$('
';
myTd.prepend(dateImage);
}
myTd
.addClass(cell.classes)
.appendTo(tr);
}
tr.appendTo(table);
}
var monthPar = date.getMonth() +1;
var yearPar = date.getFullYear();
setEvent(yearPar, monthPar);
return table;
},
drawFooter: function(date, options) {
var footer = $('').addClass('main_calendar-footer input-prepend');
if(options.footer == false) {
footer.hide();
}
if(options.todayButton) {
$('')
.text('Today')
.addClass('btn main_calendar-today')
.attr('type', 'button')
.appendTo(footer);
$('')
.addClass('next-month change-day btn')
.html('»')
.appendTo(footer);
}
if(options.showInput) {
$('')
.text(pMethods.formatDate(date, options))
.addClass('main_calendar-input uneditable-input span2')
.appendTo(footer);
}
$('')
.prop('type', 'hidden')
.val(parseInt(date.getTime() / 1000), 10)
.appendTo(footer);
return footer;
},
// Split out into helper object
formatDate: function(date, options) {
return options.format
.replace('d', ('0' + date.getDate()).substr(-2))
.replace('m', ('0' + (date.getMonth() + 1)).substr(-2))
.replace('y', date.getFullYear().toString().substr(-2))
.replace('Y', date.getFullYear());
}
};
var methods = {
init: function(settings) {
var options = $.extend({}, defaults, settings);
var sche_txt = '';
if( getLocale() == 'en_US' ){
sche_txt = 'Your Oshimen daily schedule';
}else{
sche_txt = '推しメンの日別スケジュール';
}
category = options['category'];
if(oshimenId>0){
$('#oshimenTitleId')
.addClass('scheduleTitle')
.html(sche_txt);
}
// Events
if(!$(document).data('main_calendar-events')) {
$(document)
.on('click.main_calendar', '.main_calendar .change-month', function(e) {// change month method
e.preventDefault();
e.stopPropagation();
methods.changeMonth.apply($(this).closest('.main_calendar'), [ $(this).hasClass('next-month') ? 1 : -1, $(this).closest('.main_calendar').data('options') ]);
})
.on('click.main_calendar', '.main_calendar .change-day', function() {// change month method
var container = $(this).closest('.main_calendar');
var table = $(this).closest('table');
table.find('.selected').removeClass('selected');
if(table){
methods.changeMonth.apply($(this).closest('.main_calendar'), [ now, $(this).closest('.main_calendar').data('options') ]);
}else{
}
})
.on('click.main_calendar', '.main_calendar-today', function() {
methods.changeMonth.apply($(this).closest('.main_calendar'), [ now, $(this).closest('.main_calendar').data('options') ]);
})
.on('click.main_calendar', '.main_calendar table.current td', function() {
var container = $(this).closest('.main_calendar');
var table = $(this).closest('.table');
var status = $(this).data('status');
if(status=='currentmonth'){
table.find('.selected').removeClass('selected');
$(this).addClass('selected');
selectedDate = $(this).data('date');
ShowArticles(selectedDate);
container.data('date', $(this).data('date'));
ScrollArticle();
}
})
.on('click','#previous_day', function(){
var container = $('.main_calendar');
var table = $('.main_calendar table.current td').closest('table');
var prevDate = new Date(''+selectedDate);
var flagDate = parseInt(''+(container.data('date').getMonth()+1));
prevDate.setDate(prevDate.getDate()-1);
var prevFlag = parseInt(''+(prevDate.getMonth()+1));
selectedDate = prevDate;
if(flagDate!=prevFlag){
methods.changeMonth.apply($('.calendar'), [prevDate, $('.calendar').data('options') ]);
}
var classId = '#date_'+prevDate.getFullYear()+'_'+(prevDate.getMonth()+1)+'_'+prevDate.getDate();
table.find('.selected').removeClass('selected');
table.find(classId).addClass('selected');
ShowArticles(prevDate);
ScrollArticle();
})
.on('click','#next_day', function(){
var container = $('.main_calendar');
var table = $('.main_calendar table.current td').closest('table');
var nextDate = new Date(''+selectedDate);
var flagDate = parseInt(''+(container.data('date').getMonth()+1));
nextDate.setDate(nextDate.getDate()+1);
var nextFlag = parseInt(''+(nextDate.getMonth()+1));
selectedDate = nextDate;
if(flagDate!=nextFlag){
methods.changeMonth.apply($('.calendar'), [nextDate, $('.calendar').data('options') ]);
}
var classId ='#date_'+nextDate.getFullYear()+'_'+(nextDate.getMonth()+1)+'_'+nextDate.getDate();
table.find('.selected').removeClass('selected');
table.find(classId).addClass('selected');
ShowArticles(nextDate);
ScrollArticle();
})
// Popups
.on('click.main_calendar', '.main_calendar-popup-trigger', function(e) {
$(this).parent('.main_calendar-popup-wrapper').addClass('main_calendar-open').find('.main_calendar-popup').show();
// Close other calendars
$('.main_calendar-popup-wrapper.main_calendar-open').not($(this).parent()).removeClass('main_calendar-open').find('.main_calendar-popup').hide();
})
.on('click.main_calendar', function(e) {
var target = $(e.target);
if(!target.closest('.main_calendar-popup-wrapper').length) {
$('.main_calendar-popup-wrapper.main_calendar-open').removeClass('main_calendar-open').find('.main_calendar-popup').hide();
}
})
.on('click.main_calendar', '.main_calendar td', function() {
var thisDate = $(this).data('date');
var originalElement = $(this).closest('.main_calendar').data('element');
var formattedDate = pMethods.formatDate($(this).data('date'), $(this).closest('.main_calendar').data('options'));
$(originalElement).trigger('dateselect', [ thisDate ]);
// Set date on input element if it exists
$(originalElement).children('.main_calendar-popup-trigger').val(formattedDate).trigger('change');
});
$(document).data('main_calendar-events', true);
}
return this.each(function() {
if($(this).is(':input')) {
var element = $('');
$(this).addClass('main_calendar-target').after(element);
} else {
var element = this;
}
$(element).addClass('main_calendar ' + options.transition);
$(element).data('options', options);
if(options.transition) {
$(element).addClass('transition');
}
if(options.condensed) {
$(element).addClass('condensed');
}
switch(options.mode) {
case 'popup':
$(element).addClass('main_calendar-popup-trigger');
pMethods.drawPopupCalendar.apply(element, options.date);
break;
case 'widget':
default:
pMethods.drawCalendar.call(element, options.date);
}
});
},
changeDay: function(month, options){
var container = $(this).closest('.main_calendar');
var table = $(this).closest('table');
var dateSelected = table.find('.selected');
dateSelected.removeClass('selected');
dateSelected.next('.main_calendar').addClass('selected');
selectedDate = dateSelected.next('.main_calendar').data('date');
},
changeMonth: function(month, options) {
var newDay, newDate, direction, newCalendar;
var container = $(this);
var calendar = $(this).find('table');
var currentDate = container.data('date');
var calWidth = calendar.outerWidth(true);
var calHeight = calendar.outerHeight(true);
var data = new Array();
calendar.parent().height(calHeight); // Set height of calendar's container to height of table
if(typeof month === 'number') {
direction = month > 0 ? 1 : -1;
newDay = Math.min(currentDate.daysInMonth(month), currentDate.getDate()); // 31st of March clamped to 28th Feb, for example
newDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + direction, newDay);
} else if(month instanceof Date) {
direction = (month > currentDate) ? 1 : -1;
newDate = month;
}
calendar.stop(true, true);
container.data('date', newDate);
newCalendar = pMethods.drawMonth(newDate, options).addClass('current');
switch(options.transition) {
case 'fade':
calendar.fadeOut(options.animDuration, function() {
$(this).replaceWith(newCalendar.hide().fadeIn(options.animDuration));
});
break;
case 'crossfade': // Crossfade
calendar.removeClass('current').after(newCalendar);
calendar.animate({ opacity: 0 }, options.animDuration);
newCalendar.css({ opacity: 0, position: 'absolute', top: 0 }).animate({ opacity: 1 }, options.animDuration);
break;
case 'carousel-horizontal':
calendar.css({ position: 'absolute' }).animate({ left: -(calWidth * direction) }).after(newCalendar);
newCalendar.css({ left: direction * calWidth, position: 'absolute' }).animate({ left: 0 }, {
complete: function() {
setTimeout(function(){$(".main_calendar-month").css("height", ($(".main_calendar-month > table").height()) + 'px');}, 1);
}
});
break;
case 'carousel-vertical': // Vertical slide
calendar.css({ position: 'absolute' }).animate({ top: -(calHeight * direction) }).after(newCalendar);
newCalendar.css({ top: direction * calHeight, position: 'absolute' }).animate({ top: 0 });
break;
default: // No transition - default
calendar.replaceWith(newCalendar);
break;
}
// Remove old calendar
newCalendar.promise().done(function() {
container.find('table').not(newCalendar).remove();
});
// Update header and footer
container.find('.main_calendar-header').replaceWith(pMethods.drawHeader(newDate, options));
// Set date on input element if it exists
if(options.setOnMonthChange) {
container.prev('.main_calendar-target').val($(this).data('date')).trigger('change');
}
},
date: function() { // Return current selected date
if($(this).next('.main_calendar').length) {
return $(this).next('.main_calendar').data('date');
} else if($(this).data('main_calendar')) {
return $(this).data('date');
}
return false;
}
};
if(methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if(typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
}
function setEvent(year,month){
var data = new Array();
data["month"] = month;
data["year"] = year;
data["category"] = category;
var post_data = array2post(data);
$.ajax({
type: 'POST',
url: '/public/api/schedule/calendar/',
data: post_data,
dataType: 'json',
success:function(event) {
if(event['result']=="ok"){
monthEvent =null;
oshimenMonthEvent=null;
monthEventArticle=null;
monthEvent = event['data']['thismonth'];
$.each(monthEvent,function(key,value){
var todayEvent = value;
var oshimenArr = 0, categoryArr=0, iconArr=0;
for(var i=0; i< todayEvent.length;i++){
if(category==null || category == 0 || category==todayEvent[i]['parent_category']){
if(monthEventArticle ==null){
monthEventArticle = new Array();
}
if(monthEventArticle[key]==null){
monthEventArticle[key] = new Array();
}
monthEventArticle[key][categoryArr] = todayEvent[i];
categoryArr+=1;
if(todayEvent[i]['member']!=null && todayEvent[i]['member']!=''){
var splitMemberData = todayEvent[i]['member'].split(',');
$.each(splitMemberData, function(index, value){
if(value==oshimenId){
if(oshimenMonthEvent == null){
oshimenMonthEvent = new Array();
}
if(oshimenMonthEvent[key]==null){
oshimenMonthEvent[key] = new Array();
}
oshimenMonthEvent[key][oshimenArr] = todayEvent[i];
oshimenArr+=1;
return;
}
});
}
}
var showIcon = true;
var get_param = getParam();
var filter = typeof(get_param) != 'undefined' ? get_param['category'] : false;
if ( !filter ) {
if ( todayEvent[i]['parent_category'] == iconSelectionCategory ){
showIcon = false;
}
}
else {
if ( todayEvent[i]['parent_category'] != filter ) {
showIcon = false;
}
}
if ( showIcon == true ) {
var tiptext = todayEvent[i]['title'].length > 0 ? todayEvent[i]['title'] : todayEvent[i]['body'].substring(0, 64);
var topMargin = (iconArr+1) * 21;
var ts = Math.round(new Date().getTime() / 1000);
var image_cal_url = null;
if ( todayEvent[i]['cal_image'] != null && todayEvent[i]['cal_image'] != '' && todayEvent[i]['cal_image'] != undefined ) {
if ( todayEvent[i]['parent_category'] == iconSelectionCategory ) {
image_cal_url = "/common/img/about/schedule/scheduleCal/icon_media.gif";
}
else {
image_cal_url = todayEvent[i]['cal_image'];
}
}
else {
image_cal_url = "/common/img/about/schedule/scheduleCal/icon_cd.gif";
}
var cate_text = todayEvent[i]['title'].length > 0 ? todayEvent[i]['title'] : todayEvent[i]['body'];
var cate_icon = $('')
.addClass('tooltipactive')
.addClass('scheduleArticleLink')
.css('height', (topMargin + 31)+'px')
.attr('title' , tiptext)
.css('position','absolute')
.css('left', '7px')
.css('top',topMargin+ 'px')
.css('width','91px')
.css('height','18px')
.attr('data-url' , '/modal/detail_schedule.php?sid='+todayEvent[i]['schedule_id']+'&ts='+ts);
cate_icon.prepend($('