From cfc5baef61154b820faef812d04753677db145e6 Mon Sep 17 00:00:00 2001 From: rlx <0x0073@0x2620.org> Date: Sat, 22 Oct 2011 14:50:31 +0000 Subject: [PATCH] change autovalidate function for input elements so that it returns, along with the new value, a valid flag that indicates if the value is already valid --- source/Ox.UI/js/Form/Ox.Form.js | 2 +- source/Ox.UI/js/Form/Ox.FormElementGroup.js | 7 +++--- source/Ox.UI/js/Form/Ox.Input.js | 26 +++++++++++++-------- source/Ox/js/String.js | 2 ++ 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/source/Ox.UI/js/Form/Ox.Form.js b/source/Ox.UI/js/Form/Ox.Form.js index bd6635bf..b126afd9 100644 --- a/source/Ox.UI/js/Form/Ox.Form.js +++ b/source/Ox.UI/js/Form/Ox.Form.js @@ -51,7 +51,6 @@ Ox.Form = function(options, self) { }, */ autovalidate: function(data) { - data.valid = !!data.value.length; validate(i, data.valid); data.valid && self.$items[i].setMessage(''); }, @@ -65,6 +64,7 @@ Ox.Form = function(options, self) { }, */ change: function(data) { + // fixme: shouldn't this be key/value instead of id/data? that.triggerEvent('change', { id: self.itemIds[i], data: data diff --git a/source/Ox.UI/js/Form/Ox.FormElementGroup.js b/source/Ox.UI/js/Form/Ox.FormElementGroup.js index 663ef408..d627987e 100644 --- a/source/Ox.UI/js/Form/Ox.FormElementGroup.js +++ b/source/Ox.UI/js/Form/Ox.FormElementGroup.js @@ -36,10 +36,11 @@ Ox.FormElementGroup = function(options, self) { float: self.options.float // fixme: make this a class }) .bindEvent({ + autovalidate: function(data) { + that.triggerEvent({autovalidate: data}); + }, validate: function(data) { - that.triggerEvent({ - validate: data - }); + that.triggerEvent({validate: data}); } }) .appendTo(that); diff --git a/source/Ox.UI/js/Form/Ox.Input.js b/source/Ox.UI/js/Form/Ox.Input.js index a559563e..a7ec1bde 100644 --- a/source/Ox.UI/js/Form/Ox.Input.js +++ b/source/Ox.UI/js/Form/Ox.Input.js @@ -450,10 +450,13 @@ Ox.Input = function(options, self) { } function autovalidateFunction(value) { - var regexp = new RegExp(self.options.autovalidate); - return value.split('').map(function(v) { - return regexp.test(v) ? v : null; + value = value.split('').map(function(v) { + return self.options.autovalidate.test(v) ? v : null; }).join(''); + return { + valid: !!value.length, + value: value + }; } function autovalidateTypeFunction(type, value) { @@ -467,7 +470,6 @@ Ox.Input = function(options, self) { + '$)' ) : new RegExp('(^' + (self.options.min < 0 ? '\\-?' : '') + '\\d+)'); if (type == 'float') { - //Ox.print('--float--', value) if (value === '') { value = '0.' + self.decimals; cursor = [0, value.length]; @@ -484,7 +486,6 @@ Ox.Input = function(options, self) { value = '0' + value; cursor = [2, value.length]; } else if (/\.$/.test(value)) { - //Ox.print('$$$$$$$$$$$') value += self.decimals; cursor = [value.indexOf('.') + 1, value.length]; } else if (/\./.test(value) && self.options.decimals) { @@ -510,18 +511,23 @@ Ox.Input = function(options, self) { value = oldValue; cursor = oldCursor; } - autovalidateCallback(value, cursor); + autovalidateCallback({ + cursor: cursor, + valid: true, + value: value + }); } - function autovalidateCallback(newValue, newCursor) { + function autovalidateCallback(data) { //Ox.print('autovalidateCallback', newValue, oldCursor) - self.options.value = newValue; + self.options.value = data.value; self.$input.val(self.options.value); !blur && cursor( - newCursor || (oldCursor[1] + newValue.length - oldValue.length) + data.cursor || (oldCursor[1] + data.value.length - oldValue.length) ); that.triggerEvent('autovalidate', { - value: self.options.value + valid: data.valid, + value: data.value }); } diff --git a/source/Ox/js/String.js b/source/Ox/js/String.js index 52851f11..6365404a 100644 --- a/source/Ox/js/String.js +++ b/source/Ox/js/String.js @@ -23,6 +23,8 @@ Ox.clean Remove leading, trailing and double whitespace from a string "foo bar" > Ox.clean(" foo \n bar ") "foo\nbar" + > Ox.clean(" foo\tbar ") + "foo bar" @*/ Ox.clean = function(str) { return Ox.map(str.split('\n'), function(str) {