Adding Non-WebMap Angular Forms
/***********************************************************************
* Copyright (C) Mobilize.Net <[email protected]> - All Rights Reserved
*
* This file is part of the Mobilize Frameworks, which is
* proprietary and confidential.
*
* NOTICE: All information contained herein is, and remains
* the property of Mobilize.Net Corporation.
* The intellectual and technical concepts contained herein are
* proprietary to Mobilize.Net Corporation and may be covered
* by U.S. Patents, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Mobilize.Net Corporation.
*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.txt', which is part of this source code package.
***********************************************************************/
import {
ChangeDetectorRef,
Component,
ComponentFactoryResolver,
ComponentRef,
OnDestroy,
OnInit,
ViewChild,
} from '@angular/core';
import { WebMapService, WMConstants } from '@mobilize/angularclient';
import { WMLogger } from '@mobilize/logging';
import { ContainerDirective } from '@mobilize/base-components';
import { TypeResolver } from '@mobilize/base-components';
import { ErrorCodes, ExceptionHandlerClass } from '@mobilize/webmap-core';
@Component({
selector: 'wm-nowebmapformscontainer',
styleUrls: ['./no-web-map-forms-container.component.css'],
templateUrl: './no-web-map-forms-container.component.html'
})
@ExceptionHandlerClass(ErrorCodes.BaseComponents)
export class NoWebMapFormsContainerComponent implements OnInit, OnDestroy {
constructor(
private changeDetector: ChangeDetectorRef,
private componentFactoryResolver: ComponentFactoryResolver,
private webmapService: WebMapService
) {}
@ViewChild(ContainerDirective, { static: true })
componentContainer: ContainerDirective;
private openedComponents = {};
private events = [];
/* istanbul ignore next */
ngOnInit(): void {
this.events.push(
this.webmapService.core.getEvent().subscribe('showForm', (url) => {
this.navigateToForm(url);
})
);
this.events.push(
this.webmapService.core.getEvent().subscribe('closeForm', (url) => {
this.disposeComponent(url);
})
);
}
/**
* Load a new component inside the formsContainerComponent
* @param url the url object of the component.
*/
/* istanbul ignore next */
navigateToForm(id: any): void {
const componentType = TypeResolver.getType(id);
/* istanbul ignore else */
if (!componentType) {
WMLogger.instance()
.error(`There is not a type registered with the name ${id} in the TypeResolver.
Please check that the name is well written or the dataTransfer is the same in the backend`);
return;
}
this.loadComponent(componentType, id);
}
/**
* Creates a new component instance inside the formscontainer component
* @param componentToLoad The new component to load
* @param name The name of the component.
*/
/* istanbul ignore next */
loadComponent(componentToLoad: any, id: any): ComponentRef<any> {
const componentFactory =
this.componentFactoryResolver.resolveComponentFactory(componentToLoad);
const viewContainerRef = this.componentContainer.viewContainerRef;
const componentRef = viewContainerRef.createComponent(
componentFactory
) as ComponentRef<any>;
if (componentRef.instance.afterLoadComponent) {
componentRef.instance.afterLoadComponent();
}
this.openedComponents[id] = componentRef;
return componentRef;
}
/**
* Removes a component inside the formsContainerComponent
* @param url the url object of the component.
*/
/* istanbul ignore next */
disposeComponent(id: any): void {
if (id && this.openedComponents[id] && this.openedComponents[id].hostView) {
const viewContainerRef = this.componentContainer.viewContainerRef;
const vcrIndex = viewContainerRef.indexOf(
this.openedComponents[id].hostView
);
try {
viewContainerRef.remove(vcrIndex);
} catch (e) {
// Avoid triggering synchronization error while deleting the component.
WMLogger.instance().debug(e);
}
delete this.openedComponents[id];
} else {
WMLogger.instance().debug(`The view ${id} to be removed was not found:
${
this.openedComponents[id]
? 'HostView property undefined.'
: ' The view is not present in the openedComponents collection.'
}`);
}
}
/**
* Executes on component destroy to unsubscribe the events
*/
ngOnDestroy(): void {
this.events.forEach((evnt) => {
this.webmapService.core.getEvent().unSubscribe(evnt);
});
}
}

Last updated
Was this helpful?