Skip to content

Commit

Permalink
refator: Create panel components
Browse files Browse the repository at this point in the history
Less verbose internal panels creation

Expose single method for creating external panels with and sb buttons
  • Loading branch information
FilipLeitner authored and jmacura committed Feb 13, 2024
1 parent 9ab90dd commit 32c7855
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import {HsConfig} from '../../../config.service';
import {Injectable, Type} from '@angular/core';

import {HsButton} from '../../sidebar/button.interface';
import {HsPanelContainerService} from './panel-container.service';
import {HsSidebarService} from '../../sidebar/sidebar.service';
import {HsUtilsService} from '../../utils/utils.service';
import {skip} from 'rxjs';

@Injectable({
providedIn: 'root',
})
export class HsPanelConstructorService {
constructor(
private hsConfig: HsConfig,
private HsPanelContainerService: HsPanelContainerService,
private hsUtilsService: HsUtilsService,
private hsSidebarService: HsSidebarService,
) {
this.hsConfig.configChanges.pipe(skip(1)).subscribe(async () => {
/**
* Create panels activated after app init. Buttons are handled in sidebarService
*/
const activePanels = Object.entries(this.hsConfig.panelsEnabled).reduce(
(acc, [panel, isEnabled]) => (isEnabled ? [...acc, panel] : acc),
[],
);
const created = this.HsPanelContainerService.panels.map((p) => p.name);
const toBeCreated = activePanels.filter((p) => !created.includes(p));
for (const panel of toBeCreated) {
await this._createPanel(panel);
}
});
}

/**
* Create HSLayers panel component based on name string
*/
private async _createPanel(name: string, data?: any) {
const cName = `Hs${this.hsUtilsService.capitalizeFirstLetter(
name,
)}Component`;
const kebabName = this.hsUtilsService.camelToKebab(name);
const i = await import(`../../${kebabName}/${kebabName}.component`);
this.HsPanelContainerService.create(i[cName], data || {});
}

/**
* INTERNAL. You most likely want to use 'createPanelandButton' to create additional panel
* @param name - Name of panel used in panelsEnabled config
* @param data - Extra misc data object to be stored in panel
* @param buttonDefinition - HS Button definition object
*/
async _createPanelAndButton(
name: string,
data?: any,
buttonDefinition?: HsButton,
): Promise<void> {
this._createPanel(name, data);
this.hsSidebarService.addButton(
this.hsSidebarService.buttonDefinition[name] || buttonDefinition,
);
}

/**
* Creates additional panel and sidebar button
* @param name - Name of panel used in panelsEnabled config
* @param buttonDefinition - HS Button definition object
* @param data - Extra misc data object to be stored in panel
*/
createPanelAndButton(
component: Type<any>,
buttonDefinition: HsButton,
data?: any,
) {
this.HsPanelContainerService.create(component, data || {});
this.hsSidebarService.addButton(buttonDefinition);
}

/**
* Wrapper function which creates panels and corresponding sidebar buttons
* for panels which are set to be active in config.panelsEnabled
*/
createActivePanels() {
const activePanels = Object.entries(this.hsConfig.panelsEnabled).reduce(
(acc, [panel, isEnabled]) => (isEnabled ? [...acc, panel] : acc),
[],
);
for (const panel of activePanels) {
if (
this.hsSidebarService.buttonDefinition[panel] &&
this.hsConfig.panelsEnabled[panel]
) {
this._createPanelAndButton(panel, {});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {ReplaySubject, Subject} from 'rxjs';
import {HsPanelComponent} from './panel-component.interface';
import {HsPanelContainerServiceInterface} from './panel-container.service.interface';
import {HsPanelItem} from './panel-item';
import {KeyNumberDict} from '../../../config.service';

@Injectable({
providedIn: 'root',
Expand Down
23 changes: 19 additions & 4 deletions projects/hslayers/src/components/sidebar/sidebar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import {Component, OnDestroy, OnInit} from '@angular/core';

import {Subject, Subscription, debounceTime, takeUntil} from 'rxjs';

import {HS_PRMS} from '../permalink/get-params';
import {HS_PRMS} from '../share/get-params';
import {HsButton} from './button.interface';
import {HsConfig} from '../../config.service';
import {HsCoreService} from './../core/core.service';
import {HsEventBusService} from '../core/event-bus.service';
import {HsLayoutService} from '../layout/layout.service';
import {HsShareUrlService} from '../permalink/share-url.service';
import {HsShareUrlService} from '../share/share-url.service';
import {HsSidebarService} from './sidebar.service';

@Component({
Expand Down Expand Up @@ -73,7 +73,7 @@ export class HsSidebarComponent implements OnInit, OnDestroy {
*/
refreshButtons() {
const disabledPanels = Object.entries(this.HsConfig.panelsEnabled).reduce(
(acc, [panel, isEnabled]) => (!isEnabled ? [...acc, panel] : acc),
(acc, [panel, isEnabled]) => (isEnabled ? [...acc, panel] : acc),
[],
);

Expand All @@ -93,14 +93,29 @@ export class HsSidebarComponent implements OnInit, OnDestroy {
(acc, [panel, isEnabled]) => (isEnabled ? [...acc, panel] : acc),
[],
)
.filter((b) => !filteredButtonsPanels.includes(b));
.filter((b) =>
['search', 'measure'].includes(b)
? this.resloveBtnWithCondition(b)
: !filteredButtonsPanels.includes(b),
);

for (const b of toBeActivated) {
filteredButtons.push(this.HsSidebarService.buttonDefinition[b]);
}
this.HsSidebarService.buttonsSubject.next(filteredButtons);
}

/**
* Resolve wether search/measure buttons should be visible after config update.
*/
private resloveBtnWithCondition(panel: string) {
return !(
this.HsConfig.componentsEnabled['guiOverlay'] &&
this.HsConfig.componentsEnabled['toolbar'] &&
this.HsConfig.componentsEnabled[`${panel}Toolbar`]
);
}

/**
* Seat whether to show all sidebar buttons or just a
* subset of important ones
Expand Down
12 changes: 6 additions & 6 deletions projects/hslayers/src/components/sidebar/sidebar.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export class HsSidebarService {
description: 'SIDEBAR.descriptions.MAP_SWIPE',
icon: 'icon-resizehorizontalalt',
},
'layermanager': {
panel: 'layermanager',
'layerManager': {
panel: 'layerManager',
module: 'hs.layermanager',
order: 0,
fits: true,
Expand All @@ -49,8 +49,8 @@ export class HsSidebarService {
description: 'SIDEBAR.descriptions.ADDLAYERS',
icon: 'icon-database',
},
'composition_browser': {
panel: 'composition_browser',
'compositions': {
panel: 'compositions',
module: 'hs.compositions',
order: 3,
fits: true,
Expand Down Expand Up @@ -78,8 +78,8 @@ export class HsSidebarService {
return this.HsLanguageService.getCurrentLanguageCode().toUpperCase();
},
},
'permalink': {
panel: 'permalink',
'share': {
panel: 'share',
module: 'hs.permalink',
order: 11,
fits: true,
Expand Down
81 changes: 17 additions & 64 deletions projects/hslayers/src/hslayers.component.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,32 @@
import {Component, Input, OnInit, Type, ViewChild} from '@angular/core';
import {Component, Input, OnInit, ViewChild} from '@angular/core';

import {HsAddDataComponent} from './components/add-data/add-data.component';
import {HsCompositionsComponent} from './components/compositions/compositions.component';
import {HsConfig, HsConfigObject} from './config.service';
import {HsDrawComponent} from './components/draw/draw.component';

import {HsDrawToolbarComponent} from './components/draw/draw-toolbar/draw-toolbar.component';
// import {HsFeatureInfoComponent} from './components/query/query-popup-feature/feature-widgets/feature-info.component';
import {HsExternalService} from './components/external/external.service';
import {HsFeatureTableComponent} from './components/feature-table/feature-table.component';

import {HsGeolocationComponent} from './components/geolocation/geolocation.component';
import {HsInfoComponent} from './components/info/info.component';
import {HsLanguageComponent} from './components/language/language.component';
import {HsLayerManagerComponent} from './components/layermanager/layermanager.component';
import {HsLayerManagerGalleryComponent} from './components/layermanager/gallery/layermanager-gallery.component';
import {HsLayerManagerService} from './components/layermanager/layermanager.service';

import {HsLayerManagerGalleryComponent} from './components/layer-manager/gallery/layermanager-gallery.component';
import {HsLayoutComponent} from './components/layout/layout.component';
import {HsLayoutService} from './components/layout/layout.service';
import {HsLegendComponent} from './components/legend/legend.component';
import {HsMapSwipeComponent} from './components/map-swipe/map-swipe.component';

import {HsMapSwipeService} from './components/map-swipe/map-swipe.service';
import {HsMeasureComponent} from './components/measure/measure.component';

import {HsMeasureToolbarComponent} from './components/measure/measure-toolbar.component';
import {HsQueryComponent} from './components/query/query.component';

import {HsQueryPopupComponent} from './components/query/query-popup/query-popup.component';
import {HsQueryPopupService} from './components/query/query-popup.service';
import {HsQueryPopupWidgetContainerService} from './components/query/query-popup-widget-container.service';
import {HsSaveMapComponent} from './components/save-map/save-map.component';
import {HsSearchComponent} from './components/search/search.component';

import {HsSearchToolbarComponent} from './components/search/search-toolbar.component';
import {HsShareComponent} from './components/permalink/share.component';
import {HsStylerComponent} from './components/styles/styler.component';
import {HsToolbarComponent} from './components/toolbar/toolbar.component';
import {HsToolbarPanelContainerService} from './components/toolbar/toolbar-panel-container.service';
import {HsTripPlannerComponent} from './components/trip-planner/trip-planner.component';

import {HsOverlayPanelContainerService} from './components/layout/overlay-panel-container.service';
import {HsPanelConstructorService} from './components/layout/panels/panel-constructor.service';
import {HsPanelContainerService} from './components/layout/panels/panel-container.service';
import {HsSidebarService} from './components/sidebar/sidebar.service';

@Component({
// eslint-disable-next-line @angular-eslint/component-selector
Expand All @@ -51,65 +41,28 @@ export class HslayersComponent implements OnInit {

constructor(
public hsConfig: HsConfig,
private hsLayoutService: HsLayoutService,
private HsLayerManagerService: HsLayerManagerService,
private HsPanelContainerService: HsPanelContainerService,
private HsOverlayPanelContainerService: HsOverlayPanelContainerService,
private hsToolbarPanelContainerService: HsToolbarPanelContainerService,
private hsQueryPopupService: HsQueryPopupService,
private HsMapSwipeService: HsMapSwipeService, //Leave this, need to inject somewhere
private hsQueryPopupWidgetContainerService: HsQueryPopupWidgetContainerService, //Leave this, need to inject somewhere
private hsExternalService: HsExternalService, //Leave this, need to inject somewhere
private hsSidebarService: HsSidebarService,
private HsPanelContainerService: HsPanelContainerService,
private HsPanelConstructorService: HsPanelConstructorService,
) {}

/**
* Check if panel is configured to be visible in hsConfig.panelsEnabled
* or hsLayoutService.panelsEnabledDefaults and create one if so.
* @param name - Name of panel used in panelsEnabled config
* @param panelComponent - Class defining panel
* @param data - Extra misc data object to be stored in panel
*/
createPanel(name: string, panelComponent: Type<any>, data?: any): void {
if (this.hsConfig == undefined || this.hsConfig.panelsEnabled[name]) {
this.HsPanelContainerService.create(panelComponent, data || {});
}
}
ngOnInit(): void {
async ngOnInit(): Promise<void> {
if (this.config) {
this.hsConfig.update(this.config);
}
if (this.id) {
this.hsConfig.setAppId(this.id);
}

const activePanels = Object.entries(this.hsConfig.panelsEnabled).reduce(
(acc, [panel, isEnabled]) => (isEnabled ? [...acc, panel] : acc),
[],
);

for (const panel of activePanels) {
if (this.hsSidebarService.buttonDefinition[panel]) {
this.hsSidebarService.addButton(
this.hsSidebarService.buttonDefinition[panel],
);
}
}

this.createPanel('tripPlanner', HsTripPlannerComponent, {});
this.createPanel('addData', HsAddDataComponent, {});
this.createPanel('draw', HsDrawComponent, {});
this.createPanel('search', HsSearchComponent, {});
this.createPanel('feature_table', HsFeatureTableComponent, {});
this.createPanel('saveMap', HsSaveMapComponent, {});
this.createPanel('language', HsLanguageComponent, {});
this.createPanel('query', HsQueryComponent, {});
this.createPanel('permalink', HsShareComponent, {});
this.createPanel('measure', HsMeasureComponent, {});
this.createPanel('composition_browser', HsCompositionsComponent, {});
this.createPanel('legend', HsLegendComponent, {});
this.createPanel('layermanager', HsLayerManagerComponent, {});
this.createPanel('mapSwipe', HsMapSwipeComponent, {});
/**
* Create panel components
*/
this.HsPanelConstructorService.createActivePanels();

this.HsPanelContainerService.create(HsStylerComponent, {});

Expand Down
4 changes: 2 additions & 2 deletions projects/hslayers/src/hslayers.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import {HsFeatureTableModule} from './components/feature-table/feature-table.mod
import {HsGeolocationModule} from './components/geolocation/geolocation.module';
import {HsInfoModule} from './components/info/info.module';
import {HsLanguageModule} from './components/language/language.module';
import {HsLayerManagerModule} from './components/layermanager/layermanager.module';
import {HsLayerManagerModule} from './components/layer-manager/layer-manager.module';
import {HsLayoutModule} from './components/layout/layout.module';
import {HsLegendModule} from './components/legend/legend.module';
import {HsMapSwipeModule} from './components/map-swipe/map-swipe.module';
import {HsMeasureModule} from './components/measure/measure.module';
import {HsQueryModule} from './components/query/query.module';
import {HsSaveMapModule} from './components/save-map/save-map.module';
import {HsSearchModule} from './components/search/search.module';
import {HsShareModule} from './components/permalink/share.module';
import {HsShareModule} from './components/share/share.module';
import {HsStylerModule} from './components/styles/styles.module';
import {HsToolbarModule} from './components/toolbar/toolbar.module';
import {HsTripPlannerModule} from './components/trip-planner/trip-planner.module';
Expand Down
31 changes: 17 additions & 14 deletions projects/test-app/src/hslayers-app/hslayers-app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {InterpolatedSource} from 'hslayers-ng/common/layers/hs.source.interpolat
import {SPOI} from 'hslayers-ng/common/layers/hs.source.SPOI';
import {SparqlJson} from 'hslayers-ng/common/layers/hs.source.SparqlJson';

import {HsPanelConstructorService} from '../../../hslayers/src/components/layout/panels/panel-constructor.service';
import {PopupWidgetComponent} from './popup-widget.component';
import {SomeComponent} from './some-panel/some-panel.component';

Expand All @@ -33,25 +34,27 @@ import {SomeComponent} from './some-panel/some-panel.component';
export class HslayersAppComponent {
constructor(
public hsConfig: HsConfig,
private hsUtilsService: HsUtilsService,
private hsEventBusService: HsEventBusService,
private hsQueryPopupWidgetContainerService: HsQueryPopupWidgetContainerService,
private hsUtilsService: HsUtilsService,
private httpClient: HttpClient,
public hsSidebarService: HsSidebarService,
public hsPanelContainerService: HsPanelContainerService,
public hsPanelConstructorService: HsPanelConstructorService,
public hsLayoutService: HsLayoutService,
) {
/* Create new button in the sidebar */
this.hsSidebarService.addButton({
panel: 'custom',
module: 'some',
order: 0,
title: 'Custom panel',
description: 'Custom panel with some fancy features',
icon: 'icon-analytics-piechart',
});
/* Create new panel itself */
this.hsPanelContainerService.create(SomeComponent, {});
/* Create new panel and its button in the sidebar */
this.hsPanelConstructorService.createPanelAndButton(
SomeComponent,
{
panel: 'custom',
module: 'some',
order: 0,
title: 'Custom panel',
description: 'Custom panel with some fancy features',
icon: 'icon-analytics-piechart',
},
{},
);
/* Switch to it */
this.hsEventBusService.layoutLoads.subscribe(() => {
this.hsLayoutService.setDefaultPanel('custom');
Expand Down Expand Up @@ -339,7 +342,7 @@ export class HslayersAppComponent {
});
this.hsConfig.update({
panelsEnabled: {
draw: true,
draw: false,
mapSwipe: true,
language: true,
},
Expand Down

0 comments on commit 32c7855

Please sign in to comment.