(fix) Datepicker: Use Globalize 1.0.0

- user cannot provide locale: {fn1: ..., fn2:...} with all the
  formatters and parsers
This commit is contained in:
Rafael Xavier de Souza 2014-09-04 15:47:48 -03:00 committed by Felix Nagel
parent b6d7436756
commit 5d044dd88c
3 changed files with 33 additions and 46 deletions

View File

@ -71,8 +71,7 @@ return $.widget( "ui.calendar", {
this._setLocale( this.options.locale ); this._setLocale( this.options.locale );
this.date = new $.ui.calendarDate( this.options.value, this.options.locale ); this.date = new $.ui.calendarDate( this.options.value, this._calendarDateOptions );
this.date = $.date( this.options.value, this.options.dateFormat );
this.viewDate = this.date.clone(); this.viewDate = this.date.clone();
this.viewDate.eachDay = this.options.eachDay; this.viewDate.eachDay = this.options.eachDay;
@ -178,23 +177,17 @@ return $.widget( "ui.calendar", {
}, },
_setLocale: function( locale ) { _setLocale: function( locale ) {
var globalize; var globalize = new Globalize( locale );
if ( typeof locale === "string" ) { this._format = function( date ) {
globalize = new Globalize( locale );
locale = {
format: function( date ) {
return globalize.formatDate( date, { date: "short" } ); return globalize.formatDate( date, { date: "short" } );
},
parse: function( stringDate ) {
return globalize.parseDate( stringDate, { date: "short" } );
}
}; };
}
if ( !locale.firstDay ) { this._parse = function( stringDate ) {
globalize = globalize || new Globalize( locale._locale ); return globalize.parseDate( stringDate, { date: "short" } );
$.extend( locale, { };
this._calendarDateOptions = {
firstDay: globalize.cldr.supplemental.weekData.firstDay(), firstDay: globalize.cldr.supplemental.weekData.firstDay(),
formatWeekdayShort: function( date ) { formatWeekdayShort: function( date ) {
@ -212,11 +205,9 @@ return $.widget( "ui.calendar", {
}, },
formatWeekOfYear: function( date ) { formatWeekOfYear: function( date ) {
return globalize.formatDate( date, { pattern: "w" } ); return globalize.formatDate( date, { pattern: "w" } );
} },
}); parse: this._parse
} };
this.options.locale = locale;
}, },
_createCalendar: function() { _createCalendar: function() {
@ -562,11 +553,10 @@ return $.widget( "ui.calendar", {
}, },
value: function( value ) { value: function( value ) {
var locale = this.options.locale;
if ( arguments.length ) { if ( arguments.length ) {
this.valueAsDate( locale.parse( value ) ); this.valueAsDate( locale.parse( value ) );
} else { } else {
return locale.format( this.option( "value" ) ); return this._format( this.option( "value" ) );
} }
}, },
@ -658,7 +648,7 @@ return $.widget( "ui.calendar", {
if ( key === "locale" ) { if ( key === "locale" ) {
this._setLocale( value ); this._setLocale( value );
this.date.setAttributes( this.options.locale ); this.date.setAttributes( this._calendarDateOptions );
this.refresh(); this.refresh();
} }
} }

View File

@ -38,7 +38,7 @@ var _Date,
_Date = function( date, attributes ) { _Date = function( date, attributes ) {
if ( !( this instanceof _Date ) ) { if ( !( this instanceof _Date ) ) {
return new _Date( date, options ); return new _Date( date, attributes );
} }
this.setAttributes( attributes ); this.setAttributes( attributes );

View File

@ -61,10 +61,10 @@ var widget = $.widget( "ui.datepicker", {
this._setLocale( this.options.locale ); this._setLocale( this.options.locale );
if ( $.type( this.options.max ) === "string" ) { if ( $.type( this.options.max ) === "string" ) {
this.options.max = this.options.locale.parseYMD( this.options.max ); this.options.max = this._parseYMD( this.options.max );
} }
if ( $.type( this.options.min ) === "string" ) { if ( $.type( this.options.min ) === "string" ) {
this.options.min = this.options.locale.parseYMD( this.options.min ); this.options.min = this._parseYMD( this.options.min );
} }
this._createCalendar(); this._createCalendar();
@ -282,22 +282,19 @@ var widget = $.widget( "ui.datepicker", {
}, },
_setLocale: function( locale ) { _setLocale: function( locale ) {
if ( typeof locale === "string" ) { var globalize = new Globalize( locale );
globalize = new Globalize( locale );
locale = { this._format = function( date ) {
_locale: locale,
format: function( date ) {
return globalize.formatDate( date, { date: "short" } ); return globalize.formatDate( date, { date: "short" } );
},
parse: function( stringDate ) {
return globalize.parseDate( stringDate, { date: "short" } );
},
parseYMD: function( stringDate ) {
return globalize.parseDate( stringDate, { pattern: "yyyy-MM-dd" } );
}
}; };
}
this.options.locale = locale; this._parse = function( stringDate ) {
return globalize.parseDate( stringDate, { date: "short" } );
};
this._parseYMD = function( stringDate ) {
return globalize.parseDate( stringDate, { pattern: "yyyy-MM-dd" } );
};
}, },
_buildPosition: function() { _buildPosition: function() {
@ -306,7 +303,7 @@ var widget = $.widget( "ui.datepicker", {
value: function( value ) { value: function( value ) {
if ( arguments.length ) { if ( arguments.length ) {
this.valueAsDate( this.options.locale.parse( value ) ); this.valueAsDate( this._parse( value ) );
} else { } else {
return this._getParsedValue() ? this.element.val() : null; return this._getParsedValue() ? this.element.val() : null;
} }
@ -316,7 +313,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.options.locale.format( value ) ); this.element.val( this._format( value ) );
} }
} else { } else {
return this._getParsedValue(); return this._getParsedValue();
@ -338,7 +335,7 @@ var widget = $.widget( "ui.datepicker", {
}, },
_getParsedValue: function() { _getParsedValue: function() {
return this.options.locale.parse( this.element.val() ); return this._parse( this.element.val() );
}, },
_setOption: function( key, value ) { _setOption: function( key, value ) {