mirror of
https://github.com/Redocly/redoc.git
synced 2025-01-31 10:04:08 +03:00
Do spec load after bootstrap
This commit is contained in:
parent
f9a3e42f15
commit
5597a58846
|
@ -1,4 +1,4 @@
|
||||||
<div class="redoc-wrap">
|
<div class="redoc-wrap" *ngIf="specLoaded">
|
||||||
<div class="menu-content" sticky-sidebar [scrollParent]="options.$scrollParent" [scrollYOffset]="options.scrollYOffset">
|
<div class="menu-content" sticky-sidebar [scrollParent]="options.$scrollParent" [scrollYOffset]="options.scrollYOffset">
|
||||||
<api-logo> </api-logo>
|
<api-logo> </api-logo>
|
||||||
<side-menu> </side-menu>
|
<side-menu> </side-menu>
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
import { ElementRef, ComponentRef, AfterViewInit, Component, ChangeDetectionStrategy} from '@angular/core';
|
import { ElementRef, ComponentRef, ChangeDetectorRef, Input,
|
||||||
|
Component, OnInit, ChangeDetectionStrategy} from '@angular/core';
|
||||||
|
|
||||||
import { BrowserDomAdapter as DOM } from '../../utils/browser-adapter';
|
import { BrowserDomAdapter as DOM } from '../../utils/browser-adapter';
|
||||||
import { BaseComponent } from '../base';
|
import { BaseComponent } from '../base';
|
||||||
|
@ -16,31 +17,32 @@ import { OptionsService, RedocEventsService } from '../../services/index';
|
||||||
styleUrls: ['./redoc.css'],
|
styleUrls: ['./redoc.css'],
|
||||||
changeDetection: ChangeDetectionStrategy.OnPush
|
changeDetection: ChangeDetectionStrategy.OnPush
|
||||||
})
|
})
|
||||||
export class Redoc extends BaseComponent implements AfterViewInit {
|
export class Redoc extends BaseComponent implements OnInit {
|
||||||
static appRef: ComponentRef<any>;
|
static appRef: ComponentRef<any>;
|
||||||
static _preOptions: any;
|
static _preOptions: any;
|
||||||
|
specLoaded: boolean;
|
||||||
|
|
||||||
public options: any;
|
public options: any;
|
||||||
|
|
||||||
private element: any;
|
private element: any;
|
||||||
|
|
||||||
static showLoadingAnimation() {
|
@Input() specUrl: string;
|
||||||
let elem = DOM.query('redoc');
|
|
||||||
DOM.addClass(elem, 'loading');
|
// TODO: refactor in separate component/service
|
||||||
|
showLoadingAnimation() {
|
||||||
|
DOM.addClass(this.element, 'loading');
|
||||||
}
|
}
|
||||||
|
|
||||||
static hideLoadingAnimation() {
|
hideLoadingAnimation() {
|
||||||
let redocEl = DOM.query('redoc');
|
DOM.addClass(this.element, 'loading-remove');
|
||||||
if (!redocEl) return;
|
|
||||||
DOM.addClass(redocEl, 'loading-remove');
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
DOM.removeClass(redocEl, 'loading-remove');
|
DOM.removeClass(this.element, 'loading-remove');
|
||||||
DOM.removeClass(redocEl, 'loading');
|
DOM.removeClass(this.element, 'loading');
|
||||||
}, 400);
|
}, 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
static displayError(err) {
|
static displayError(err, elem?) {
|
||||||
let redocEl = DOM.query('redoc');
|
let redocEl = elem || DOM.query('redoc');
|
||||||
if (!redocEl) return;
|
if (!redocEl) return;
|
||||||
let heading = 'Oops... ReDoc failed to render this spec';
|
let heading = 'Oops... ReDoc failed to render this spec';
|
||||||
let details = err.message;
|
let details = err.message;
|
||||||
|
@ -51,10 +53,10 @@ export class Redoc extends BaseComponent implements AfterViewInit {
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(specMgr: SpecManager, optionsMgr:OptionsService, elementRef:ElementRef,
|
constructor(specMgr: SpecManager, optionsMgr:OptionsService, elementRef:ElementRef,
|
||||||
public events:RedocEventsService) {
|
public events:RedocEventsService, private changeDetector: ChangeDetectorRef) {
|
||||||
super(specMgr);
|
super(specMgr);
|
||||||
// merge options passed before init
|
// merge options passed before init
|
||||||
optionsMgr.options = Redoc._preOptions;
|
optionsMgr.options = Redoc._preOptions || {};
|
||||||
this.element = elementRef.nativeElement;
|
this.element = elementRef.nativeElement;
|
||||||
//parse options (top level component doesn't support inputs)
|
//parse options (top level component doesn't support inputs)
|
||||||
optionsMgr.parseOptions( this.element );
|
optionsMgr.parseOptions( this.element );
|
||||||
|
@ -62,12 +64,27 @@ export class Redoc extends BaseComponent implements AfterViewInit {
|
||||||
if (scrollParent === DOM.defaultDoc().body) scrollParent = window;
|
if (scrollParent === DOM.defaultDoc().body) scrollParent = window;
|
||||||
optionsMgr.options.$scrollParent = scrollParent;
|
optionsMgr.options.$scrollParent = scrollParent;
|
||||||
this.options = optionsMgr.options;
|
this.options = optionsMgr.options;
|
||||||
this.events = events;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ngAfterViewInit() {
|
load() {
|
||||||
setTimeout( () => {
|
this.showLoadingAnimation();
|
||||||
|
SpecManager.instance().load(this.options.specUrl).then(() => {
|
||||||
|
this.specLoaded = true;
|
||||||
|
this.changeDetector.markForCheck();
|
||||||
|
//this.changeDetector.detectChanges();
|
||||||
this.events.bootstrapped.next({});
|
this.events.bootstrapped.next({});
|
||||||
});
|
this.hideLoadingAnimation();
|
||||||
|
}).catch((err) => {
|
||||||
|
this.hideLoadingAnimation();
|
||||||
|
Redoc.displayError(err, this.element);
|
||||||
|
throw err;
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
ngOnInit() {
|
||||||
|
if (this.specUrl) {
|
||||||
|
this.options.specUrl = this.specUrl;
|
||||||
|
}
|
||||||
|
this.load();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,18 +29,11 @@ export function init(specUrl:string, options:any = {}) {
|
||||||
|
|
||||||
Redoc._preOptions = options;
|
Redoc._preOptions = options;
|
||||||
options.specUrl = options.specUrl || specUrl;
|
options.specUrl = options.specUrl || specUrl;
|
||||||
|
return bootstrapRedoc()
|
||||||
Redoc.showLoadingAnimation();
|
|
||||||
return SpecManager.instance().load(specUrl)
|
|
||||||
.then(() => {
|
|
||||||
return bootstrapRedoc();
|
|
||||||
})
|
|
||||||
.then(appRef => {
|
.then(appRef => {
|
||||||
Redoc.hideLoadingAnimation();
|
|
||||||
moduleRef = appRef;
|
moduleRef = appRef;
|
||||||
console.log('ReDoc initialized!');
|
console.log('ReDoc initialized!');
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
Redoc.hideLoadingAnimation();
|
|
||||||
Redoc.displayError(err);
|
Redoc.displayError(err);
|
||||||
throw err;
|
throw err;
|
||||||
});
|
});
|
||||||
|
|
|
@ -21,7 +21,10 @@ import { SpecManager } from './utils/SpecManager';
|
||||||
MenuService,
|
MenuService,
|
||||||
WarningsService,
|
WarningsService,
|
||||||
OptionsService
|
OptionsService
|
||||||
]
|
],
|
||||||
|
exports: [Redoc]
|
||||||
})
|
})
|
||||||
export class RedocModule {
|
export class RedocModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export { Redoc, SpecManager };
|
||||||
|
|
|
@ -30,10 +30,14 @@ export class SpecManager {
|
||||||
|
|
||||||
JsonSchemaRefParser.bundle(url, {http: {withCredentials: false}})
|
JsonSchemaRefParser.bundle(url, {http: {withCredentials: false}})
|
||||||
.then(schema => {
|
.then(schema => {
|
||||||
|
try {
|
||||||
this._url = url;
|
this._url = url;
|
||||||
this._schema = schema;
|
this._schema = schema;
|
||||||
this.init();
|
this.init();
|
||||||
return resolve(this._schema);
|
resolve(this._schema);
|
||||||
|
} catch(err) {
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
}, err => reject(err));
|
}, err => reject(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user