2015-10-07 12:47:57 +03:00
|
|
|
'use strict';
|
|
|
|
|
2016-02-11 14:38:44 +03:00
|
|
|
import {ChangeDetectionStrategy, provide, enableProdMode} from 'angular2/core';
|
2016-02-10 14:19:50 +03:00
|
|
|
import {ElementRef} from 'angular2/core';
|
|
|
|
import {BrowserDomAdapter, bootstrap} from 'angular2/platform/browser';
|
|
|
|
import detectScollParent from 'scrollparent';
|
2015-10-09 23:19:35 +03:00
|
|
|
import {RedocComponent, BaseComponent} from '../base';
|
2015-10-27 20:44:08 +03:00
|
|
|
import SchemaManager from '../../utils/SchemaManager';
|
2015-12-26 20:44:39 +03:00
|
|
|
|
2015-10-27 20:44:08 +03:00
|
|
|
import ApiInfo from '../ApiInfo/api-info';
|
2015-12-21 00:34:20 +03:00
|
|
|
import ApiLogo from '../ApiLogo/api-logo';
|
2015-10-27 20:44:08 +03:00
|
|
|
import MethodsList from '../MethodsList/methods-list';
|
2015-11-16 02:10:04 +03:00
|
|
|
import SideMenu from '../SideMenu/side-menu';
|
2015-12-26 20:44:39 +03:00
|
|
|
import StickySidebar from '../../common/components/StickySidebar/sticky-sidebar';
|
2015-12-30 02:29:29 +03:00
|
|
|
import OptionsManager from '../../options';
|
2015-12-30 11:16:36 +03:00
|
|
|
import {redocEvents} from '../../events';
|
2015-12-26 20:44:39 +03:00
|
|
|
|
2016-02-10 16:48:07 +03:00
|
|
|
import './redoc-loading-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-02-11 14:38:44 +03:00
|
|
|
BrowserDomAdapter
|
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'],
|
2015-12-26 20:44:39 +03:00
|
|
|
directives: [ApiInfo, ApiLogo, MethodsList, SideMenu, StickySidebar],
|
2015-11-29 14:37:19 +03:00
|
|
|
changeDetection: ChangeDetectionStrategy.Default
|
2015-10-07 12:47:57 +03:00
|
|
|
})
|
2016-02-10 16:48:07 +03:00
|
|
|
@Reflect.metadata('parameters', [
|
|
|
|
[SchemaManager], [OptionsManager], [ElementRef]])
|
2015-10-27 20:44:08 +03:00
|
|
|
export default class Redoc extends BaseComponent {
|
2016-02-10 16:48:07 +03:00
|
|
|
constructor(schemaMgr, optionsMgr, elementRef) {
|
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;
|
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-02-11 14:38:44 +03:00
|
|
|
var optionsMgr = new OptionsManager();
|
|
|
|
optionsMgr.options = options;
|
2016-03-31 00:20:31 +03:00
|
|
|
optionsMgr.options.specUrl = optionsMgr.options.specUrl || specUrl;
|
2016-02-11 14:38:44 +03:00
|
|
|
var providers = [
|
|
|
|
provide(OptionsManager, {useValue: optionsMgr})
|
|
|
|
];
|
|
|
|
|
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-02-11 14:38:44 +03:00
|
|
|
if (!_modeLocked && !optionsMgr.options.debugMode) {
|
|
|
|
enableProdMode();
|
|
|
|
_modeLocked = true;
|
|
|
|
}
|
|
|
|
return bootstrap(Redoc, providers);
|
2016-01-15 12:35:45 +03:00
|
|
|
})
|
|
|
|
.then(
|
2016-01-15 20:01:09 +03:00
|
|
|
(appRef) => {
|
2016-01-24 20:01:17 +03:00
|
|
|
Redoc.hideLoadingAnimation();
|
2016-01-15 20:01:09 +03:00
|
|
|
Redoc.appRef = appRef;
|
2016-02-10 16:48:07 +03:00
|
|
|
// setTimeout to allow cached elements to init
|
2016-02-07 17:11:15 +03:00
|
|
|
setTimeout(() => redocEvents.bootstrapped.next());
|
2016-01-15 12:35:45 +03:00
|
|
|
console.log('ReDoc bootstrapped!');
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
console.log(error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
);
|
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-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
|
|
|
}
|