109 lines
2.6 KiB
JavaScript
109 lines
2.6 KiB
JavaScript
import ngModule from '../module';
|
|
import './style.scss';
|
|
|
|
class Controller {
|
|
constructor($, $stateParams) {
|
|
Object.assign(this, {
|
|
$,
|
|
$stateParams
|
|
});
|
|
|
|
this.excls = {};
|
|
this.resetEvents();
|
|
this.ndMonthDate = new Date();
|
|
this.ndMonthDate.setMonth(this.ndMonthDate.getMonth() + 1);
|
|
}
|
|
|
|
resetEvents() {
|
|
this.wdays = [];
|
|
this.days = {};
|
|
this.ranges = [];
|
|
}
|
|
|
|
get events() {
|
|
return this._events;
|
|
}
|
|
|
|
set events(value) {
|
|
this._events = value;
|
|
this.resetEvents();
|
|
if (!value) return;
|
|
|
|
function setWdays(wdays, weekDays) {
|
|
let codes = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
|
|
if (!weekDays) return [];
|
|
weekDays = weekDays.split(',');
|
|
for (let wday of weekDays)
|
|
wdays[codes.indexOf(wday)] = true;
|
|
return wdays;
|
|
}
|
|
|
|
for (let event of value) {
|
|
if (event.from && event.to) {
|
|
this.ranges.push({
|
|
from: new Date(event.from).setHours(0, 0, 0, 0),
|
|
to: new Date(event.to).setHours(0, 0, 0, 0),
|
|
wdays: setWdays([], event.weekDays)
|
|
});
|
|
} else if (event.from) {
|
|
let day = new Date(event.from).setHours(0, 0, 0, 0);
|
|
this.days[day] = true;
|
|
} else
|
|
setWdays(this.wdays, event.weekDays);
|
|
}
|
|
|
|
this.repaint();
|
|
}
|
|
|
|
get exclusions() {
|
|
return this._exclusions;
|
|
}
|
|
|
|
set exclusions(value) {
|
|
this._exclusions = value;
|
|
this.excls = {};
|
|
if (!value) return;
|
|
|
|
value.forEach(exclusion => {
|
|
let day = new Date(exclusion.day).setHours(0, 0, 0, 0);
|
|
this.excls[day] = true;
|
|
});
|
|
|
|
this.repaint();
|
|
}
|
|
|
|
hasRange(time, wday) {
|
|
let range = this.ranges.find(e => e.from <= time && e.to >= time);
|
|
return range && range.wdays[wday];
|
|
}
|
|
|
|
hasEvents(day) {
|
|
let time = day.getTime();
|
|
let wday = day.getDay();
|
|
return this.wdays[wday]
|
|
|| this.days[time]
|
|
|| this.hasRange(time, wday);
|
|
}
|
|
|
|
getClass(day) {
|
|
if (this.excls[day.getTime()])
|
|
return 'excluded';
|
|
}
|
|
|
|
repaint() {
|
|
this.$.stMonth.repaint();
|
|
this.$.ndMonth.repaint();
|
|
}
|
|
}
|
|
|
|
Controller.$inject = ['$scope', '$stateParams'];
|
|
|
|
ngModule.component('vnZoneCalendar', {
|
|
template: require('./index.html'),
|
|
controller: Controller,
|
|
bindings: {
|
|
events: '<?',
|
|
exclusions: '<?'
|
|
}
|
|
});
|