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

176 lines
4.4 KiB
JavaScript
Raw Normal View History

2019-09-25 18:06:42 +00:00
import ngModule from '../module';
2019-11-18 15:31:37 +00:00
import Section from 'salix/components/section';
2019-09-25 18:06:42 +00:00
2019-11-18 15:31:37 +00:00
class Controller extends Section {
constructor($element, $, vnWeekDays) {
super($element, $);
2019-10-23 15:38:35 +00:00
this.vnWeekDays = vnWeekDays;
this.editMode = 'include';
2020-06-18 10:43:33 +00:00
}
2019-09-25 18:06:42 +00:00
2020-06-18 10:43:33 +00:00
$onInit() {
2019-09-25 18:06:42 +00:00
this.refresh();
}
2020-06-18 10:43:33 +00:00
get path() {
return `Zones/${this.$params.id}/events`;
}
get exclusionsPath() {
return `Zones/${this.$params.id}/exclusions`;
}
2019-09-25 18:06:42 +00:00
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;
2019-11-18 15:31:37 +00:00
let abrWdays = weekDays
.split(',')
.map(wday => this.vnWeekDays.map[wday].localeAbr);
2019-09-25 18:06:42 +00:00
return abrWdays.length < 7
? abrWdays.join(', ')
2019-11-18 15:31:37 +00:00
: this.$t('Everyday');
2019-09-25 18:06:42 +00:00
}
2019-11-19 12:49:10 +00:00
onSelection(days, type, weekday, events, exclusions) {
2019-10-23 15:38:35 +00:00
if (this.editMode == 'include') {
2019-11-19 12:49:10 +00:00
if (events.length)
this.edit(events[0]);
2019-10-23 15:38:35 +00:00
else
this.create(type, days, weekday);
2019-10-23 15:38:35 +00:00
} else {
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;
this.selected = angular.copy(row);
2019-11-18 15:31:37 +00:00
this.selected.wdays = this.vnWeekDays.fromSet(row.weekDays);
2019-09-25 18:06:42 +00:00
this.$.dialog.show();
}
create(type, days, weekday) {
2019-09-25 18:06:42 +00:00
this.isNew = true;
2019-10-23 15:38:35 +00:00
if (type == 'weekday') {
let wdays = [];
if (weekday) wdays[weekday] = true;
2019-11-18 15:31:37 +00:00
this.selected = {
type: 'indefinitely',
wdays
};
} else {
this.selected = {
type: 'day',
dated: 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-11-18 15:31:37 +00:00
let type = selected.type;
2019-09-25 18:06:42 +00:00
2019-11-18 15:31:37 +00:00
selected.weekDays = this.vnWeekDays.toSet(selected.wdays);
2019-09-25 18:06:42 +00:00
2019-11-18 15:31:37 +00:00
if (type == 'day')
2019-10-23 15:38:35 +00:00
selected.weekDays = '';
2019-11-18 15:31:37 +00:00
else
selected.dated = null;
if (type != 'range') {
selected.started = null;
selected.ended = null;
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
}
2019-11-18 15:31:37 +00:00
case 'delete':
2019-10-30 15:57:14 +00:00
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) {
2019-11-18 15:31:37 +00:00
let exclusions = days.map(dated => {
return {dated};
2019-10-23 15:38:35 +00:00
});
this.$http.post(this.exclusionsPath, exclusions)
.then(() => this.refresh());
}
2019-11-19 12:49:10 +00:00
exclusionDelete(exclusions) {
let reqs = [];
2019-10-23 15:38:35 +00:00
2019-11-19 12:49:10 +00:00
for (let exclusion of exclusions) {
if (!exclusion.id) continue;
let path = `${this.exclusionsPath}/${exclusion.id}`;
reqs.push(this.$http.delete(path));
2019-10-23 15:38:35 +00:00
}
2019-11-19 12:49:10 +00:00
this.$q.all(reqs)
2019-10-23 15:38:35 +00:00
.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
});