redoc/lib/components/Redoc/redoc.ts

153 lines
3.7 KiB
TypeScript
Raw Normal View History

'use strict';
2016-10-23 20:18:42 +03:00
import { ElementRef,
ChangeDetectorRef,
Input,
Component,
OnInit,
2016-12-19 18:13:39 +03:00
OnDestroy,
2016-10-23 20:18:42 +03:00
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';
2017-03-30 15:17:08 +03:00
import {
SearchService,
OptionsService,
Options,
Hash,
AppStateService,
SchemaHelper,
MenuService,
Marker
} from '../../services/';
2016-11-23 02:23:32 +03:00
import { LazyTasksService } from '../../shared/components/LazyFor/lazy-for';
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'],
2017-03-30 15:17:08 +03:00
providers: [
SpecManager,
MenuService,
SearchService,
LazyTasksService,
Marker
]
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-10-23 20:18:42 +03:00
error: any;
specLoaded: boolean;
options: Options;
2016-01-24 20:01:17 +03:00
2016-11-23 02:23:32 +03:00
loadingProgress: number;
2016-12-19 18:36:44 +03:00
private element: HTMLElement;
private $parent: Element;
private $refElem: Element;
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;
2016-12-19 18:13:39 +03:00
this.$parent = this.element.parentElement;
this.$refElem = this.element.nextElementSibling;
2016-06-13 20:54:24 +03:00
//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() {
// bunlde spec directly if passsed or load by URL
this.specMgr.load(this.options.spec || this.options.specUrl).catch(err => {
2016-10-31 10:16:39 +03:00
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 {
2017-03-29 13:29:54 +03:00
this.specLoaded = true;
2016-10-23 20:18:42 +03:00
this.changeDetector.markForCheck();
2016-11-23 02:23:32 +03:00
this.changeDetector.detectChanges();
2016-11-24 16:29:29 +03:00
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
this.appState.stopLoading();
if (this.loadingProgress === 100) return;
2016-10-31 10:16:39 +03:00
this.error = _err;
this.changeDetector.markForCheck();
});
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
}
2016-12-19 18:13:39 +03:00
ngOnDestroy() {
let $clone = this.element.cloneNode();
this.$parent.insertBefore($clone, this.$refElem);
}
}