Toggle navigation
P3X GitList Snapshot
GitHub
Repo
Changelog
To do
Releases
Themes
Change log
Loading change log ...
To do ...
Loading todo ...
browsing:
b038b35e61a78e59e69601b21d3f628f6b15d1a6
Branches
master
Tags
1.1.129-287
1.1.113-149
1.1.108-143
1.1.95-138
1.1.92-119
1.0.35-18
1.0.13-14
Files
Commits
Log
Graph
Stats
angular-compile.git
src
CompileAttribute.ts
RSS
Git
Fetch origin
Download
ZIP
TAR
Clone
Raw
View
History
Clone
SSH
HTTPS
Blames found: 37
Mode: application/typescript
Binary: false
Hang on, we reloading big blames...
f46e4d96
import {
c605ea6f
Component,
f46e4d96
Input, Injectable, OnInit, OnChanges, SimpleChanges, Type, ModuleWithProviders,
69db3407
NgModule,
c605ea6f
Compiler, NgModuleFactory,
f46e4d96
} from '@angular/core';
c605ea6f
import { CommonModule } from '@angular/common'; import { BrowserModule } from '@angular/platform-browser';
f46e4d96
c605ea6f
//import { CompileService } from './CompileService'; let SingletonDefaultModule: NgModule; import { cloneDeep } from 'lodash'; //const cache : any = {}; @Component({ selector: '[p3x-compile]', template: ` <span *ngIf="html !== undefined && html !== null && html.trim() !== '' && dynamicComponent !== undefined && dynamicModule !== undefined"> <ng-container *ngComponentOutlet="dynamicComponent; ngModuleFactory: dynamicModule;"></ng-container> </span>
7d7aeb47
`
c605ea6f
})
f46e4d96
@Injectable() export class CompileAttribute implements OnInit, OnChanges{ @Input('p3x-compile') html: string; @Input('p3x-compile-ctx') context: any;
e0fc8584
@Input('p3x-compile-error-handler')
0bce968e
errorHandler: (ex: any) => void = console.error;
c605ea6f
dynamicComponent: any; dynamicModule: NgModuleFactory<any> | any;
69db3407
@Input('p3x-compile-module') module: NgModule;
f46e4d96
@Input('p3x-compile-imports') imports: Array<Type<any> | ModuleWithProviders | any[]>; async update() { if (this.html === undefined || this.html.trim() === '') {
c605ea6f
// this.container.clear(); this.dynamicComponent = undefined; this.dynamicModule = undefined;
f46e4d96
return; }
7d7aeb47
/* const cacheKey = this.html;
c605ea6f
7d7aeb47
if (Object.keys(cache).indexOf(cacheKey) > -1) { return cache[cacheKey]; } */
c605ea6f
try { this.dynamicComponent = this.createNewComponent(this.html, this.context); this.dynamicModule = this.compiler.compileModuleSync(this.createComponentModule(this.dynamicComponent)); // cache[cacheKey] = this.dynamicComponent; } catch (e) {
0bce968e
this.errorHandler(e);
c605ea6f
} /*
f46e4d96
await this.service.compile({ template: this.html, container: this.container, context: this.context,
69db3407
imports: this.imports, module: this.module
f46e4d96
})
c605ea6f
*/ } private createComponentModule (componentType: any) { let module : NgModule = {}; if (this.module !== undefined) { module = cloneDeep(this.module); } else if (SingletonDefaultModule !== undefined && SingletonDefaultModule !== null) { module = cloneDeep(SingletonDefaultModule); } module.imports = module.imports || []; module.imports.push( CommonModule ); // module.imports.push( BrowserModule ); if (this.imports !== undefined) { module.imports = module.imports.concat(this.imports) } if (module.declarations === undefined) { module.declarations = [ componentType ]; } else { module.declarations.push(componentType); } module.entryComponents = [ componentType ]; @NgModule(module) class RuntimeComponentModule { } return RuntimeComponentModule; } private createNewComponent (html:string, context: any) { @Component({ selector: 'dynamic-component', template: html }) class DynamicComponent { context: any = context; } return DynamicComponent;
f46e4d96
}
c605ea6f
async ngOnInit() {
f46e4d96
this.update(); }
c605ea6f
async ngOnChanges(changes: SimpleChanges) {
f46e4d96
//fixme only update with the required changes this.update(); } constructor(
c605ea6f
// private container: ViewContainerRef, // private service: CompileService
7d7aeb47
private compiler: Compiler
f46e4d96
) {} }