UNPKG

mframejs

Version:
172 lines 8.99 kB
import { subscribeClassProperty } from './property/subscribeClassProperty'; import { BindingEngine } from './bindingEngine'; import { CONSTANTS } from '../interface/exported'; import { createBindingContext } from './createBindingContext'; const subscribeChangeBaseClass = class { constructor(_class, key, meta) { this._class = _class; this.key = key; this.meta = meta; } call(newValue, oldValue) { if (oldValue !== newValue) { const key = this.key; const _class = this._class; const META = this.meta; if (_class.$bindingContext && key in _class.$bindingContext.$context) { if (_class.$bindingContext.$context[key] !== newValue) { _class.$bindingContext.$context[key] = newValue; } } if (_class[`${key}Changed`]) { _class[`${key}Changed`](newValue, oldValue); } if (_class[`attributesChanged`]) { _class[`attributesChanged`](key, newValue, oldValue); } if (META[key].options.changeHandler) { _class[META[key].options.changeHandler](key, newValue, oldValue); } } } }; export function subscribeClassMetaBinding(_class) { const META = _class.__proto__[CONSTANTS.META_BINDABLE]; if (META) { const keys = Object.keys(META); for (const key of keys) { if (!_class.__metaBinding) { _class.__metaBinding = {}; } if (!_class.__metaBinding[key]) { _class.__metaBinding[key] = {}; _class.__metaBinding[key].key = key; _class.__metaBinding[key].options = {}; } const CLASSMETA = _class.__metaBinding[key]; const subscribeInternal = new subscribeChangeBaseClass(_class, key, META); subscribeClassProperty(createBindingContext(_class), key, subscribeInternal); CLASSMETA.options.subscribeInternal = subscribeInternal; if (META[key].observableOnly === true) { } else { if (_class.$attributes && !_class.$attribute) { if (_class.$element && _class.$bindingContext) { const el = _class.$element; let att = `${META[key].options.attribute}.bind`; let attrValue = el.getAttribute(att); if (attrValue) { const subscribeExternal = Object.create({ call: (newValue, oldValue) => { if (oldValue !== newValue) { _class[key] = newValue; } } }); subscribeClassProperty(_class.$bindingContext, attrValue, subscribeExternal); CLASSMETA.options.subscribeExternal = subscribeExternal; } else { att = `${META[key].options.attribute}`; attrValue = el.getAttribute(att); if (attrValue) { if (attrValue.indexOf('${') !== -1 || attrValue.indexOf('@{') !== -1) { const val = BindingEngine.tokenizeParseAndTraverseAST(attrValue, _class.$bindingContext); if (val) { _class[key] = val; } } else { _class[key] = attrValue; } } } } } else { if (_class.$element && _class.$bindingContext) { if (keys.length === 1 && key === 'value') { const el = _class.$element; const att = _class.$attribute.name; const haveBind = att.indexOf('.bind'); let attrValue = el.getAttribute(att); if (haveBind !== -1) { const subscribeExternal = Object.create({ call: (newValue, oldValue) => { if (oldValue !== newValue) { _class[key] = newValue; } } }); subscribeClassProperty(_class.$bindingContext, attrValue, subscribeExternal); CLASSMETA.options.subscribeExternal = subscribeExternal; } else { attrValue = el.getAttribute(att); if (attrValue) { if (attrValue.indexOf('${') !== -1 || attrValue.indexOf('@{') !== -1) { const val = BindingEngine.tokenizeParseAndTraverseAST(attrValue, _class.$bindingContext); if (val) { _class[key] = val; } } else { _class[key] = attrValue; } } } } else { const el = _class.$element; const attributeName = _class.$attribute.name; let att = `${META[key].options.attribute}.bind`; const attrValues = el.getAttribute(attributeName) ? el.getAttribute(attributeName).split(';') : null; if (attrValues) { let attrValue; attrValues.forEach((value) => { const test = value.split(':'); if (test && test[0].trim() === att) { attrValue = test[1]; } }); if (attrValue) { const subscribeExternal = Object.create({ call: (newValue, oldValue) => { if (oldValue !== newValue) { _class[key] = newValue; } } }); subscribeClassProperty(_class.$bindingContext, attrValue, subscribeExternal); CLASSMETA.options.subscribeExternal = subscribeExternal; } else { att = `${META[key].options.attribute}`; let attrValue; attrValues.forEach((value) => { const test = value.split(':'); if (test && test[0].trim() === att) { attrValue = test[1]; } }); if (attrValue) { if (attrValue.indexOf('${') !== -1 || attrValue.indexOf('@{') !== -1) { const val = BindingEngine.tokenizeParseAndTraverseAST(attrValue, _class.$bindingContext); if (val) { _class[key] = val; } } else { _class[key] = attrValue; } } } } } } } } } } } //# sourceMappingURL=subscribeClassMetaBinding.js.map