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

170 lines
4.9 KiB
JavaScript
Raw Normal View History

2020-11-11 11:34:07 +00:00
import ngModule from '../module';
import Section from 'salix/components/section';
import './style.scss';
2020-11-20 14:05:57 +00:00
class Controller extends Section {
2021-01-18 07:33:20 +00:00
constructor($element, $, vnReport) {
2020-11-20 14:05:57 +00:00
super($element, $);
2021-01-18 07:33:20 +00:00
this.vnReport = vnReport;
2020-11-20 14:05:57 +00:00
const draggable = this.element.querySelector('.travel-list');
draggable.addEventListener('dragstart',
event => this.dragStart(event));
draggable.addEventListener('dragend',
event => this.dragEnd(event));
draggable.addEventListener('dragover',
event => this.dragOver(event));
draggable.addEventListener('dragenter',
event => this.dragEnter(event));
draggable.addEventListener('dragleave',
event => this.dragLeave(event));
this.draggableElement = 'tr[draggable]';
this.droppableElement = 'tbody[vn-droppable]';
2020-11-20 14:05:57 +00:00
2022-03-29 08:17:26 +00:00
const twoDays = 2;
2022-03-28 11:24:26 +00:00
const shippedFrom = new Date();
2022-03-29 08:17:26 +00:00
shippedFrom.setDate(shippedFrom.getDate() - twoDays);
2022-03-28 11:24:26 +00:00
shippedFrom.setHours(0, 0, 0, 0);
2020-11-20 14:05:57 +00:00
2022-03-28 11:24:26 +00:00
const sevenDays = 7;
2020-11-20 14:05:57 +00:00
const landedTo = new Date();
2022-03-28 11:24:26 +00:00
landedTo.setDate(landedTo.getDate() + sevenDays);
2020-11-20 14:05:57 +00:00
landedTo.setHours(23, 59, 59, 59);
this.defaultFilter = {
2022-03-28 11:24:26 +00:00
shippedFrom: shippedFrom,
2020-11-20 14:05:57 +00:00
landedTo: landedTo,
continent: 'AM'
};
this.smartTableOptions = {};
2020-11-20 14:05:57 +00:00
}
2021-07-02 12:35:24 +00:00
get hasDateRange() {
const userParams = this.$.model.userParams;
2022-03-28 11:24:26 +00:00
const hasLanded = userParams.landedTo;
const hasShipped = userParams.shippedFrom;
2022-03-28 09:57:19 +00:00
const hasContinent = userParams.continent;
const hasWarehouseOut = userParams.warehouseOutFk;
2021-07-02 12:35:24 +00:00
2022-03-28 09:57:19 +00:00
return hasLanded || hasShipped || hasContinent || hasWarehouseOut;
2021-07-02 12:35:24 +00:00
}
onDragInterval() {
if (this.dragClientY > 0 && this.dragClientY < 75)
this.$window.scrollTo(0, this.$window.scrollY - 10);
const maxHeight = window.screen.availHeight - (window.outerHeight - window.innerHeight);
if (this.dragClientY > maxHeight - 75 && this.dragClientY < maxHeight)
this.$window.scrollTo(0, this.$window.scrollY + 10);
}
2020-11-20 14:05:57 +00:00
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');
2020-11-23 07:12:33 +00:00
const id = parseInt(draggable.id);
2020-11-20 14:05:57 +00:00
this.entryId = id;
this.entry = draggable;
this.interval = setInterval(() => this.onDragInterval(), 50);
2020-11-20 14:05:57 +00:00
}
dragEnd($event) {
const draggable = this.findDraggable($event);
draggable.classList.remove('dragging');
this.entryId = null;
this.entry = null;
clearInterval(this.interval);
2020-11-20 14:05:57 +00:00
}
onDrop($event) {
const model = this.$.model;
const droppable = this.findDroppable($event);
2020-11-23 07:12:33 +00:00
const travelId = parseInt(droppable.id);
2020-11-20 14:05:57 +00:00
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!')));
}
}
undrop() {
if (!this.dropping) return;
this.dropping.classList.remove('dropping');
this.dropping = null;
}
dragOver($event) {
this.dragClientY = $event.clientY;
$event.preventDefault();
}
dragEnter($event) {
let element = this.findDroppable($event);
if (element) this.dropCount++;
if (element != this.dropping) {
this.undrop();
if (element) element.classList.add('dropping');
this.dropping = element;
}
}
dragLeave($event) {
let element = this.findDroppable($event);
if (element) {
this.dropCount--;
if (this.dropCount == 0) this.undrop();
}
}
2021-01-07 13:21:17 +00:00
save(id, data) {
const endpoint = `Travels/${id}`;
this.$http.patch(endpoint, data)
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')));
}
2021-01-18 07:33:20 +00:00
get reportParams() {
const userParams = this.$.model.userParams;
return Object.assign({
authorization: this.vnToken.token
}, userParams);
}
showReport() {
this.vnReport.show('extra-community', this.reportParams);
}
2020-11-20 14:05:57 +00:00
}
2021-01-18 07:33:20 +00:00
Controller.$inject = ['$element', '$scope', 'vnReport'];
2020-11-11 11:34:07 +00:00
ngModule.vnComponent('vnTravelExtraCommunity', {
template: require('./index.html'),
2020-11-20 14:05:57 +00:00
controller: Controller
2020-11-11 11:34:07 +00:00
});