redoc/lib/components/Redoc/redoc.ts

70 lines
2.0 KiB
TypeScript
Raw Normal View History

'use strict';
2016-08-22 12:12:13 +03:00
import { ElementRef, ComponentRef, AfterViewInit, Component } from '@angular/core';
2016-08-28 21:46:10 +03:00
import { BrowserDomAdapter as DOM } from '../../utils/browser-adapter';
2016-08-22 12:12:13 +03:00
import { BaseComponent } from '../base';
2016-05-09 22:55:16 +03:00
2016-08-28 21:46:10 +03:00
import * as detectScollParent from 'scrollparent';
2016-05-20 20:36:10 +03:00
2016-08-22 12:12:13 +03:00
import { SpecManager } from '../../utils/SpecManager';
import { OptionsService, RedocEventsService } from '../../services/index';
2016-05-20 20:36:10 +03:00
2016-08-22 12:12:13 +03:00
@Component({
selector: 'redoc',
templateUrl: './redoc.html',
styleUrls: ['./redoc.css'],
})
export class Redoc extends BaseComponent implements AfterViewInit {
static appRef: ComponentRef<any>;
2016-06-06 19:32:20 +03:00
2016-06-13 20:54:24 +03:00
options: any;
2016-05-06 00:48:41 +03:00
2016-06-13 20:54:24 +03:00
private element: any;
2015-12-30 11:16:36 +03:00
2016-01-24 20:01:17 +03:00
static showLoadingAnimation() {
2016-08-28 21:46:10 +03:00
let elem = DOM.query('redoc');
DOM.addClass(elem, 'loading');
2016-01-24 20:01:17 +03:00
}
static hideLoadingAnimation() {
2016-08-28 21:46:10 +03:00
let redocEl = DOM.query('redoc');
if (!redocEl) return;
2016-08-28 21:46:10 +03:00
DOM.addClass(redocEl, 'loading-remove');
2016-01-24 20:01:17 +03:00
setTimeout(() => {
2016-08-28 21:46:10 +03:00
DOM.removeClass(redocEl, 'loading-remove');
DOM.removeClass(redocEl, 'loading');
2016-01-24 20:01:17 +03:00
}, 400);
}
2016-05-20 19:31:30 +03:00
static displayError(err) {
2016-08-28 21:46:10 +03:00
let redocEl = DOM.query('redoc');
if (!redocEl) return;
2016-05-20 19:31:30 +03:00
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;
}
constructor(specMgr: SpecManager, optionsMgr:OptionsService, elementRef:ElementRef,
2016-06-13 20:54:24 +03:00
public events:RedocEventsService) {
super(specMgr);
2016-06-13 20:54:24 +03:00
this.element = elementRef.nativeElement;
//parse options (top level component doesn't support inputs)
optionsMgr.parseOptions( this.element );
2016-08-28 21:46:10 +03:00
let scrollParent = detectScollParent( this.element );
if (scrollParent === DOM.defaultDoc().body) scrollParent = window;
optionsMgr.options.$scrollParent = scrollParent;
2016-06-13 20:54:24 +03:00
this.options = optionsMgr.options;
this.events = events;
}
ngAfterViewInit() {
setTimeout( () => {
this.events.bootstrapped.next({});
});
}
}