import { computed, makeObservable, observable, override } from 'mobx'; import { AttributeValue, ElementData } from '../interfaces/element.interface' import { GisAction, LayerGisActions } from '../interfaces/gis.interface' import { DatabaseFeature } from '../interfaces/gis.interface' import { MainStore } from '../stores/mainStore' import { GisFeature } from './_default/gisFeature' import { Element } from './element' export class GisElement extends Element { feature?: GisFeature = undefined constructor( store: MainStore, hash: string, data?: ElementData, shouldLoad = false, feature?: DatabaseFeature, ) { super(store, hash, data, shouldLoad) makeObservable(this, { feature: observable, gisActions: computed, setAttributeValue: override, }) if (feature) { // this.feature = store.gisstore.elementStore.instantiateFeatures([feature])[0] } } // async initialize() { // if (!this.feature) { // await this.resolveGisFeature() // } // } static get ATTRIBUTES() { return { ...Element.ATTRIBUTES, GEO_IDENTIFIER: 'geo_identifier', } } static get LAYER_NAME() { return '' } get GEO_IDENTIFIER(): string { return this.getAttributeRawValue( (this.constructor as typeof GisElement).ATTRIBUTES.GEO_IDENTIFIER, ) as string } async resolveGisFeature(): Promise { if (!this.GEO_IDENTIFIER) { return } const featureRequest = await this.store.geojson.getFeature( this.GEO_IDENTIFIER, ) if (!featureRequest.data || !featureRequest.ok) { return } const feature = this.store.gisStore.instantiateFeatures([ featureRequest.data, ])?.[0] this.feature = feature return featureRequest.data } get gisActions(): LayerGisActions | undefined { if (!this.feature) { return } const orgConfig = this.store.userStore.organizationConfig const actions = orgConfig.map?.layerMetadata?.[this.feature.layerName]?.actions return actions } setAttributeValue(attribute: string, value: AttributeValue) { if (this.gisActions && this.gisActions[attribute]) { const gisAction = this.gisActions[attribute].actions.find( (a) => a.set_value === value, ) if (gisAction && gisAction.callback) { gisAction.callback(this, attribute, value as string) } } return super.setAttributeValue(attribute, value) } performAction(attribute: string, action: GisAction) { if (!this.gisActions?.[attribute]) { return } this.setAttributeValue(attribute, action.set_value) return true } }