From 11b30e8db9353b3f79041bf3e47d03ee2904f554 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 16 Jul 2009 23:52:35 +0000 Subject: [PATCH] MINOR Moved NumericField? javascript logic from CalendarDateField?.js into new NumericField?.js. Removed unused DMYCalendarDateField javascript logic, the field behaves just fine with standard input fields and no auto-focus magic on the input fields. (merged from branches/2.3-nzct) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@82061 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- forms/DMYCalendarDateField.php | 1 + javascript/CalendarDateField.js | 130 -------------------------------- javascript/NumericField.js | 60 +++++++++++++++ 3 files changed, 61 insertions(+), 130 deletions(-) create mode 100644 javascript/NumericField.js diff --git a/forms/DMYCalendarDateField.php b/forms/DMYCalendarDateField.php index c0ed6d4fb..cbd30001e 100755 --- a/forms/DMYCalendarDateField.php +++ b/forms/DMYCalendarDateField.php @@ -16,6 +16,7 @@ class DMYCalendarDateField extends CalendarDateField { Requirements::javascript(THIRDPARTY_DIR . "/calendar/calendar-setup.js"); Requirements::css(SAPPHIRE_DIR . "/css/CalendarDateField.css"); Requirements::css(THIRDPARTY_DIR . "/calendar/calendar-win2k-1.css"); + Requirements::javascript(SAPPHIRE_DIR . "/javascript/NumericField.js"); Requirements::javascript(SAPPHIRE_DIR . "/javascript/CalendarDateField.js"); $field = DateField::Field(); diff --git a/javascript/CalendarDateField.js b/javascript/CalendarDateField.js index f3fb70ebe..e69de29bb 100755 --- a/javascript/CalendarDateField.js +++ b/javascript/CalendarDateField.js @@ -1,130 +0,0 @@ - - -// _DAYS_IN_MONTH = new Array( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); - -NumericField = Class.create(); -NumericField.applyTo('input.numeric'); -NumericField.prototype = { - initialize: function() { - this.oldValue = this.value; - }, - - setRange: function( minValue, maxValue ) { - this.minValue = minValue; - this.maxValue = maxValue; - }, - - onkeyup: function() { - var testValue = this.value; - - if( testValue == this.oldValue ) - return; - - var length = this.maxLength; - this.value = ''; - - var testedOk = true; - - var regex = new RegExp( '^\\d{0,' + length + '}$' ); - - // check that the value is numeric - if( !testValue.match( regex ) ) - testedOk = false; - - if( testedOk && testValue.length > 0 ) { - - // check that the number is not outside the range - if( testedOk && typeof this.minValue != 'undefined' && parseInt(testValue) < this.minValue ) - testedOk = false; - - if( testedOk && typeof this.maxValue != 'undefined' && parseInt(testValue) > this.maxValue ) - testedOk = false; - - // use any external tests - if( testedOk && typeof this.externalValidate != 'undefined' && !this.externalValidate( testValue ) ) - testedOk = false; - - } - - if( testedOk ) { - this.oldValue = this.value = testValue; - - // DEBUG This produces weird javascript-errors, and is not very useable at all - // DONT MERGE - /* - if( this.value.length == this.maxLength && this.nextField ) - this.nextField.focus(); - */ - - if( this.callOnValidate ) - this.callOnValidate(); - } else - this.value = this.oldValue; - } -} - - -DMYCalendarDateField = Class.create(); -DMYCalendarDateField.applyTo('div.dmycalendardate'); -DMYCalendarDateField.prototype = { - initialize: function() { - - // the hidden field will contain the full date string to make it backwards compatible - // with the full date fields - this.hiddenDateField = this.getElementsByTagName('input')[0]; - this.dayField = this.getElementsByTagName('input')[1]; - this.monthField = this.getElementsByTagName('input')[2]; - this.yearField = this.getElementsByTagName('input')[3]; - - this.hiddenDateField.onchange = this.updateVisibleDate.bind(this); - this.dayField.onchange = this.updateDate.bind(this); - this.monthField.onchange = this.updateDate.bind(this); - this.yearField.onchange = this.updateDate.bind(this); - - // this field is updated and then validated before the hidden or visible - // fields are updated - - this.oldDay = ''; - this.oldMonth = ''; - this.oldYear = ''; - - // these fields are numeric - // TODO, validate date range - - this.dayField.callOnValidate = this.updateDate.bind(this); - this.monthField.callOnValidate = this.updateDate.bind(this); - this.yearField.callOnValidate = this.updateDate.bind(this); - - this.dayField.setRange( 1, 31 ); - // this.dayField.externalValidate = this.validateDay.bind(this); - - this.monthField.setRange( 1, 12 ); - - this.dayField.nextField = this.monthField; - this.monthField.nextField = this.yearField; - - this.dayField.oldlength = 0; - this.monthField.oldlength = 0; - this.yearField.oldlength = 0; - }, - // C'mon it's a great name for a function - updateDate: function() { - this.hiddenDateField.value = this.dayField.value + '/' + this.monthField.value + '/' + this.yearField.value; - //alert(this.hiddenDateField.name); - }, - updateVisibleDate: function() { - var matches = this.hiddenDateField.value.match( /(\d{2})\/(\d{2})\/(\d{4})/ ); - this.dayField.value = matches[1]; - this.monthField.value = matches[2]; - this.yearField.value = matches[3]; - }, - validateDay: function( value ) { - /*if( this.monthField.value.length == 0 ) - return true; - - if( parseInt( value ) > _DAYS_IN_MONTH[parseInt( this.monthField.value )] ) - return false;*/ - - return true; - } -}; \ No newline at end of file diff --git a/javascript/NumericField.js b/javascript/NumericField.js new file mode 100644 index 000000000..ac05025c7 --- /dev/null +++ b/javascript/NumericField.js @@ -0,0 +1,60 @@ +NumericField = Class.create(); +NumericField.applyTo('input.numeric'); +NumericField.prototype = { + initialize: function() { + this.oldValue = this.value; + }, + + setRange: function( minValue, maxValue ) { + this.minValue = minValue; + this.maxValue = maxValue; + }, + + onkeyup: function() { + var testValue = this.value; + + if( testValue == this.oldValue ) + return; + + var length = this.maxLength; + this.value = ''; + + var testedOk = true; + + var regex = new RegExp( '^\\d{0,' + length + '}$' ); + + // check that the value is numeric + if( !testValue.match( regex ) ) + testedOk = false; + + if( testedOk && testValue.length > 0 ) { + + // check that the number is not outside the range + if( testedOk && typeof this.minValue != 'undefined' && parseInt(testValue) < this.minValue ) + testedOk = false; + + if( testedOk && typeof this.maxValue != 'undefined' && parseInt(testValue) > this.maxValue ) + testedOk = false; + + // use any external tests + if( testedOk && typeof this.externalValidate != 'undefined' && !this.externalValidate( testValue ) ) + testedOk = false; + + } + + if( testedOk ) { + this.oldValue = this.value = testValue; + + // DEBUG This produces weird javascript-errors, and is not very useable at all + // DONT MERGE + /* + if( this.value.length == this.maxLength && this.nextField ) + this.nextField.focus(); + */ + + if( this.callOnValidate ) + this.callOnValidate(); + } else + this.value = this.oldValue; + } +} \ No newline at end of file