redoc/lib/components/Redoc/redoc.js

127 lines
3.6 KiB
JavaScript
Raw Normal View History

'use strict';
import {ChangeDetectionStrategy, provide, enableProdMode} from 'angular2/core';
import {ElementRef} from 'angular2/core';
import {BrowserDomAdapter, bootstrap} from 'angular2/platform/browser';
import detectScollParent from 'scrollparent';
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';
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
import './redoc-loading-styles.css!css';
var dom = new BrowserDomAdapter();
var _modeLocked = false;
2016-01-24 20:01:17 +03:00
@RedocComponent({
selector: 'redoc',
providers: [
SchemaManager,
BrowserDomAdapter
],
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],
changeDetection: ChangeDetectionStrategy.Default
})
@Reflect.metadata('parameters', [
[SchemaManager], [OptionsManager], [ElementRef]])
2015-10-27 20:44:08 +03:00
export default class Redoc extends BaseComponent {
constructor(schemaMgr, optionsMgr, elementRef) {
super(schemaMgr);
2015-12-26 20:44:39 +03:00
this.element = elementRef.nativeElement;
//parse options (top level component doesn't support inputs)
optionsMgr.parseOptions( this.element );
optionsMgr.options.$scrollParent = detectScollParent( this.element );
this.options = optionsMgr.options;
}
2015-12-30 11:16:36 +03:00
2016-01-24 20:01:17 +03:00
static showLoadingAnimation() {
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) {
var optionsMgr = new OptionsManager();
optionsMgr.options = options;
2016-03-31 00:20:31 +03:00
optionsMgr.options.specUrl = optionsMgr.options.specUrl || specUrl;
var providers = [
provide(OptionsManager, {useValue: optionsMgr})
];
2016-01-15 20:01:09 +03:00
if (Redoc.appRef) {
Redoc.dispose();
}
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(() => {
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;
// 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-01-15 20:01:09 +03:00
static dispose() {
let el = dom.query('redoc');
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
elClone = el.cloneNode(false);
2016-01-20 17:37:23 +03:00
if (Redoc.appRef) {
Redoc.appRef.dispose();
Redoc.appRef = null;
2016-01-15 20:01:09 +03:00
2016-01-20 17:37:23 +03:00
// Redoc dispose removes host element, so need to restore it
elClone.innerHTML = 'Loading...';
parent && parent.insertBefore(elClone, nextSibling);
2016-01-20 17:37:23 +03:00
}
2016-01-15 20:01:09 +03:00
}
}