redoc/lib/components/Redoc/redoc.ts

91 lines
2.7 KiB
TypeScript
Raw Normal View History

'use strict';
2016-10-14 11:44:18 +03:00
import { ElementRef, ComponentRef, ChangeDetectorRef, Input,
Component, OnInit, ChangeDetectionStrategy} from '@angular/core';
2016-08-22 12:12:13 +03:00
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'],
changeDetection: ChangeDetectionStrategy.OnPush
})
2016-10-14 11:44:18 +03:00
export class Redoc extends BaseComponent implements OnInit {
static appRef: ComponentRef<any>;
2016-08-29 07:30:49 +03:00
static _preOptions: any;
2016-10-14 11:44:18 +03:00
specLoaded: boolean;
2016-06-06 19:32:20 +03:00
2016-08-29 07:30:49 +03:00
public 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-10-14 11:44:18 +03:00
@Input() specUrl: string;
// TODO: refactor in separate component/service
showLoadingAnimation() {
DOM.addClass(this.element, 'loading');
2016-01-24 20:01:17 +03:00
}
2016-10-14 11:44:18 +03:00
hideLoadingAnimation() {
DOM.addClass(this.element, 'loading-remove');
2016-01-24 20:01:17 +03:00
setTimeout(() => {
2016-10-14 11:44:18 +03:00
DOM.removeClass(this.element, 'loading-remove');
DOM.removeClass(this.element, 'loading');
2016-01-24 20:01:17 +03:00
}, 400);
}
2016-10-14 11:44:18 +03:00
static displayError(err, elem?) {
let redocEl = elem || 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-10-14 11:44:18 +03:00
public events:RedocEventsService, private changeDetector: ChangeDetectorRef) {
super(specMgr);
2016-08-29 07:30:49 +03:00
// merge options passed before init
2016-10-14 11:44:18 +03:00
optionsMgr.options = Redoc._preOptions || {};
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;
}
2016-10-14 11:44:18 +03:00
load() {
this.showLoadingAnimation();
SpecManager.instance().load(this.options.specUrl).then(() => {
this.specLoaded = true;
this.changeDetector.markForCheck();
//this.changeDetector.detectChanges();
2016-06-13 20:54:24 +03:00
this.events.bootstrapped.next({});
2016-10-14 11:44:18 +03:00
this.hideLoadingAnimation();
}).catch((err) => {
this.hideLoadingAnimation();
Redoc.displayError(err, this.element);
throw err;
})
}
ngOnInit() {
if (this.specUrl) {
this.options.specUrl = this.specUrl;
}
this.load();
2016-06-13 20:54:24 +03:00
}
}