all files / montage/ui/ control.js

70.89% Statements 56/79
59.46% Branches 22/37
78.57% Functions 22/28
70.89% Lines 56/79
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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488                              109×                                                                                                                                                               106×                           111×                                                                                                                                     17×           17× 12×   11×       17×   17×     17×                                                                                                                                                                                                                         29× 28× 28×     28×                           28× 28×     28×   28×   28×                                                                         12×     28× 28× 28×                                     28×                                                                                                                                
/**
    @module montage/ui/control
*/
 
var Component = require("ui/component").Component,
    deprecate = require("core/deprecate"),
    Map = require("collections/map");
 
/**
    Base component for all native components, such as RadioButton and Checkbox.
    @class module:montage/ui/control.Control
    @extends module:montage/ui/component.Component
 */
var Control = exports.Control = Component.specialize(/** @lends module:montage/ui/control.Control# */ {
 
    constructor: {
        value: function Control () {
            this.defineBindings({
                // classList management
                "classList.has('montage--disabled')": {
                    "<-": "disabled"
                },
                "classList.has('montage--active')": {
                    "<-": "active"
                }
            });
        }
    },
 
    /**
     * Dispatched when the button is activated through a mouse click, finger
     * tap, or when focused and the spacebar is pressed.
     *
     * @event AbstractControl#action
     * @type {Event}
     * @property {Map} detail - pass custom data in this property
     */
 
    /**
     * @function
     * @fires AbstractControl#action
     */
    dispatchActionEvent: {
        value: function () {
            return this.dispatchEvent(this.createActionEvent());
        }
    },
 
    /**
     * A promise that indicates an action event triggered an asynchronous task.
     * The control will stop listening to user input until actionCompletionPromise
     * is resoved or rejected, and uses CSS classes to represent the Promise resolution
     * @property {Promise} value
     * @default undefined
     */
    actionCompletionPromise: {
        value: undefined
    },
 
    /**
     * @private
     * @property {Map} value
     * @default null
     */
    _detail: {
        value: null
    },
 
    /**
     * The data property of the action event.
     *
     * Example to toggle the complete class: `"detail.get('selectedItem')" : {
     * "<-" : "@repetition:iteration.object"}`
     *
     * @returns {Map}
     */
    detail: {
        get: function () {
            Eif (this._detail == null) {
                this._detail = new Map();
            }
            return this._detail;
        }
    },
 
    /**
     * Overrides {@link Component#createActionEvent}
     * by adding {@link AbstractControl#detail} custom data
     *
     * @function
     * @returns {AbstractControl#action}
     */
    createActionEvent: {
        value: function () {
            var actionEvent = document.createEvent("CustomEvent"),
                eventDetail;
 
            eventDetail = this._detail;
            actionEvent.initCustomEvent("action", true, true, eventDetail);
            return actionEvent;
        }
    },
 
    hasTemplate: {
        get: function() {
            return !this.hasStandardElement;
        }
    },
 
    standardElementTagName: {
        value: "INPUT"
    },
 
    _hasStandardElement: {
        value:null
    },
 
    hasStandardElement: {
        get: function () {
            return typeof this._hasStandardElement === "boolean" ?  this._hasStandardElement : (this._hasStandardElement = this.element.tagName === this.standardElementTagName);
        }
    },
 
    prepareForActivationEvents: {
        value: function () {
            this.element.addEventListener("focus", this, false);
            this.element.addEventListener('blur', this, false);
        }
    },
 
    /**
     * This property is meant to be used as a way to flag when a component is being interacted with, either through mouse or touch event. false by default,
     * specialized components like controls would set it to true when being interacted with.
     * @type {boolean}
     * @default false
     */
    active: {
        value: false
    },
 
    enabled: {
        get: function () {
            return !this.disabled;
        },
        set: function (value) {
            Eif (typeof value === "boolean") {
                this.disabled = !value;
            }
        }
    },
 
    _focusBlur: {
        value: undefined
    },
 
    focus: {
        value: function () {
            this._focusBlur = 1;
            this.needsDraw = true;
 
            if (!this.preparedForActivationEvents) {
                this._prepareForActivationEvents();
            }
        }
    },
 
    blur: {
        value: function() {
            this._focusBlur = 0;
            this.needsDraw = true;
        }
    },
 
    /*  In some cases elements can't receive focus()
        http://stackoverflow.com/questions/1599660/which-html-elements-can-receive-focus
        http://snook.ca/archives/accessibility_and_usability/elements_focusable_with_tabindex
        https://davidwalsh.name/tabindex-focus
        http://www.456bereastreet.com/archive/201302/making_elements_keyboard_focusable_and_clickable/
        Look at tab-index
    */
    hasFocus: {
        enumerable: false,
        value: false
    },
 
    _elementNeedsTabIndexRegex: {
        value: /INPUT|TEXTAREA|A|SELECT|BUTTON|LABEL/
    },
 
    _elementNeedsTabIndex: {
        value: function () {
            return this.element.tagName.match(this._elementNeedsTabIndexRegex) === null;
        }
    },
 
    draw: {
        value: function () {
        if (this._elementNeedsTabIndex()) {
            if (this._preventFocus) {
                this.element.removeAttribute("tabindex");
            } else {
                this.element.setAttribute("tabindex", "0");
            }
        }
 
          Iif (this._focusBlur === 1) {
                this._element.focus();
            } else Iif (this._focusBlur === 0 || !this.drawsFocusOnPointerActivation) {
                this._element.blur();
            }
            this._focusBlur = void 0;
            // this.drawValue();
        }
    },
 
    acceptsActiveTarget: {
        get: function () {
            //Should the be done in focus instead?
            var shouldBeginEditing = this.callDelegateMethod("shouldBeginEditing", this);
            return (shouldBeginEditing !== false);
        }
    },
 
    willBecomeActiveTarget: {
        value: function (event) {
            this.hasFocus = true;
            this.callDelegateMethod("didBeginEditing", this);
        }
    },
 
    surrendersActiveTarget: {
        value: function (event) {
            var shouldEnd = this.callDelegateMethod("shouldEndEditing", this);
 
            if (shouldEnd === false) {
                return false;
            } else {
                this.hasFocus = false;
                //Check if that's not redundant with "didEndEditing" triggered from handleBlur
                this.callDelegateMethod("didEndEditing", this);
            }
 
            return true;
        }
    },
 
    _preventFocus: {
        enumerable: false,
        value: false
    },
 
/**
    Specifies whether the button should receive focus or not.
    @type {boolean}
    @default false
    @event longpress @benoit: no events here?
*/
    preventFocus: {
        get: function () {
            return this._preventFocus;
        },
        set: function (value) {
            this._preventFocus = !!value;
            this.needsDraw = true;
        }
    },
 
    drawsFocusOnPointerActivation : {
        value: false
    },
 
    /**
     * Description TODO
     * @function
     * @param {Event Handler} event TODO
     */
    handleFocus: {
        enumerable: false,
        value: function (event) {
            this.hasFocus = true;
            this.drawsFocusOnPointerActivation = true;
        }
    },
 
    handleBlur: {
        enumerable: false,
        value: function (event) {
            this.hasFocus = false;
            this.drawsFocusOnPointerActivation = false;
            this.callDelegateMethod("didEndEditing", this);
            //This create an issue in textfield, to investigate
            this.dispatchActionEvent();
        }
    },
 
    _value: {
        value: null
    },
 
    /**
     * This property is meant to return the value of a control's element. specialized controls can override this to access different DOM properties if needed
     * @type {String}
     */
    elementValue: {
        get: function () {
            return this.element.value;
        }
    },
    /*
    The value associated with the checkbox. Per the WC3 specification, if the element has a <code>value</code> attribute then the value of that attribute's value is returned; otherwise, it returns "on".
 
    The "typed" data value associated with the input element. When this
    property is set, if the component's <code>converter</code> property is
    non-null then its <code>revert()</code> method is invoked, passing it
    the newly assigned value. The <code>revert()</code> function is
    responsible for validating and converting the user-supplied value to
    its typed format. For example, in the case of a DateInput component
    (which extends TextInput) a user enters a string for the date (for
    example, "10-12-2005"). A <code>DateConverter</code> object is assigned
    to the component's <code>converter</code> property.
 
    If the comopnent doesn't specify a converter object then the raw value
    is assigned to <code>value</code>.
 
    @type {string}
    @default "on"
    */
    value: {
        get: function () {
            return this._value;
        },
        set: function (value, fromInput) {
 
            if (value !== this._value) {
                var shouldAcceptValue
                Eif (!this.delegate ||  (shouldAcceptValue = this.callDelegateMethod("shouldAcceptValue", this, value) ) === undefined ? true : shouldAcceptValue ){
                    // console.log("_setValue past first step value is ",value);
 
                    Iif (this.converter) {
                        var convertedValue;
                        try {
                            //Where is the matching convert?
                            convertedValue = this.converter.revert(value);
                            this.error = null;
                            this._value = convertedValue;
                        } catch (e) {
                            // unable to convert - maybe error
                            this._value = value;
                            //FIXME: we don't handle required field.
                            this.error = value !== "" && value !== void 0 && value !== null ? e : null;
                        }
                    } else {
                        this._value = value;
                        this.error = null;
                    }
 
                    this.callDelegateMethod("didChange", this);
 
                    this._elementAttributeValues["value"] = value;
 
                    this.needsDraw = true;
                }
            }
        }
    },
 
    /**
        A reference to a Converter object whose <code>revert()</code> function is invoked when a new value is assigned to the TextInput object's <code>value</code> property. The revert() function attempts to transform the newly assigned value into a "typed" data property. For instance, a DateInput component could assign a DateConverter object to this property to convert a user-supplied date string into a standard date format.
        @type {Converter}
        @default null
        @see {@link module:montage/core/converter.Converter}
    */
    converter:{
        value: null
    },
 
    _error: {
        value: null
    },
 
 
 
/* Look at
    https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation
    https://developer.mozilla.org/en-US/docs/Web/API/ValidityState
    https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Data_form_validation
    http://stackoverflow.com/questions/16634471/how-can-i-get-the-html5-validity-state-of-an-input-text-box
    http://html5index.org/DOM%20-%20ValidityState.html
*/
 
/**
    If an error is thrown by the converter object during a new value assignment, this property is set to <code>true</code>, and schedules a new draw cycle so the the UI can be updated to indicate the error state. the <code>montage--invalidText</code> CSS class is assigned to the component's DOM element during the next draw cycle.
    @type {boolean}
    @default false
*/
    error: {
        get: function () {
            return this._error;
        },
        set: function (v) {
            this._error = v;
            this.errorMessage = this._error ? this._error.message : null;
            this.needsDraw = true;
        }
    },
 
    _errorMessage: {value: null},
 
    /**
     * The message to display when the component is in an error state.
     * @type {string}
     * @default null
     * @todo: @benoit: we should maybe take a look at ValidityState
     * https://developer.mozilla.org/en/docs/Web/API/ValidityState
     * https://msdn.microsoft.com/en-us/library/windows/apps/hh441292.aspx
     */
    errorMessage: {
        get: function () {
            return this._errorMessage;
        },
        set: function (v) {
            this._errorMessage = v;
        }
    },
 
    // // set value from user input
    // /**
    //   @private
    // */
    takeValueFromElement: {
        value: function() {
            this.value = this.elementValue;
            // Object.getPropertyDescriptor(this, "value").set.call(this, this.element.value, true);
        }
    },
    /**
        Returns a Boolean value indicating whether the control dispatched its action event continuously when value changes.
 
        @type {}
        @default false
        * @returns {boolean}
        @see {@link module:montage/core/converter.Converter}
    */
    isContinuous: {
        value: false
    }
 
});
 
//http://www.w3.org/TR/html5/elements.html#global-attributes
Control.addAttributes( /** @lends module:montage/ui/control.Control# */ {
    /**
        Specifies if the control should receive focus when the document loads. Because Montage components are loaded asynchronously after the document has loaded, setting this property has no effect on the element's focus state.
        @type {boolean}
        @default false
    */
        autofocus: {value: false, dataType: 'boolean'},
 
    /**
        Specifies if the control is disabled.
        @type {boolean}
        @default false
    */
        disabled: {value: false, dataType: 'boolean'},
 
    /**
        The value of the id attribute of the form with which to associate the element.
        @type {string}
        @default null
    */
        form: null,
 
    /**
        The name part of the name/value pair associated with this element for the purposes of form submission.
        @type {string}
        @default null
    */
        name: null,
 
    /**
        Specifies if this control is readonly.
        @type {boolean}
        @default false
    */
        readonly: {value: false, dataType: 'boolean'}
 
});