Skip to content

Commit 455ed11

Browse files
committed
Datepicker: Improve localization handling, code style
1 parent 854e214 commit 455ed11

File tree

2 files changed

+46
-41
lines changed

2 files changed

+46
-41
lines changed

tests/unit/calendar/calendar_core.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ test( "base structure", function() {
6464
});
6565

6666
test( "Localization", function() {
67-
expect( 5 );
67+
expect( 10 );
6868

6969
var defaultLocale = Globalize.locale(),
7070
element = $( "#calendar" ),
@@ -102,6 +102,12 @@ test( "Localization", function() {
102102
testLocalization( "Init: " );
103103
element.calendar( "destroy" );
104104

105+
Globalize.locale( defaultLocale.locale );
106+
initCalendar();
107+
Globalize.locale( "de-DE" );
108+
element.calendar( "refresh" );
109+
testLocalization( "After init: " );
110+
105111
Globalize.locale( defaultLocale.locale );
106112
});
107113

ui/calendar.js

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ return $.widget( "ui.calendar", {
4343

4444
_create: function() {
4545
this.id = this.element.uniqueId().attr( "id" );
46+
this.labels = Globalize.translate( "datepicker" );
4647

4748
this.date = $.date( this.options.value, this.options.dateFormat ).select();
4849
this.date.eachDay = this.options.eachDay;
@@ -73,7 +74,7 @@ return $.widget( "ui.calendar", {
7374
"keydown .ui-calendar-calendar": "_handleKeydown"
7475
});
7576

76-
this.element.on( "mouseenter.calendar mouseleave.calendar", ".ui-calendar-header a, .ui-calendar-calendar a", function() {
77+
this.element.on( "mouseenter.calendar mouseleave.calendar", ".ui-calendar-header button, .ui-calendar-calendar a", function() {
7778
$( this ).toggleClass( "ui-state-hover" );
7879
});
7980

@@ -162,8 +163,6 @@ return $.widget( "ui.calendar", {
162163
})
163164
.html( pickerHtml );
164165

165-
this.element.find( "button" ).button();
166-
167166
this.grid = this.element.find( ".ui-calendar-calendar" );
168167
},
169168

@@ -207,41 +206,39 @@ return $.widget( "ui.calendar", {
207206

208207
_buildHeader: function() {
209208
return "<div class='ui-calendar-header ui-widget-header ui-helper-clearfix ui-corner-all'>" +
210-
this._buildPreviousLink() +
211-
this._buildNextLink() +
212-
this._buildTitlebar() +
213-
"</div>";
209+
this._buildPreviousLink() +
210+
this._buildNextLink() +
211+
this._buildTitlebar() +
212+
"</div>";
214213
},
215214

216215
_buildPreviousLink: function() {
217-
var labels = Globalize.translate( "datepicker" );
218-
219-
return "<button class='ui-calendar-prev ui-corner-all' title='" + labels.prevText + "'>" +
216+
return "<button class='ui-calendar-prev ui-corner-all' title='" +
217+
this.labels.prevText + "'>" +
220218
"<span class='ui-icon ui-icon-circle-triangle-w'>" +
221-
labels.prevText +
219+
this.labels.prevText +
222220
"</span>" +
223221
"</button>";
224222
},
225223

226224
_buildNextLink: function() {
227-
var labels = Globalize.translate( "datepicker" );
228-
229-
return "<button class='ui-calendar-next ui-corner-all' title='" + labels.nextText + "'>" +
225+
return "<button class='ui-calendar-next ui-corner-all' title='" +
226+
this.labels.nextText + "'>" +
230227
"<span class='ui-icon ui-icon-circle-triangle-e'>" +
231-
labels.nextText +
228+
this.labels.nextText +
232229
"</span>" +
233230
"</button>";
234231
},
235232

236233
_buildTitlebar: function() {
237-
var labels = Globalize.translate( "datepicker" );
238-
239234
return "<div role='header' id='" + this.id + "-title'>" +
240-
"<div id='" + this.id + "-month-lbl' class='ui-calendar-title'>" +
241-
this._buildTitle() +
242-
"</div>" +
243-
"<span class='ui-helper-hidden-accessible'>, " + labels.datePickerRole + "</span>" +
244-
"</div>";
235+
"<div id='" + this.id + "-month-lbl' class='ui-calendar-title'>" +
236+
this._buildTitle() +
237+
"</div>" +
238+
"<span class='ui-helper-hidden-accessible'>, " +
239+
this.labels.datePickerRole +
240+
"</span>" +
241+
"</div>";
245242
},
246243

247244
_buildTitle: function() {
@@ -255,36 +252,36 @@ return $.widget( "ui.calendar", {
255252

256253
_buildGrid: function() {
257254
return "<table class='ui-calendar-calendar' role='grid' aria-readonly='true' " +
258-
"aria-labelledby='" + this.id + "-month-lbl' tabindex='0' aria-activedescendant='" + this.id + "-" + this.date.day() + "'>" +
259-
this._buildGridHeading() +
260-
this._buildGridBody() +
261-
"</table>";
255+
"aria-labelledby='" + this.id + "-month-lbl' tabindex='0' " +
256+
"aria-activedescendant='" + this.id + "-" + this.date.day() + "'>" +
257+
this._buildGridHeading() +
258+
this._buildGridBody() +
259+
"</table>";
262260
},
263261

264262
_buildGridHeading: function() {
265263
var cells = "",
266264
i = 0,
267-
labels = Globalize.translate( "datepicker" ),
268265
weekDayLength = this.date.weekdays().length;
269266

270267
if ( this.options.showWeek ) {
271-
cells += "<th class='ui-calendar-week-col'>" + labels.weekHeader + "</th>";
268+
cells += "<th class='ui-calendar-week-col'>" + this.labels.weekHeader + "</th>";
272269
}
273270
for ( ; i < weekDayLength; i++ ) {
274271
cells += this._buildGridHeaderCell( this.date.weekdays()[ i ] );
275272
}
276273

277274
return "<thead role='presentation'>" +
278-
"<tr role='row'>" + cells + "</tr>" +
279-
"</thead>";
275+
"<tr role='row'>" + cells + "</tr>" +
276+
"</thead>";
280277
},
281278

282279
_buildGridHeaderCell: function( day ) {
283280
return "<th role='columnheader' abbr='" + day.fullname + "' aria-label='" + day.fullname + "'>" +
284-
"<span title='" + day.fullname + "'>" +
285-
day.shortname +
286-
"</span>" +
287-
"</th>";
281+
"<span title='" + day.fullname + "'>" +
282+
day.shortname +
283+
"</span>" +
284+
"</th>";
288285
},
289286

290287
_buildGridBody: function() {
@@ -339,7 +336,6 @@ return $.widget( "ui.calendar", {
339336

340337
_buildDayElement: function( day, selectable ) {
341338
var classes = [ "ui-state-default" ],
342-
labels = Globalize.translate( "datepicker" ),
343339
content = "";
344340

345341
if ( day === this.date && selectable ) {
@@ -364,18 +360,16 @@ return $.widget( "ui.calendar", {
364360
}
365361

366362
if ( day.today ) {
367-
content += "<span class='ui-helper-hidden-accessible'>, " + labels.currentText + "</span>";
363+
content += "<span class='ui-helper-hidden-accessible'>, " + this.labels.currentText + "</span>";
368364
}
369365

370366
return content;
371367
},
372368

373369
_buildButtons: function() {
374-
var labels = Globalize.translate( "datepicker" );
375-
376370
return "<div class='ui-calendar-buttonpane ui-widget-content'>" +
377-
"<button class='ui-calendar-current'>" + labels.currentText + "</button>" +
378-
"</div>";
371+
"<button class='ui-calendar-current'>" + this.labels.currentText + "</button>" +
372+
"</div>";
379373
},
380374

381375
// Refreshing the entire calendar during interaction confuses screen readers, specifically
@@ -384,13 +378,18 @@ return $.widget( "ui.calendar", {
384378
// with the prev and next links would cause loss of focus issues because the links being
385379
// interacted with will disappear while focused.
386380
refresh: function() {
381+
this.labels = Globalize.translate( "datepicker" );
387382

388383
// Determine which day gridcell to focus after refresh
389384
// TODO: Prevent disabled cells from being focused
390385
if ( this.options.numberOfMonths === 1 ) {
391386
this.grid = $( this._buildGrid() );
392387
this.element.find( ".ui-calendar-title" ).html( this._buildTitle() );
393388
this.element.find( ".ui-calendar-calendar" ).replaceWith( this.grid );
389+
$( ".ui-calendar-prev", this.element ).attr( "title", this.labels.prevText )
390+
.children().html( this.labels.prevText );
391+
$( ".ui-calendar-next", this.element ).attr( "title", this.labels.nextText )
392+
.children().html( this.labels.nextText );
394393
} else {
395394
this._refreshMultiplePicker();
396395
}

0 commit comments

Comments
 (0)