import ngModule from '../module'; import Section from 'salix/components/section'; class Controller extends Section { constructor($element, $, vnWeekDays) { super($element, $); this.vnWeekDays = vnWeekDays; this.editMode = 'include'; this.path = `Zones/${this.$params.id}/events`; this.exclusionsPath = `Zones/${this.$params.id}/exclusions`; this.refresh(); } refresh() { 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; }); } 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) { if (this.editMode == 'include') { if (events.length) this.edit(events[0]); else this.create(type, days, weekday); } else { if (exclusions.length) this.exclusionDelete(exclusions); else this.exclusionCreate(days); } } 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.$.dialog.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.$.dialog.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'); } } 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()); } } Controller.$inject = ['$element', '$scope', 'vnWeekDays']; ngModule.component('vnZoneEvents', { template: require('./index.html'), controller: Controller });