import ngModule from '../module'; import Section from 'core/lib/section'; import './style.scss'; class Controller extends Section { constructor($el, $, $t, $http, $state) { super($el, $, $t, $http, $state); 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); this.abrWdays[wday.code] = locale.substr(0, 3); wday.abr = locale.substr(0, 1); } this.$http.get(`/api/Zones/${this.$stateParams.id}/exclusions`) .then(res => this.$.exclusions = res.data); this.path = `/api/Zones/${this.$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._('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($day, $type, $weekday) { 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]}; 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; }); } } ngModule.component('vnZoneEvents', { template: require('./index.html'), controller: Controller });