var eventsFilterDateCurrent = new Date(2018,01,23); var eventsFilterDateSelectedLastTime = []; eventsFilterDateCurrent.setMonth(eventsFilterDateCurrent.getMonth() - 1); var eventsFilterDate = { dates: [], firstRun: true, markToday: true, month: eventsFilterDateCurrent.getMonth() + 1, year: eventsFilterDateCurrent.getFullYear(), week: eventsFilterDateCurrent.getWeek(0), dateCurrent: 0, weeks: [], multiselect: true, hlDates: [], weekSkip: 0, weekOfMonth: 0, init: function(multiselect) { if (typeof(multiselect) != 'undefined') { this.multiselect = multiselect; } this.weeksUpdate(); for (var i in this.weeks) { if (eventsFilterDateCurrent >= this.weeks[i]) { this.weekOfMonth = i; } } eventsFilterDateCurrent = this.weeks[this.weekOfMonth]; this.dateCurrent = eventsFilterDateCurrent; this.updateWeek(); }, setMonth: function(month) { this.month = month; eventsFilterDateCurrent.setDate(1); eventsFilterDateCurrent.setMonth(this.month - 1); this.dateCurrent = eventsFilterDateCurrent; this.weeksUpdate(); this.updateWeek(); }, setYear: function(year) { this.year = year; this.month = 1; eventsFilterDateCurrent.setDate(1); eventsFilterDateCurrent.setMonth(0); eventsFilterDateCurrent.setYear(this.year); this.dateCurrent = eventsFilterDateCurrent; this.weeksUpdate(); this.updateWeek(); }, weeksUpdate: function() { $("#chooser-month-value").html(months2[eventsFilterDateCurrent.getMonth()]); $("#chooser-year-value").html(eventsFilterDateCurrent.getFullYear()); this.week = eventsFilterDateCurrent.getWeek(0); var todayMonth = new Date(eventsFilterDateCurrent.getTime());; todayMonth.setDate(1); this.weeks = [todayMonth]; this.weeks = this.weeks.concat(WeekStart(this.month - 1, this.year, 1)); this.weekOfMonth = 0; }, clearSelection: function() { $(".weekler tr td").removeClass('active'); eventsFilterDate.days = []; }, weekNext: function() { if (parseInt(this.weekOfMonth) + 1 < this.weeks.length) { this.weekOfMonth++; eventsFilterDateCurrent = this.weeks[this.weekOfMonth]; this.dateCurrent = eventsFilterDateCurrent; } else { this.monthNext(); } this.updateWeek(); }, weekPrev: function() { if (this.weekOfMonth > 0) { this.weekOfMonth--; eventsFilterDateCurrent = this.weeks[this.weekOfMonth]; this.dateCurrent = eventsFilterDateCurrent; } else { this.monthPrev(); } this.updateWeek(); }, monthNext: function() { var year = this.year; var month = this.month + 1; if (month > 12) { year++; month = 1; } this.setYear(year); this.setMonth(month); }, monthPrev: function() { var year = this.year; var month = this.month - 1; if (month < 1) { year--; month = 12; } this.setYear(year); this.setMonth(month); // Мотаем в конец месяца this.weekOfMonth = this.weeks.length - 1; eventsFilterDateCurrent = this.weeks[this.weekOfMonth]; this.dateCurrent = eventsFilterDateCurrent; this.updateWeek(); }, updateWeek: function() { var object = this; this.clearSelection(); var firstRun = this.firstRun; var markToday = this.markToday; var day = eventsFilterDateCurrent.getDate(); var weekDay = eventsFilterDateCurrent.getDay(); var i = 0; var ii = 0; var otherItem = 1; var today = -1; var curDate = new Date(); $(".weekler .switches .day").each(function(){ var index = $(".weekler .switches .day").index($(this)); if (((i == weekDay - 1 || (i == 6 && weekDay == 0)) && day + ii <= daysInMonth(eventsFilterDate.month - 1, eventsFilterDate.year))) { if (day + ii == curDate.getDate()) { if (markToday && object.multiselect) { $(this).parent('td').addClass('active'); } if (firstRun) { $(".weekler .today:eq("+index+")").html('сегодня'); } } if (eventsFilterDate.year == curDate.getFullYear() && eventsFilterDate.month - 1 == curDate.getMonth() && day + ii == curDate.getDate()) { today = index; } $(this).html(day + ii).removeClass('other'); ii++; weekDay++; } else { $(this).addClass('other'); if ($(".weekler tr.switches td:first .day").hasClass('other')) { var prevYear = object.year; var prevMonth = object.month - 1; if (prevMonth < 1) { prevYear--; prevMonth = 12; } var daysInPrevMonth = daysInMonth(prevMonth - 1, prevYear); $(this).html(daysInPrevMonth); if (index > 0) { var prevDaysIterator = 0; $(".weekler tr.switches td .day:lt(" + index + ")").each(function(){ $(this).html(daysInPrevMonth - index + prevDaysIterator); prevDaysIterator++; }); } } else { var nextYear = object.year; var nextMonth = object.month + 1; if (nextMonth > 12) { nextYear++; nextMonth = 1; } var daysInNextMonth = daysInMonth(nextMonth - 1, nextYear); $(this).html(otherItem); otherItem++; } } i++; }); var i = 0; if (firstRun && eventsFilterDateSelectedLastTime.length > 0) { for (ii in eventsFilterDateSelectedLastTime) { var index = eventsFilterDateSelectedLastTime[ii]; $(".weekler .switches td:eq("+index+")").addClass('active'); } } $(".weekler .today").each(function(){ if (today == i) { $(this).html('сегодня'); } else { $(this).html(' '); } i++; }); this.firstRun = false; this.markToday = false; this.weekSkip = $(".page-events-filter-week .weekler tr.switches td:first .day.other").length ? $(".page-events-filter-week .weekler tr.switches td .day.other").length : 0; }, highlight: function(dates) { var day = eventsFilterDateCurrent.getDate(); var ii = 0; this.hlDates = []; for (i in dates) { this.hlDates.push(dates[i]); } var object = this; $(".weekler .switches .day").each(function(){ var index = $(".weekler .switches .day").index($(this)); var hl = false; for (it in object.hlDates) { var hlDateSliced = object.hlDates[it].toString().split('.'); // IE7, что с тобой не так?! var hlDateIteration = new Date(); hlDateIteration.setTime(Date.parse(hlDateSliced[1] + '/' + hlDateSliced[2] + '/' + hlDateSliced[0])); if (hlDateIteration.getFullYear() == object.year && hlDateIteration.getMonth() == object.month - 1 && hlDateIteration.getDate() == day + ii - object.weekSkip) { hl = true; } } if (hl) { $(this).parent('td').addClass('active'); } else { $(this).parent('td').removeClass('active'); } ii++; }); }/*, selectToNextMonth: function() { } */ }; jQuery(function($) { $("#eventsFilterDatePrev").click(function(){ eventsFilterDate.weekPrev(); return false; }); $("#eventsFilterDateNext").click(function(){ eventsFilterDate.weekNext(); return false; }); $(".chooser-month li a").click(function(){ eventsFilterDate.setMonth($(".chooser-month li").index($(this).parent('li')) + 1); $("#chooser-month-value").html(months2[eventsFilterDate.month - 1]); $(this).parents('ul').hide(); return false; }); $(".chooser-year li a").click(function(){ eventsFilterDate.setYear($(this).text()); $("#chooser-year-value").html(eventsFilterDate.year); $(this).parents('ul').hide(); return false; }); $(".weekler tr.controls .chooser").hover(function(){ $(this).children('ul').show(); }, function(){ $(this).children('ul').hide(); }); $(".weekler tr.switches td").click(function(){ if (!$(this).children('.day').hasClass('other')) { $(this).toggleClass('active'); filterProcess(); } else { if ($(".weekler tr.switches td:first .day").hasClass('other')) { eventsFilterDate.monthPrev(); } else { eventsFilterDate.monthNext(); } } return false; }); });