2019-09-25 18:06:42 +00:00
|
|
|
import ngModule from '../module';
|
2019-10-24 10:44:36 +00:00
|
|
|
import Component from 'core/lib/component';
|
2019-09-25 18:06:42 +00:00
|
|
|
|
2019-10-24 10:44:36 +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
|
|
|
|
2019-10-24 22:53:53 +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-01 11:45:43 +00:00
|
|
|
|
2019-10-23 15:38:35 +00:00
|
|
|
if (type == 'weekday') {
|
2019-10-01 11:45:43 +00:00
|
|
|
let wdays = [];
|
2019-10-23 15:38:35 +00:00
|
|
|
let code = this.vnWeekDays.days[weekday].code;
|
|
|
|
wdays[code] = true;
|
2019-10-01 11:45:43 +00:00
|
|
|
this.selected = {wdays};
|
|
|
|
} else
|
2019-10-23 15:38:35 +00:00
|
|
|
this.selected = {from: days[0]};
|
2019-10-01 11:45:43 +00:00
|
|
|
|
2019-09-25 18:06:42 +00:00
|
|
|
this.$.dialog.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
onSave(response) {
|
2019-10-23 15:38:35 +00:00
|
|
|
if (response == 'ACCEPT') {
|
|
|
|
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-23 15:38:35 +00:00
|
|
|
req.then(() => {
|
|
|
|
this.selected = null;
|
|
|
|
this.isNew = null;
|
|
|
|
this.$.dialog.hide();
|
|
|
|
this.refresh();
|
|
|
|
});
|
2019-09-25 18:06:42 +00:00
|
|
|
|
2019-10-23 15:38:35 +00:00
|
|
|
return false;
|
|
|
|
} else if (response == 'DELETE') {
|
|
|
|
this.onDelete(this.selected.id);
|
|
|
|
return false;
|
|
|
|
}
|
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-09-25 18:06:42 +00:00
|
|
|
this.deleteId = id;
|
2019-10-23 15:38:35 +00:00
|
|
|
this.$.confirm.show();
|
2019-09-25 18:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete(response) {
|
|
|
|
if (response != 'ACCEPT') return;
|
|
|
|
if (!this.deleteId) return;
|
|
|
|
this.$http.delete(`${this.path}/${this.deleteId}`)
|
|
|
|
.then(() => {
|
|
|
|
this.refresh();
|
|
|
|
this.deleteId = null;
|
2019-10-23 15:38:35 +00:00
|
|
|
this.$.dialog.hide();
|
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
|
|
|
}
|
2019-10-24 10:44:36 +00:00
|
|
|
Controller.$inject = ['$element', '$scope', 'vnWeekDays'];
|
2019-09-25 18:06:42 +00:00
|
|
|
|
|
|
|
ngModule.component('vnZoneEvents', {
|
|
|
|
template: require('./index.html'),
|
|
|
|
controller: Controller
|
|
|
|
});
|