silverstripe-framework/javascript/CalendarDateField.js

130 lines
3.7 KiB
JavaScript
Raw Normal View History

// _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;
switch externals to trunk. Inform-merge: from the changeset: r32477: Merge 2.0-inform from trunk previously r32478: Merge 2.0-inform from trunk previously r32481: merge 2.0infom with lastes chunk r32483: merge 2.0infom with lastes chunk r33526: Final styling of all forms and combined communication form add/alter javascript for height adjustment of First / Second block r33580: styling for combined form communication in myinfom pages r33706: styling of combined form (communication) in Email r33881: made compatible to $extraClass r33885: added defaultVal r33887: fixed typo r34728: modified SmallFieldHolder?() r34729: added "validationError"-class r34914: WIP3866: Factfinder: Hide "self emplyed" block r34964: Change current plan upto TraumaInsurance? r35038: disabled friggin field focus r35230: #1032 Fixed hash-link insertion r35887: conditionally setting parameters in sourceID() - to avoid empty overrides r35892: Saving value in SQL-compatible format (YYYY-MM-DD instead of DD/MM/YYYY), with fallback for non-sql values (just passed through without conversion) r35928: Removed "create a" from PageType?-dropdown, sorting alphabetically, falling back to $singular_name r35990: branched off for membertablefield r35994: fix for membertablefield r36024: added array-condition needed for DMYDateField r36083: fix bug for compositeField -> dropDatalessField r36394: removed debug code r36826: change wrong indent format r36828: WIP 4262: Logging out of My Inform goes to blank page r36858: Fixed error caused in r12472 while merging to Session-class r37132: Merged partial changesets from branches/2.0-nzct, only adding childID to detailform when not in add-mode r40815: add an unsubscribe record when a member subscribe a newslettertype r41113: fix the bug described in http://support.silverstripe.com/info/ticket/31: CRM not showing search results r43226: fixed search (partial merge from trunk) r43268: merged createNewPassword() from trunk, was referencing a non-existinent global function randomString() git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@45473 467b73ca-7a2a-4603-9d3b-597d59a354a9
2007-11-23 02:10:19 +01:00
// 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();
switch externals to trunk. Inform-merge: from the changeset: r32477: Merge 2.0-inform from trunk previously r32478: Merge 2.0-inform from trunk previously r32481: merge 2.0infom with lastes chunk r32483: merge 2.0infom with lastes chunk r33526: Final styling of all forms and combined communication form add/alter javascript for height adjustment of First / Second block r33580: styling for combined form communication in myinfom pages r33706: styling of combined form (communication) in Email r33881: made compatible to $extraClass r33885: added defaultVal r33887: fixed typo r34728: modified SmallFieldHolder?() r34729: added "validationError"-class r34914: WIP3866: Factfinder: Hide "self emplyed" block r34964: Change current plan upto TraumaInsurance? r35038: disabled friggin field focus r35230: #1032 Fixed hash-link insertion r35887: conditionally setting parameters in sourceID() - to avoid empty overrides r35892: Saving value in SQL-compatible format (YYYY-MM-DD instead of DD/MM/YYYY), with fallback for non-sql values (just passed through without conversion) r35928: Removed "create a" from PageType?-dropdown, sorting alphabetically, falling back to $singular_name r35990: branched off for membertablefield r35994: fix for membertablefield r36024: added array-condition needed for DMYDateField r36083: fix bug for compositeField -> dropDatalessField r36394: removed debug code r36826: change wrong indent format r36828: WIP 4262: Logging out of My Inform goes to blank page r36858: Fixed error caused in r12472 while merging to Session-class r37132: Merged partial changesets from branches/2.0-nzct, only adding childID to detailform when not in add-mode r40815: add an unsubscribe record when a member subscribe a newslettertype r41113: fix the bug described in http://support.silverstripe.com/info/ticket/31: CRM not showing search results r43226: fixed search (partial merge from trunk) r43268: merged createNewPassword() from trunk, was referencing a non-existinent global function randomString() git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@45473 467b73ca-7a2a-4603-9d3b-597d59a354a9
2007-11-23 02:10:19 +01:00
*/
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;
}
};