@lipagas/fleetops-engine
Version:
Fleet & Transport Management Extension for Fleetbase
162 lines (142 loc) • 4.08 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 { isArray } from '@ember/array';
import FuelReportPanelDetailsComponent from './fuel-report-panel/details';
import contextComponentCallback from '@lipagas/ember-core/utils/context-component-callback';
import applyContextComponentArguments from '@lipagas/ember-core/utils/apply-context-component-arguments';
export default class FuelReportPanelComponent extends Component {
/**
* Service for fetching data.
*
* @type {Service}
*/
fetch;
/**
* Service for managing modals.
*
* @type {Service}
*/
modalsManager;
/**
* Universe service for managing global data and settings.
*
* @type {Service}
*/
universe;
/**
* Ember data store service.
*
* @type {Service}
*/
store;
/**
* Service for managing routing within the host app.
*
* @type {Service}
*/
hostRouter;
/**
* Service for managing the context panel.
*
* @type {Service}
*/
contextPanel;
/**
* The current active tab.
*
* @type {Object}
* @tracked
*/
tab;
/**
* The fuel-report being displayed or edited.
*
* @type {fuelReport}
* @tracked
*/
fuelReport;
/**
* Returns the array of tabs available for the panel.
*
* @type {Array}
*/
get tabs() {
const registeredTabs = this.universe.getMenuItemsFromRegistry('component:fuel-report-panel');
// this.universe._createMenuItem('Tracking', null, { icon: 'satellite-dish', component: VehiclePanelTrackingComponent }),
const defaultTabs = [this.universe._createMenuItem('Details', null, { icon: 'circle-info', component: FuelReportPanelDetailsComponent })];
if (isArray(registeredTabs)) {
return [...defaultTabs, ...registeredTabs];
}
return defaultTabs;
}
/**
* Initializes the fuel-report panel component.
*/
constructor() {
super(...arguments);
this.fuelReport = this.args.fuelReport;
this.tab = this.getTabUsingSlug(this.args.tab);
applyContextComponentArguments(this);
}
/**
* Sets the overlay context.
*
* @action
* @param {OverlayContextObject} overlayContext
*/
setOverlayContext(overlayContext) {
this.context = overlayContext;
contextComponentCallback(this, 'onLoad', ...arguments);
}
/**
* Handles changing the active tab.
*
* @method
* @param {String} tab - The new tab to switch to.
* @action
*/
onTabChanged(tab) {
this.tab = this.getTabUsingSlug(tab);
contextComponentCallback(this, 'onTabChanged', tab);
}
/**
* Handles edit action for the fuel-report.
*
* @method
* @action
*/
onEdit() {
const isActionOverrided = contextComponentCallback(this, 'onEdit', this.fuelReport);
if (!isActionOverrided) {
this.contextPanel.focus(this.fuelReport, 'editing', {
onAfterSave: () => {
this.contextPanel.clear();
},
});
}
}
/**
* Handles the cancel action.
*
* @method
* @action
* @returns {Boolean} Indicates whether the cancel action was overridden.
*/
onPressCancel() {
return contextComponentCallback(this, 'onPressCancel', this.fuelReport);
}
/**
* Finds and returns a tab based on its slug.
*
* @param {String} tabSlug - The slug of the tab.
* @returns {Object|null} The found tab or null.
*/
getTabUsingSlug(tabSlug) {
if (tabSlug) {
return this.tabs.find(({ slug }) => slug === tabSlug);
}
return this.tabs[0];
}
}