redoc/lib/index.js

64 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-06-06 19:32:20 +03:00
'use strict';
2016-08-01 07:23:01 +03:00
import './components/Redoc/redoc-initial-styles.css!css';
2016-06-06 19:32:20 +03:00
import 'dropkickjs/build/css/dropkick.css!css';
import 'prismjs/themes/prism-dark.css!css';
import 'hint.css/hint.base.css!css';
2016-08-22 12:12:13 +03:00
//import { redocVersion } from './version.js';
2016-06-06 19:32:20 +03:00
2016-08-22 12:12:13 +03:00
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { RedocModule } from './redoc.module';
2016-06-06 19:32:20 +03:00
import { Redoc } from './components/index';
2016-08-22 12:12:13 +03:00
import { optionsService } from './redoc.module';
import { SpecManager } from './utils/SpecManager';
import { BrowserDomAdapter } from '@angular/platform-browser/src/browser/browser_adapter';
2016-06-06 19:32:20 +03:00
2016-08-22 12:12:13 +03:00
var dom = new BrowserDomAdapter();
2016-08-22 12:12:13 +03:00
//Redoc.version = redocVersion;
var moduleRef;
export function init(specUrl:string, options?) {
if (!optionsService.options.debugMode) {
enableProdMode();
}
2016-06-06 19:32:20 +03:00
2016-08-22 12:12:13 +03:00
if (moduleRef) {
destroy();
}
optionsService.options = options;
optionsService.options.specUrl = optionsService.options.specUrl || specUrl;
Redoc.showLoadingAnimation();
return SpecManager.instance().load(specUrl)
.then(() => {
return platformBrowserDynamic().bootstrapModule(RedocModule);
})
.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';
let redocEl = dom.query('redoc');
if (!redocEl) return;
if (dom.hasAttribute(redocEl, specUrlAttributeName)) {
let url = dom.getAttribute(redocEl, specUrlAttributeName);
init(url);
}
};
autoInit();