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

162 lines
3.9 KiB
JavaScript

import ngModule from '../module';
import './style.scss';
class Controller {
constructor($, _, $http, $stateParams) {
Object.assign(this, {
$,
_,
$http
});
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 = _.instant(wday.name);
this.abrWdays[wday.code] = locale.substr(0, 3);
wday.abr = locale.substr(0, 1);
}
this.$http.get(`/api/Zones/${$stateParams.id}/exclusions`)
.then(res => this.$.exclusions = res.data);
this.path = `/api/Zones/${$stateParams.id}/events`;
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(', ')
: this._.instant('Everyday');
}
onEdit(row, event) {
if (event.defaultPrevented) return;
this.isNew = false;
if (row.from && !row.to)
this.eventType = 'day';
else if (!row.from)
this.eventType = 'indefinitely';
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() {
this.isNew = true;
this.eventType = 'day';
this.selected = {};
this.$.dialog.show();
}
onSave(response) {
if (response != 'ACCEPT') return;
let selected = this.selected;
if (this.eventType == 'indefinitely') {
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;
});
}
}
Controller.$inject = ['$scope', '$translate', '$http', '$stateParams'];
ngModule.component('vnZoneEvents', {
template: require('./index.html'),
controller: Controller
});