2015-10-07 12:47:57 +03:00
|
|
|
'use strict';
|
|
|
|
|
2016-05-09 22:55:16 +03:00
|
|
|
import { provide, enableProdMode, ElementRef } from '@angular/core';
|
2016-05-06 12:46:41 +03:00
|
|
|
import { bootstrap } from '@angular/platform-browser-dynamic';
|
2016-05-07 10:54:44 +03:00
|
|
|
import { BrowserDomAdapter } from '@angular/platform-browser/src/browser/browser_adapter';
|
2016-05-06 12:46:41 +03:00
|
|
|
import { RedocComponent, BaseComponent } from '../base';
|
2016-05-09 22:55:16 +03:00
|
|
|
|
2016-05-20 20:36:10 +03:00
|
|
|
import detectScollParent from 'scrollparent';
|
|
|
|
|
2016-05-09 22:55:16 +03:00
|
|
|
import { ApiInfo } from '../ApiInfo/api-info';
|
|
|
|
import { ApiLogo } from '../ApiLogo/api-logo';
|
|
|
|
import { MethodsList } from '../MethodsList/methods-list';
|
|
|
|
import { SideMenu } from '../SideMenu/side-menu';
|
|
|
|
|
2016-05-06 00:48:41 +03:00
|
|
|
import { StickySidebar } from '../../shared/components/index';
|
2016-05-06 12:46:41 +03:00
|
|
|
import SchemaManager from '../../utils/SchemaManager';
|
|
|
|
import { OptionsService, RedocEventsService } from '../../services/index';
|
2016-05-20 20:36:10 +03:00
|
|
|
import redocVersion from '../../version.json!json';
|
|
|
|
|
2016-05-06 00:48:41 +03:00
|
|
|
|
2016-05-20 19:31:30 +03:00
|
|
|
import './redoc-initial-styles.css!css';
|
2015-10-07 12:47:57 +03:00
|
|
|
|
2016-02-11 14:38:44 +03:00
|
|
|
var dom = new BrowserDomAdapter();
|
|
|
|
var _modeLocked = false;
|
2016-01-24 20:01:17 +03:00
|
|
|
|
2015-10-09 23:19:35 +03:00
|
|
|
@RedocComponent({
|
2015-10-07 12:47:57 +03:00
|
|
|
selector: 'redoc',
|
2016-02-10 16:48:07 +03:00
|
|
|
providers: [
|
|
|
|
SchemaManager,
|
2016-05-06 00:48:41 +03:00
|
|
|
BrowserDomAdapter,
|
|
|
|
RedocEventsService
|
2016-02-10 16:48:07 +03:00
|
|
|
],
|
2015-10-07 12:47:57 +03:00
|
|
|
templateUrl: './lib/components/Redoc/redoc.html',
|
2015-11-16 02:15:06 +03:00
|
|
|
styleUrls: ['./lib/components/Redoc/redoc.css'],
|
2016-05-18 16:59:54 +03:00
|
|
|
directives: [ ApiInfo, ApiLogo, MethodsList, SideMenu, StickySidebar ],
|
|
|
|
detect: true,
|
|
|
|
onPushOnly: false
|
2015-10-07 12:47:57 +03:00
|
|
|
})
|
2016-02-10 16:48:07 +03:00
|
|
|
@Reflect.metadata('parameters', [
|
2016-05-06 12:46:41 +03:00
|
|
|
[SchemaManager], [OptionsService], [ElementRef], [RedocEventsService]])
|
2016-05-06 00:48:41 +03:00
|
|
|
export class Redoc extends BaseComponent {
|
|
|
|
constructor(schemaMgr, optionsMgr, elementRef, events) {
|
2015-10-09 23:19:35 +03:00
|
|
|
super(schemaMgr);
|
2015-12-26 20:44:39 +03:00
|
|
|
this.element = elementRef.nativeElement;
|
|
|
|
//parse options (top level component doesn't support inputs)
|
2016-02-10 16:48:07 +03:00
|
|
|
optionsMgr.parseOptions( this.element );
|
|
|
|
optionsMgr.options.$scrollParent = detectScollParent( this.element );
|
|
|
|
this.options = optionsMgr.options;
|
2016-05-06 00:48:41 +03:00
|
|
|
this.events = events;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngAfterViewInit() {
|
|
|
|
setTimeout( () => {
|
|
|
|
this.events.bootstrapped.next();
|
|
|
|
});
|
2015-10-07 12:47:57 +03:00
|
|
|
}
|
2015-12-30 11:16:36 +03:00
|
|
|
|
2016-01-24 20:01:17 +03:00
|
|
|
static showLoadingAnimation() {
|
2016-01-24 22:27:15 +03:00
|
|
|
let elem = dom.query('redoc');
|
|
|
|
dom.addClass(elem, 'loading');
|
2016-01-24 20:01:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static hideLoadingAnimation() {
|
|
|
|
let redocEl = dom.query('redoc');
|
|
|
|
dom.addClass(redocEl, 'loading-remove');
|
|
|
|
setTimeout(() => {
|
|
|
|
dom.removeClass(redocEl, 'loading-remove');
|
|
|
|
dom.removeClass(redocEl, 'loading');
|
|
|
|
}, 400);
|
|
|
|
}
|
|
|
|
|
2016-03-31 00:20:31 +03:00
|
|
|
static init(specUrl, options) {
|
2016-05-06 12:46:41 +03:00
|
|
|
var optionsService = new OptionsService(dom);
|
|
|
|
optionsService.options = options;
|
|
|
|
optionsService.options.specUrl = optionsService.options.specUrl || specUrl;
|
2016-02-11 14:38:44 +03:00
|
|
|
var providers = [
|
2016-05-06 12:46:41 +03:00
|
|
|
provide(OptionsService, {useValue: optionsService})
|
2016-02-11 14:38:44 +03:00
|
|
|
];
|
|
|
|
|
2016-01-15 20:01:09 +03:00
|
|
|
if (Redoc.appRef) {
|
2016-04-30 00:45:53 +03:00
|
|
|
Redoc.destroy();
|
2016-01-15 20:01:09 +03:00
|
|
|
}
|
2016-01-24 20:01:17 +03:00
|
|
|
Redoc.showLoadingAnimation();
|
2016-03-31 00:20:31 +03:00
|
|
|
return SchemaManager.instance().load(specUrl)
|
2016-01-15 12:35:45 +03:00
|
|
|
.then(() => {
|
2016-05-06 12:46:41 +03:00
|
|
|
if (!_modeLocked && !optionsService.options.debugMode) {
|
2016-02-11 14:38:44 +03:00
|
|
|
enableProdMode();
|
|
|
|
_modeLocked = true;
|
|
|
|
}
|
|
|
|
return bootstrap(Redoc, providers);
|
2016-01-15 12:35:45 +03:00
|
|
|
})
|
2016-05-20 19:31:30 +03:00
|
|
|
.then(appRef => {
|
|
|
|
Redoc.hideLoadingAnimation();
|
|
|
|
Redoc.appRef = appRef;
|
|
|
|
console.log('ReDoc bootstrapped!');
|
|
|
|
}, err => {
|
|
|
|
console.log(err);
|
|
|
|
Redoc.hideLoadingAnimation();
|
|
|
|
Redoc.displayError(err);
|
|
|
|
});
|
2015-12-30 11:16:36 +03:00
|
|
|
}
|
2016-01-15 12:35:45 +03:00
|
|
|
|
2016-01-20 18:05:43 +03:00
|
|
|
static autoInit() {
|
|
|
|
const specUrlAttributeName = 'spec-url';
|
|
|
|
let redocEl = dom.query('redoc');
|
|
|
|
if (!redocEl) return;
|
|
|
|
if (dom.hasAttribute(redocEl, specUrlAttributeName)) {
|
|
|
|
let url = dom.getAttribute(redocEl, specUrlAttributeName);
|
|
|
|
Redoc.init(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 19:31:30 +03:00
|
|
|
static displayError(err) {
|
|
|
|
console.log(err);
|
|
|
|
let redocEl = dom.query('redoc');
|
|
|
|
let heading = 'Oops... ReDoc failed to render this spec';
|
|
|
|
let details = err.message;
|
|
|
|
let erroHtml = `<div class="redoc-error">
|
|
|
|
<h1>${heading}</h1>
|
|
|
|
<div class='redoc-error-details'>${details}</div>`;
|
|
|
|
redocEl.innerHTML = erroHtml;
|
|
|
|
}
|
|
|
|
|
2016-05-20 20:36:10 +03:00
|
|
|
static get version() {
|
|
|
|
return redocVersion;
|
|
|
|
}
|
|
|
|
|
2016-04-30 00:45:53 +03:00
|
|
|
static destroy() {
|
2016-01-15 20:01:09 +03:00
|
|
|
let el = dom.query('redoc');
|
2016-01-23 00:30:06 +03:00
|
|
|
let elClone;
|
2016-01-20 17:37:23 +03:00
|
|
|
let parent;
|
|
|
|
let nextSibling;
|
|
|
|
if (el) {
|
|
|
|
parent = el.parentElement;
|
|
|
|
nextSibling = el.nextElementSibling;
|
|
|
|
}
|
2016-01-15 20:01:09 +03:00
|
|
|
|
2016-01-23 00:30:06 +03:00
|
|
|
elClone = el.cloneNode(false);
|
|
|
|
|
2016-01-20 17:37:23 +03:00
|
|
|
if (Redoc.appRef) {
|
2016-04-30 00:45:53 +03:00
|
|
|
Redoc.appRef.destroy();
|
2016-01-20 17:37:23 +03:00
|
|
|
Redoc.appRef = null;
|
2016-01-15 20:01:09 +03:00
|
|
|
|
2016-04-30 00:45:53 +03:00
|
|
|
// Redoc destroy removes host element, so need to restore it
|
2016-01-23 00:30:06 +03:00
|
|
|
elClone.innerHTML = 'Loading...';
|
|
|
|
parent && parent.insertBefore(elClone, nextSibling);
|
2016-01-20 17:37:23 +03:00
|
|
|
}
|
2016-01-15 20:01:09 +03:00
|
|
|
}
|
2015-10-07 12:47:57 +03:00
|
|
|
}
|