/* This file contains proprietary software owned by Motorola Mobility, Inc.
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
*/ /*global require, exports*/ /** @module "montage/ui/button.reel" @requires montage/core/core @requires montage/ui/component @requires montage/ui/native-control @requires montage/ui/composer/press-composer */ var Montage = require("montage").Montage, Component = require("ui/component").Component, NativeControl = require("ui/native-control").NativeControl, PressComposer = require("ui/composer/press-composer").PressComposer; /** Wraps a native <button> or <input[type="button"]> HTML element. The element's standard attributes are exposed as bindable properties. @class module:"montage/ui/button.reel".Button @extends module:montage/native-control.NativeControl @example JavaScript example var b1 = Button.create(); b1.element = document.querySelector("btnElement"); b1.addEventListener("action", function(event) { console.log("Got event 'action' event"); }); @example Serialized example { "aButton": { "prototype": "ui/button.reel", "properties": { "element": {"#": "btnElement"} }, "listeners": [ { "type": "action", "listener": {"@": "appListener"} } ] }, "listener": { "prototype": "appListener" } } <button data-montage-id="btnElement"> */ var Button = exports.Button = Montage.create(NativeControl, /** @lends module:"montage/ui/button.reel".Button# */ { _preventFocus: { enumerable: false, value: false }, /** Specifies whether the button should receive focus or not. @type {boolean} @default false @event longpress */ preventFocus: { get: function () { return this._preventFocus; }, set: function (value) { if (value === true) { this._preventFocus = true; } else { this._preventFocus = false; } } }, /** Enables or disables the Button from user input. When this property is set to false, the "disabled" CSS style is applied to the button's DOM element during the next draw cycle. When set to true the "disabled" CSS class is removed from the element's class list. */ //TODO we should prefer positive properties like enabled vs disabled, get rid of disabled enabled: { dependencies: ["disabled"], get: function () { return !this._disabled; }, set: function (value) { this.disabled = !value; } }, /** A Montage converter object used to convert or format the label displayed by the Button instance. When a new value is assigned to label, the converter object's convert() method is invoked, passing it the newly assigned label value. @type {Property} @default null */ converter: { value: null }, /** Stores the node that contains this button's value. Only used for non-`` elements. @private */ _labelNode: {value:undefined, enumerable: false}, _label: { value: undefined, enumerable: false }, /** The label for the button. In an <input> element this is taken from the element's value attribute. On any other element (including <button>) this is the first child node which is a text node. If one isn't found then it will be created. If the button has a non-null converter property, the converter object's convert() method is called on the value before being assigned to the button instance. @type {string} @default undefined */ label: { get: function() { return this._label; }, set: function(value) { if (value && value.length > 0 && this.converter) { try { value = this.converter.convert(value); if (this.error) { this.error = null; } } catch(e) { // unable to convert - maybe error this.error = e; } } this._label = value; if (this._isInputElement) { this._value = value; } this.needsDraw = true; } }, /** The amount of time in milliseconds the user must press and hold the button a hold event is dispatched. The default is 1 second. @type {number} @default 1000 */ holdThreshold: { get: function() { return this._pressComposer.longPressThreshold; }, set: function(value) { this._pressComposer.longPressThreshold = value; } }, _pressComposer: { enumberable: false, value: null }, _active: { enumerable: false, value: false }, /** This property is true when the button is being interacted with, either through mouse click or touch event, otherwise false. @type {boolean} @default false */ active: { get: function() { return this._active; }, set: function(value) { this._active = value; this.needsDraw = true; } }, // HTMLInputElement/HTMLButtonElement methods blur: { value: function() { this._element.blur(); } }, focus: { value: function() { this._element.focus(); } }, // click() deliberately omitted (it isn't available on