Skip to content

Commit

Permalink
feat(feature form): add a way to retrieve a feature form's data witho…
Browse files Browse the repository at this point in the history
…ut submitting it
  • Loading branch information
cbourget authored and mbarbeau committed Sep 25, 2019
1 parent d5bf0ba commit 9527600
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

<igo-form
#igoForm
[form]="form"
[formData]="feature$ | async"
(submitForm)="onSubmit($event)">
Expand Down
82 changes: 13 additions & 69 deletions packages/geo/src/lib/feature/feature-form/feature-form.component.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import * as olstyle from 'ol/style';

import {
Component,
Input,
Output,
EventEmitter,
OnChanges,
OnDestroy,
ChangeDetectionStrategy,
SimpleChanges
ViewChild
} from '@angular/core';

import { BehaviorSubject } from 'rxjs';

import { Form, getEntityRevision } from '@igo2/common';
import { Form, FormComponent, getEntityRevision } from '@igo2/common';
import { uuid } from '@igo2/utils';

import { FEATURE } from '../shared/feature.enums';
import { Feature, FeatureMeta } from '../shared/feature.interfaces';
import { FeatureStore } from '../shared/store';
import { FeatureStoreSelectionStrategy } from '../shared/strategies/selection';

/**
* A configurable form, optionnally bound to a feature.
Expand All @@ -34,8 +28,7 @@ import { FeatureStoreSelectionStrategy } from '../shared/strategies/selection';
styleUrls: ['./feature-form.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class FeatureFormComponent implements OnChanges, OnDestroy {
public feature$: BehaviorSubject<Feature> = new BehaviorSubject(undefined);
export class FeatureFormComponent {

/**
* Form
Expand All @@ -45,40 +38,19 @@ export class FeatureFormComponent implements OnChanges, OnDestroy {
/**
* Feature to update
*/
@Input() feature: Feature | undefined;

/**
* The store the feature belongs to. Required to manage the
* visiblity and selection.
*/
@Input() store: FeatureStore | undefined;
@Input()
set feature(value: Feature | undefined) { this.feature$.next(value); }
get feature(): Feature | undefined { return this.feature$.value; }
readonly feature$: BehaviorSubject<Feature> = new BehaviorSubject(undefined);

/**
* Event emitted when the form is submitted
*/
@Output() submitForm = new EventEmitter<Feature>();

constructor() {}

ngOnChanges(changes: SimpleChanges) {
const store = changes.store;
if (store && store.currentValue !== store.previousValue) {
this.setStore(store.currentValue);
}
@ViewChild('igoForm') igoForm: FormComponent;

const feature = changes.feature;
if (feature && feature.currentValue !== feature.previousValue) {
this.feature$.next(feature.currentValue);
}
}

/**
* Show the original feature and reactivate the selection
* @internal
*/
ngOnDestroy() {
this.setStore(undefined);
}
constructor() {}

/**
* Transform the form data to a feature and emit an event
Expand All @@ -90,6 +62,10 @@ export class FeatureFormComponent implements OnChanges, OnDestroy {
this.submitForm.emit(feature);
}

getData(): Feature {
return this.formDataToFeature(this.igoForm.getData());
}

/**
* Transform the form data to a feature
* @param data Form data
Expand Down Expand Up @@ -129,36 +105,4 @@ export class FeatureFormComponent implements OnChanges, OnDestroy {
properties
};
}

private setStore(store: FeatureStore) {
if (this.store !== undefined) {
this.activateStoreSelection(this.store);
}
if (store !== undefined) {
this.deactivateStoreSelection(store);
}
this.store = store;
}

/**
* Deactivate feature selection from the store and from the map
*/
private deactivateStoreSelection(store: FeatureStore) {
const selectionStrategy = store.getStrategyOfType(
FeatureStoreSelectionStrategy
);
if (selectionStrategy !== undefined) {
selectionStrategy.deactivate();
(selectionStrategy as FeatureStoreSelectionStrategy).unselectAll();
}
}

/**
* Reactivate feature selection from the store and from the map
*/
private activateStoreSelection(store: FeatureStore) {
// TODO: maybe we should recativate the strategies only if they
// were active in the first place
store.activateStrategyOfType(FeatureStoreSelectionStrategy);
}
}
2 changes: 2 additions & 0 deletions packages/geo/src/lib/feature/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './shared';

export * from './feature-form/feature-form.component';

0 comments on commit 9527600

Please sign in to comment.