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

192 lines
5.0 KiB
JavaScript

import ngModule from '../module';
import Component from 'core/lib/component';
class Controller extends Component {
constructor($element, $, vnWeekDays) {
super($element, $);
this.vnWeekDays = vnWeekDays;
this.editMode = 'include';
this.path = `Zones/${this.$params.id}/events`;
this.exclusionsPath = `Zones/${this.$params.id}/exclusions`;
this.refresh();
}
refresh() {
let data = {};
this.$q.all([
this.$http.get(this.path)
.then(res => data.events = res.data),
this.$http.get(this.exclusionsPath)
.then(res => data.exclusions = res.data)
]).finally(() => {
this.$.data = data;
});
}
formatWdays(weekDays) {
if (!weekDays) return;
let abrWdays = [];
for (let wday of weekDays.split(','))
abrWdays.push(this.vnWeekDays.map[wday].localeAbr);
return abrWdays.length < 7
? abrWdays.join(', ')
: this._('Everyday');
}
onSelection(days, type, weekday, data) {
if (this.editMode == 'include') {
let dayData = data[0] || {};
let event = dayData.events && dayData.events[0];
if (event)
this.edit(event);
else
this.create(days, type, weekday);
} else {
let exclusions = [];
for (let dayData of data) {
if (dayData.exclusion)
exclusions.push(dayData.exclusion.id);
}
if (exclusions.length)
this.exclusionDelete(exclusions);
else
this.exclusionCreate(days);
}
}
onEditClick(row, event) {
if (event.defaultPrevented) return;
this.edit(row);
}
edit(row) {
this.isNew = false;
if (row.from && !row.to)
this.eventType = 'day';
else if (!row.from)
this.eventType = 'indefinitely';
else
this.eventType = 'range';
this.selected = angular.copy(row);
this.selected.wdays = {};
if (row.weekDays) {
let weekDays = row.weekDays.split(',');
for (let day of weekDays)
this.selected.wdays[day] = true;
}
this.$.dialog.show();
}
create(days, type, weekday) {
this.isNew = true;
this.eventType = type == 'day' ? 'day' : 'indefinitely';
if (type == 'weekday') {
let wdays = [];
let code = this.vnWeekDays.days[weekday].code;
wdays[code] = true;
this.selected = {wdays};
} else
this.selected = {from: days[0]};
this.$.dialog.show();
}
onIncludeResponse(response) {
switch (response) {
case 'accept': {
let selected = this.selected;
if (this.eventType == 'indefinitely') {
selected.from = null;
selected.to = null;
}
if (this.eventType != 'day') {
let weekDays = [];
for (let wday in selected.wdays) {
if (selected.wdays[wday])
weekDays.push(wday);
}
selected.weekDays = weekDays.join(',');
} else {
selected.to = null;
selected.weekDays = '';
}
let req;
if (this.isNew)
req = this.$http.post(this.path, selected);
else
req = this.$http.put(`${this.path}/${selected.id}`, selected);
return req.then(() => {
this.selected = null;
this.isNew = null;
this.refresh();
});
}
case 'DELETE':
return this.onDelete(this.selected.id)
.then(response => response == 'accept');
}
}
onDeleteClick(id, event) {
if (event.defaultPrevented) return;
event.preventDefault();
this.onDelete(id);
}
onDelete(id) {
return this.$.confirm.show(
response => this.onDeleteResponse(response, id));
}
onDeleteResponse(response, id) {
if (response != 'accept' || !id) return;
return this.$http.delete(`${this.path}/${id}`)
.then(() => this.refresh());
}
exclusionCreate(days) {
let exclusions = days.map(day => {
return {day};
});
this.$http.post(this.exclusionsPath, exclusions)
.then(() => this.refresh());
}
exclusionDelete(ids) {
let promises = [];
for (let id of ids) {
promises.push(
this.$http.delete(`${this.exclusionsPath}/${id}`)
);
}
this.$q.all(promises)
.then(() => this.refresh());
}
}
Controller.$inject = ['$element', '$scope', 'vnWeekDays'];
ngModule.component('vnZoneEvents', {
template: require('./index.html'),
controller: Controller
});