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

192 lines
5.0 KiB
JavaScript
Raw Normal View History

2019-09-25 18:06:42 +00:00
import ngModule from '../module';
import Component from 'core/lib/component';
2019-09-25 18:06:42 +00:00
class Controller extends Component {
constructor($element, $, vnWeekDays) {
super($element, $);
2019-10-23 15:38:35 +00:00
this.vnWeekDays = vnWeekDays;
this.editMode = 'include';
2019-09-25 18:06:42 +00:00
this.path = `Zones/${this.$params.id}/events`;
this.exclusionsPath = `Zones/${this.$params.id}/exclusions`;
2019-09-25 18:06:42 +00:00
this.refresh();
}
refresh() {
2019-10-23 15:38:35 +00:00
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;
});
2019-09-25 18:06:42 +00:00
}
formatWdays(weekDays) {
if (!weekDays) return;
let abrWdays = [];
for (let wday of weekDays.split(','))
2019-10-23 15:38:35 +00:00
abrWdays.push(this.vnWeekDays.map[wday].localeAbr);
2019-09-25 18:06:42 +00:00
return abrWdays.length < 7
? abrWdays.join(', ')
2019-10-01 14:31:33 +00:00
: this._('Everyday');
2019-09-25 18:06:42 +00:00
}
2019-10-23 15:38:35 +00:00
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) {
2019-09-25 18:06:42 +00:00
if (event.defaultPrevented) return;
2019-10-23 15:38:35 +00:00
this.edit(row);
}
2019-09-25 18:06:42 +00:00
2019-10-23 15:38:35 +00:00
edit(row) {
2019-09-25 18:06:42 +00:00
this.isNew = false;
if (row.from && !row.to)
this.eventType = 'day';
else if (!row.from)
2019-09-25 21:09:10 +00:00
this.eventType = 'indefinitely';
2019-09-25 18:06:42 +00:00
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();
}
2019-10-23 15:38:35 +00:00
create(days, type, weekday) {
2019-09-25 18:06:42 +00:00
this.isNew = true;
2019-10-23 15:38:35 +00:00
this.eventType = type == 'day' ? 'day' : 'indefinitely';
2019-10-23 15:38:35 +00:00
if (type == 'weekday') {
let wdays = [];
2019-10-23 15:38:35 +00:00
let code = this.vnWeekDays.days[weekday].code;
wdays[code] = true;
this.selected = {wdays};
} else
2019-10-23 15:38:35 +00:00
this.selected = {from: days[0]};
2019-09-25 18:06:42 +00:00
this.$.dialog.show();
}
2019-10-30 15:57:14 +00:00
onIncludeResponse(response) {
switch (response) {
case 'accept': {
2019-10-23 15:38:35 +00:00
let selected = this.selected;
2019-09-25 18:06:42 +00:00
2019-10-23 15:38:35 +00:00
if (this.eventType == 'indefinitely') {
selected.from = null;
selected.to = null;
}
2019-09-25 18:06:42 +00:00
2019-10-23 15:38:35 +00:00
if (this.eventType != 'day') {
let weekDays = [];
2019-09-25 18:06:42 +00:00
2019-10-23 15:38:35 +00:00
for (let wday in selected.wdays) {
if (selected.wdays[wday])
weekDays.push(wday);
}
2019-09-25 18:06:42 +00:00
2019-10-23 15:38:35 +00:00
selected.weekDays = weekDays.join(',');
} else {
selected.to = null;
selected.weekDays = '';
2019-09-25 18:06:42 +00:00
}
2019-10-23 15:38:35 +00:00
let req;
2019-09-25 18:06:42 +00:00
2019-10-23 15:38:35 +00:00
if (this.isNew)
req = this.$http.post(this.path, selected);
else
req = this.$http.put(`${this.path}/${selected.id}`, selected);
2019-09-25 18:06:42 +00:00
2019-10-30 15:57:14 +00:00
return req.then(() => {
2019-10-23 15:38:35 +00:00
this.selected = null;
this.isNew = null;
this.refresh();
});
2019-10-30 15:57:14 +00:00
}
case 'DELETE':
return this.onDelete(this.selected.id)
.then(response => response == 'accept');
2019-10-23 15:38:35 +00:00
}
2019-09-25 18:06:42 +00:00
}
2019-10-23 15:38:35 +00:00
onDeleteClick(id, event) {
if (event.defaultPrevented) return;
2019-09-25 18:06:42 +00:00
event.preventDefault();
2019-10-23 15:38:35 +00:00
this.onDelete(id);
}
onDelete(id) {
2019-10-30 15:57:14 +00:00
return this.$.confirm.show(
response => this.onDeleteResponse(response, id));
2019-09-25 18:06:42 +00:00
}
2019-10-30 15:57:14 +00:00
onDeleteResponse(response, id) {
if (response != 'accept' || !id) return;
return this.$http.delete(`${this.path}/${id}`)
.then(() => this.refresh());
2019-09-25 18:06:42 +00:00
}
2019-10-23 15:38:35 +00:00
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());
}
2019-09-25 18:06:42 +00:00
}
Controller.$inject = ['$element', '$scope', 'vnWeekDays'];
2019-09-25 18:06:42 +00:00
ngModule.component('vnZoneEvents', {
template: require('./index.html'),
controller: Controller
});