UNPKG

ngx-manage

Version:

- angular class descriptor and state

855 lines (846 loc) 24.8 kB
import { __awaiter } 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 {?} */ const BASE_SHEMA = '$base'; /** @enum {number} */ const 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} */ const BaseValueType = { Text: 'text', Number: 'number', Array: 'array', Object: 'object', Boolean: 'boolean', Undefined: 'undefined', Date: 'date', Time: 'time', Error: 'error', Custom: 'custom', }; class BaseParam { /** * @param {?=} type * @param {?=} shema * @param {?=} prop * @param {?=} template * @param {?=} provider * @param {?=} validator */ constructor(type = BaseValueType.Text, shema = BASE_SHEMA, prop = BaseValueProp.Text, template = '', provider = '', validator = '') { this.type = type; this.shema = shema; this.prop = prop; this.template = template; this.provider = provider; this.validator = validator; } } 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; } class BaseValue { /** * @param {?} name * @param {?} param */ constructor(name, param) { this.name = name; this.param = param; } } 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 {?} */ const a = new constructor(); if (!property) { property = Object.getOwnPropertyNames(a); } /** @type {?} */ const array = property; /** @type {?} */ let trg = constructor.prototype; //console.log('Descriptor',array,trg); /** * @param {?} data * @param {?} propertyKey * @return {?} */ function proc(data, propertyKey) { /** @type {?} */ let 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 {?} */ d => { array.forEach((/** * @param {?} name * @return {?} */ name => { /** @type {?} */ let nd = Object.assign({}, d); /** @type {?} */ const t = nd.type; if (t === BaseValueType.Undefined && a) { // get value type nd.type = objectType(a[name]); } proc(nd, name); })); /** @type {?} */ let base = Object.getOwnPropertyDescriptor(trg, d.shema); if (base) { base.value.list = property.slice(); Object.defineProperty(trg, d.shema, base); } })); return class extends constructor { }; }); } /** * @param {?} descriptor * @return {?} */ function BaseDescriptors(descriptor) { /** * @param {?} data * @param {?} target * @param {?} propertyKey * @return {?} */ function proc(data, target, propertyKey) { /** @type {?} */ let 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 {?} */ d => { proc(d, target, propertyKey); })); }); } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ const Injector = new class { constructor() { this.hash = {}; } // resolving instances /** * @template T * @param {?} target * @return {?} */ resolveHash(target) { if (this.hash[target.name]) { return this.hash[target.name]; } // tokens are required dependencies, while injections are resolved tokens from the Injector /** @type {?} */ let tokens = Reflect.getMetadata('design:paramtypes', target) || []; /** @type {?} */ let injections = tokens.map((/** * @param {?} token * @return {?} */ token => Injector.resolve(token))); this.hash[target.name] = new target(...injections); return this.hash[target.name]; } /** * @template T * @param {?} target * @return {?} */ resolve(target) { // tokens are required dependencies, while injections are resolved tokens from the Injector /** @type {?} */ let tokens = Reflect.getMetadata('design:paramtypes', target) || []; /** @type {?} */ let injections = tokens.map((/** * @param {?} token * @return {?} */ token => Injector.resolve(token))); return new target(...injections); } }; class StateMessage { /** * @param {?} name * @param {?} state * @param {?} msg */ constructor(name, state, msg) { this.name = name; this.state = state; this.msg = msg; } } 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) { }; } class StateMessageService { /** * @param {?} name * @param {?} message * @param {?} state * @return {?} */ SendMessage(name, message, state) { return __awaiter(this, void 0, void 0, function* () { /** @type {?} */ var msg = {}; state.sendMessage(name, msg); return; }); } } /** * @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) { }; } class StateService { constructor() { this.version = '1.0.2'; this.subjects = {}; this.state = {}; } /** * @private * @param {?} name * @return {?} */ init(name) { if (!this.subjects[name]) { this.state = {}; this.subjects[name] = new Subject(); } return this.subjects[name]; } /** * @param {?} name * @param {?} cb * @return {?} */ getState(name, cb) { return this.init(name).asObservable().subscribe((/** * @param {?} msg * @return {?} */ (msg) => cb(msg))); } /** * @param {?} set * @return {?} */ getStates(set) { /** @type {?} */ var subscribtion = []; for (var s in set) { subscribtion.push(this.init(s).asObservable().subscribe((/** * @param {?} msg * @return {?} */ (msg) => { /** @type {?} */ const fn = set[msg.name] || set[s]; fn(msg); }))); } return subscribtion; } /** * @param {?} name * @param {?} message * @return {?} */ sendMessage(name, message) { /** @type {?} */ var a = this.init(name); a.next({ msg: message, name: name }); } /** * @param {?} name * @param {?} id * @return {?} */ read(name, id) { /** @type {?} */ const d = this.state[name] || {}; /** @type {?} */ const 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 {?} */ write(name, id, data, 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); } } } 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 extends constr { constructor() { super(...arguments); this.message = service; this.subscribtion = []; } /** * @param {?} set * @return {?} */ getStates(set) { this.subscribtion = state.getStates(set); } /** * @param {?} name * @param {?} cb * @return {?} */ getState(name, cb) { this.subscribtion.push(state.getState(name, cb)); } /** * @param {?} name * @param {?} message * @return {?} */ sendMessage(name, message) { if (this.message) { this.message.SendMessage(name, message, state); } } /** * @return {?} */ ngOnDestroy() { this.subscribtion.forEach((/** * @param {?} e * @return {?} */ (e) => e.unsubscribe())); } }; }); } class StateBase { constructor() { } /** * @param {?} name * @param {?} cb * @return {?} */ getState(name, cb) { } ; /** * @param {?} name * @param {?} message * @return {?} */ sendMessage(name, message) { } /** * @return {?} */ ngOnDestroy() { } /** * @param {?} set * @return {?} */ getStates(set) { } ; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class TemplateServiceMessage { /** * @param {?} Template * @param {?=} Shema */ constructor(Template, Shema = BASE_SHEMA) { this.Template = Template; this.Shema = Shema; } } if (false) { /** @type {?} */ TemplateServiceMessage.prototype.Template; /** @type {?} */ TemplateServiceMessage.prototype.Shema; } class TemplateService { constructor() { this.subject = new BehaviorSubject(undefined); } /** * @param {?} Template * @param {?=} Shema * @return {?} */ load(Template, Shema = BASE_SHEMA) { this.subject.next(new TemplateServiceMessage(Template, Shema)); } /** * @return {?} */ getTemplates() { return this.subject.asObservable(); } } TemplateService.decorators = [ { type: Injectable, args: [{ providedIn: 'root' },] } ]; /** @nocollapse */ TemplateService.ctorParameters = () => []; /** @nocollapse */ TemplateService.ngInjectableDef = ɵɵdefineInjectable({ factory: function TemplateService_Factory() { return new TemplateService(); }, token: TemplateService, providedIn: "root" }); if (false) { /** * @type {?} * @private */ TemplateService.prototype.subject; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class BaseComponent { /** * @param {?} template */ constructor(template) { this.template = template; this.values = []; } /** * @return {?} */ ngOnInit() { // <pre>{{Value | json}}</pre> this.template.getTemplates().subscribe((/** * @param {?} message * @return {?} */ (message) => { if (message) { //console.log('subscribe',this); /** @type {?} */ const _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 {?} */ const overwriteProp = this.owner ? this.owner.prop : undefined; if (this.descriptor && this.descriptor.value) { /** @type {?} */ const v = this.descriptor.value; for (var name of v.list) { /** @type {?} */ const descriptor = v.hash[name]; /** @type {?} */ let template; 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 {?} */ let 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 }); } } } this.values = all; } })); } /** * @return {?} */ ngAfterViewInit() { } } 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 = () => [ { type: TemplateService } ]; BaseComponent.propDecorators = { _value: [{ type: Input, args: ['value',] }], owner: [{ type: Input, args: ['owner',] }] }; 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; } class TemplateBaseComponent { constructor() { } /** * @param {?} value * @return {?} */ isDisabled(value) { return value.prop == BaseValueProp.Disabled; } /** * @param {?} value * @return {?} */ isHidden(value) { return value.prop == BaseValueProp.Hidden; } /** * @param {?} value * @return {?} */ isInput(value) { return value.prop == BaseValueProp.Input; } /** * @param {?} value * @return {?} */ isText(value) { return value.prop == BaseValueProp.Text; } /** * @return {?} */ ngOnInit() { 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',] }] }; 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 */ class NgxManageModule { } NgxManageModule.decorators = [ { type: NgModule, args: [{ declarations: [BaseComponent], imports: [ CommonModule ], exports: [BaseComponent], providers: [ TemplateService ] },] } ]; /** * @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