2015-10-09 23:19:35 +03:00
|
|
|
'use strict';
|
2016-06-15 21:48:04 +03:00
|
|
|
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-07-21 13:35:27 +03:00
|
|
|
import { MarkedPipe, JsonPointerEscapePipe, SafePipe } from '../utils/pipes';
|
2015-10-09 23:19:35 +03:00
|
|
|
|
2016-06-22 21:17:48 +03:00
|
|
|
export { SpecManager };
|
2016-02-10 14:19:50 +03:00
|
|
|
|
2015-10-09 23:19:35 +03:00
|
|
|
// 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;
|
2015-10-09 23:19:35 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-09 23:19:35 +03:00
|
|
|
/**
|
|
|
|
* 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
|
2015-10-09 23:19:35 +03:00
|
|
|
* @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);
|
2016-07-21 13:35:27 +03:00
|
|
|
let pipes = safeConcat(options.pipes, [JsonPointerEscapePipe, MarkedPipe, JsonPipe, AsyncPipe, SafePipe]);
|
2016-05-18 16:59:54 +03:00
|
|
|
if (options.onPushOnly === undefined) options.onPushOnly = true;
|
2015-10-09 23:19:35 +03:00
|
|
|
|
|
|
|
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,
|
2015-10-09 23:19:35 +03:00
|
|
|
templateUrl: options.templateUrl,
|
|
|
|
template: options.template,
|
|
|
|
styles: options.styles,
|
|
|
|
directives: directives,
|
|
|
|
pipes: pipes
|
|
|
|
});
|
|
|
|
|
2016-03-27 18:25:46 +03:00
|
|
|
return componentDecorator(target) || target;
|
2015-10-09 23:19:35 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic Component
|
|
|
|
* @class
|
|
|
|
*/
|
2016-06-15 21:48:04 +03:00
|
|
|
export class BaseComponent implements OnInit, OnDestroy {
|
2016-06-12 20:44:34 +03:00
|
|
|
componentSchema: any = null;
|
2016-07-26 12:03:15 +03:00
|
|
|
pointer: string;
|
2016-06-22 19:13:57 +03:00
|
|
|
dereferencedCache = {};
|
2016-06-13 20:54:24 +03:00
|
|
|
|
2016-06-23 17:36:38 +03:00
|
|
|
constructor(public specMgr: SpecManager) {
|
2015-10-09 23:19:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* onInit method is run by angular2 after all component inputs are resolved
|
|
|
|
*/
|
2015-12-14 18:20:40 +03:00
|
|
|
ngOnInit() {
|
2016-06-23 17:36:38 +03:00
|
|
|
this.componentSchema = this.specMgr.byPointer(this.pointer || '');
|
2015-10-17 21:12:46 +03:00
|
|
|
this.init();
|
2015-10-09 23:19:35 +03:00
|
|
|
}
|
|
|
|
|
2015-12-21 22:35:57 +03:00
|
|
|
ngOnDestroy() {
|
|
|
|
this.destroy();
|
|
|
|
}
|
|
|
|
|
2015-10-09 23:19:35 +03:00
|
|
|
/**
|
2016-07-20 11:07:08 +03:00
|
|
|
* 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
|
|
|
|
}
|
2015-10-09 23:19:35 +03:00
|
|
|
}
|