All files / mframejs/view interpolateController.ts

95.83% Statements 23/24
100% Branches 6/6
100% Functions 6/6
95.83% Lines 23/24

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 10729x     29x               1049x 1049x       1049x       1341x       1341x                       29x     1049x                       1049x 1049x         11x   1049x   1049x                   1049x 1049x 1049x                         1047x                   856x 856x 856x 856x 856x            
import { BindingEngine } from '../binding/exported';
import { IBindingContext, IListener } from '../interface/exported';
import { ViewController } from './viewController';
import { Logger } from '../utils/exported';
 
/**
 * internal subscribe caller of Interpolate controller
 * @internal
 *
 */
const SubscribeInternal = class implements IListener {
    private firstRun = true;
    public name = 'Interpolate';
    public node: Node;
 
    constructor(node: Node) {
        this.node = node;
    }
    public call(newValue: any, oldValue: any) {
        if (oldValue !== newValue || this.firstRun) {
            this.firstRun = false;
            if ((this.node as any).nodeType === 2) {
                (this.node as Attr).value = newValue;
            } else {
                (this.node as Text).data = newValue;
            }
        }
    }
};
 
 
 
/**
 * Interpolate controller watches the text and updates node
 *
 */
export class InterpolateController {
    private subscribeInternal: IListener;
    private logger: Logger;
    private isAttibuteNode = false;
    public classInstance: any;
 
 
    /**
     * Creates instance of InterpolateController
     * @param bindingContext - Binding context to use
     * @param htmlNode - html node of text
     * @param viewController - viewController to use
     * @param _isAttributeValue - is it a attibute value ? debug helper
     */
    constructor(
        private bindingContext: IBindingContext,
        private htmlNode: Node,
        viewController: ViewController,
        _isAttributeValue: boolean) {
 
        if (!(htmlNode as any).data) {
            this.isAttibuteNode = true;
        }
        this.logger = Logger.getLogger(this.isAttibuteNode ? (htmlNode as any).value.trim() : (htmlNode as any).data.trim(), 'interpolate');
 
        viewController.addInterpolate(this);
    }
 
 
 
    /**
     * start element life cycle
     *
     */
    public init(): void {
        this.logger.log('init');
        this.subscribeInternal = new SubscribeInternal(this.htmlNode);
        BindingEngine.subscribeClassProperty(
            this.bindingContext,
            this.isAttibuteNode ? (this.htmlNode as any).value : (this.htmlNode as any).data,
            this.subscribeInternal);
    }
 
 
 
    /**
     * attached... dont really do much atm... Todo, should I subscribe here ?
     *
     */
    public attached(): void {
        this.logger.log('attached');
    }
 
 
 
    /**
     * detached, unsubscribes and clear out
     *
     */
    public detached(): void {
        this.logger.log('detached');
        BindingEngine.unSubscribeClassProperty(this.bindingContext, this.subscribeInternal);
        this.subscribeInternal = null;
        this.bindingContext = null;
        this.htmlNode = null;
    }
 
 
 
}