All files / mframejs/attribute delgateEventsAttribute.ts

100% Statements 14/14
100% Branches 0/0
100% Functions 5/5
100% Lines 13/13

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 8329x   29x 29x 29x             29x                                         1x 1x 1x 1x 1x           1x                                         1x                     3x                
import { customAttribute/*, inject*/ } from '../decorator/exported';
import { IAttribute, IBindingContext } from '../interface/exported';
import { BindingEngine } from '../binding/exported';
import { connectBehavior /*,DomEventHandler*/ } from '../utils/exported';
import { createBindingContext } from '../binding/createBindingContext';
 
/**
 * uses what ever name it gets with ".delegate" and uses the name as event listener
 *
 */
@customAttribute('#VARIABLE#.delegate')
export class DelgateEventsAttribute implements IAttribute {
 
    public $element: HTMLInputElement;
    public $attribute: Attr;
    public $bindingContext: IBindingContext;
    private value: string;
    private name: string;
    private expressionValue: string;
    public eventHandlerBinded: any;
    // private listerID: number;
 
    constructor(/*private domEventHandler: DomEventHandler*/) {
        // nothing..
    }
 
    /**
     * created
     *
     */
    public created() {
 
        this.name = (this.$attribute as any).name.replace('.delegate', '');
        this.value = (this.$attribute as any).value;
        this.expressionValue = (this.$attribute as any).value;
        this.eventHandlerBinded = this.eventHandler.bind(this);
        connectBehavior(this.expressionValue, this);
 
        // TODO... this isn't really delegate atm, just trigger really!!
        // Need to make a main listener on body and use capture/bubble
        // this.listerID = this.domEventHandler.listenFor(this.name, this.$element, this.eventHandlerBinded);
 
        (this.$element as any).addEventListener(this.name, (this.eventHandlerBinded as EventListenerOrEventListenerObject), false);
    }
 
 
 
    /**
     * attached
     *
     */
    public attached() {
        // nothing atm
    }
 
 
 
    /**
     * detached
     *
     */
    public detached() {
        // this.domEventHandler.removeListener(this.listerID);
        this.$element.removeEventListener(this.name, this.eventHandlerBinded);
        // this.domEventHandler = null;
    }
 
 
 
    /**
     * when event triggers it uses the value with ast
     *
     */
    public eventHandler(event: any): void {
        BindingEngine.tokenizeParseAndTraverseAST(this.value, createBindingContext(
            this.$bindingContext.$context,
            this.$bindingContext.$overrideContext,
            event
        ));
    }
 
}