Toggle navigation
P3X GitList Snapshot
GitHub
Repo
Changelog
To do
Releases
Themes
Change log
Loading change log ...
To do ...
Loading todo ...
browsing:
3793eedd20758369edc1a3999527be03b7a5a624
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: 52
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,
83396ced
Inject,
f46e4d96
} from '@angular/core';
c605ea6f
import { CommonModule } from '@angular/common';
d9151425
//import { BrowserModule } from '@angular/platform-browser';
f46e4d96
c605ea6f
let SingletonDefaultModule: NgModule; import { cloneDeep } from 'lodash';
d9151425
//import { CorifeusMaterialModule } from 'corifeus-web-material';
83396ced
3793eedd
const reverse = function (str: string) { return str.split('').reverse().join('') } const random = () => { return (Math.floor(Math.random() * (99999999999999999 - 10000000000000000)) + 10000000000000000).toString(16) } let currentIdTime : number; let currentId = 0; const nextId = () => { const now = Date.now(); if (currentIdTime !== now) { currentId = 0; currentIdTime = now } const comingId = ++currentId; const randomHex = reverse(random()).padStart(15, '0'); const timeHex = reverse(currentIdTime.toString(16).padStart(12, '0')) const comingIdHex = reverse(comingId.toString(16).padStart(3, '0')) const newId = `p3x-angular-compile-${timeHex}${comingIdHex}${randomHex}`; //console.log(newId) return newId }
c605ea6f
//const cache : any = {}; @Component({ selector: '[p3x-compile]', template: `
2dd8c32e
<ng-container *ngIf="html !== undefined && html !== null && html.trim() !== ''">
c1ce854b
<ng-container *ngComponentOutlet="dynamicComponent; ngModuleFactory: dynamicModule;"></ng-container> </ng-container>
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')
2dd8c32e
errorHandler: Function = undefined;
0bce968e
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[]>;
2dd8c32e
update() {
f46e4d96
if (this.html === undefined || this.html.trim() === '') {
c605ea6f
// this.container.clear(); this.dynamicComponent = undefined; this.dynamicModule = undefined;
f46e4d96
return; }
c605ea6f
3793eedd
/* console.log('html', this.html) const cacheKey = this.html; console.log(Object.keys(cache).indexOf(cacheKey), cache) if (cache.hasOwnProperty(cacheKey)) { const currentCache = cache[cacheKey]; this.dynamicComponent = currentCache.dynamicComponent this.dynamicModule = currentCache.dynamicModule return ; }
7d7aeb47
*/
c605ea6f
try { this.dynamicComponent = this.createNewComponent(this.html, this.context); this.dynamicModule = this.compiler.compileModuleSync(this.createComponentModule(this.dynamicComponent));
3793eedd
/* cache[cacheKey] = { dynamicComponent: this.dynamicComponent, dynamicModule: this.dynamicModule, }; */
c605ea6f
} catch (e) {
2dd8c32e
if (this.errorHandler === undefined) { throw e; } else { 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 ); 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({
3793eedd
selector: nextId(),
c605ea6f
template: html }) class DynamicComponent { context: any = context; } return DynamicComponent;
f46e4d96
}
2dd8c32e
ngOnInit() {
f46e4d96
this.update(); }
2dd8c32e
ngOnChanges(changes: SimpleChanges) {
f46e4d96
//fixme only update with the required changes this.update(); } constructor(
c605ea6f
// private container: ViewContainerRef, // private service: CompileService
83396ced
private compiler: Compiler, // @Inject('config') private config:CompileServiceConfig ) { }
c1ce854b
}