redoc/lib/components/base.ts

85 lines
1.6 KiB
TypeScript
Raw Normal View History

'use strict';
2016-08-22 12:12:13 +03:00
import { OnInit, OnDestroy } from '@angular/core';
2016-10-23 20:18:42 +03:00
import { SpecManager } from '../utils/spec-manager';
import { AppStateService } from '../services/app-state.service';
import { Subscription } from 'rxjs/Subscription';
2016-08-22 12:12:13 +03:00
export { SpecManager };
/**
* Generic Component
* @class
*/
export class BaseComponent implements OnInit, OnDestroy {
2016-07-26 12:03:15 +03:00
pointer: string;
2016-08-22 12:12:13 +03:00
componentSchema: any = null;
2016-06-22 19:13:57 +03:00
dereferencedCache = {};
2016-06-13 20:54:24 +03:00
constructor(public specMgr: SpecManager) {
}
/**
* onInit method is run by angular2 after all component inputs are resolved
*/
ngOnInit() {
2016-08-28 21:46:10 +03:00
this.preinit();
}
preinit() {
this.componentSchema = this.specMgr.byPointer(this.pointer || '');
2015-10-17 21:12:46 +03:00
this.init();
}
2015-12-21 22:35:57 +03:00
ngOnDestroy() {
this.destroy();
}
/**
* Used to initialize component
2015-10-17 21:12:46 +03:00
* @abstract
*/
2016-06-13 20:54:24 +03:00
init() {
// empty
}
2015-12-21 22:35:57 +03:00
/**
+ Used to destroy component
* @abstract
*/
2016-06-13 20:54:24 +03:00
destroy() {
// emtpy
}
}
2017-01-28 16:57:22 +03:00
export abstract class BaseSearchableComponent extends BaseComponent implements OnDestroy {
searchSubscription: Subscription;
constructor(public specMgr: SpecManager, public app: AppStateService) {
super(specMgr);
}
subscribeForSearch() {
this.searchSubscription = this.app.searchContainingPointers.subscribe(ptrs => {
for (let i = 0; i < ptrs.length; ++i) {
2017-01-28 19:47:12 +03:00
if (ptrs[i]) this.ensureSearchIsShown(ptrs[i]);
}
});
}
preinit() {
super.preinit();
this.subscribeForSearch();
}
ngOnDestroy() {
2017-01-28 16:57:22 +03:00
if (this.searchSubscription) {
this.searchSubscription.unsubscribe();
}
}
/**
+ Used to destroy component
* @abstract
*/
2017-01-28 16:57:22 +03:00
abstract ensureSearchIsShown(ptr: string);
}