2016-01-24 20:02:02 +03:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-27 18:25:46 +03:00
|
|
|
import {Component, ElementRef} from 'angular2/core';
|
2016-01-24 20:02:02 +03:00
|
|
|
import {CORE_DIRECTIVES} from 'angular2/common';
|
|
|
|
import {DynamicComponentLoader} from 'angular2/src/core/linker/dynamic_component_loader';
|
2016-02-10 14:19:50 +03:00
|
|
|
|
|
|
|
import JsonSchema from './json-schema';
|
2016-01-24 22:27:15 +03:00
|
|
|
import OptionsManager from '../../options';
|
2016-02-07 17:11:15 +03:00
|
|
|
import SchemaManager from '../../utils/SchemaManager';
|
|
|
|
|
|
|
|
|
|
|
|
var cache = {};
|
|
|
|
|
2016-01-24 20:02:02 +03:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'json-schema-lazy',
|
2016-04-27 22:34:27 +03:00
|
|
|
inputs: ['pointer', 'auto', 'skipReadOnly'],
|
2016-01-24 20:02:02 +03:00
|
|
|
template: '',
|
|
|
|
directives: [CORE_DIRECTIVES]
|
|
|
|
})
|
2016-02-10 16:48:07 +03:00
|
|
|
@Reflect.metadata('parameters', [[SchemaManager], [ElementRef], [DynamicComponentLoader], [OptionsManager]])
|
2016-01-24 20:02:02 +03:00
|
|
|
export default class JsonSchemaLazy {
|
|
|
|
|
2016-02-10 16:48:07 +03:00
|
|
|
constructor(schemaMgr, elementRef, dcl, optionsMgr) {
|
2016-01-24 20:02:02 +03:00
|
|
|
this.elementRef = elementRef;
|
|
|
|
this.dcl = dcl;
|
2016-02-10 16:48:07 +03:00
|
|
|
this.optionsMgr = optionsMgr;
|
|
|
|
this.schemaMgr = schemaMgr;
|
2016-01-24 20:02:02 +03:00
|
|
|
}
|
|
|
|
|
2016-02-07 17:11:15 +03:00
|
|
|
normalizePointer() {
|
2016-02-10 16:48:07 +03:00
|
|
|
let schema = this.schemaMgr.byPointer(this.pointer);
|
2016-02-07 17:11:15 +03:00
|
|
|
return schema && schema.$ref || this.pointer;
|
|
|
|
}
|
|
|
|
|
2016-01-24 20:02:02 +03:00
|
|
|
load() {
|
2016-02-10 16:48:07 +03:00
|
|
|
if (this.optionsMgr.options.disableLazySchemas) return;
|
2016-01-24 22:27:15 +03:00
|
|
|
if (this.loaded) return;
|
|
|
|
if (this.pointer) {
|
|
|
|
this.dcl.loadNextToLocation(JsonSchema, this.elementRef).then((compRef) => {
|
2016-04-27 22:34:27 +03:00
|
|
|
this.initComponent(compRef);
|
2016-03-27 18:25:46 +03:00
|
|
|
// trigger change detection
|
2016-03-27 20:17:28 +03:00
|
|
|
compRef.hostView.changeDetectorRef.detectChanges();
|
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() {
|
|
|
|
this.pointer = this.normalizePointer(this.pointer);
|
|
|
|
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-02-10 16:48:07 +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-02-07 17:11:15 +03:00
|
|
|
this.dcl.loadNextToLocation(JsonSchema, this.elementRef).then((compRef) => {
|
2016-04-27 22:34:27 +03:00
|
|
|
this.initComponent(compRef);
|
2016-02-07 17:11:15 +03:00
|
|
|
compRef.hostView.changeDetectorRef.markForCheck();
|
|
|
|
});
|
|
|
|
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 {
|
|
|
|
cache[this.pointer] = this.dcl.loadNextToLocation(JsonSchema, this.elementRef).then((compRef) => {
|
2016-04-27 22:34:27 +03:00
|
|
|
this.initComponent(compRef);
|
2016-02-07 17:11:15 +03:00
|
|
|
compRef.hostView.changeDetectorRef.markForCheck();
|
|
|
|
return compRef;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 22:34:27 +03:00
|
|
|
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();
|
|
|
|
}
|
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
|
|
|
}
|