redoc/lib/components/JsonSchema/json-schema-lazy.ts

100 lines
3.0 KiB
TypeScript
Raw Normal View History

'use strict';
import { Component, ElementRef, ViewContainerRef, OnDestroy, OnInit, Input,
2016-08-22 12:12:13 +03:00
AfterViewInit, ComponentFactoryResolver, Renderer } from '@angular/core';
2016-05-06 12:46:41 +03:00
import { JsonSchema } from './json-schema';
import { OptionsService } from '../../services/options.service';
2016-10-23 20:18:42 +03:00
import { SpecManager } from '../../utils/spec-manager';
2016-02-07 17:11:15 +03:00
var cache = {};
@Component({
selector: 'json-schema-lazy',
2016-08-22 12:12:13 +03:00
entryComponents: [ JsonSchema ],
2016-11-23 02:23:32 +03:00
template: '',
styles: [':host { display:none }']
})
export class JsonSchemaLazy implements OnDestroy, OnInit, AfterViewInit {
@Input() pointer: string;
@Input() absolutePointer: string;
@Input() auto: boolean;
@Input() isRequestSchema: boolean;
@Input() final: boolean = false;
@Input() nestOdd: boolean;
@Input() childFor: string;
@Input() isArray: boolean;
2016-07-28 19:59:42 +03:00
disableLazy: boolean = false;
loaded: boolean = false;
constructor(private specMgr:SpecManager, private location:ViewContainerRef, private elementRef:ElementRef,
2016-08-22 12:12:13 +03:00
private resolver:ComponentFactoryResolver, private optionsService:OptionsService, private _renderer: Renderer) {
2016-07-28 19:59:42 +03:00
this.disableLazy = this.optionsService.options.disableLazySchemas;
}
2016-02-07 17:11:15 +03:00
normalizePointer() {
let schema = this.specMgr.byPointer(this.pointer);
2016-02-07 17:11:15 +03:00
return schema && schema.$ref || this.pointer;
}
2016-05-20 19:28:16 +03:00
_loadAfterSelf() {
2016-08-22 12:12:13 +03:00
var componentFactory = this.resolver.resolveComponentFactory(JsonSchema);
let contextInjector = this.location.parentInjector;
let compRef = this.location.createComponent(componentFactory, null, contextInjector, null);
2016-10-23 20:18:42 +03:00
this.projectComponentInputs(compRef.instance);
2016-08-22 12:12:13 +03:00
this._renderer.setElementAttribute(compRef.location.nativeElement, 'class', this.location.element.nativeElement.className);
compRef.changeDetectorRef.detectChanges();
2016-07-10 16:05:28 +03:00
this.loaded = true;
2016-08-22 12:12:13 +03:00
return compRef;
2016-05-20 19:28:16 +03:00
}
load() {
2016-09-01 09:53:42 +03:00
if (this.disableLazy) return;
if (this.loaded) return;
if (this.pointer) {
2016-05-20 19:28:16 +03:00
this._loadAfterSelf();
}
}
2016-02-07 17:11:15 +03:00
// cache JsonSchema view
loadCached() {
this.pointer = this.normalizePointer();
2016-02-07 17:11:15 +03:00
if (cache[this.pointer]) {
2016-08-22 12:12:13 +03:00
let compRef = cache[this.pointer];
2016-10-23 20:18:42 +03:00
let $element = compRef.location.nativeElement;
2016-02-07 17:11:15 +03:00
2016-10-23 20:18:42 +03:00
// skip caching view with descendant schemas
// as it needs attached controller
if (!this.disableLazy && (compRef.instance.hasDescendants || compRef.instance._hasSubSchemas)) {
this._loadAfterSelf();
return;
}
insertAfter($element.cloneNode(true), this.elementRef.nativeElement);
2016-10-23 20:18:42 +03:00
this.loaded = true;
2016-02-07 17:11:15 +03:00
} else {
2016-05-20 19:28:16 +03:00
cache[this.pointer] = this._loadAfterSelf();
2016-02-07 17:11:15 +03:00
}
}
2016-10-23 20:18:42 +03:00
projectComponentInputs(instance:JsonSchema) {
Object.assign(instance, this);
}
ngOnInit() {
if (!this.absolutePointer) this.absolutePointer = this.pointer;
}
2016-02-07 17:11:15 +03:00
ngAfterViewInit() {
2016-07-28 19:59:42 +03:00
if (!this.auto && !this.disableLazy) return;
2016-02-07 17:11:15 +03:00
this.loadCached();
}
ngOnDestroy() {
// clear cache
cache = {};
}
2016-02-07 17:11:15 +03:00
}
function insertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}