UNPKG

@aca-1/a2-composer

Version:
260 lines 8.83 kB
import { Directive, ElementRef, EventEmitter, HostListener, Input, Output, Renderer } from '@angular/core'; import { SystemsService } from '../services'; import { COMPOSER } from '../settings'; var Binding = (function () { function Binding(el, serv, renderer) { this.el = el; this.serv = serv; this.renderer = renderer; this.valueChange = new EventEmitter(); this.ignore = 0; this.ontap = new EventEmitter(); this.onpress = new EventEmitter(); this.onrelease = new EventEmitter(); this.id = ''; this.started = false; this.module_id = ''; this.prev = null; this.prev_exec = null; this.i = 0; this.ignore_cnt = 0; this.init = false; this.debug = false; this.service = serv; this.id = (Math.floor(Math.random() * 899999) + 100000).toString(); this.renderer.setElementClass(this.el.nativeElement, "binding-directive-" + this.id, true); } Binding.prototype.ngOnInit = function () { this.init = false; }; Binding.prototype.ngOnChanges = function (changes) { var _this = this; if (!this.serv.is_setup) { setTimeout(function () { _this.ngOnChanges(changes); }, 500); return; } if (changes.sys && this.hasChanged('system')) { this.cleanModule(); this.getSystem(); this.getModule(); this.getBinding(); } else if (changes.mod) { this.cleanModule(); if (this.hasChanged('module')) { this.getModule(); this.getBinding(); } } else if (changes.index) { this.cleanModule(); this.getModule(); this.getBinding(); } else if (changes.bind) { this.getBinding(); } if (this.init && this.prev_exec !== this.exec && this.bind && this.bind !== '') { this.ignore_cnt++; if (this.ignore_cnt > this.ignore) { COMPOSER.log('Binding', this.id + ": Execute function changed. " + this.prev_exec + " \u2192 " + this.exec); this.call_exec(); } } if (this.init && this.binding && this.value !== this.binding.current && this.value !== this.prev) { var change = this.prev + " \u2192 " + this.value; COMPOSER.log('Binding', this.id + ": Local value changed calling exec. " + change); this.ignore_cnt++; if (this.ignore_cnt > this.ignore) { this.call_exec(); } else { this.prev = this.value; } } if (!this.init) { setTimeout(function () { _this.prev = _this.value; _this.init = true; }, 100); } }; Binding.prototype.ngOnDestroy = function () { if (this.unbind) { this.unbind(); } }; Binding.prototype.call_exec = function (exec) { if (!this || !this.module || this.exec === undefined || (!this.binding && (!this.exec || this.exec === ''))) { return; } if (this.exec === null || this.exec === '') { this.exec = this.binding.id; } this.prev_exec = this.exec; this.prev = this.value; var bind_info = this.sys + ", " + this.mod + ", " + this.bind; COMPOSER.log('Binding', this.id + ": Calling exec from directive " + this.id + ": " + bind_info); var binding = this.binding ? this.binding.id : ''; var params = this.params || (!this.bind || this.bind === '') ? this.params : this.value; this.module.exec(this.exec, binding, params) .then(function (res) { return; }, function (err) { return; }); }; Binding.prototype.ngOnDestory = function () { if (this.unbind) { this.unbind(); this.unbind = null; } }; Binding.prototype.onClick = function (e) { var _this = this; if (e) { e.exec = function (exec) { _this.call_exec(exec); }; } this.ontap.emit(e); }; Binding.prototype.onRelease = function (e) { var _this = this; if (e) { e.exec = function (exec) { _this.call_exec(exec); }; } this.onrelease.emit(e); }; Binding.prototype.onPress = function (e) { var _this = this; if (e) { e.exec = function (exec) { _this.call_exec(exec); }; } this.onpress.emit(e); }; Binding.prototype.checkVisibility = function () { if (!this.checkElement()) { if (this.unbind) { this.unbind(); this.unbind = null; } } else { if (!this.unbind) { this.getBinding(); } } }; Binding.prototype.checkElement = function () { var el = this.el.nativeElement; while (el !== null) { if (el.nodeName === 'BODY' || el.nodeName === 'HTML') { return true; } el = el.parentElement; } return false; }; Binding.prototype.cleanModule = function () { var mod = this.mod.split('_'); var index = mod.pop(); if (isNaN(+index)) { mod.push(index); if (!this.index || this.index <= 0) { this.index = 1; } } else { this.index = +index; } this.module_id = mod.join('_'); }; Binding.prototype.hasChanged = function (type) { if (type === 'system') { return (this.sys && this.sys !== this.system && (typeof this.system !== 'object' || (this.sys !== this.system.id && this.sys !== ''))); } else if (type === 'module') { return (this.module_id && this.mod !== this.module && (typeof this.module !== 'object' || (this.module_id !== this.module.id && this.module_id !== ''))); } else { return true; } }; Binding.prototype.getSystem = function () { if (!this.service) { return; } if (typeof this.sys === 'string') { this.system = this.service.get(this.sys); } else { this.system = this.sys; } }; Binding.prototype.getModule = function () { if (!this.system) { return; } if (typeof this.module_id === 'string') { this.module = this.system.get(this.module_id, !this.index && this.index !== 0 ? 1 : +this.index); } else { this.module = this.module_id; } if (this.module) { this.binding = this.module.get(this.bind); } }; Binding.prototype.getBinding = function () { var _this = this; if (!this.bind || this.bind === '' || !this.module) { return; } if (this.unbind !== undefined && this.unbind !== null) { this.unbind(); } this.binding = this.module.get(this.bind); this.unbind = this.module.bind(this.bind, function (curr, prev) { _this.valueChange.emit(curr); }); this.value = this.binding.current; this.prev = this.value; var msg = this.id + ": Binding to '" + this.bind + "' on " + this.sys + ", " + this.module.id + " " + this.module.index; COMPOSER.log('Binding', msg); if (this.unbind === null) { setTimeout(function () { _this.getBinding(); }, 200); } }; return Binding; }()); export { Binding }; Binding.decorators = [ { type: Directive, args: [{ selector: '[binding]', providers: [], },] }, ]; Binding.ctorParameters = function () { return [ { type: ElementRef, }, { type: SystemsService, }, { type: Renderer, }, ]; }; Binding.propDecorators = { 'bind': [{ type: Input },], 'sys': [{ type: Input },], 'mod': [{ type: Input },], 'index': [{ type: Input },], 'value': [{ type: Input },], 'valueChange': [{ type: Output },], 'exec': [{ type: Input },], 'params': [{ type: Input },], 'ignore': [{ type: Input },], 'ontap': [{ type: Output },], 'onpress': [{ type: Output },], 'onrelease': [{ type: Output },], 'onClick': [{ type: HostListener, args: ['tap', ['$event'],] },], 'onRelease': [{ type: HostListener, args: ['pressup', ['$event'],] },], 'onPress': [{ type: HostListener, args: ['press', ['$event'],] },], }; //# sourceMappingURL=binding.directive.js.map