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

386 lines
10 KiB
JavaScript

import ngModule from '../module';
import Section from 'salix/components/section';
import './style.scss';
class Controller extends Section {
constructor($element, $, vnWeekDays) {
super($element, $);
this.vnWeekDays = vnWeekDays;
this.editMode = 'exclude';
this.exclusions;
this.exclusionsGeo;
}
$onInit() {
this.refresh();
}
get path() {
return `Zones/${this.$params.id}/events`;
}
get exclusionsPath() {
return `Zones/${this.$params.id}/exclusions`;
}
get exclusionsGeoPath() {
return `Zones/exclusionGeo`;
}
get checked() {
const geos = this.$.model.data || [];
const checkedLines = [];
for (let geo of geos) {
if (geo.checked)
checkedLines.push(geo);
}
return checkedLines;
}
refresh() {
this.$.data = null;
this.$.$applyAsync(() => {
const params = {
zoneFk: this.$params.id,
started: this.$.calendar.firstDay,
ended: this.$.calendar.lastDay
};
this.$http.get(`Zones/getEventsFiltered`, {params}).then(res => {
const data = res.data;
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, events, exclusions, exclusionsGeo) {
if (this.editMode == 'include') {
if (events.length)
this.editInclusion(events[0]);
else
this.createInclusion(type, days, weekday);
} else {
if (exclusionsGeo.length) {
this.exclusionsGeo = exclusionsGeo;
this.editExclusion();
} else if (exclusions.length) {
this.exclusions = exclusions;
this.editExclusion();
} else
this.createExclusion(days);
}
}
editExclusion() {
this.isNew = false;
if (this.exclusionsGeo && this.exclusionsGeo.length) {
this.excludeSelected = angular.copy(this.exclusionsGeo[0]);
this.excludeSelected.type = 'specificLocations';
}
if (this.exclusions && this.exclusions.length) {
this.excludeSelected = angular.copy(this.exclusions[0]);
this.excludeSelected.type = 'all';
}
this.$.excludeDialog.show();
}
createExclusion(days) {
this.isNew = true;
this.excludeSelected = {
type: 'all',
dated: days[0]
};
this.exclusionsGeo = [];
this.$.excludeDialog.show();
}
onEditClick(row, event) {
if (event.defaultPrevented) return;
this.editInclusion(row);
}
editInclusion(row) {
this.isNew = false;
this.selected = angular.copy(row);
this.selected.wdays = this.vnWeekDays.fromSet(row.weekDays);
this.$.includeDialog.show();
}
createInclusion(type, days, weekday) {
this.isNew = true;
if (type == 'weekday') {
let wdays = [];
if (weekday) wdays[weekday] = true;
this.selected = {
type: 'indefinitely',
wdays
};
} else {
this.selected = {
type: 'day',
dated: days[0]
};
}
this.$.includeDialog.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');
}
}
onExcludeResponse(response) {
const type = this.excludeSelected.type;
switch (response) {
case 'accept': {
if (type == 'all')
return this.exclusionCreate();
else
return this.exclusionGeoCreate();
}
case 'delete':
if (type == 'all')
return this.exclusionDelete(this.exclusions);
if (type == 'specificLocations')
return this.exclusionGeoDelete(this.exclusionsGeo);
}
}
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() {
const excludeSelected = this.excludeSelected;
const dated = excludeSelected.dated;
let req;
if (this.isNew)
req = this.$http.post(this.exclusionsPath, [{dated}]);
else
req = this.$http.put(`${this.exclusionsPath}/${excludeSelected.id}`, {dated});
return req.then(() => {
this.excludeSelected = null;
this.isNew = null;
this.refresh();
});
}
exclusionGeoCreate() {
const excludeSelected = this.excludeSelected;
let req;
const geoIds = [];
for (let zoneGeo of this.checked) {
geoIds.push({
id: zoneGeo.id
});
}
const params = {
zoneFk: parseInt(this.$params.id),
date: excludeSelected.dated,
geoIds: geoIds
};
if (this.isNew)
req = this.$http.post(this.exclusionsGeoPath, params);
else {
let actualPosition = 0;
let geoId;
for (let exclusionGeo of this.exclusionsGeo) {
if (geoIds[actualPosition]) geoId = geoIds[actualPosition].id;
else geoId = null;
if (geoId && geoId == exclusionGeo.geoFk) {
const params = {
zoneExclusionFk: exclusionGeo.zoneExclusionFk,
geoFk: geoId
};
req = this.$http.put(`ZoneExclusionGeos/${exclusionGeo.id}`, params);
actualPosition++;
} else
req = this.$http.delete(`ZoneExclusionGeos/${exclusionGeo.id}`);
}
while (actualPosition <= geoIds.length - 1) {
const params = {
zoneExclusionFk: excludeSelected.zoneExclusionFk,
geoFk: geoIds[actualPosition].id
};
req = this.$http.post(`ZoneExclusionGeos`, params);
actualPosition++;
}
}
return req.then(() => {
this.excludeSelected = null;
this.isNew = null;
this.refresh();
});
}
exclusionDelete(exclusions) {
let reqs = [];
for (let exclusion of exclusions) {
if (!exclusion.id) continue;
let path = `${this.exclusionsPath}/${exclusion.id}`;
reqs.push(this.$http.delete(path));
}
this.$q.all(reqs)
.then(() => {
this.excludeSelected = null;
this.exclusions = null;
this.refresh();
});
}
exclusionGeoDelete(exclusionsGeo) {
let reqs = [];
for (let exclusionGeo of exclusionsGeo) {
if (!exclusionGeo.id) continue;
let path = `${this.exclusionsPath}/${exclusionGeo.zoneExclusionFk}`;
reqs.push(this.$http.delete(path));
}
this.$q.all(reqs)
.then(() => {
this.excludeSelected = null;
this.exclusionsGeo = null;
this.refresh();
});
}
set excludeSearch(value) {
this._excludeSearch = value;
if (!value) this.onSearch();
}
get excludeSearch() {
return this._excludeSearch;
}
onKeyDown(event) {
if (event.key == 'Enter') {
event.preventDefault();
this.onSearch();
}
}
onSearch() {
const params = {search: this._excludeSearch};
if (this.excludeSelected.type == 'specificLocations') {
this.$.model.applyFilter({}, params).then(() => {
const data = this.$.model.data;
for (let geo of data) {
for (let exclusionGeo of this.exclusionsGeo) {
if (geo.id == exclusionGeo.geoFk)
geo.checked = true;
}
}
this.$.treeview.data = data;
});
}
}
onFetch(item) {
const params = item ? {parentId: item.id} : null;
return this.$.model.applyFilter({}, params)
.then(() => this.$.model.data);
}
onSort(a, b) {
if (b.selected !== a.selected) {
if (a.selected == null)
return 1;
if (b.selected == null)
return -1;
return b.selected - a.selected;
}
return a.name.localeCompare(b.name);
}
exprBuilder(param, value) {
switch (param) {
case 'search':
return {name: {like: `%${value}%`}};
}
}
}
Controller.$inject = ['$element', '$scope', 'vnWeekDays'];
ngModule.vnComponent('vnZoneEvents', {
template: require('./index.html'),
controller: Controller,
bindings: {
zone: '<'
},
require: {
card: '^vnZoneCard'
}
});