(function () { const currentPath = location.pathname; const condition = currentPath.indexOf('/teach/control/stream/view/'); const isEditPage = currentPath.split('/')[currentPath.split('/').length - 2]; if (condition !== -1 && isEditPage !== 'editMode') { let today = moment().locale('ru'); function Calendar(selector, events) { this.el = document.querySelector(selector); this.events = events; this.current = moment().locale('ru').date(1); this.draw(); this.lastClickDay = []; let current = document.querySelector('.today'); if (current) { let self = this; window.setTimeout(function () { self.openDay(current); }, 500); } } Calendar.prototype.draw = function () { this.drawHeader(); this.drawMonth(); this.drawLegend(); } Calendar.prototype.drawHeader = function () { let self = this; if (!this.header) { //Create the header elements this.header = createElement('div', 'header'); this.header.className = 'header'; this.title = createElement('h1'); let right = createElement('div', 'right'); right.addEventListener('click', function () { self.nextMonth(); }); let left = createElement('div', 'left'); left.addEventListener('click', function () { self.prevMonth(); }); //Append the Elements this.header.appendChild(this.title); this.header.appendChild(right); this.header.appendChild(left); this.el.appendChild(this.header); } this.title.innerHTML = this.current.format('MMMM YYYY'); } Calendar.prototype.drawMonth = function () { let self = this; this.events.forEach(function (ev) { ev.date = self.current.clone().date(1); ev.date._d = ev.startDate; }); if (this.month) { this.oldMonth = this.month; this.oldMonth.className = 'month out ' + (self.next ? 'next' : 'prev'); this.oldMonth.addEventListener('webkitAnimationEnd', function () { self.oldMonth.parentNode.removeChild(self.oldMonth); self.month = createElement('div', 'month'); self.backFill(); self.currentMonth(); self.fowardFill(); self.el.appendChild(self.month); window.setTimeout(function () { self.month.className = 'month in ' + (self.next ? 'next' : 'prev'); }, 16); }); } else { this.month = createElement('div', 'month'); this.el.appendChild(this.month); this.backFill(); this.currentMonth(); this.fowardFill(); this.month.className = 'month new'; } } Calendar.prototype.backFill = function () { let clone = this.current.clone(); let dayOfWeek = clone.day(); if (!dayOfWeek) { return; } clone.subtract('days', dayOfWeek + 1); for (let i = dayOfWeek; i > 0; i--) { this.drawDay(clone.add('days', 1)); } } Calendar.prototype.fowardFill = function () { let clone = this.current.clone().add('months', 1).subtract('days', 1); let dayOfWeek = clone.day(); if (dayOfWeek === 6) { return; } for (let i = dayOfWeek; i < 6; i++) { this.drawDay(clone.add('days', 1)); } } Calendar.prototype.currentMonth = function () { let clone = this.current.clone(); while (clone.month() === this.current.month()) { this.drawDay(clone); clone.add('days', 1); } } Calendar.prototype.getWeek = function (day) { if (!this.week || day.day() === 0) { this.week = createElement('div', 'week'); this.month.appendChild(this.week); } } Calendar.prototype.drawDay = function (day) { let self = this; this.getWeek(day); let outer = createElement('div', this.getDayClass(day)); outer.addEventListener('click', function (evt) { self.openDay(this); }); let name = createElement('div', 'day-name', day.format('ddd')); let number = createElement('div', 'day-number', day.format('DD')); let events = createElement('div', 'day-events'); this.drawEvents(day, number); outer.appendChild(name); outer.appendChild(number); outer.appendChild(events); this.week.appendChild(outer); } Calendar.prototype.drawEvents = function (day, element) { if (day.month() === this.current.month()) { let todaysEvents = this.events.reduce(function (memo, ev) { if (ev.date.isSame(day, 'day')) { memo.push(ev); } return memo; }, []); todaysEvents.forEach(function (ev) { element.style = 'color: #17005a'; }); } } Calendar.prototype.getDayClass = function (day) { classes = ['day']; if (day.month() !== this.current.month()) { classes.push('other'); } else if (today.isSame(day, 'day')) { classes.push('today'); } return classes.join(' '); } Calendar.prototype.openDay = function (el) { let details, arrow; let dayNumber = +el.querySelectorAll('.day-number')[0].innerText || +el.querySelectorAll('.day-number')[0].textContent; let day = this.current.clone().date(dayNumber); let currentOpened = document.querySelector('.details'); this.lastClickDay.push(el); //Check to see if there is an open detais box on the current row if (currentOpened && currentOpened.parentNode === el.parentNode) { details = currentOpened; arrow = document.querySelector('.arrow'); } else { //Close the open events on differnt week row //currentOpened && currentOpened.parentNode.removeChild(currentOpened); if (currentOpened) { currentOpened.addEventListener('webkitAnimationEnd', function () { currentOpened.parentNode.removeChild(currentOpened); }); currentOpened.addEventListener('oanimationend', function () { currentOpened.parentNode.removeChild(currentOpened); }); currentOpened.addEventListener('msAnimationEnd', function () { currentOpened.parentNode.removeChild(currentOpened); }); currentOpened.addEventListener('animationend', function () { currentOpened.parentNode.removeChild(currentOpened); }); currentOpened.className = 'details out'; } //Create the Details Container details = createElement('div', 'details in'); //Create the arrow let arrow = createElement('div', 'arrow'); //Create the event wrapper details.appendChild(arrow); el.parentNode.appendChild(details); } let todaysEvents = this.events.reduce(function (memo, ev) { if (ev.date.isSame(day, 'day')) { memo.push(ev); } return memo; }, []); this.renderEvents(todaysEvents, details); if (arrow) { arrow.style.left = el.offsetLeft - el.parentNode.offsetLeft + 27 + 'px'; } if (this.lastClickDay[this.lastClickDay.length - 1] === this.lastClickDay[this.lastClickDay.length - 2]) { if (currentOpened) { currentOpened.remove(); } } } Calendar.prototype.renderEvents = function (events, ele) { //Remove any events in the current details element let currentWrapper = ele.querySelector('.events'); let wrapper = createElement('div', 'events in' + (currentWrapper ? ' new' : '')); events.forEach(function (ev) { let div = createElement('div', 'event'); let square = createElement('div', 'event-category ' + ev.color); let span = createElement('span', 'event-span'); let link = createElement('a', 'event-link', ev.eventName); link.href = ev.eventLink; div.appendChild(square); span.appendChild(link); div.appendChild(span); wrapper.appendChild(div); }); if (!events.length) { let div = createElement('div', 'event empty'); let span = createElement('span', '', 'Сегодня нет занятий'); div.appendChild(span); wrapper.appendChild(div); } if (currentWrapper) { currentWrapper.className = 'events out'; currentWrapper.addEventListener('webkitAnimationEnd', function () { currentWrapper.parentNode.removeChild(currentWrapper); ele.appendChild(wrapper); }); currentWrapper.addEventListener('oanimationend', function () { currentWrapper.parentNode.removeChild(currentWrapper); ele.appendChild(wrapper); }); currentWrapper.addEventListener('msAnimationEnd', function () { currentWrapper.parentNode.removeChild(currentWrapper); ele.appendChild(wrapper); }); currentWrapper.addEventListener('animationend', function () { currentWrapper.parentNode.removeChild(currentWrapper); ele.appendChild(wrapper); }); } else { ele.appendChild(wrapper); } } Calendar.prototype.drawLegend = function () { let legend = createElement('div', 'legend'); let calendars = this.events.map(function (e) { return e.calendar + '|' + e.color; }).reduce(function (memo, e) { if (memo.indexOf(e) === -1) { memo.push(e); } return memo; }, []).forEach(function (e) { let parts = e.split('|'); let entry = createElement('span', 'entry ' + parts[1], parts[0]); legend.appendChild(entry); }); this.el.appendChild(legend); } Calendar.prototype.nextMonth = function () { this.current.add('months', 1); this.next = true; this.draw(); } Calendar.prototype.prevMonth = function () { this.current.subtract('months', 1); this.next = false; this.draw(); } window.Calendar = Calendar; function createElement(tagName, className, innerText) { let ele = document.createElement(tagName); if (className) { ele.className = className; } if (innerText) { ele.innerText = ele.textContent = innerText; } return ele; } let initCalendar = (data) => { if (document.querySelector('.cst-container') && document.querySelector('#calendar')) { return new Calendar('#calendar', data); } }; initCalendar(window.cstData); } })();