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

317 lines
8.1 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.days;
}
$onInit() {
this.refresh();
}
get path() {
return `Zones/${this.$params.id}/events`;
}
get exclusionsPath() {
return `Zones/${this.$params.id}/exclusions`;
}
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, geoExclusions) {
if (this.editMode == 'include') {
if (events.length)
this.edit(events[0]);
else
this.create(type, days, weekday);
} else {
if (geoExclusions.length)
this.editExclusion(geoExclusions);
else if (exclusions.length)
this.editExclusion(exclusions);
else
this.createExclusion(exclusions, days);
}
}
editExclusion(rows) {
this.isNew = false;
this.exclusions = rows;
this.excludeSelected = angular.copy(rows[0]);
if (this.excludeSelected.geoFk)
this.excludeSelected.type = 'specificLocations';
else
this.excludeSelected.type = 'all';
console.log(this.excludeSelected);
// const geos = this.$.model.data || [];
// for (let geo of geos) {
// for (let row of rows) {
// if (geo.id == row.geoFk) {
// geo.checked = true;
// console.log(geo);
// }
// }
// }
this.$.excludeDialog.show();
}
createExclusion(exclusions, days) {
this.isNew = true;
this.excludeSelected = {
type: 'all',
dated: days[0]
};
this.exclusions = exclusions;
this.days = days;
this.$.excludeDialog.show();
}
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.$.includeDialog.show();
}
create(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) {
console.log(this.$.model.data);
switch (response) {
case 'accept': {
let excludeSelected = this.excludeSelected;
let type = excludeSelected.type;
if (type == 'all')
return this.exclusionCreate(this.days);
else {
const geoIds = [];
for (let zoneGeo of this.checked) {
geoIds.push({
id: zoneGeo.id
});
}
const params = {
zoneFk: parseInt(this.$params.id),
date: this.days[0],
geoIds: geoIds
};
return this.$http.post(`Zones/exclusionGeo`, params)
.then(() => this.refresh());
}
}
case 'delete':
return this.exclusionDelete(this.exclusions);
}
}
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(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.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;
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'
}
});