refactor(aio): simplify DocViewer

This commit is contained in:
Ward Bell 2017-05-04 00:37:55 -07:00 committed by Pete Bacon Darwin
parent 2f66932bd1
commit aec65dee71

View File

@ -26,7 +26,7 @@ const initialDocViewerContent = initialDocViewerElement ? initialDocViewerElemen
})
export class DocViewerComponent implements DoCheck, OnDestroy {
private displayedDoc: DisplayedDoc;
private embeddedComponents: ComponentRef<any>[] = [];
private embeddedComponentFactories: Map<string, EmbeddedComponentFactory> = new Map();
private hostElement: HTMLElement;
@ -67,8 +67,6 @@ export class DocViewerComponent implements DoCheck, OnDestroy {
*/
private build(doc: DocumentContents) {
const displayedDoc = this.displayedDoc = new DisplayedDoc(doc);
// security: the doc.content is always authored by the documentation team
// and is considered to be safe
this.hostElement.innerHTML = doc.contents || '';
@ -87,7 +85,7 @@ export class DocViewerComponent implements DoCheck, OnDestroy {
// security: the source of this innerHTML is always authored by the documentation team
// and is considered to be safe
element[contentPropertyName] = element.innerHTML;
displayedDoc.addEmbeddedComponent(factory.create(this.injector, [], element));
this.embeddedComponents.push(factory.create(this.injector, [], element));
}
});
}
@ -109,18 +107,13 @@ export class DocViewerComponent implements DoCheck, OnDestroy {
}
ngDoCheck() {
// TODO: make sure this isn't called too often on the same doc
if (this.displayedDoc) {
this.displayedDoc.detectChanges();
}
this.embeddedComponents.forEach(comp => comp.changeDetectorRef.detectChanges());
}
ngOnDestroy() {
// destroy components otherwise there will be memory leaks
if (this.displayedDoc) {
this.displayedDoc.destroy();
this.displayedDoc = undefined;
}
// destroy these components else there will be memory leaks
this.embeddedComponents.forEach(comp => comp.destroy());
this.embeddedComponents.length = 0;
}
/**
@ -131,24 +124,3 @@ export class DocViewerComponent implements DoCheck, OnDestroy {
return selector.replace(/-(.)/g, (match, $1) => $1.toUpperCase()) + 'Content';
}
}
class DisplayedDoc {
private embeddedComponents: ComponentRef<any>[] = [];
constructor(private doc: DocumentContents) {}
addEmbeddedComponent(component: ComponentRef<any>) {
this.embeddedComponents.push(component);
}
detectChanges() {
this.embeddedComponents.forEach(comp => comp.changeDetectorRef.detectChanges());
}
destroy() {
// destroy components otherwise there will be memory leaks
this.embeddedComponents.forEach(comp => comp.destroy());
this.embeddedComponents.length = 0;
}
}