177 lines
4.5 KiB
JavaScript
177 lines
4.5 KiB
JavaScript
import ngModule from '../module';
|
|
import Section from 'salix/components/section';
|
|
|
|
class Controller extends Section {
|
|
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 = weekDays
|
|
.split(',')
|
|
.map(wday => this.vnWeekDays.map[wday].localeAbr);
|
|
|
|
return abrWdays.length < 7
|
|
? abrWdays.join(', ')
|
|
: this.$t('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;
|
|
this.selected = angular.copy(row);
|
|
this.selected.wdays = this.vnWeekDays.fromSet(row.weekDays);
|
|
this.$.dialog.show();
|
|
}
|
|
|
|
create(days, type, weekday) {
|
|
this.isNew = true;
|
|
|
|
if (type == 'weekday') {
|
|
let wdays = [];
|
|
wdays[weekday] = true;
|
|
|
|
this.selected = {
|
|
type: 'indefinitely',
|
|
wdays
|
|
};
|
|
} else {
|
|
this.selected = {
|
|
type: 'day',
|
|
dated: days[0]
|
|
};
|
|
}
|
|
|
|
this.$.dialog.show();
|
|
}
|
|
|
|
onIncludeResponse(response) {
|
|
switch (response) {
|
|
case 'accept': {
|
|
let selected = this.selected;
|
|
let type = selected.type;
|
|
|
|
selected.weekDays = this.vnWeekDays.toSet(selected.wdays);
|
|
|
|
if (type == 'day')
|
|
selected.weekDays = '';
|
|
else
|
|
selected.dated = null;
|
|
|
|
if (type != 'range') {
|
|
selected.started = null;
|
|
selected.ended = null;
|
|
}
|
|
|
|
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(dated => {
|
|
return {dated};
|
|
});
|
|
|
|
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
|
|
});
|