redoc/lib/components/Redoc/redoc.ts

132 lines
3.4 KiB
TypeScript
Raw Normal View History

'use strict';
2016-10-23 20:18:42 +03:00
import { ElementRef,
ComponentRef,
ChangeDetectorRef,
Input,
Component,
OnInit,
ChangeDetectionStrategy,
HostBinding
} 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-10-23 20:18:42 +03:00
import { SpecManager } from '../../utils/spec-manager';
import { OptionsService, Hash, AppStateService, SchemaHelper } from '../../services/index';
2016-11-23 02:23:32 +03:00
import { LazyTasksService } from '../../shared/components/LazyFor/lazy-for';
2016-10-23 20:18:42 +03:00
import { CustomErrorHandler } from '../../utils/';
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'],
2016-11-23 02:23:32 +03:00
//changeDetection: ChangeDetectionStrategy.OnPush
})
2016-10-14 11:44:18 +03:00
export class Redoc extends BaseComponent implements OnInit {
2016-08-29 07:30:49 +03:00
static _preOptions: 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-23 20:18:42 +03:00
error: any;
specLoaded: boolean;
options: any;
2016-01-24 20:01:17 +03:00
2016-11-23 02:23:32 +03:00
loadingProgress: number;
2016-10-23 20:18:42 +03:00
@Input() specUrl: string;
@HostBinding('class.loading') specLoading: boolean = false;
@HostBinding('class.loading-remove') specLoadingRemove: boolean = false;
2016-05-20 19:31:30 +03:00
2016-10-23 20:18:42 +03:00
constructor(
specMgr: SpecManager,
optionsMgr: OptionsService,
elementRef: ElementRef,
private changeDetector: ChangeDetectorRef,
2016-11-23 02:23:32 +03:00
private appState: AppStateService,
private lazyTasksService: LazyTasksService,
private hash: Hash
2016-10-23 20:18:42 +03:00
) {
super(specMgr);
2016-11-24 16:29:29 +03:00
SchemaHelper.setSpecManager(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-10-23 20:18:42 +03:00
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-11-30 14:23:24 +03:00
this.lazyTasksService.allSync = !this.options.lazyRendering;
2016-06-13 20:54:24 +03:00
}
2016-10-23 20:18:42 +03:00
hideLoadingAnimation() {
2016-11-24 16:29:29 +03:00
requestAnimationFrame(() => {
2016-10-23 20:18:42 +03:00
this.specLoadingRemove = true;
2016-11-24 16:29:29 +03:00
setTimeout(() => {
this.specLoadingRemove = false;
this.specLoading = false;
}, 400);
});
2016-10-23 20:18:42 +03:00
}
2016-11-23 02:23:32 +03:00
showLoadingAnimation() {
this.specLoading = true;
this.specLoadingRemove = false;
}
2016-10-14 11:44:18 +03:00
load() {
2016-10-31 10:16:39 +03:00
this.specMgr.load(this.options.specUrl).catch(err => {
throw err;
});
2016-11-23 02:23:32 +03:00
this.appState.loading.subscribe(loading => {
if (loading) {
this.showLoadingAnimation();
} else {
this.hideLoadingAnimation();
}
});
2016-10-23 20:18:42 +03:00
this.specMgr.spec.subscribe((spec) => {
if (!spec) {
2016-11-23 02:23:32 +03:00
this.appState.startLoading();
2016-10-23 20:18:42 +03:00
} else {
this.changeDetector.markForCheck();
2016-11-23 02:23:32 +03:00
this.changeDetector.detectChanges();
2016-11-24 16:29:29 +03:00
this.specLoaded = true;
setTimeout(() => {
this.hash.start();
});
2016-10-23 20:18:42 +03:00
}
});
2016-10-14 11:44:18 +03:00
}
ngOnInit() {
2016-11-23 02:23:32 +03:00
this.lazyTasksService.loadProgress.subscribe(progress => this.loadingProgress = progress)
2016-10-23 20:18:42 +03:00
this.appState.error.subscribe(_err => {
2016-10-31 10:16:39 +03:00
if (!_err) return;
2016-10-30 18:54:44 +03:00
2016-10-31 10:16:39 +03:00
if (this.specLoading) {
this.specLoaded = true;
this.hideLoadingAnimation();
}
this.error = _err;
this.changeDetector.markForCheck();
2016-10-30 18:54:44 +03:00
setTimeout(() => {
this.changeDetector.detectChanges()
});
2016-10-23 20:18:42 +03:00
})
2016-10-14 11:44:18 +03:00
if (this.specUrl) {
this.options.specUrl = this.specUrl;
}
this.load();
2016-06-13 20:54:24 +03:00
}
}