All files / mframejs/binding subscribeClassMetaBinding.ts

95.52% Statements 64/67
95.45% Branches 21/22
100% Functions 8/8
95.52% Lines 64/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 26129x   29x 29x           29x                             2081x 2081x 2081x                       439x 439x 439x                 60x                                         29x 387x   256x       256x     2081x 2081x 2081x   2081x   2081x 2081x     2081x                           2043x 2043x 2043x     135x     375x         135x     135x         1908x 1908x     3x   3x     1x                                 6x 6x 6x 6x         3x     3x         3x     3x       3x     2x   2x     1x                   32x 32x 32x 32x               32x 64x   16x             16x     10x         16x     16x           16x   16x 32x   16x           2x   2x     14x                            
import { subscribeClassProperty } from './property/subscribeClassProperty';
import { PropertyObserverHandler } from './property/propertyObserverHandler';
import { BindingEngine } from './bindingEngine';
import {
    IAttribute,
    CONSTANTS,
    IElement,
    IListener
} from '../interface/exported';
import { createBindingContext } from './createBindingContext';
 
 
 
/**
  * callbacks changes in class if chnage method is added
  *
  */
const subscribeChangeBaseClass = class implements IListener {
    private key: string;
    private _class: IElement | IAttribute;
    private meta: any;
    public caller: PropertyObserverHandler;
 
    constructor(_class: IElement | IAttribute, key: string, meta: any) {
        this._class = _class;
        this.key = key;
        this.meta = meta;
 
    }
 
 
 
    /**
     * called on changes
     *
     */
    public call(newValue: any, oldValue: any) {
        if (oldValue !== newValue) {
            const key = this.key;
            const _class = this._class;
            const META = this.meta;
 
            if (_class.$bindingContext && key in _class.$bindingContext.$context) { // I should call this before?
                if (_class.$bindingContext.$context[key] !== newValue) {
                    _class.$bindingContext.$context[key] = newValue;
                }
            }
 
            if (_class[`${key}Changed`]) {
                _class[`${key}Changed`](newValue, oldValue);
            }
 
            if (_class[`attributesChanged`]) { // usefull so we dont need a handler for each
                _class[`attributesChanged`](key, newValue, oldValue);
            }
 
            if (META[key].options.changeHandler) {
                _class[META[key].options.changeHandler](key, newValue, oldValue);
            }
 
        }
    }
};
 
 
 
/**
  * creates a observer if bindable meta data is added
  *
  */
export function subscribeClassMetaBinding(_class: IElement | IAttribute) {
    const META = (_class as any).__proto__[CONSTANTS.META_BINDABLE];
    if (META) {
        const keys = Object.keys(META);
        for (const key of keys) {
 
            if (!(_class as any).__metaBinding) {
                (_class as any).__metaBinding = {};
            }
            if (!(_class as any).__metaBinding[key]) {
                (_class as any).__metaBinding[key] = {};
                (_class as any).__metaBinding[key].key = key;
                (_class as any).__metaBinding[key].options = {};
            }
            const CLASSMETA = (_class as any).__metaBinding[key];
 
            const subscribeInternal = new subscribeChangeBaseClass(_class, key, META);
            subscribeClassProperty(createBindingContext(_class), key, subscribeInternal);
 
            // save it for later so we can unsubscribe
            CLASSMETA.options.subscribeInternal = subscribeInternal;
 
            if (META[key].observableOnly === true) {
                // nothing atm
            } else {
 
                if ((_class as IElement).$attributes && !(_class as IAttribute).$attribute) {
 
                    // custom element
                    // we now check for if attribute have bind, else we just give the value
 
                    if (_class.$element && _class.$bindingContext) {
                        // need to make sure there is parent and element
 
                        const el = _class.$element as Element;
                        let att = `${META[key].options.attribute}.bind`;
                        let attrValue = el.getAttribute(att);
                        if (attrValue) {
 
                            const subscribeExternal: IListener = Object.create({
                                call: (newValue: any, oldValue: any) => {
                                    if (oldValue !== newValue) {
                                        _class[key] = newValue;
                                    }
                                }
                            });
 
                            subscribeClassProperty(_class.$bindingContext, attrValue, subscribeExternal);
 
                            // save it for later so we can unsubscribe
                            CLASSMETA.options.subscribeExternal = subscribeExternal;
 
                        } else {
 
                            // just set value, no bind
                            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 {
 
                    // custom attribute
                    // - > need to extract values, they can be many in 1
 
                    if (_class.$element && _class.$bindingContext) {
 
                        // many or 1 check, if just value we only want that
                        if (keys.length === 1 && key === 'value') {
 
                            // only 1 key and its value, then we only use this
                            const el = _class.$element as Element;
                            const att = (_class as IAttribute).$attribute.name;
                            const haveBind = att.indexOf('.bind');
                            let attrValue = el.getAttribute(att);
 
                            if (haveBind !== -1) {
                                // binding
 
                                const subscribeExternal: IListener = Object.create({
                                    call: (newValue: any, oldValue: any) => {
                                        if (oldValue !== newValue) {
                                            _class[key] = newValue;
                                        }
                                    }
                                });
 
                                subscribeClassProperty(_class.$bindingContext, attrValue, subscribeExternal);
 
                                // save it for later so we can unsubscribe
                                CLASSMETA.options.subscribeExternal = subscribeExternal;
                            } else {
 
                                // no bind, just get value
                                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 {
 
 
                            // not just value and 1 @bindable
 
                            const el = _class.$element as Element;
                            const attributeName = (_class as IAttribute).$attribute.name;
                            let att = `${META[key].options.attribute}.bind`;
                            const attrValues: any[] = el.getAttribute(attributeName) ? el.getAttribute(attributeName).split(';') : null;
 
                            // do we have many values inside?
                            if (attrValues) {
 
                                // yes, lets get them and check for key/att
                                // atm checking for .bind
                                let attrValue: string;
                                attrValues.forEach((value: any) => {
                                    const test = value.split(':');
                                    if (test && test[0].trim() === att) {
                                        attrValue = test[1];
                                    }
                                });
 
 
                                if (attrValue) {
 
                                    const subscribeExternal: IListener = Object.create({
                                        call: (newValue: any, oldValue: any) => {
                                            if (oldValue !== newValue) {
                                                _class[key] = newValue;
                                            }
                                        }
                                    });
 
                                    subscribeClassProperty(_class.$bindingContext, attrValue, subscribeExternal);
 
                                    // save it for later so we can unsubscribe
                                    CLASSMETA.options.subscribeExternal = subscribeExternal;
 
                                } else {
 
                                    // no .bind
                                    // just set value if it is there
                                    att = `${META[key].options.attribute}`;
                                    let attrValue: string;
                                    attrValues.forEach((value: any) => {
                                        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;
                                        }
                                    }
                                }
                            }
                        }
                    }
 
                }
            }
 
        }
    }
}