all files / montage/ui/base/ abstract-checkbox.js

93.33% Statements 28/30
80% Branches 8/10
90.91% Functions 10/11
93.33% Lines 28/30
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                                      73×     71× 71×   71×                                                                                     40×     181×                                                                 18×   14× 14×           26×   26×                         18× 18×                             33× 33× 33×                        
/*global require, exports, document, Error*/
var AbstractControl = require("./abstract-control").AbstractControl,
    PressComposer = require("../../composer/press-composer").PressComposer;
 
var CLASS_PREFIX = "montage-Checkbox";
 
/**
 * @class AbstractCheckbox
 * @extends AbstractControl
 */
var AbstractCheckbox = exports.AbstractCheckbox = AbstractControl.specialize( /** @lends AbstractCheckbox# */ {
 
    /**
     * Dispatched when the checkbox is activated through a mouse click,
     * finger tap, or when focused and the spacebar is pressed.
     * @event action
     * @memberof AbstractCheckbox
     * @param {Event} event
     */
 
    constructor: {
        value: function AbstractCheckbox() {
            if(this.constructor === AbstractCheckbox) {
                throw new Error("AbstractCheckbox cannot be instantiated.");
            }
 
            this._pressComposer = new PressComposer();
            this.addComposer(this._pressComposer);
 
            this.defineBindings({
                // classList management
                "classList.has('montage--disabled')": {
                    "<-": "!enabled"
                },
                "classList.has('montage--active')": {
                    "<-": "active"
                },
                "classList.has('montage-Checkbox--checked')": {
                    "<-": "checked"
                }
            });
        }
    },
 
    hasTemplate: {
        value: false
    },
 
    /**
     * This property is true when the checkbox is being interacted with, either through mouse click or touch event, otherwise false.
     * @type {boolean}
     * @default false
     */
    active: {
        value: false
    },
 
    value: {
        value: null
    },
 
    _checked: {
        value: false
    },
 
    /**
     * This property reflect the checked state of the checkbox.
     * @type {boolean}
     * @default false
     */
    checked: {
        set: function (value) {
            this._checked = value;
        },
        get: function () {
            return this._checked;
        }
    },
 
    /**
     * Enables or disables the checkbox from user input. When this property is set to ```false```,
     * the "montage--disabled" CSS class is applied to the checkbox's DOM element during the next draw cycle. When set to
     * ```true``` the "montage--disabled" CSS class is removed from the element's class list.
     * @type {boolean}
     */
    enabled: {
        value: true
    },
 
    _pressComposer: {
        value: null
    },
 
    enterDocument: {
        value: function (firstTime) {
            Eif (firstTime) {
                this.element.setAttribute("role", "checkbox");
            }
        }
    },
 
    draw: {
        value: function () {
            if (this.checked) {
                this.element.setAttribute("aria-checked", "true");
            } else {
                this.element.setAttribute("aria-checked", "false");
            }
        }
    },
 
    toggleChecked: {
        value: function () {
            if (!this.enabled) {
                return;
            }
            this.checked = !this.checked;
            this.dispatchActionEvent();
        }
    },
 
    handlePressStart: {
        value: function (event) {
            this.active = true;
 
            Iif (event.touch) {
                // Prevent default on touchmove so that if we are inside a scroller,
                // it scrolls and not the webpage
                document.addEventListener("touchmove", this, false);
            }
        }
    },
 
    /**
     * Handle press event from press composer
     */
    handlePress: {
        value: function (/* event */) {
            this.active = false;
            this.toggleChecked();
        }
    },
 
    /**
     * Called when all interaction is over.
     * @private
     */
    handlePressCancel: {
        value: function (/* event */) {
            this.active = false;
            document.removeEventListener("touchmove", this, false);
        }
    },
 
    prepareForActivationEvents: {
        value: function () {
            this._pressComposer.addEventListener("pressStart", this, false);
            this._pressComposer.addEventListener("press", this, false);
            this._pressComposer.addEventListener("pressCancel", this, false);
        }
    },
 
    activate: {
        value: function () {
            this.toggleChecked();
        }
    }
 
});