2016-01-24 20:02:02 +03:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-29 18:02:19 +03:00
|
|
|
import { Component, ElementRef, ViewContainerRef, OnDestroy, Input,
|
|
|
|
AfterViewInit, ComponentResolver, Renderer } from '@angular/core';
|
2016-05-06 12:46:41 +03:00
|
|
|
import { CORE_DIRECTIVES } from '@angular/common';
|
2016-02-10 14:19:50 +03:00
|
|
|
|
2016-05-06 12:46:41 +03:00
|
|
|
import { JsonSchema } from './json-schema';
|
2016-05-25 18:34:31 +03:00
|
|
|
import { OptionsService } from '../../services/options.service';
|
2016-06-22 21:17:48 +03:00
|
|
|
import { SpecManager } from '../../utils/SpecManager';
|
2016-02-07 17:11:15 +03:00
|
|
|
|
|
|
|
var cache = {};
|
|
|
|
|
2016-01-24 20:02:02 +03:00
|
|
|
@Component({
|
|
|
|
selector: 'json-schema-lazy',
|
|
|
|
template: '',
|
|
|
|
directives: [CORE_DIRECTIVES]
|
|
|
|
})
|
2016-06-15 21:48:04 +03:00
|
|
|
export class JsonSchemaLazy implements OnDestroy, AfterViewInit {
|
2016-05-25 18:34:31 +03:00
|
|
|
@Input() pointer: string;
|
|
|
|
@Input() auto: boolean;
|
|
|
|
@Input() isRequestSchema: boolean;
|
2016-06-29 18:02:19 +03:00
|
|
|
@Input() final: boolean = false;
|
|
|
|
@Input() nestOdd: boolean;
|
|
|
|
@Input() childFor: string;
|
|
|
|
@Input() isArray: boolean;
|
2016-05-25 18:34:31 +03:00
|
|
|
loaded: boolean = false;
|
2016-06-29 18:02:19 +03:00
|
|
|
constructor(private specMgr:SpecManager, private location:ViewContainerRef, private elementRef:ElementRef,
|
|
|
|
private resolver:ComponentResolver, private optionsService:OptionsService, private _renderer: Renderer) {
|
2016-01-24 20:02:02 +03:00
|
|
|
}
|
|
|
|
|
2016-02-07 17:11:15 +03:00
|
|
|
normalizePointer() {
|
2016-06-23 17:36:38 +03:00
|
|
|
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-06-22 12:45:03 +03:00
|
|
|
// FIXME: get rid of DynamicComponentLoader as it is deprecated
|
2016-06-29 18:02:19 +03:00
|
|
|
return this.resolver.resolveComponent(JsonSchema).then(componentFactory => {
|
|
|
|
let contextInjector = this.location.parentInjector;
|
|
|
|
let compRef = this.location.createComponent(
|
|
|
|
componentFactory, null, contextInjector, null);
|
|
|
|
this.initComponent(compRef.instance);
|
|
|
|
this._renderer.setElementAttribute(compRef.location.nativeElement, 'class', this.location.element.nativeElement.className);
|
2016-06-29 19:07:18 +03:00
|
|
|
compRef.changeDetectorRef.detectChanges();
|
2016-05-20 19:28:16 +03:00
|
|
|
return compRef;
|
2016-06-29 18:02:19 +03:00
|
|
|
}).catch(err => {
|
2016-06-23 17:36:38 +03:00
|
|
|
console.log(err);
|
|
|
|
throw err;
|
2016-05-20 19:28:16 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-24 20:02:02 +03:00
|
|
|
load() {
|
2016-05-06 12:46:41 +03:00
|
|
|
if (this.optionsService.options.disableLazySchemas) return;
|
2016-01-24 22:27:15 +03:00
|
|
|
if (this.loaded) return;
|
|
|
|
if (this.pointer) {
|
2016-05-20 19:28:16 +03:00
|
|
|
this._loadAfterSelf();
|
2016-01-24 22:27:15 +03:00
|
|
|
}
|
|
|
|
this.loaded = true;
|
2016-01-24 20:02:02 +03:00
|
|
|
}
|
2016-02-07 17:11:15 +03:00
|
|
|
|
|
|
|
// cache JsonSchema view
|
|
|
|
loadCached() {
|
2016-05-25 18:34:31 +03:00
|
|
|
this.pointer = this.normalizePointer();
|
2016-02-07 17:11:15 +03:00
|
|
|
if (cache[this.pointer]) {
|
|
|
|
cache[this.pointer].then((compRef) => {
|
|
|
|
setTimeout( ()=> {
|
2016-02-10 16:48:07 +03:00
|
|
|
let $element = compRef.location.nativeElement;
|
2016-02-07 17:11:15 +03:00
|
|
|
|
2016-06-22 19:13:57 +03:00
|
|
|
// skip caching view with tabs inside (discriminator)
|
|
|
|
// as it needs attached controller
|
2016-06-29 19:07:18 +03:00
|
|
|
if (compRef.instance.hasDescendants || compRef.instance._hasSubSchemas) {
|
2016-06-22 19:13:57 +03:00
|
|
|
this._loadAfterSelf();
|
2016-02-07 17:11:15 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-02-10 16:48:07 +03:00
|
|
|
insertAfter($element.cloneNode(true), this.elementRef.nativeElement);
|
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-06-29 18:02:19 +03:00
|
|
|
initComponent(instance:JsonSchema) {
|
|
|
|
Object.assign(instance, this);
|
2016-04-27 22:34:27 +03:00
|
|
|
}
|
|
|
|
|
2016-02-07 17:11:15 +03:00
|
|
|
ngAfterViewInit() {
|
2016-06-29 19:07:18 +03:00
|
|
|
if (this.optionsService.options.disableLazySchemas) {
|
|
|
|
this._loadAfterSelf();
|
|
|
|
return;
|
|
|
|
}
|
2016-02-07 17:11:15 +03:00
|
|
|
if (!this.auto) return;
|
|
|
|
this.loadCached();
|
|
|
|
}
|
2016-02-10 16:48:07 +03:00
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
// clear cache
|
|
|
|
cache = {};
|
|
|
|
}
|
2016-02-07 17:11:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function insertAfter(newNode, referenceNode) {
|
|
|
|
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
|
2016-01-24 20:02:02 +03:00
|
|
|
}
|