redoc/lib/index.ts

65 lines
1.5 KiB
TypeScript
Raw Normal View History

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';
var bootstrapRedoc;
if (AOT) {
bootstrapRedoc = require('./bootstrap').bootstrapRedoc;
} else {
bootstrapRedoc = require('./bootstrap.dev').bootstrapRedoc;
}
2016-06-06 19:32:20 +03:00
2016-08-29 18:24:50 +03:00
if (IS_PRODUCTION) {
disableDebugTools();
enableProdMode();
}
2016-08-28 21:46:10 +03:00
export const version = LIB_VERSION;
2016-08-22 12:12:13 +03:00
var moduleRef;
2016-08-30 13:40:26 +03:00
export function init(specUrl:string, options:any = {}) {
2016-08-28 21:46:10 +03:00
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();