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

72.22% Statements 39/54
63.64% Branches 14/22
62.5% Functions 10/16
72.22% Lines 39/54
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 261 262 263 264 265 266 267 268 269                                                        54×     53× 53× 53×   53×   53×                                           53×     53× 53×                                                                                                                   65× 54×   54×   54×                     382×     10× 10×   10×                                                                                                                                                   95×   34×     95× 95×             176× 62×   176×           55× 55× 55×                              
/*global require, exports*/
 
/**
 * @module montage/ui/base/abstract-select.reel
 */
var AbstractControl = require("./abstract-control").AbstractControl,
    PressComposer = require("../../composer/press-composer").PressComposer,
    RangeController = require("../../core/range-controller").RangeController;
 
/**
 * @class AbstractSelect
 * @extends AbstractControl
 * @fires action
 * @fires longAction
 */
var AbstractSelect = exports.AbstractSelect = AbstractControl.specialize( /** @lends AbstractSelect# */ {
 
    /**
     * Dispatched when the select is changed through a mouse click or finger
     * tap.
     * @event action
     * @memberof AbstractSelect
     * @param {Event} event
     */
 
    /**
     * @private
     */
    constructor: {
        value: function AbstractSelect() {
            if(this.constructor === AbstractSelect) {
                throw new Error("AbstractSelect cannot be instantiated.");
            }
 
            this._pressComposer = new PressComposer();
            this.addComposer(this._pressComposer);
            this.contentController = new RangeController();
 
            this.addPathChangeListener("contentController", this, "handleContentControllerChange");
 
            this.defineBindings({
                "content": {
                    "<->": "contentController.content"
                },
                // FIXME: due to issues with 2 way bindings with rangeContent()
                // we aren't currently able to have this "value" binding.
                // "value": {
                //     "<->": "values.one()"
                // },
                "contentController.allowsMultipleSelection": {
                    "<-": "multiSelect"
                },
                // classList management
                "classList.has('montage--disabled')": {
                    "<-": "!enabled"
                },
                "classList.has('montage--active')": {
                    "<-": "active"
                }
            });
 
            // Need to draw when "content" or "values" change
            this.addRangeAtPathChangeListener("content", this, "handleContentRangeChange");
 
            // TODO: "value" <-> "values.one()"
            this.addRangeAtPathChangeListener("values", this, "handleValuesRangeChange");
            this.classList.add("matte-Select");
        }
    },
 
    /**
     * Enables or disables the Select from user input. When this property is
     * set to `false`, the "montage--disabled" CSS style is applied to the
     * select's DOM element during the next draw cycle. When set to `true` the
     * "disabled" CSS class is removed from the element's class list.
     * @type {boolean}
     */
    enabled: {
        value: true
    },
 
    acceptsActiveTarget: {
        value: true
    },
 
    _pressComposer: {
        value: null
    },
 
    active: {
        value: false
    },
 
    content: {
        value: null
    },
 
    contentController: {
        value: null
    },
 
    _labelPropertyName: {
        value: "label"
    },
 
    labelPropertyName: {
        set: function (value) {
            Eif (value) {
                this._labelPropertyName = value;
            } else {
                this._labelPropertyName = "label";
            }
            this._contentIsDirty = true;
            this.needsDraw = true;
        },
        get: function () {
            return this._labelPropertyName;
        }
    },
 
    _value: {
        value: null
    },
 
    value: {
        get: function () {
            return this._value;
        },
        set: function (value) {
            if (value !== this._value) {
                this._value = value;
                // TODO: "value" <-> "values.one()"
                if (this.values[0] !== value) {
                    this.values.splice(0, this.values.length, value);
                }
                this.needsDraw = true;
            }
        }
    },
 
    _values: {
        value: null
    },
 
    values: {
        get: function () {
            return this._values;
        },
        set: function (value) {
            var args = [0, this._values.length].concat(value);
            this._values.splice.apply(this._values, args);
 
            this.needsDraw = true;
        }
    },
 
    multiSelect: {
        value: false
    },
 
    _contentIsDirty: {
        value: true
    },
 
    prepareForActivationEvents: {
        value: function () {
            this._pressComposer.addEventListener("pressStart", this, false);
            this._pressComposer.addEventListener("press", this, false);
            this._pressComposer.addEventListener("pressCancel", this, false);
        }
    },
 
    // Handlers
 
    /**
     * Called when the user starts interacting with the component.
     */
    handlePressStart: {
        value: function (event) {
            this.active = true;
 
            if (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);
            }
        }
    },
 
    /**
     * Called when the user has interacted with the select.
     */
    handlePress: {
        value: function (event) {
            this.active = false;
 
            if (!this.enabled) {
                return;
            }
 
            this.dispatchActionEvent();
            document.removeEventListener("touchmove", this, false);
        }
    },
 
    /**
     * Called when all interaction is over.
     * @private
     */
    handlePressCancel: {
        value: function (event) {
            this.active = false;
            document.removeEventListener("touchmove", this, false);
        }
    },
 
    handleTouchmove: {
        value: function (event) {
            event.preventDefault();
        }
    },
 
    handleContentRangeChange: {
        value: function () {
            // When the content changes we need to update the "value" if none is
            // set (new content) or if the previous "value" was removed in this
            // range change.
            // FIXME: we only operate on the selection and not on the "values"
            // to avoid issues with 2-way binding to rangeContent().
            if (this.contentController.selection.length === 0 &&
                this.contentController.organizedContent.length > 0) {
                this.contentController.selection.push(this.contentController.organizedContent[0]);
            }
 
            this._contentIsDirty = true;
            this.needsDraw = true;
        }
    },
 
    handleValuesRangeChange: {
        value: function () {
            // TODO: "value" <-> "values.one()"
            if (this.values.length > 0) {
                this.value = this.values.one();
            }
            this.needsDraw = true;
        }
    },
 
    handleContentControllerChange: {
        value: function (value) {
            Eif (value) {
                this._values = value.selection;
                this.handleValuesRangeChange();
            }
        }
    },
 
    enterDocument: {
        value: function (firstDraw) {
            if(firstDraw) {
                this.element.setAttribute("role", "listbox");
            }
        }
    }
 
});