redoc/lib/components/Redoc/redoc.js

134 lines
3.7 KiB
JavaScript
Raw Normal View History

'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
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-06 00:48:41 +03:00
2016-05-06 12:46:41 +03:00
import detectScollParent from 'scrollparent';
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,
2016-05-06 00:48:41 +03:00
BrowserDomAdapter,
RedocEventsService
],
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
})
@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) {
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;
2016-05-06 00:48:41 +03:00
this.events = events;
}
ngAfterViewInit() {
setTimeout( () => {
this.events.bootstrapped.next();
});
}
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) {
2016-05-06 12:46:41 +03:00
var optionsService = new OptionsService(dom);
optionsService.options = options;
optionsService.options.specUrl = optionsService.options.specUrl || specUrl;
var providers = [
2016-05-06 12:46:41 +03:00
provide(OptionsService, {useValue: optionsService})
];
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) {
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-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');
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) {
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
elClone.innerHTML = 'Loading...';
parent && parent.insertBefore(elClone, nextSibling);
2016-01-20 17:37:23 +03:00
}
2016-01-15 20:01:09 +03:00
}
}