salix/modules/travel/front/extra-community/index.js

118 lines
3.3 KiB
JavaScript

import ngModule from '../module';
import Section from 'salix/components/section';
import './style.scss';
class Controller extends Section {
constructor($element, $, vnReport) {
super($element, $);
this.vnReport = vnReport;
const draggable = this.element.querySelector('.travel-list');
draggable.addEventListener('dragstart',
event => this.dragStart(event));
draggable.addEventListener('dragend',
event => this.dragEnd(event));
this.draggableElement = 'a[draggable]';
this.droppableElement = 'vn-tbody[vn-droppable]';
const twoDays = 2;
const shippedFrom = new Date();
shippedFrom.setDate(shippedFrom.getDate() - twoDays);
shippedFrom.setHours(0, 0, 0, 0);
const sevenDays = 7;
const landedTo = new Date();
landedTo.setDate(landedTo.getDate() + sevenDays);
landedTo.setHours(23, 59, 59, 59);
this.defaultFilter = {
shippedFrom: shippedFrom,
landedTo: landedTo,
continent: 'AM'
};
}
get hasDateRange() {
const userParams = this.$.model.userParams;
const hasLanded = userParams.landedTo;
const hasShipped = userParams.shippedFrom;
const hasContinent = userParams.continent;
const hasWarehouseOut = userParams.warehouseOutFk;
return hasLanded || hasShipped || hasContinent || hasWarehouseOut;
}
findDraggable($event) {
const target = $event.target;
const draggable = target.closest(this.draggableElement);
return draggable;
}
findDroppable($event) {
const target = $event.target;
const droppable = target.closest(this.droppableElement);
return droppable;
}
dragStart($event) {
const draggable = this.findDraggable($event);
draggable.classList.add('dragging');
const id = parseInt(draggable.id);
this.entryId = id;
this.entry = draggable;
}
dragEnd($event) {
const draggable = this.findDraggable($event);
draggable.classList.remove('dragging');
this.entryId = null;
this.entry = null;
}
onDrop($event) {
const model = this.$.model;
const droppable = this.findDroppable($event);
const travelId = parseInt(droppable.id);
const currentDroppable = this.entry.closest(this.droppableElement);
if (currentDroppable == droppable) return;
if (this.entryId && travelId) {
const path = `Entries/${this.entryId}`;
this.$http.patch(path, {travelFk: travelId})
.then(() => model.refresh())
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')));
}
}
save(id, data) {
const endpoint = `Travels/${id}`;
this.$http.patch(endpoint, data)
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')));
}
get reportParams() {
const userParams = this.$.model.userParams;
return Object.assign({
authorization: this.vnToken.token
}, userParams);
}
showReport() {
this.vnReport.show('extra-community', this.reportParams);
}
}
Controller.$inject = ['$element', '$scope', 'vnReport'];
ngModule.vnComponent('vnTravelExtraCommunity', {
template: require('./index.html'),
controller: Controller
});