2016-06-06 19:32:20 +03:00
|
|
|
'use strict';
|
2016-08-28 21:46:10 +03:00
|
|
|
import './components/Redoc/redoc-initial-styles.css';
|
2016-06-06 19:32:20 +03:00
|
|
|
|
2016-08-22 12:12:13 +03:00
|
|
|
import { enableProdMode } from '@angular/core';
|
2016-06-06 19:32:20 +03:00
|
|
|
import { Redoc } from './components/index';
|
2016-08-22 12:12:13 +03:00
|
|
|
import { SpecManager } from './utils/SpecManager';
|
2016-08-28 21:46:10 +03:00
|
|
|
import { BrowserDomAdapter as DOM } from './utils/browser-adapter';
|
|
|
|
import { disableDebugTools } from '@angular/platform-browser';
|
|
|
|
import { bootstrapRedoc } from './bootstrap';
|
2016-06-06 19:32:20 +03:00
|
|
|
|
2016-08-28 21:46:10 +03:00
|
|
|
export const version = LIB_VERSION;
|
2016-06-23 17:36:38 +03:00
|
|
|
|
2016-08-22 12:12:13 +03:00
|
|
|
var moduleRef;
|
|
|
|
export function init(specUrl:string, options?) {
|
2016-08-28 21:46:10 +03:00
|
|
|
if (IS_PRODUCTION) {
|
|
|
|
disableDebugTools();
|
|
|
|
enableProdMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (moduleRef) {
|
|
|
|
destroy();
|
|
|
|
}
|
2016-08-29 07:30:49 +03:00
|
|
|
|
|
|
|
Redoc._preOptions = options;
|
|
|
|
options.specUrl = options.specUrl || specUrl;
|
2016-08-22 12:12:13 +03:00
|
|
|
|
|
|
|
Redoc.showLoadingAnimation();
|
|
|
|
return SpecManager.instance().load(specUrl)
|
|
|
|
.then(() => {
|
2016-08-28 21:46:10 +03:00
|
|
|
return bootstrapRedoc();
|
2016-08-22 12:12:13 +03:00
|
|
|
})
|
|
|
|
.then(appRef => {
|
|
|
|
Redoc.hideLoadingAnimation();
|
|
|
|
moduleRef = appRef;
|
|
|
|
console.log('ReDoc initialized!');
|
|
|
|
}).catch(err => {
|
|
|
|
Redoc.hideLoadingAnimation();
|
|
|
|
Redoc.displayError(err);
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export function destroy() {
|
|
|
|
moduleRef.destroy();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function autoInit() {
|
|
|
|
const specUrlAttributeName = 'spec-url';
|
2016-08-28 21:46:10 +03:00
|
|
|
let redocEl = DOM.query('redoc');
|
2016-08-22 12:12:13 +03:00
|
|
|
if (!redocEl) return;
|
2016-08-28 21:46:10 +03:00
|
|
|
if (DOM.hasAttribute(redocEl, specUrlAttributeName)) {
|
|
|
|
let url = DOM.getAttribute(redocEl, specUrlAttributeName);
|
2016-08-22 12:12:13 +03:00
|
|
|
init(url);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
autoInit();
|