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

150 lines
3.9 KiB
JavaScript

import ngModule from '../module';
class Controller {
constructor($scope, $stateParams, $http) {
this.$stateParams = $stateParams;
this.$scope = $scope;
this.$http = $http;
this.stMonthDate = new Date();
this.ndMonthDate = new Date();
this.ndMonthDate.setMonth(this.ndMonthDate.getMonth() + 1);
this.events = [];
}
$postLink() {
this.stMonth = this.$scope.stMonth;
this.ndMonth = this.$scope.ndMonth;
}
get zone() {
return this._zone;
}
set zone(value) {
this._zone = value;
if (!value) return;
let query = '/agency/api/LabourHolidays/getByWarehouse';
this.$http.get(query, {params: {warehouseFk: value.warehouseFk}}).then(res => {
if (!res.data) return;
const events = [];
res.data.forEach(holiday => {
events.push({
date: holiday.dated,
color: 'blue-circle',
title: holiday.description || holiday.name,
isRemovable: false
});
});
this.events = this.events.concat(events);
});
}
get data() {
return this._data;
}
set data(value) {
this._data = value;
if (!value) return;
const events = [];
value.forEach(event => {
events.push({
date: event.delivered,
color: 'green-circle',
title: 'Has delivery',
isRemovable: true
});
});
this.events = this.events.concat(events);
}
onSelection(calendar, values) {
let totalEvents = 0;
values.forEach(day => {
const exists = this.events.findIndex(event => {
return event.date >= day.date && event.date <= day.date
&& event.isRemovable;
});
if (exists > -1) totalEvents++;
});
if (totalEvents > (values.length / 2))
this.removeEvents(values);
else
this.addEvents(values);
}
addEvents(days) {
days.forEach(day => {
const event = this.events.find(event => {
return event.date >= day.date && event.date <= day.date;
});
if (event)
return false;
this.$scope.model.insert({
zoneFk: this.zone.id,
delivered: day.date
});
this.stMonth.addEvent(day.date, 'green-circle', 'Has delivery', true);
this.stMonth.repaint();
this.ndMonth.addEvent(day.date, 'green-circle', 'Has delivery', true);
this.ndMonth.repaint();
});
this.$scope.model.save();
}
removeEvents(days) {
let dates = [];
days.forEach(day => {
const event = this.events.find(event => {
return event.date >= day.date && event.date <= day.date;
});
if (event && !event.isRemovable)
return false;
// FIXME - Date offset
let date = new Date(day.date);
date.setHours(0, 0, 0, 0);
dates.push(date);
this.stMonth.removeEvent(day.date);
this.stMonth.repaint();
this.ndMonth.removeEvent(day.date);
this.ndMonth.repaint();
});
if (dates.length == 0) return;
const params = {zoneFk: this.zone.id, dates};
this.$http.post('/agency/api/zoneCalendars/removeByDate', params);
}
onMoveNext(calendar) {
calendar.moveNext(2);
}
onMovePrevious(calendar) {
calendar.movePrevious(2);
}
}
Controller.$inject = ['$scope', '$stateParams', '$http'];
ngModule.component('vnZoneCalendar', {
template: require('./index.html'),
controller: Controller,
bindings: {
zone: '<'
}
});