salix/modules/worker/front/calendar/index.js

243 lines
6.5 KiB
JavaScript
Raw Normal View History

2019-03-22 07:28:57 +00:00
import ngModule from '../module';
2020-03-18 11:55:22 +00:00
import Section from 'salix/components/section';
2019-03-22 07:28:57 +00:00
import './style.scss';
2020-03-18 11:55:22 +00:00
class Controller extends Section {
constructor($element, $) {
super($element, $);
2019-10-23 15:38:35 +00:00
this.date = new Date();
this.events = {};
2020-08-10 12:44:57 +00:00
this.buildYearFilter();
}
get year() {
return this.date.getFullYear();
}
set year(value) {
const newYear = new Date();
newYear.setFullYear(value);
this.date = newYear;
2020-08-18 11:50:08 +00:00
this.refresh().then(() => this.repaint());
2019-03-22 07:28:57 +00:00
}
2019-10-23 15:38:35 +00:00
get date() {
return this._date;
2019-03-22 07:28:57 +00:00
}
2019-10-23 15:38:35 +00:00
set date(value) {
this._date = value;
value.setHours(0, 0, 0, 0);
2019-03-22 07:28:57 +00:00
2019-10-23 15:38:35 +00:00
const started = new Date(value.getTime());
started.setMonth(0);
started.setDate(1);
this.started = started;
2019-03-22 07:28:57 +00:00
2019-10-23 15:38:35 +00:00
const ended = new Date(value.getTime());
ended.setMonth(12);
ended.setDate(0);
this.ended = ended;
2019-03-22 07:28:57 +00:00
2019-10-23 15:38:35 +00:00
this.months = new Array(12);
2019-03-22 07:28:57 +00:00
2019-10-23 15:38:35 +00:00
for (let i = 0; i < this.months.length; i++) {
const now = new Date(value.getTime());
now.setDate(1);
now.setMonth(i);
this.months[i] = now;
}
2019-03-22 07:28:57 +00:00
}
2019-10-23 15:38:35 +00:00
get worker() {
return this._worker;
2019-03-22 07:28:57 +00:00
}
2019-10-23 15:38:35 +00:00
set worker(value) {
this._worker = value;
2019-03-22 07:28:57 +00:00
2020-07-15 09:35:34 +00:00
if (value) {
2020-07-11 16:30:27 +00:00
this.refresh().then(() => this.repaint());
2020-07-15 09:35:34 +00:00
this.getIsSubordinate();
}
}
2020-08-10 12:44:57 +00:00
buildYearFilter() {
const currentYear = new Date().getFullYear();
const minRange = currentYear - 5;
const years = [];
for (let i = currentYear; i > minRange; i--)
years.push({year: i});
this.yearFilter = years;
}
2020-07-15 09:35:34 +00:00
getIsSubordinate() {
this.$http.get(`Workers/${this.worker.id}/isSubordinate`).then(res =>
this.isSubordinate = res.data
);
2019-03-22 07:28:57 +00:00
}
2019-10-23 15:38:35 +00:00
onData(data) {
this.events = {};
this.calendar = data.calendar;
2019-03-22 07:28:57 +00:00
2019-10-23 15:38:35 +00:00
let addEvent = (day, event) => {
this.events[new Date(day).getTime()] = event;
};
2019-03-22 07:28:57 +00:00
2019-10-23 15:38:35 +00:00
if (data.holidays) {
data.holidays.forEach(holiday => {
const holidayDetail = holiday.detail && holiday.detail.description;
const holidayType = holiday.type && holiday.type.name;
const holidayName = holidayDetail || holidayType;
2019-03-22 07:28:57 +00:00
2019-10-23 15:38:35 +00:00
addEvent(holiday.dated, {
name: holidayName,
color: '#ff0'
});
});
}
if (data.absences) {
data.absences.forEach(absence => {
let type = absence.absenceType;
addEvent(absence.dated, {
name: type.name,
2020-07-08 13:05:56 +00:00
color: type.rgb,
type: type.code,
absenceId: absence.id
2019-10-23 15:38:35 +00:00
});
});
2019-03-22 07:28:57 +00:00
}
}
2019-10-23 15:38:35 +00:00
repaint() {
let calendars = this.element.querySelectorAll('vn-calendar');
for (let calendar of calendars)
calendar.$ctrl.repaint();
}
2019-10-23 15:38:35 +00:00
formatDay(day, element) {
let event = this.events[day.getTime()];
if (!event) return;
let dayNumber = element.firstElementChild;
dayNumber.title = event.name;
dayNumber.style.backgroundColor = event.color;
2019-11-15 09:03:46 +00:00
dayNumber.style.color = 'rgba(0, 0, 0, 0.7)';
}
2020-07-07 12:29:57 +00:00
pick(absenceType) {
2020-07-15 09:35:34 +00:00
if (!this.isSubordinate) return;
2020-07-07 12:29:57 +00:00
if (absenceType == this.absenceType)
absenceType = null;
this.absenceType = absenceType;
}
2020-07-11 16:30:27 +00:00
onSelection($event, $days) {
2020-07-07 12:29:57 +00:00
if (!this.absenceType)
2020-07-11 16:30:27 +00:00
return this.vnApp.showMessage(this.$t('Choose an absence type from the right menu'));
2020-07-07 12:29:57 +00:00
2020-08-18 11:50:08 +00:00
if (this.year != new Date().getFullYear())
return this.vnApp.showMessage(this.$t('You can just add absences within the current year'));
2020-07-08 13:05:56 +00:00
const day = $days[0];
const stamp = day.getTime();
const event = this.events[stamp];
2020-07-11 16:30:27 +00:00
const calendar = $event.target.closest('vn-calendar').$ctrl;
2020-07-08 13:05:56 +00:00
if (event) {
if (event.type == this.absenceType.code)
2020-07-11 16:30:27 +00:00
this.delete(calendar, day, event);
2020-07-08 13:05:56 +00:00
else
2020-07-11 16:30:27 +00:00
this.edit(calendar, event);
2020-07-08 13:05:56 +00:00
} else
2020-07-11 16:30:27 +00:00
this.create(calendar, day);
2020-07-07 12:29:57 +00:00
}
2020-07-11 16:30:27 +00:00
create(calendar, dated) {
2020-07-08 13:05:56 +00:00
const absenceType = this.absenceType;
const params = {
dated: dated,
absenceTypeId: absenceType.id
2020-07-07 12:29:57 +00:00
};
2020-07-08 13:05:56 +00:00
const path = `Workers/${this.$params.id}/createAbsence`;
this.$http.post(path, params).then(res => {
2020-07-11 16:30:27 +00:00
const newEvent = res.data;
this.events[dated.getTime()] = {
name: absenceType.name,
color: absenceType.rgb,
type: absenceType.code,
absenceId: newEvent.id
};
this.repaintCanceller(() =>
this.refresh().then(calendar.repaint())
);
2020-07-08 13:05:56 +00:00
});
2020-07-07 12:29:57 +00:00
}
2020-07-11 16:30:27 +00:00
edit(calendar, event) {
2020-07-08 13:05:56 +00:00
const absenceType = this.absenceType;
const params = {
absenceId: event.absenceId,
absenceTypeId: absenceType.id
};
const path = `Workers/${this.$params.id}/updateAbsence`;
2020-07-11 16:30:27 +00:00
this.$http.patch(path, params).then(() => {
event.color = absenceType.rgb;
event.name = absenceType.name;
2020-07-16 08:04:16 +00:00
event.type = absenceType.code;
2020-07-11 16:30:27 +00:00
this.repaintCanceller(() =>
this.refresh().then(calendar.repaint())
);
});
2020-07-07 12:29:57 +00:00
}
2020-07-11 16:30:27 +00:00
delete(calendar, day, event) {
2020-07-08 13:05:56 +00:00
const params = {absenceId: event.absenceId};
const path = `Workers/${this.$params.id}/deleteAbsence`;
2020-07-11 16:30:27 +00:00
this.$http.delete(path, {params}).then(() => {
delete this.events[day.getTime()];
this.repaintCanceller(() =>
this.refresh().then(calendar.repaint())
);
});
}
repaintCanceller(cb) {
if (this.canceller) {
clearTimeout(this.canceller);
this.canceller = null;
2020-07-07 12:29:57 +00:00
}
2020-07-08 13:05:56 +00:00
2020-07-11 16:30:27 +00:00
this.canceller = setTimeout(
() => cb(), 500);
2020-07-07 12:29:57 +00:00
}
refresh() {
const params = {
workerFk: this.worker.id,
started: this.started,
ended: this.ended
};
2020-08-10 12:29:25 +00:00
return this.$http.get(`Calendars/absences`, {params})
2020-07-07 12:29:57 +00:00
.then(res => this.onData(res.data));
}
2019-03-22 07:28:57 +00:00
}
ngModule.vnComponent('vnWorkerCalendar', {
2019-03-22 07:28:57 +00:00
template: require('./index.html'),
controller: Controller,
bindings: {
worker: '<'
}
});