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

149 lines
3.8 KiB
JavaScript
Raw Normal View History

2018-12-04 12:18:27 +00:00
import ngModule from '../module';
2018-09-24 12:51:27 +00:00
class Controller {
2019-02-01 16:11:14 +00:00
constructor($element, $scope, $stateParams, $http) {
this.$element = $element;
2019-01-21 10:45:53 +00:00
this.$stateParams = $stateParams;
2018-09-24 12:51:27 +00:00
this.$scope = $scope;
2019-01-21 10:45:53 +00:00
this.$http = $http;
this.stMonthDate = new Date();
this.ndMonthDate = new Date();
this.ndMonthDate.setMonth(this.ndMonthDate.getMonth() + 1);
2018-11-12 10:31:58 +00:00
}
2019-01-21 10:45:53 +00:00
$postLink() {
this.stMonth = this.$scope.stMonth;
this.ndMonth = this.$scope.ndMonth;
}
2019-02-25 09:03:50 +00:00
// Disabled until implementation
// of holidays by node
/* get zone() {
2019-01-21 10:45:53 +00:00
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,
2019-03-22 07:28:57 +00:00
className: 'red',
2019-01-21 10:45:53 +00:00
title: holiday.description || holiday.name,
isRemovable: false
});
});
this.events = this.events.concat(events);
});
2019-02-25 09:03:50 +00:00
} */
2019-01-21 10:45:53 +00:00
get data() {
return this._data;
}
set data(value) {
this._data = value;
if (!value) return;
const events = [];
value.forEach(event => {
events.push({
dated: event.delivered,
2019-03-22 07:28:57 +00:00
className: 'green-circle',
title: 'Has delivery'
2019-01-21 10:45:53 +00:00
});
});
this.events = events;
2019-01-21 10:45:53 +00:00
}
onSelection(calendar, values) {
let totalEvents = 0;
values.forEach(day => {
const exists = calendar.events.findIndex(event => {
return event.dated >= day.dated && event.dated <= day.dated
2019-01-21 10:45:53 +00:00
&& event.isRemovable;
});
if (exists > -1) totalEvents++;
});
if (totalEvents > (values.length / 2))
this.removeEvents(calendar, values);
2019-01-21 10:45:53 +00:00
else
this.insertEvents(calendar, values);
2019-01-21 10:45:53 +00:00
}
insertEvents(calendar, days) {
2019-01-21 10:45:53 +00:00
days.forEach(day => {
const event = calendar.events.find(event => {
return event.dated >= day.dated && event.dated <= day.dated;
2019-01-21 10:45:53 +00:00
});
if (event) return false;
2019-01-21 10:45:53 +00:00
this.$scope.model.insert({
zoneFk: this.zone.id,
delivered: day.dated
});
calendar.addEvent({
dated: day.dated,
className: 'green-circle',
title: 'Has delivery'
2019-01-21 10:45:53 +00:00
});
});
2019-01-21 10:45:53 +00:00
this.$scope.model.save().then(() => {
this.events = calendar.events;
2019-01-21 10:45:53 +00:00
});
}
removeEvents(calendar, days) {
2019-01-21 10:45:53 +00:00
let dates = [];
days.forEach(day => {
const event = calendar.events.find(event => {
return event.dated >= day.dated && event.dated <= day.dated;
2019-01-21 10:45:53 +00:00
});
if (event && !event.isRemovable)
return false;
dates.push(day.dated);
2019-01-21 10:45:53 +00:00
calendar.removeEvent(day.dated);
2019-01-21 10:45:53 +00:00
});
if (dates.length == 0) return;
const params = {zoneFk: this.zone.id, dates};
this.$http.post('/agency/api/zoneCalendars/removeByDate', params).then(() => {
this.events = calendar.events;
});
2019-01-21 10:45:53 +00:00
}
onMoveNext(calendar) {
calendar.moveNext(2);
}
onMovePrevious(calendar) {
calendar.movePrevious(2);
2018-09-24 12:51:27 +00:00
}
}
2019-02-01 16:11:14 +00:00
Controller.$inject = ['$element', '$scope', '$stateParams', '$http'];
2018-09-24 12:51:27 +00:00
ngModule.component('vnZoneCalendar', {
template: require('./index.html'),
controller: Controller,
bindings: {
zone: '<'
}
});