ngx-manage
Version:
- angular class descriptor and state
997 lines (988 loc) • 30.2 kB
JavaScript
import { __assign, __extends, __spread, __awaiter, __generator, __values } from 'tslib';
import { Subject, BehaviorSubject } from 'rxjs';
import 'reflect-metadata';
import { Injectable, ɵɵdefineInjectable, Component, Input, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
var BASE_SHEMA = '$base';
/** @enum {number} */
var BaseValueProp = {
None: 0,
Text: 1,
Input: 2,
Disabled: 3,
Hidden: 4,
};
BaseValueProp[BaseValueProp.None] = 'None';
BaseValueProp[BaseValueProp.Text] = 'Text';
BaseValueProp[BaseValueProp.Input] = 'Input';
BaseValueProp[BaseValueProp.Disabled] = 'Disabled';
BaseValueProp[BaseValueProp.Hidden] = 'Hidden';
/** @enum {string} */
var BaseValueType = {
Text: 'text',
Number: 'number',
Array: 'array',
Object: 'object',
Boolean: 'boolean',
Undefined: 'undefined',
Date: 'date',
Time: 'time',
Error: 'error',
Custom: 'custom',
};
var BaseParam = /** @class */ (function () {
function BaseParam(type, shema, prop, template, provider, validator) {
if (type === void 0) { type = BaseValueType.Text; }
if (shema === void 0) { shema = BASE_SHEMA; }
if (prop === void 0) { prop = BaseValueProp.Text; }
if (template === void 0) { template = ''; }
if (provider === void 0) { provider = ''; }
if (validator === void 0) { validator = ''; }
this.type = type;
this.shema = shema;
this.prop = prop;
this.template = template;
this.provider = provider;
this.validator = validator;
}
return BaseParam;
}());
if (false) {
/** @type {?} */
BaseParam.prototype.type;
/** @type {?} */
BaseParam.prototype.shema;
/** @type {?} */
BaseParam.prototype.prop;
/** @type {?} */
BaseParam.prototype.template;
/** @type {?} */
BaseParam.prototype.provider;
/** @type {?} */
BaseParam.prototype.validator;
}
var BaseValue = /** @class */ (function () {
function BaseValue(name, param) {
this.name = name;
this.param = param;
}
return BaseValue;
}());
if (false) {
/** @type {?} */
BaseValue.prototype.name;
/** @type {?} */
BaseValue.prototype.param;
}
/**
* @param {?} obj
* @return {?}
*/
function objectType(obj) {
if (obj !== undefined) {
if (obj instanceof Array) {
return BaseValueType.Array;
}
else if (typeof obj === 'boolean') {
return BaseValueType.Boolean;
}
else if (typeof obj === 'number') {
return BaseValueType.Number;
}
else if (obj && obj.hours !== undefined && '' + obj.hours) {
return BaseValueType.Time;
}
else if (obj && typeof obj.getMilliseconds === 'function') {
return BaseValueType.Date;
}
else {
return BaseValueType.Text;
}
}
return BaseValueType.Undefined;
}
/**
* @param {?} descriptor
* @param {?} property
* @return {?}
*/
function Descriptor(descriptor, property) {
return (/**
* @template T
* @param {?} constructor
* @return {?}
*/
function (constructor) {
/** @type {?} */
var a = new constructor();
if (!property) {
property = Object.getOwnPropertyNames(a);
}
/** @type {?} */
var array = property;
/** @type {?} */
var trg = constructor.prototype;
//console.log('Descriptor',array,trg);
/**
* @param {?} data
* @param {?} propertyKey
* @return {?}
*/
function proc(data, propertyKey) {
/** @type {?} */
var base = Object.getOwnPropertyDescriptor(trg, data.shema);
if (!base) {
Object.defineProperty(trg, data.shema, {
configurable: false,
enumerable: true,
value: {
list: [],
hash: {},
},
});
base = Object.getOwnPropertyDescriptor(trg, data.shema);
}
// keep previous definition !
if (base && !base.value.hash[propertyKey]) {
base.value.hash[propertyKey] = new BaseValue(propertyKey, data);
base.value.list.push(propertyKey);
Object.defineProperty(trg, data.shema, base);
}
}
descriptor.forEach((/**
* @param {?} d
* @return {?}
*/
function (d) {
array.forEach((/**
* @param {?} name
* @return {?}
*/
function (name) {
/** @type {?} */
var nd = __assign({}, d);
/** @type {?} */
var t = nd.type;
if (t === BaseValueType.Undefined && a) {
// get value type
nd.type = objectType(a[name]);
}
proc(nd, name);
}));
/** @type {?} */
var base = Object.getOwnPropertyDescriptor(trg, d.shema);
if (base) {
base.value.list = property.slice();
Object.defineProperty(trg, d.shema, base);
}
}));
return /** @class */ (function (_super) {
__extends(class_1, _super);
function class_1() {
return _super !== null && _super.apply(this, arguments) || this;
}
return class_1;
}(constructor));
});
}
/**
* @param {?} descriptor
* @return {?}
*/
function BaseDescriptors(descriptor) {
/**
* @param {?} data
* @param {?} target
* @param {?} propertyKey
* @return {?}
*/
function proc(data, target, propertyKey) {
/** @type {?} */
var base = Object.getOwnPropertyDescriptor(target, data.shema);
if (!base) {
Object.defineProperty(target, data.shema, {
configurable: false,
enumerable: true,
value: {
hash: {},
list: [],
},
});
base = Object.getOwnPropertyDescriptor(target, data.shema);
}
if (base) {
base.value.hash[propertyKey] = new BaseValue(propertyKey, data);
base.value.list.push(propertyKey);
Object.defineProperty(target, data.shema, base);
}
}
return (/**
* @param {?} target
* @param {?} propertyKey
* @return {?}
*/
function (target, propertyKey) {
descriptor.forEach((/**
* @param {?} d
* @return {?}
*/
function (d) {
proc(d, target, propertyKey);
}));
});
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
var Injector = new /** @class */ (function () {
function class_1() {
this.hash = {};
}
// resolving instances
// resolving instances
/**
* @template T
* @param {?} target
* @return {?}
*/
class_1.prototype.resolveHash =
// resolving instances
/**
* @template T
* @param {?} target
* @return {?}
*/
function (target) {
if (this.hash[target.name]) {
return this.hash[target.name];
}
// tokens are required dependencies, while injections are resolved tokens from the Injector
/** @type {?} */
var tokens = Reflect.getMetadata('design:paramtypes', target) || [];
/** @type {?} */
var injections = tokens.map((/**
* @param {?} token
* @return {?}
*/
function (token) { return Injector.resolve(token); }));
this.hash[target.name] = new (target.bind.apply(target, __spread([void 0], injections)))();
return this.hash[target.name];
};
/**
* @template T
* @param {?} target
* @return {?}
*/
class_1.prototype.resolve = /**
* @template T
* @param {?} target
* @return {?}
*/
function (target) {
// tokens are required dependencies, while injections are resolved tokens from the Injector
/** @type {?} */
var tokens = Reflect.getMetadata('design:paramtypes', target) || [];
/** @type {?} */
var injections = tokens.map((/**
* @param {?} token
* @return {?}
*/
function (token) { return Injector.resolve(token); }));
return new (target.bind.apply(target, __spread([void 0], injections)))();
};
return class_1;
}());
var StateMessage = /** @class */ (function () {
function StateMessage(name, state, msg) {
this.name = name;
this.state = state;
this.msg = msg;
}
return StateMessage;
}());
if (false) {
/** @type {?} */
StateMessage.prototype.name;
/** @type {?} */
StateMessage.prototype.state;
/** @type {?} */
StateMessage.prototype.msg;
}
/**
* @record
*/
function IStateMessageService() { }
if (false) {
/**
* @param {?} name
* @param {?} message
* @param {?} state
* @return {?}
*/
IStateMessageService.prototype.SendMessage = function (name, message, state) { };
}
var StateMessageService = /** @class */ (function () {
function StateMessageService() {
}
/**
* @param {?} name
* @param {?} message
* @param {?} state
* @return {?}
*/
StateMessageService.prototype.SendMessage = /**
* @param {?} name
* @param {?} message
* @param {?} state
* @return {?}
*/
function (name, message, state) {
return __awaiter(this, void 0, void 0, function () {
var msg;
return __generator(this, function (_a) {
msg = {};
state.sendMessage(name, msg);
return [2 /*return*/];
});
});
};
return StateMessageService;
}());
/**
* @record
*/
function IStateService() { }
if (false) {
/**
* @param {?} name
* @param {?} cb
* @return {?}
*/
IStateService.prototype.getState = function (name, cb) { };
/**
* @param {?} name
* @param {?} message
* @return {?}
*/
IStateService.prototype.sendMessage = function (name, message) { };
/**
* @param {?} set
* @return {?}
*/
IStateService.prototype.getStates = function (set) { };
/**
* @param {?} name
* @param {?} id
* @return {?}
*/
IStateService.prototype.read = function (name, id) { };
/**
* @param {?} name
* @param {?} id
* @param {?} data
* @param {?} cache
* @return {?}
*/
IStateService.prototype.write = function (name, id, data, cache) { };
}
var StateService = /** @class */ (function () {
function StateService() {
this.version = '1.0.2';
this.subjects = {};
this.state = {};
}
/**
* @private
* @param {?} name
* @return {?}
*/
StateService.prototype.init = /**
* @private
* @param {?} name
* @return {?}
*/
function (name) {
if (!this.subjects[name]) {
this.state = {};
this.subjects[name] = new Subject();
}
return this.subjects[name];
};
/**
* @param {?} name
* @param {?} cb
* @return {?}
*/
StateService.prototype.getState = /**
* @param {?} name
* @param {?} cb
* @return {?}
*/
function (name, cb) {
return this.init(name).asObservable().subscribe((/**
* @param {?} msg
* @return {?}
*/
function (msg) { return cb(msg); }));
};
/**
* @param {?} set
* @return {?}
*/
StateService.prototype.getStates = /**
* @param {?} set
* @return {?}
*/
function (set) {
/** @type {?} */
var subscribtion = [];
for (var s in set) {
subscribtion.push(this.init(s).asObservable().subscribe((/**
* @param {?} msg
* @return {?}
*/
function (msg) {
/** @type {?} */
var fn = set[msg.name] || set[s];
fn(msg);
})));
}
return subscribtion;
};
/**
* @param {?} name
* @param {?} message
* @return {?}
*/
StateService.prototype.sendMessage = /**
* @param {?} name
* @param {?} message
* @return {?}
*/
function (name, message) {
/** @type {?} */
var a = this.init(name);
a.next({ msg: message, name: name });
};
/**
* @param {?} name
* @param {?} id
* @return {?}
*/
StateService.prototype.read = /**
* @param {?} name
* @param {?} id
* @return {?}
*/
function (name, id) {
/** @type {?} */
var d = this.state[name] || {};
/** @type {?} */
var i = d[id];
if (i)
return i;
/** @type {?} */
var data = localStorage.getItem(this.version + '_' + name + '_' + id);
/** @type {?} */
var cacheData = JSON.parse(data);
if (cacheData) {
this.state[name] = this.state[name] || {};
this.state[name][id] = cacheData;
this.write(name, id, cacheData, true);
return this.state[name][id];
}
};
/**
* @param {?} name
* @param {?} id
* @param {?} data
* @param {?=} cache
* @return {?}
*/
StateService.prototype.write = /**
* @param {?} name
* @param {?} id
* @param {?} data
* @param {?=} cache
* @return {?}
*/
function (name, id, data, cache) {
if (cache === void 0) { cache = false; }
if (!this.state[name])
this.state[name] = {};
if (this.state[name]) {
this.state[name][id] = data;
}
if (!cache) {
/** @type {?} */
var c = JSON.stringify(data);
localStorage.setItem(this.version + '_' + name + '_' + id, c);
}
};
return StateService;
}());
if (false) {
/**
* @type {?}
* @private
*/
StateService.prototype.version;
/**
* @type {?}
* @private
*/
StateService.prototype.subjects;
/**
* @type {?}
* @private
*/
StateService.prototype.state;
}
/**
* @param {?} config
* @return {?}
*/
function StateDecorator(config) {
return (/**
* @template T
* @param {?} constr
* @return {?}
*/
function _DecoratorName(constr) {
/** @type {?} */
var service;
/** @type {?} */
var state = Injector.resolveHash(StateService);
if (config.service) {
/** @type {?} */
var i = config.service;
if (i.static)
service = Injector.resolveHash(i.def);
else
service = Injector.resolve(i.def);
}
return /** @class */ (function (_super) {
__extends(class_2, _super);
function class_2() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.message = service;
_this.subscribtion = [];
return _this;
}
/**
* @param {?} set
* @return {?}
*/
class_2.prototype.getStates = /**
* @param {?} set
* @return {?}
*/
function (set) {
this.subscribtion = state.getStates(set);
};
/**
* @param {?} name
* @param {?} cb
* @return {?}
*/
class_2.prototype.getState = /**
* @param {?} name
* @param {?} cb
* @return {?}
*/
function (name, cb) {
this.subscribtion.push(state.getState(name, cb));
};
/**
* @param {?} name
* @param {?} message
* @return {?}
*/
class_2.prototype.sendMessage = /**
* @param {?} name
* @param {?} message
* @return {?}
*/
function (name, message) {
if (this.message) {
this.message.SendMessage(name, message, state);
}
};
/**
* @return {?}
*/
class_2.prototype.ngOnDestroy = /**
* @return {?}
*/
function () {
this.subscribtion.forEach((/**
* @param {?} e
* @return {?}
*/
function (e) { return e.unsubscribe(); }));
};
return class_2;
}(constr));
});
}
var StateBase = /** @class */ (function () {
function StateBase() {
}
/**
* @param {?} name
* @param {?} cb
* @return {?}
*/
StateBase.prototype.getState = /**
* @param {?} name
* @param {?} cb
* @return {?}
*/
function (name, cb) { };
;
/**
* @param {?} name
* @param {?} message
* @return {?}
*/
StateBase.prototype.sendMessage = /**
* @param {?} name
* @param {?} message
* @return {?}
*/
function (name, message) { };
/**
* @return {?}
*/
StateBase.prototype.ngOnDestroy = /**
* @return {?}
*/
function () { };
/**
* @param {?} set
* @return {?}
*/
StateBase.prototype.getStates = /**
* @param {?} set
* @return {?}
*/
function (set) { };
;
return StateBase;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var TemplateServiceMessage = /** @class */ (function () {
function TemplateServiceMessage(Template, Shema) {
if (Shema === void 0) { Shema = BASE_SHEMA; }
this.Template = Template;
this.Shema = Shema;
}
return TemplateServiceMessage;
}());
if (false) {
/** @type {?} */
TemplateServiceMessage.prototype.Template;
/** @type {?} */
TemplateServiceMessage.prototype.Shema;
}
var TemplateService = /** @class */ (function () {
function TemplateService() {
this.subject = new BehaviorSubject(undefined);
}
/**
* @param {?} Template
* @param {?=} Shema
* @return {?}
*/
TemplateService.prototype.load = /**
* @param {?} Template
* @param {?=} Shema
* @return {?}
*/
function (Template, Shema) {
if (Shema === void 0) { Shema = BASE_SHEMA; }
this.subject.next(new TemplateServiceMessage(Template, Shema));
};
/**
* @return {?}
*/
TemplateService.prototype.getTemplates = /**
* @return {?}
*/
function () {
return this.subject.asObservable();
};
TemplateService.decorators = [
{ type: Injectable, args: [{
providedIn: 'root'
},] }
];
/** @nocollapse */
TemplateService.ctorParameters = function () { return []; };
/** @nocollapse */ TemplateService.ngInjectableDef = ɵɵdefineInjectable({ factory: function TemplateService_Factory() { return new TemplateService(); }, token: TemplateService, providedIn: "root" });
return TemplateService;
}());
if (false) {
/**
* @type {?}
* @private
*/
TemplateService.prototype.subject;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var BaseComponent = /** @class */ (function () {
function BaseComponent(template) {
this.template = template;
this.values = [];
}
/**
* @return {?}
*/
BaseComponent.prototype.ngOnInit = /**
* @return {?}
*/
function () {
// <pre>{{Value | json}}</pre>
var _this = this;
this.template.getTemplates().subscribe((/**
* @param {?} message
* @return {?}
*/
function (message) {
var e_1, _a;
if (message) {
//console.log('subscribe',this);
/** @type {?} */
var _base_ = message.Template[BaseValueType.Text];
_this.error = message.Template[BaseValueType.Error];
_this.descriptor = _this._value !== undefined ? Object.getOwnPropertyDescriptor(_this._value.__proto__.__proto__, message.Shema) : undefined;
/** @type {?} */
var all = [];
/** @type {?} */
var overwriteProp = _this.owner ? _this.owner.prop : undefined;
if (_this.descriptor && _this.descriptor.value) {
/** @type {?} */
var v = _this.descriptor.value;
try {
for (var _b = __values(v.list), _c = _b.next(); !_c.done; _c = _b.next()) {
var name = _c.value;
/** @type {?} */
var descriptor = v.hash[name];
/** @type {?} */
var template = void 0;
if (descriptor.param.type == BaseValueType.Custom && descriptor.param.template) {
template = message.Template[descriptor.param.template];
}
else {
template = message.Template[descriptor.param.type];
}
template = template || _base_;
if (descriptor.param.prop !== BaseValueProp.None) {
/** @type {?} */
var prop = descriptor.param.prop == BaseValueProp.Input ?
(overwriteProp && overwriteProp == BaseValueProp.Disabled ? BaseValueProp.Disabled : descriptor.param.prop)
: descriptor.param.prop;
all.push({
value: {
target: _this._value,
prop: prop,
name: name,
owner: _this.owner,
param: descriptor.param,
uid: Math.random().toString(36).substring(2) + Date.now().toString(36),
/**
* @return {?}
*/
get value() { return this.target[this.name]; },
/**
* @param {?} value
* @return {?}
*/
set value(value) {
this.target[this.name] = value;
}
},
template: template
});
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
}
_this.values = all;
}
}));
};
/**
* @return {?}
*/
BaseComponent.prototype.ngAfterViewInit = /**
* @return {?}
*/
function () {
};
BaseComponent.decorators = [
{ type: Component, args: [{
selector: 'ngx-base',
template: "<table class=\"table table-scroll\">\n <tr *ngFor=\"let data of values\">\n <ng-template \n [ngIf]=\"data.template\" \n [ngTemplateOutlet]=\"data.template\" \n [ngTemplateOutletContext]=\"{ $implicit: data.value }\"\n >\n </ng-template>\n </tr>\n</table>\n<div *ngIf=\"values.length==0 && error\">\n <ng-template \n [ngIf]=\"error\" \n [ngTemplateOutlet]=\"error\" \n [ngTemplateOutletContext]=\"{ $implicit: _value }\"\n >\n </ng-template>\n</div>\n",
styles: [""]
}] }
];
/** @nocollapse */
BaseComponent.ctorParameters = function () { return [
{ type: TemplateService }
]; };
BaseComponent.propDecorators = {
_value: [{ type: Input, args: ['value',] }],
owner: [{ type: Input, args: ['owner',] }]
};
return BaseComponent;
}());
if (false) {
/** @type {?} */
BaseComponent.prototype._value;
/** @type {?} */
BaseComponent.prototype.owner;
/**
* @type {?}
* @private
*/
BaseComponent.prototype.descriptor;
/** @type {?} */
BaseComponent.prototype.values;
/** @type {?} */
BaseComponent.prototype.error;
/**
* @type {?}
* @private
*/
BaseComponent.prototype.template;
}
var TemplateBaseComponent = /** @class */ (function () {
function TemplateBaseComponent() {
}
/**
* @param {?} value
* @return {?}
*/
TemplateBaseComponent.prototype.isDisabled = /**
* @param {?} value
* @return {?}
*/
function (value) {
return value.prop == BaseValueProp.Disabled;
};
/**
* @param {?} value
* @return {?}
*/
TemplateBaseComponent.prototype.isHidden = /**
* @param {?} value
* @return {?}
*/
function (value) {
return value.prop == BaseValueProp.Hidden;
};
/**
* @param {?} value
* @return {?}
*/
TemplateBaseComponent.prototype.isInput = /**
* @param {?} value
* @return {?}
*/
function (value) {
return value.prop == BaseValueProp.Input;
};
/**
* @param {?} value
* @return {?}
*/
TemplateBaseComponent.prototype.isText = /**
* @param {?} value
* @return {?}
*/
function (value) {
return value.prop == BaseValueProp.Text;
};
/**
* @return {?}
*/
TemplateBaseComponent.prototype.ngOnInit = /**
* @return {?}
*/
function () {
this.disabled = this.Value.prop == BaseValueProp.Disabled;
if (this.Value.prop == BaseValueProp.Input || this.Value.prop == BaseValueProp.Disabled) {
this.type = "text";
}
else if (this.Value.prop == BaseValueProp.Hidden) {
this.type = "hidden";
}
};
TemplateBaseComponent.propDecorators = {
Value: [{ type: Input, args: ['value',] }],
Owner: [{ type: Input, args: ['owner',] }]
};
return TemplateBaseComponent;
}());
if (false) {
/** @type {?} */
TemplateBaseComponent.prototype.Value;
/** @type {?} */
TemplateBaseComponent.prototype.Owner;
/** @type {?} */
TemplateBaseComponent.prototype.type;
/** @type {?} */
TemplateBaseComponent.prototype.disabled;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var NgxManageModule = /** @class */ (function () {
function NgxManageModule() {
}
NgxManageModule.decorators = [
{ type: NgModule, args: [{
declarations: [BaseComponent],
imports: [
CommonModule
],
exports: [BaseComponent],
providers: [
TemplateService
]
},] }
];
return NgxManageModule;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { BASE_SHEMA, BaseComponent, BaseDescriptors, BaseParam, BaseValue, BaseValueProp, BaseValueType, Descriptor, NgxManageModule, StateBase, StateDecorator, StateMessage, StateMessageService, TemplateBaseComponent, TemplateService, TemplateServiceMessage };
//# sourceMappingURL=ngx-manage.js.map