From a3024011a91d3941f81481dd4d600e9684eb0fd4 Mon Sep 17 00:00:00 2001 From: Valerio Virgillito Date: Thu, 2 Feb 2012 00:11:51 -0800 Subject: upgrading to Montage v0.6 Signed-off-by: Valerio Virgillito --- node_modules/montage/ui/text-input.js | 304 ++++++++++++++++++++++++++++++++++ 1 file changed, 304 insertions(+) create mode 100644 node_modules/montage/ui/text-input.js (limited to 'node_modules/montage/ui/text-input.js') diff --git a/node_modules/montage/ui/text-input.js b/node_modules/montage/ui/text-input.js new file mode 100644 index 00000000..5b8025a2 --- /dev/null +++ b/node_modules/montage/ui/text-input.js @@ -0,0 +1,304 @@ +/* + 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. +
*/ + +var Montage = require("montage").Montage, + Component = require("ui/component").Component, + NativeControl = require("ui/native-control").NativeControl; + + // Standard tag attributes - http://www.w3.org/TR/html5/the-input-element.html#the-input-element + +exports.StandardInputAttributes = { + accept: null, + alt: null, + autocomplete: null, + autofocus: {dataType: "boolean"}, + checked: {dataType: "boolean"}, + dirname: null, + disabled: {dataType: 'boolean'}, + form: null, + formaction: null, + formenctype: null, + formmethod: null, + formnovalidate: null, + formtarget: null, + height: null, + list: null, + max: null, + maxlength: null, + min: null, + multiple: {dataType: 'boolean'}, + name: null, + pattern: null, + placeholder: null, + readonly: {dataType: 'boolean'}, + required: {dataType: 'boolean'}, + size: null, + src: null, + step: null, + width: null + // "type" is not bindable and "value" is handled as a special attribute +}; + +var TextInput = exports.TextInput = Montage.create(NativeControl, { + +/** + Description TODO + @private +*/ + _hasFocus: { + enumerable: false, + value: false + }, +/** + Description TODO + @private +*/ + _value: { + enumerable: false, + value: null + }, +/** + Description TODO + @private +*/ + _valueSyncedWithInputField: { + enumerable: false, + value: false + }, +/** + Description TODO + @type {Function} + @default null + */ + value: { + enumerable: true, + serializable: true, + get: function() { + return this._value; + }, + set: function(value, fromInput) { + + if (value && value.length > 0 && this.converter) { + var convertedValue; + try { + convertedValue = this.converter.revert(value); + if (this.error) { + this.error = null; + } + this._value = convertedValue; + + } catch(e) { + // unable to convert - maybe error + this.error = e; + this._valueSyncedWithInputField = false; + } + } else { + this._value = value; + } + if(fromInput) { + this._valueSyncedWithInputField = true; + //this.needsDraw = true; + } else { + this._valueSyncedWithInputField = false; + this.needsDraw = true; + } + } + }, + + // set value from user input + /** + @private + */ + _setValue: { + value: function() { + var newValue = this.element.value; + Object.getPropertyDescriptor(this, "value").set.call(this, newValue, true); + } + }, +/** + Description TODO + @type {Property} + @default null + */ + converter:{ + value: null + }, +/** + Description TODO + @private +*/ + _error: { + value: false + }, + /** + Description TODO + @type {Function} + @default {Boolean} 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}, + errorMessage: { + get: function() { + return this._errorMessage; + }, + set: function(v) { + this._errorMessage = v; + } + }, + +/** + Description TODO + @private +*/ + _updateOnInput: { + value: true + }, + + // switch to turn off auto update upon keypress overriding the Converter flag +/** + Description TODO + @type {Function} + @default {Boolean} true + */ + updateOnInput: { + get: function() { + return !!this._updateOnInput; + }, + set: function(v) { + this._updateOnInput = v; + } + }, + + // Callbacks + /** + Description TODO + @function + */ + prepareForDraw: { + enumerable: false, + value: function() { + var el = this.element; + el.addEventListener("focus", this); + el.addEventListener('input', this); + el.addEventListener('change', this); + el.addEventListener('blur', this); + } + }, +/** + Description TODO + @private +*/ + _setElementValue: { + value: function(v) { + this.element.value = v; + } + }, +/** + Description TODO + @function + */ + draw: { + enumerable: false, + value: function() { + + var t = this.element; + + if (!this._valueSyncedWithInputField) { + this._setElementValue(this.converter ? this.converter.convert(this._value) : this._value); + } + + + if (this.error) { + t.classList.add('montage-text-invalid'); + t.title = this.error.message || ''; + } else { + t.classList.remove("montage-text-invalid"); + t.title = ''; + } + + var fn = Object.getPrototypeOf(TextInput).draw; + fn.call(this); + + } + }, +/** + Description TODO + @function + */ + didDraw: { + enumerable: false, + value: function() { + if (this._hasFocus && this._value != null) { + var length = this._value.toString().length; + this.element.setSelectionRange(length, length); + } + this._valueSyncedWithInputField = true; + } + }, + // Event handlers +/** + Description TODO + @function + */ + handleInput: { + enumerable: false, + value: function() { + if (this.converter) { + if (this.converter.allowPartialConversion === true && this.updateOnInput === true) { + this._setValue(); + } + } else { + this._setValue(); + } + } + }, +/** + Description TODO + @function + @param {Event Handler} event TODO + */ + handleChange: { + enumerable: false, + value: function(event) { + this._setValue(); + this._hasFocus = false; + } + }, +/** + Description TODO + @function + @param {Event Handler} event TODO + */ + handleBlur: { + enumerable: false, + value: function(event) { + this._hasFocus = false; + } + }, +/** + Description TODO + @function + @param {Event Handler} event TODO + */ + handleFocus: { + enumerable: false, + value: function(event) { + this._hasFocus = true; + } + } + +}); + -- cgit v1.2.3