redoc/lib/index.ts

58 lines
1.3 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-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-10-14 11:44:18 +03:00
return bootstrapRedoc()
2016-08-22 12:12:13 +03:00
.then(appRef => {
moduleRef = appRef;
console.log('ReDoc initialized!');
}).catch(err => {
2016-10-23 20:18:42 +03:00
//Redoc.displayError(err);
2016-08-22 12:12:13 +03:00
throw err;
});
};
export function destroy() {
moduleRef.destroy();
2016-10-12 23:05:30 +03:00
moduleRef = null;
2016-08-22 12:12:13 +03:00
};
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();