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

167 lines
4.1 KiB
JavaScript
Raw Normal View History

2019-09-25 18:06:42 +00:00
import ngModule from '../module';
import Section from 'core/lib/section';
2019-09-25 18:06:42 +00:00
import './style.scss';
class Controller extends Section {
constructor($el, $, $t, $http, $state) {
super($el, $, $t, $http, $state);
2019-09-25 18:06:42 +00:00
this.wdays = [
{
code: 'mon',
name: 'Monday'
}, {
code: 'tue',
name: 'Tuesday'
}, {
code: 'wed',
name: 'Wednesday'
}, {
code: 'thu',
name: 'Thursday'
}, {
code: 'fri',
name: 'Friday'
}, {
code: 'sat',
name: 'Saturday'
}, {
code: 'sun',
name: 'Sunday'
}
];
this.abrWdays = {};
for (let wday of this.wdays) {
let locale = this._(wday.name);
2019-09-25 18:06:42 +00:00
this.abrWdays[wday.code] = locale.substr(0, 3);
wday.abr = locale.substr(0, 1);
}
this.$http.get(`/api/Zones/${this.$stateParams.id}/exclusions`)
2019-09-25 18:06:42 +00:00
.then(res => this.$.exclusions = res.data);
this.path = `/api/Zones/${this.$stateParams.id}/events`;
2019-09-25 18:06:42 +00:00
this.refresh();
}
refresh() {
this.$http.get(this.path)
.then(res => this.$.data = res.data);
}
formatWdays(weekDays) {
if (!weekDays) return;
let abrWdays = [];
for (let wday of weekDays.split(','))
abrWdays.push(this.abrWdays[wday]);
return abrWdays.length < 7
? abrWdays.join(', ')
2019-10-01 14:31:33 +00:00
: this._('Everyday');
2019-09-25 18:06:42 +00:00
}
onEdit(row, event) {
if (event.defaultPrevented) return;
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();
}
onCreate($day, $type, $weekday) {
2019-09-25 18:06:42 +00:00
this.isNew = true;
this.eventType = $type == 'day' ? 'day' : 'indefinitely';
if ($type == 'weekday') {
let wdays = [];
let index = $weekday - 1;
if (index < 0) index = 7 - index;
wdays[this.wdays[index].code] = true;
this.selected = {wdays};
} else
this.selected = {from: $day[0]};
2019-09-25 18:06:42 +00:00
this.$.dialog.show();
}
onSave(response) {
if (response != 'ACCEPT') return;
let selected = this.selected;
2019-09-25 21:09:10 +00:00
if (this.eventType == 'indefinitely') {
2019-09-25 18:06:42 +00:00
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);
req.then(() => {
this.selected = null;
this.isNew = null;
this.$.dialog.hide();
this.refresh();
});
return false;
}
onDelete(id, event) {
event.preventDefault();
this.$.confirm.show();
this.deleteId = id;
}
delete(response) {
if (response != 'ACCEPT') return;
if (!this.deleteId) return;
this.$http.delete(`${this.path}/${this.deleteId}`)
.then(() => {
this.refresh();
this.deleteId = null;
});
}
}
ngModule.component('vnZoneEvents', {
template: require('./index.html'),
controller: Controller
});