Skip to content

Commit 4e65e51

Browse files
committed
(fix) Datepicker: Use Globalize 1.0.0
- user cannot provide locale: {fn1: ..., fn2:...} with all the formatters and parsers;
1 parent 62f193c commit 4e65e51

File tree

3 files changed

+51
-63
lines changed

3 files changed

+51
-63
lines changed

ui/calendar.js

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ return $.widget( "ui.calendar", {
6060

6161
this._setLocale( this.options.locale );
6262

63-
this.date = new $.ui.calendarDate( this.options.value, this.options.locale ).select();
63+
this.date = new $.ui.calendarDate( this.options.value, this._calendarDateOptions ).select();
6464
this.date.eachDay = this.options.eachDay;
6565

6666
this._on( this.element, {
@@ -158,45 +158,37 @@ return $.widget( "ui.calendar", {
158158
},
159159

160160
_setLocale: function( locale ) {
161-
var globalize;
162-
163-
if ( typeof locale === "string" ) {
164-
globalize = new Globalize( locale );
165-
locale = {
166-
format: function( date ) {
167-
return globalize.formatDate( date, { date: "short" } );
168-
},
169-
parse: function( stringDate ) {
170-
return globalize.parseDate( stringDate, { date: "short" } );
171-
}
172-
};
173-
}
161+
var globalize = new Globalize( locale );
174162

175-
if ( !locale.firstDay ) {
176-
globalize = globalize || new Globalize( locale._locale );
177-
$.extend( locale, {
178-
firstDay: globalize.cldr.supplemental.weekData.firstDay(),
179-
formatWeekdayShort: function( date ) {
163+
this._format = function( date ) {
164+
return globalize.formatDate( date, { date: "short" } );
165+
};
180166

181-
// Return the short weekday if its length is < 3. Otherwise, its narrow form.
182-
var shortWeekday = globalize.formatDate( date, { pattern: "EEEEEE" } );
183-
return shortWeekday.length > 3 ?
184-
globalize.formatDate( date, { pattern: "EEEEE" } ) :
185-
shortWeekday;
186-
},
187-
formatWeekdayFull: function( date ) {
188-
return globalize.formatDate( date, { pattern: "EEEE" } );
189-
},
190-
formatMonth: function( date ) {
191-
return globalize.formatDate( date, { pattern: "MMMM" } );
192-
},
193-
formatWeekOfYear: function( date ) {
194-
return globalize.formatDate( date, { pattern: "w" } );
195-
}
196-
});
197-
}
167+
this._parse = function( stringDate ) {
168+
return globalize.parseDate( stringDate, { date: "short" } );
169+
};
170+
171+
this._calendarDateOptions = {
172+
firstDay: globalize.cldr.supplemental.weekData.firstDay(),
173+
formatWeekdayShort: function( date ) {
198174

199-
this.options.locale = locale;
175+
// Return the short weekday if its length is < 3. Otherwise, its narrow form.
176+
var shortWeekday = globalize.formatDate( date, { pattern: "EEEEEE" } );
177+
return shortWeekday.length > 3 ?
178+
globalize.formatDate( date, { pattern: "EEEEE" } ) :
179+
shortWeekday;
180+
},
181+
formatWeekdayFull: function( date ) {
182+
return globalize.formatDate( date, { pattern: "EEEE" } );
183+
},
184+
formatMonth: function( date ) {
185+
return globalize.formatDate( date, { pattern: "MMMM" } );
186+
},
187+
formatWeekOfYear: function( date ) {
188+
return globalize.formatDate( date, { pattern: "w" } );
189+
},
190+
parse: this._parse
191+
};
200192
},
201193

202194
_createCalendar: function() {
@@ -527,11 +519,10 @@ return $.widget( "ui.calendar", {
527519
},
528520

529521
value: function( value ) {
530-
var locale = this.options.locale;
531522
if ( arguments.length ) {
532-
this._setOption( "value", locale.parse( value ) );
523+
this._setOption( "value", this._parse( value ) );
533524
} else {
534-
return locale.format( this.option( "value" ) );
525+
return this._format( this.option( "value" ) );
535526
}
536527
},
537528

@@ -615,7 +606,7 @@ return $.widget( "ui.calendar", {
615606

616607
if ( key === "locale" ) {
617608
this._setLocale( value );
618-
this.date.setAttributes( this.options.locale );
609+
this.date.setAttributes( this._calendarDateOptions );
619610
this.refresh();
620611
}
621612

ui/calendar/date.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var _Date,
3838

3939
_Date = function( date, attributes ) {
4040
if ( !( this instanceof _Date ) ) {
41-
return new _Date( date, options );
41+
return new _Date( date, attributes );
4242
}
4343

4444
this.setAttributes( attributes );

ui/datepicker.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ widget = $.widget( "ui.datepicker", {
5858
this._setLocale( this.options.locale );
5959

6060
if ( $.type( this.options.max ) === "string" ) {
61-
this.options.max = this.options.locale.parseYMD( this.options.max );
61+
this.options.max = this._parseYMD( this.options.max );
6262
}
6363
if ( $.type( this.options.min ) === "string" ) {
64-
this.options.min = this.options.locale.parseYMD( this.options.min );
64+
this.options.min = this._parseYMD( this.options.min );
6565
}
6666

6767
this._createCalendar();
@@ -279,22 +279,19 @@ widget = $.widget( "ui.datepicker", {
279279
},
280280

281281
_setLocale: function( locale ) {
282-
if ( typeof locale === "string" ) {
283-
globalize = new Globalize( locale );
284-
locale = {
285-
_locale: locale,
286-
format: function( date ) {
287-
return globalize.formatDate( date, { date: "short" } );
288-
},
289-
parse: function( stringDate ) {
290-
return globalize.parseDate( stringDate, { date: "short" } );
291-
},
292-
parseYMD: function( stringDate ) {
293-
return globalize.parseDate( stringDate, { pattern: "yyyy-MM-dd" } );
294-
}
295-
};
296-
}
297-
this.options.locale = locale;
282+
var globalize = new Globalize( locale );
283+
284+
this._format = function( date ) {
285+
return globalize.formatDate( date, { date: "short" } );
286+
};
287+
288+
this._parse = function( stringDate ) {
289+
return globalize.parseDate( stringDate, { date: "short" } );
290+
};
291+
292+
this._parseYMD = function( stringDate ) {
293+
return globalize.parseDate( stringDate, { pattern: "yyyy-MM-dd" } );
294+
};
298295
},
299296

300297
_buildPosition: function() {
@@ -303,7 +300,7 @@ widget = $.widget( "ui.datepicker", {
303300

304301
value: function( value ) {
305302
if ( arguments.length ) {
306-
this.valueAsDate( this.options.locale.parse( value ) );
303+
this.valueAsDate( this._parse( value ) );
307304
} else {
308305
return this._getParsedValue() ? this.element.val() : null;
309306
}
@@ -313,7 +310,7 @@ widget = $.widget( "ui.datepicker", {
313310
if ( arguments.length ) {
314311
if ( this.calendarInstance._isValid( value ) ) {
315312
this.calendarInstance.valueAsDate( value );
316-
this.element.val( this.options.locale.format( value ) );
313+
this.element.val( this._format( value ) );
317314
}
318315
} else {
319316
return this._getParsedValue();
@@ -335,7 +332,7 @@ widget = $.widget( "ui.datepicker", {
335332
},
336333

337334
_getParsedValue: function() {
338-
return this.options.locale.parse( this.element.val() );
335+
return this._parse( this.element.val() );
339336
},
340337

341338
_setOption: function( key, value ) {

0 commit comments

Comments
 (0)