From afee1ab975a69ec7fe785b1fdafff466e12120e3 Mon Sep 17 00:00:00 2001 From: Bruno Binet Date: Mon, 12 Aug 2013 12:59:47 +0200 Subject: [PATCH] Add VALUE_AS_NUMBER property to ol.dom.Input This should be used when one want to bind a number instead of a string. --- src/ol/dom/input.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/ol/dom/input.js b/src/ol/dom/input.js index 6e697d877d..6cf01740f2 100644 --- a/src/ol/dom/input.js +++ b/src/ol/dom/input.js @@ -11,6 +11,7 @@ goog.require('ol.Object'); */ ol.dom.InputProperty = { VALUE: 'value', + VALUE_AS_NUMBER: 'valueAsNumber', CHECKED: 'checked' }; @@ -44,6 +45,9 @@ ol.dom.Input = function(target) { goog.events.listen(this, ol.Object.getChangeEventType(ol.dom.InputProperty.VALUE), this.handleValueChanged_, false, this); + goog.events.listen(this, + ol.Object.getChangeEventType(ol.dom.InputProperty.VALUE_AS_NUMBER), + this.handleValueAsNumberChanged_, false, this); goog.events.listen(this, ol.Object.getChangeEventType(ol.dom.InputProperty.CHECKED), this.handleCheckedChanged_, false, this); @@ -77,6 +81,19 @@ goog.exportProperty( ol.dom.Input.prototype.getValue); +/** + * Get the value of the input as a number. + * @return {number|null|undefined} input value as number. + */ +ol.dom.Input.prototype.getValueAsNumber = function() { + return /** @type {number} */ (this.get(ol.dom.InputProperty.VALUE_AS_NUMBER)); +}; +goog.exportProperty( + ol.dom.Input.prototype, + 'getValueAsNumber', + ol.dom.Input.prototype.getValueAsNumber); + + /** * Sets the value of the input. * @param {string} value Value. @@ -90,6 +107,19 @@ goog.exportProperty( ol.dom.Input.prototype.setValue); +/** + * Sets the number value of the input. + * @param {number} value Number value. + */ +ol.dom.Input.prototype.setValueAsNumber = function(value) { + this.set(ol.dom.InputProperty.VALUE_AS_NUMBER, value); +}; +goog.exportProperty( + ol.dom.Input.prototype, + 'setValueAsNumber', + ol.dom.Input.prototype.setValueAsNumber); + + /** * Set whether or not a checkbox is checked. * @param {boolean} checked Checked. @@ -111,6 +141,7 @@ ol.dom.Input.prototype.handleInputChanged_ = function() { this.setChecked(this.target_.checked); } else { this.setValue(this.target_.value); + this.setValueAsNumber(this.target_.valueAsNumber); } }; @@ -129,3 +160,12 @@ ol.dom.Input.prototype.handleCheckedChanged_ = function() { ol.dom.Input.prototype.handleValueChanged_ = function() { this.target_.value = this.getValue(); }; + + +/** + * @private + */ +ol.dom.Input.prototype.handleValueAsNumberChanged_ = function() { + // firefox raises an exception if this.target_.valueAsNumber is set instead + this.target_.value = this.getValueAsNumber(); +};