UNPKG

ng2-materialize

Version:

An Angular 2+ wrap around Materialize library

102 lines (101 loc) 3.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var core_1 = require("@angular/core"); var MzInjectionService = (function () { function MzInjectionService(applicationRef, componentFactoryResolver, injector) { this.applicationRef = applicationRef; this.componentFactoryResolver = componentFactoryResolver; this.injector = injector; } /** * Appends a component to an adjacent location. * * @template T * @param {Type<T>} componentClass * @param {*} [options={}] * @param {Element} [location=this.getContainerElement()] * @returns {ComponentRef<T>} * @memberof MzInjectionService */ MzInjectionService.prototype.appendComponent = function (componentClass, options, location) { var _this = this; if (options === void 0) { options = {}; } if (location === void 0) { location = this.getContainerElement(); } // instantiate component to load var componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentClass); var componentRef = componentFactory.create(this.injector); // project the options passed to the component instance this.projectComponentInputs(componentRef, options); // attach view for dirty checking this.applicationRef.attachView(componentRef.hostView); // detach view when component is destroyed componentRef.onDestroy(function () { _this.applicationRef.detachView(componentRef.hostView); }); // append component to location in the DOM where we want it to be rendered var componentRootNode = this.getComponentRootNode(componentRef); location.appendChild(componentRootNode); return componentRef; }; /** * Overrides the default container element. * * @param {Element} container * @memberof MzInjectionService */ MzInjectionService.prototype.setRootViewContainer = function (container) { this.container = container; }; /** * Gets the html element for a component ref. * * @private * @param {ComponentRef<any>} componentRef * @returns {Element} * @memberof MzInjectionService */ MzInjectionService.prototype.getComponentRootNode = function (componentRef) { return componentRef.hostView.rootNodes[0]; }; /** * Gets the container element. * * @private * @returns {Element} * @memberof MzInjectionService */ MzInjectionService.prototype.getContainerElement = function () { return this.container || document.body; }; /** * Projects the inputs onto the component. * * @private * @template T * @param {ComponentRef<T>} component * @param {*} options * @returns {ComponentRef<T>} * @memberof MzInjectionService */ MzInjectionService.prototype.projectComponentInputs = function (component, options) { if (options) { var props = Object.getOwnPropertyNames(options); for (var _i = 0, props_1 = props; _i < props_1.length; _i++) { var prop = props_1[_i]; component.instance[prop] = options[prop]; } } return component; }; return MzInjectionService; }()); MzInjectionService.decorators = [ { type: core_1.Injectable }, ]; /** @nocollapse */ MzInjectionService.ctorParameters = function () { return [ { type: core_1.ApplicationRef, }, { type: core_1.ComponentFactoryResolver, }, { type: core_1.Injector, }, ]; }; exports.MzInjectionService = MzInjectionService;