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

111 lines
3.2 KiB
JavaScript
Raw Normal View History

'use strict';
2016-05-06 12:46:41 +03:00
import { Component, ElementRef, ViewContainerRef } from '@angular/core';
import { CORE_DIRECTIVES } from '@angular/common';
import { DynamicComponentLoader } from '@angular/core';
2016-05-06 12:46:41 +03:00
import { JsonSchema } from './json-schema';
import { OptionsService } from '../../services/index';
2016-02-07 17:11:15 +03:00
import SchemaManager from '../../utils/SchemaManager';
var cache = {};
@Component({
selector: 'json-schema-lazy',
inputs: ['pointer', 'auto', 'skipReadOnly'],
template: '',
directives: [CORE_DIRECTIVES]
})
2016-05-06 12:46:41 +03:00
@Reflect.metadata('parameters', [[SchemaManager], [ViewContainerRef], [
ElementRef], [DynamicComponentLoader], [OptionsService]])
2016-05-06 00:48:41 +03:00
export class JsonSchemaLazy {
2016-05-06 12:46:41 +03:00
constructor(schemaMgr, viewRef, elementRef, dcl, optionsService) {
2016-04-29 22:57:24 +03:00
this.viewRef = viewRef;
this.elementRef = elementRef;
this.dcl = dcl;
2016-05-06 12:46:41 +03:00
this.optionsService = optionsService;
this.schemaMgr = schemaMgr;
}
2016-02-07 17:11:15 +03:00
normalizePointer() {
let schema = this.schemaMgr.byPointer(this.pointer);
2016-02-07 17:11:15 +03:00
return schema && schema.$ref || this.pointer;
}
load() {
2016-05-06 12:46:41 +03:00
if (this.optionsService.options.disableLazySchemas) return;
if (this.loaded) return;
if (this.pointer) {
2016-04-29 22:57:24 +03:00
this.dcl.loadNextToLocation(JsonSchema, this.viewRef).then((compRef) => {
this.initComponent(compRef);
// trigger change detection
2016-05-09 22:55:16 +03:00
if (compRef.changeDetectorRef) {
compRef.changeDetectorRef.detectChanges();
} else {
compRef.hostView.changeDetectorRef.detectChanges();
}
});
}
this.loaded = true;
}
2016-02-07 17:11:15 +03:00
// cache JsonSchema view
loadCached() {
this.pointer = this.normalizePointer(this.pointer);
if (cache[this.pointer]) {
cache[this.pointer].then((compRef) => {
setTimeout( ()=> {
let $element = compRef.location.nativeElement;
2016-02-07 17:11:15 +03:00
// skip caching view with tabs inside (discriminator) as it needs attached controller
2016-03-27 01:37:56 +03:00
// FIXME: get rid of dependency on selector
if ($element.querySelector('.discriminator-wrap')) {
2016-04-29 22:57:24 +03:00
this.dcl.loadNextToLocation(JsonSchema, this.viewRef).then((compRef) => {
this.initComponent(compRef);
2016-05-09 22:55:16 +03:00
if (compRef.changeDetectorRef) {
compRef.changeDetectorRef.detectChanges();
} else {
compRef.hostView.changeDetectorRef.detectChanges();
}
2016-02-07 17:11:15 +03:00
});
return;
}
insertAfter($element.cloneNode(true), this.elementRef.nativeElement);
2016-02-07 17:11:15 +03:00
} );
});
} else {
2016-04-29 22:57:24 +03:00
cache[this.pointer] = this.dcl.loadNextToLocation(JsonSchema, this.viewRef).then((compRef) => {
this.initComponent(compRef);
2016-05-09 22:55:16 +03:00
if (compRef.changeDetectorRef) {
compRef.changeDetectorRef.detectChanges();
} else {
compRef.hostView.changeDetectorRef.detectChanges();
}
2016-02-07 17:11:15 +03:00
return compRef;
});
}
}
initComponent(compRef) {
compRef.instance.pointer = this.pointer;
compRef.instance.skipReadOnly = this.skipReadOnly;
}
2016-02-07 17:11:15 +03:00
ngAfterViewInit() {
if (!this.auto) return;
this.loadCached();
}
ngOnDestroy() {
// clear cache
cache = {};
}
2016-02-07 17:11:15 +03:00
}
function insertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}