@swimlane/ngx-charts
Version:
Declarative Charting Framework for Angular
158 lines • 6.52 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { ApplicationRef, ComponentFactoryResolver, Injectable, Injector } from '@angular/core';
/**
* Injection service is a helper to append components
* dynamically to a known location in the DOM, most
* noteably for dialogs/tooltips appending to body.
*
* @export
* @class InjectionService
*/
var InjectionService = /** @class */ (function () {
function InjectionService(applicationRef, componentFactoryResolver, injector) {
this.applicationRef = applicationRef;
this.componentFactoryResolver = componentFactoryResolver;
this.injector = injector;
}
InjectionService_1 = InjectionService;
/**
* Sets a default global root view container. This is useful for
* things like ngUpgrade that doesn't have a ApplicationRef root.
*
* @param container
*/
InjectionService.setGlobalRootViewContainer = function (container) {
InjectionService_1.globalRootViewContainer = container;
};
/**
* Gets the root view container to inject the component to.
*
* @returns {ComponentRef<any>}
*
* @memberOf InjectionService
*/
InjectionService.prototype.getRootViewContainer = function () {
var rootComponents = this.applicationRef.components;
// fix cannot read length of undefined
if (rootComponents) {
if (rootComponents.length)
return rootComponents[0];
}
if (this._container)
return this._container;
if (InjectionService_1.globalRootViewContainer)
return InjectionService_1.globalRootViewContainer;
throw new Error('View Container not found! ngUpgrade needs to manually set this via setRootViewContainer.');
};
/**
* Overrides the default root view container. This is useful for
* things like ngUpgrade that doesn't have a ApplicationRef root.
*
* @param {any} container
*
* @memberOf InjectionService
*/
InjectionService.prototype.setRootViewContainer = function (container) {
this._container = container;
};
/**
* Gets the html element for a component ref.
*
* @param {ComponentRef<any>} componentRef
* @returns {HTMLElement}
*
* @memberOf InjectionService
*/
InjectionService.prototype.getComponentRootNode = function (componentRef) {
// the top most component root node has no `hostView`
if (!componentRef.hostView)
return componentRef.element.nativeElement;
return componentRef.hostView.rootNodes[0];
};
/**
* Gets the root component container html element.
*
* @returns {HTMLElement}
*
* @memberOf InjectionService
*/
InjectionService.prototype.getRootViewContainerNode = function () {
return this.getComponentRootNode(this.getRootViewContainer());
};
/**
* Projects the bindings onto the component
*
* @param {ComponentRef<any>} component
* @param {*} options
* @returns {ComponentRef<any>}
*
* @memberOf InjectionService
*/
InjectionService.prototype.projectComponentBindings = function (component, bindings) {
if (bindings) {
if (bindings.inputs !== undefined) {
var bindingKeys = Object.getOwnPropertyNames(bindings.inputs);
for (var _i = 0, bindingKeys_1 = bindingKeys; _i < bindingKeys_1.length; _i++) {
var bindingName = bindingKeys_1[_i];
component.instance[bindingName] = bindings.inputs[bindingName];
}
}
if (bindings.outputs !== undefined) {
var eventKeys = Object.getOwnPropertyNames(bindings.outputs);
for (var _a = 0, eventKeys_1 = eventKeys; _a < eventKeys_1.length; _a++) {
var eventName = eventKeys_1[_a];
component.instance[eventName] = bindings.outputs[eventName];
}
}
}
return component;
};
/**
* Appends a component to a adjacent location
*
* @template T
* @param {Type<T>} componentClass
* @param {*} [options={}]
* @param {Element} [location=this.getRootViewContainerNode()]
* @returns {ComponentRef<any>}
*
* @memberOf InjectionService
*/
InjectionService.prototype.appendComponent = function (componentClass, bindings, location) {
if (bindings === void 0) { bindings = {}; }
if (location === void 0) { location = this.getRootViewContainerNode(); }
var componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentClass);
var componentRef = componentFactory.create(this.injector);
var appRef = this.applicationRef;
var componentRootNode = this.getComponentRootNode(componentRef);
// project the options passed to the component instance
this.projectComponentBindings(componentRef, bindings);
appRef.attachView(componentRef.hostView);
componentRef.onDestroy(function () {
appRef.detachView(componentRef.hostView);
});
// use the renderer to append the element for univseral support
var renderer = componentRef.instance.renderer;
renderer.appendChild(location, componentRootNode);
return componentRef;
};
var InjectionService_1;
InjectionService.globalRootViewContainer = null;
InjectionService = InjectionService_1 = __decorate([
Injectable(),
__metadata("design:paramtypes", [ApplicationRef,
ComponentFactoryResolver,
Injector])
], InjectionService);
return InjectionService;
}());
export { InjectionService };
//# sourceMappingURL=injection.service.js.map