@ducna01120/fleetops-engine
Version:
Fleet & Transport Management Extension for Fleetbase
139 lines (119 loc) • 4.05 kB
JavaScript
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { later } from '@ember/runloop';
import { classify } from '@ember/string';
import { calculateInPlacePosition } from 'ember-basic-dropdown/utils/calculate-position';
import { task } from 'ember-concurrency-decorators';
/**
* @class MapContainerToolbarZonesPanelComponent
* @extends {Component}
* @memberof @ducna01120/fleetops-engine
*/
export default class MapContainerToolbarZonesPanelComponent extends Component {
/**
* Ember Data store service.
* @type {Service}
* @memberof MapContainerToolbarZonesPanelComponent
*/
store;
/**
* Service areas service.
* @type {Service}
* @memberof MapContainerToolbarZonesPanelComponent
*/
serviceAreas;
/**
* Application cache service.
* @type {Service}
* @memberof MapContainerToolbarZonesPanelComponent
*/
appCache;
/**
* Indicates if the component is in a loading state.
* @type {boolean}
* @memberof MapContainerToolbarZonesPanelComponent
*/
isLoading = false;
/**
* Holds the records for service areas.
* @type {Array}
* @memberof MapContainerToolbarZonesPanelComponent
*/
serviceAreaRecords = [];
/**
* Reference to the map instance.
* @type {L.Map}
* @memberof MapContainerToolbarZonesPanelComponent
*/
map;
/**
* Reference to the live map component instance.
* @type {LiveMapComponent}
* @memberof MapContainerToolbarZonesPanelComponent
*/
liveMap;
/**
* Class constructor.
* @memberof MapContainerToolbarZonesPanelComponent
*/
constructor() {
super(...arguments);
this.map = this.args.map;
this.liveMap = this.args.liveMap;
this.serviceAreas.setMapInstance(this.args.map);
later(
this,
() => {
this.fetchServiceAreas.perform();
},
100
);
}
/**
* Calculate position for dropdown.
* @param {Element} trigger The triggering element
* @returns {Object} Position style object
* @memberof MapContainerToolbarZonesPanelComponent
*/
calculatePosition(trigger) {
const position = calculateInPlacePosition(...arguments);
const rect = trigger.getBoundingClientRect();
position.style.top = '-0.5rem';
position.style.left = `calc(${rect.width}px + 0.75rem)`;
return position;
}
/**
* Generic callback trigger function.
* @param {string} callbackFn Callback function name
* @param {...any} params Parameters for the callback function
* @memberof MapContainerToolbarZonesPanelComponent
*/
triggerCallback(callbackFn, ...params) {
const tryInvoke = (context, fnName) => {
if (context && typeof context[fnName] === 'function') {
context[fnName](...params);
}
};
tryInvoke(this, callbackFn);
tryInvoke(this.args, callbackFn);
tryInvoke(this.liveMap, callbackFn);
tryInvoke(this.serviceAreas, callbackFn);
// Additional check for event callback function in args
const eventCallbackFn = `on${classify(callbackFn)}`;
tryInvoke(this.args, eventCallbackFn);
}
/**
* Fetch service areas and update state.
* @memberof MapContainerToolbarZonesPanelComponent
*/
*fetchServiceAreas() {
if (this.appCache.has('serviceAreas')) {
this.serviceAreaRecords = this.appCache.getEmberData('serviceAreas', 'service-area');
}
const serviceAreas = yield this.store.query('service-area', { with: ['zones'] });
this.serviceAreaRecords = serviceAreas;
this.appCache.setEmberData('serviceAreas', serviceAreas);
}
}