Datepicker: Re-introduce formatDate option, simplify Globalize usage

This commit is contained in:
Felix Nagel 2015-07-28 23:10:16 +02:00
parent ce704462b2
commit 8bd4030443
3 changed files with 18 additions and 22 deletions

View File

@ -10,6 +10,7 @@ common.testWidget( "datepicker", {
buttons: [], buttons: [],
classes: {}, classes: {},
disabled: false, disabled: false,
dateFormat: { date: "short" },
eachDay: $.noop, eachDay: $.noop,
labels: { labels: {
"datePickerRole": "date picker", "datePickerRole": "date picker",

View File

@ -45,13 +45,14 @@ test( "appendTo", function() {
}); });
test( "Pass-through options", function() { test( "Pass-through options", function() {
expect( 9 ); expect( 11 );
var options = { var options = {
buttons: { "Test": $.noop }, buttons: { "Test": $.noop },
dateFormat: { date: "full" }, dateFormat: { date: "full" },
disabled: true, disabled: true,
eachDay: function( day ) { day; }, eachDay: function( day ) { day; },
locale: "de",
max: new Date( 2000, 0, 1 ), max: new Date( 2000, 0, 1 ),
min: new Date( 2000, 0, 2 ), min: new Date( 2000, 0, 2 ),
numberOfMonths: 3, numberOfMonths: 3,
@ -72,6 +73,10 @@ test( "Pass-through options", function() {
if ( key === "dateFormat" ) { if ( key === "dateFormat" ) {
equal( input.val(), "Wednesday, January 1, 2014", "option " + key + ": updated format" ); equal( input.val(), "Wednesday, January 1, 2014", "option " + key + ": updated format" );
} }
if ( key === "locale" ) {
equal( input.val(), "Mittwoch, 1. Januar 2014", "option " + key + ": updated locale" );
}
}); });
}); });

View File

@ -51,19 +51,17 @@ var widget = $.widget( "ui.datepicker", {
select: null select: null
}, },
calendarOptions: [ "buttons", "disabled", "eachDay", "labels", "locale", calendarOptions: [ "buttons", "disabled", "dateFormat", "eachDay", "labels",
"max", "min", "numberOfMonths", "showWeek" ], "locale", "max", "min", "numberOfMonths", "showWeek" ],
_create: function() { _create: function() {
this.suppressExpandOnFocus = false; this.suppressExpandOnFocus = false;
this._setLocale( this.options.locale );
if ( $.type( this.options.max ) === "string" ) { if ( $.type( this.options.max ) === "string" ) {
this.options.max = this._parseYMD( this.options.max ); this.options.max = Globalize.parseDate( this.options.max, { raw: "yyyy-MM-dd" } );
} }
if ( $.type( this.options.min ) === "string" ) { if ( $.type( this.options.min ) === "string" ) {
this.options.min = this._parseYMD( this.options.min ); this.options.min = Globalize.parseDate( this.options.min, { raw: "yyyy-MM-dd" } );
} }
this._createCalendar(); this._createCalendar();
@ -90,7 +88,8 @@ var widget = $.widget( "ui.datepicker", {
}, },
_createCalendar: function() { _createCalendar: function() {
var that = this; var that = this,
globalize = new Globalize( this.options.locale );
this.calendar = $( "<div>" ) this.calendar = $( "<div>" )
.addClass( "ui-front ui-datepicker" ) .addClass( "ui-front ui-datepicker" )
@ -99,7 +98,7 @@ var widget = $.widget( "ui.datepicker", {
// Initialize calendar widget // Initialize calendar widget
this.calendarInstance = this.calendar this.calendarInstance = this.calendar
.calendar( $.extend( {}, this.options, { .calendar( $.extend( {}, this.options, {
value: this._getParsedValue(), value: globalize.dateParser( this.options.dateFormat )( this.element.val() ),
select: function( event ) { select: function( event ) {
that.element.val( that.calendarInstance.value() ); that.element.val( that.calendarInstance.value() );
that.close(); that.close();
@ -280,21 +279,13 @@ var widget = $.widget( "ui.datepicker", {
}); });
}, },
_setLocale: function( locale ) {
var globalize = new Globalize( locale );
this._format = globalize.dateFormatter({ date: "short" });
this._parse = globalize.dateParser({ date: "short" });
this._parseYMD = globalize.dateParser({ raw: "yyyy-MM-dd" });
},
_buildPosition: function() { _buildPosition: function() {
return $.extend( { of: this.element }, this.options.position ); return $.extend( { of: this.element }, this.options.position );
}, },
value: function( value ) { value: function( value ) {
if ( arguments.length ) { if ( arguments.length ) {
this.valueAsDate( this._parse( value ) ); this.valueAsDate( this.calendarInstance._parse( value ) );
} else { } else {
return this._getParsedValue() ? this.element.val() : null; return this._getParsedValue() ? this.element.val() : null;
} }
@ -304,7 +295,7 @@ var widget = $.widget( "ui.datepicker", {
if ( arguments.length ) { if ( arguments.length ) {
if ( this.calendarInstance._isValid( value ) ) { if ( this.calendarInstance._isValid( value ) ) {
this.calendarInstance.valueAsDate( value ); this.calendarInstance.valueAsDate( value );
this.element.val( this._format( value ) ); this.element.val( this.calendarInstance._format( value ) );
} }
} else { } else {
return this._getParsedValue(); return this._getParsedValue();
@ -326,7 +317,7 @@ var widget = $.widget( "ui.datepicker", {
}, },
_getParsedValue: function() { _getParsedValue: function() {
return this._parse( this.element.val() ); return this.calendarInstance._parse( this.element.val() );
}, },
_setOption: function( key, value ) { _setOption: function( key, value ) {
@ -340,8 +331,7 @@ var widget = $.widget( "ui.datepicker", {
this.calendar.appendTo( this._appendTo() ); this.calendar.appendTo( this._appendTo() );
} }
if ( key === "locale" ) { if ( key === "locale" || key === "dateFormat" ) {
this._setLocale( value );
this.element.val( this.calendarInstance.value() ); this.element.val( this.calendarInstance.value() );
} }