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

84 lines
2.4 KiB
JavaScript
Raw Normal View History

'use strict';
import {Component, View, ElementRef} from 'angular2/core';
import {CORE_DIRECTIVES} from 'angular2/common';
import {DynamicComponentLoader} from 'angular2/src/core/linker/dynamic_component_loader';
import JsonSchema from './json-schema';
import OptionsManager from '../../options';
2016-02-07 17:11:15 +03:00
import SchemaManager from '../../utils/SchemaManager';
var cache = {};
@Component({
selector: 'json-schema-lazy',
2016-02-07 17:11:15 +03:00
inputs: ['pointer', 'auto']
})
@View({
template: '',
directives: [CORE_DIRECTIVES]
})
@Reflect.metadata('parameters', [[ElementRef], [DynamicComponentLoader]])
export default class JsonSchemaLazy {
constructor(elementRef, dcl) {
this.elementRef = elementRef;
this.dcl = dcl;
}
2016-02-07 17:11:15 +03:00
normalizePointer() {
let schema = SchemaManager.instance().byPointer(this.pointer);
return schema && schema.$ref || this.pointer;
}
load() {
if (OptionsManager.instance().options.disableLazySchemas) return;
if (this.loaded) return;
if (this.pointer) {
this.dcl.loadNextToLocation(JsonSchema, this.elementRef).then((compRef) => {
compRef.instance.pointer = this.pointer;
});
}
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;
// skip caching view with discriminator as it needs attached controller
if (element.querySelector('.discriminator')) {
this.dcl.loadNextToLocation(JsonSchema, this.elementRef).then((compRef) => {
compRef.instance.pointer = this.pointer;
compRef.hostView.changeDetectorRef.markForCheck();
});
return;
}
insertAfter(element.cloneNode(true), this.elementRef.nativeElement);
} );
});
} else {
cache[this.pointer] = this.dcl.loadNextToLocation(JsonSchema, this.elementRef).then((compRef) => {
compRef.instance.pointer = this.pointer;
compRef.hostView.changeDetectorRef.markForCheck();
return compRef;
});
}
}
ngAfterViewInit() {
if (!this.auto) return;
this.loadCached();
}
}
function insertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}