redoc/lib/components/base.ts

125 lines
3.2 KiB
TypeScript
Raw Normal View History

'use strict';
import { Component, ChangeDetectionStrategy, OnInit, OnDestroy } from '@angular/core';
2016-05-06 00:48:41 +03:00
import { CORE_DIRECTIVES, JsonPipe, AsyncPipe } from '@angular/common';
2016-06-22 21:17:48 +03:00
import { SpecManager } from '../utils/SpecManager';
2016-05-06 00:48:41 +03:00
import { MarkedPipe, JsonPointerEscapePipe } from '../utils/pipes';
2016-06-22 21:17:48 +03:00
export { SpecManager };
// common inputs for all components
let commonInputs = ['pointer']; // json pointer to the schema chunk
// internal helper function
function safeConcat(a, b) {
let res = a && a.slice() || [];
2016-06-13 20:54:24 +03:00
b = (b == undefined) ? [] : b;
return res.concat(b);
}
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;
}
/**
* Class decorator
* Simplifies setup of component metainfo
* All options are options from either Component or View angular2 decorator
* For detailed info look angular2 doc
* @param {Object} options - component options
* @param {string[]} options.inputs - component inputs
* @param {*[]} options.directives - directives used by component
* (except CORE_DIRECTIVES)
* @param {*[]} options.pipes - pipes used by component
2015-10-17 21:56:24 +03:00
* @param {*[]} options.providers - component providers
* @param {string} options.templateUrl - path to component template
* @param {string} options.template - component template html
* @param {string} options.styles - component css styles
*/
export function RedocComponent(options) {
let inputs = safeConcat(options.inputs, commonInputs);
let directives = safeConcat(options.directives, CORE_DIRECTIVES);
let pipes = safeConcat(options.pipes, [JsonPointerEscapePipe, MarkedPipe, JsonPipe, AsyncPipe]);
2016-05-18 16:59:54 +03:00
if (options.onPushOnly === undefined) options.onPushOnly = true;
return function decorator(target) {
let componentDecorator = Component({
selector: options.selector,
inputs: inputs,
2015-10-15 21:35:05 +03:00
outputs: options.outputs,
2015-11-29 13:47:42 +03:00
providers: options.providers,
2016-07-01 15:53:16 +03:00
changeDetection: options.onPushOnly ? ChangeDetectionStrategy.OnPush : ChangeDetectionStrategy.Default,
2016-06-19 17:41:04 +03:00
animations: options.animations,
templateUrl: options.templateUrl,
template: options.template,
styles: options.styles,
directives: directives,
pipes: pipes
});
return componentDecorator(target) || target;
};
}
/**
* Generic Component
* @class
*/
export class BaseComponent implements OnInit, OnDestroy {
2016-06-12 20:44:34 +03:00
componentSchema: any = null;
pointer: String;
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() {
this.componentSchema = this.specMgr.byPointer(this.pointer || '');
this.prepareModel();
2015-10-17 21:12:46 +03:00
this.init();
}
2015-12-21 22:35:57 +03:00
ngOnDestroy() {
this.destroy();
}
/**
* Used to prepare model based on component schema
* @abstract
*/
2016-06-13 20:54:24 +03:00
prepareModel():any {
// emtpy
}
2015-10-17 21:12:46 +03:00
/**
* Used to initialize component. Run after prepareModel
* @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
}
}