redoc/lib/components/base.ts

101 lines
1.8 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 };
2016-01-09 23:31:39 +03:00
function snapshot(obj) {
2016-06-13 20:54:24 +03:00
if(obj == undefined || typeof(obj) !== 'object') {
2016-01-09 23:31:39 +03:00
return obj;
}
var temp = new obj.constructor();
for(var key in obj) {
if (obj.hasOwnProperty(key)) {
temp[key] = snapshot(obj[key]);
}
}
return temp;
}
/**
* 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
}
}
export class BaseSearchableComponent extends BaseComponent {
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) {
this.ensureSearchIsShown(ptrs[i]);
}
});
}
preinit() {
super.preinit();
this.subscribeForSearch();
}
ngOnDestroy() {
this.searchSubscription.unsubscribe();
}
/**
+ Used to destroy component
* @abstract
*/
ensureSearchIsShown(ptr: string) {
// empy
}
}