var MiniCalendar = function () { var self = this; var $calendar = null; var $nextButton = null; var $prevButton = null; var widgetId = null; var contentContainerID = null; var currentMonth = null; var todayMonth = null; var preventNavigationOnArrows; var urlChangeMonth = '/Calendar/WidgetGet/ChangeMiniCalendarMonth'; var urlUpdateList = '/Calendar/WidgetGet/UpdateList'; function moveMonth(monthDate) { var miniCalendarResult; var eventListResult; var async1 = $.ajax({ type: 'POST', url: urlChangeMonth, data: JSON.stringify({ "month": monthDate.getMonth() + 1, "year": monthDate.getFullYear(), "pageID": $('#pagePageID').val(), "widgetID": widgetId, "contentContainerID": contentContainerID, "moduleID": $('#pageModuleID').val() }), contentType: 'application/json', cache: false, async: true, success: function (response) { miniCalendarResult = response; }, error: function (xhr, textStatus, exception) { alert('Error: ' + xhr.statusText + '\nStatus: ' + xhr.status); } }); var async2 = $.ajax({ type: 'POST', url: urlUpdateList, data: JSON.stringify({ "month": monthDate.getMonth() + 1, "year": monthDate.getFullYear(), "pageID": $('#pagePageID').val(), "widgetID": widgetId, "contentContainerID": contentContainerID, "moduleID": $('#pageModuleID').val() }), contentType: 'application/json', cache: false, async: true, success: function (response) { eventListResult = response; }, error: function (xhr, textStatus, exception) { alert('Error: ' + xhr.statusText + '\nStatus: ' + xhr.status); } }); $.when(async1, async2).done(function () { currentMonth = monthDate; $calendar.find('div[data-minicalendar]').replaceWith(miniCalendarResult); initButtons(); $calendar.find('div.row').html(eventListResult); }); }; function initButtons() { $nextButton = $calendar.find("button.cp-miniCalendar-arrow--right"); $nextButton.click(function (e) { if (preventNavigationOnArrows) { e.preventDefault(); } else { self.moveNextMonth(); } }); $prevButton = $calendar.find("button.cp-miniCalendar-arrow--left"); if (isCurrentMonthTodayMonth()) { $prevButton.attr("disabled", true); } else { $prevButton.click(function () { if (preventNavigationOnArrows) { e.preventDefault(); } else { self.movePrevMonth(); } }); } } function isCurrentMonthTodayMonth() { return (todayMonth.getMonth() === currentMonth.getMonth() && todayMonth.getFullYear() === currentMonth.getFullYear()); } this.moveNextMonth = function () { var nextMonth = new Date(currentMonth.getFullYear(), currentMonth.getMonth() + 1, 1); moveMonth(nextMonth); }; this.movePrevMonth = function () { if (!isCurrentMonthTodayMonth()) { var prevMonth = new Date(currentMonth.getFullYear(), currentMonth.getMonth() - 1, 1); moveMonth(prevMonth); } }; this.init = function (options) { widgetId = options.widgetId; preventNavigationOnArrows = options.preventNavigationOnArrows; $calendar = $("#widgetCalendar" + widgetId); var $pageContent = $calendar.parents('.pageContent').filter(":first"); contentContainerID = $pageContent.data('containerid'); var $monthToRender = $calendar.find('input[name="monthToRender"]'); var month = parseInt($monthToRender.data("month"), 10); var year = parseInt($monthToRender.data("year"), 10); currentMonth = new Date(year, month - 1, 1); var now = new Date(); todayMonth = new Date(now.getFullYear(), now.getMonth(), 1); initButtons(); }; };