Skip to content

Commit 34ee094

Browse files
committed
Datepicker: Replacing grid, title, and datepicker templates with builder methods.
1 parent 999e89b commit 34ee094

File tree

1 file changed

+163
-22
lines changed

1 file changed

+163
-22
lines changed

datepicker-rewrite/picker.js

Lines changed: 163 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -255,19 +255,11 @@ $.widget( "ui.datepicker", {
255255

256256
return element;
257257
},
258-
// TODO replace with builder methods
259258
_createTmpl: function() {
260259
this.date.refresh();
261260

262-
$( this.options.tmpl ).tmpl( {
263-
date: this.date,
264-
labels: Globalize.localize( "datepicker" ),
265-
instance: {
266-
id: this.id,
267-
focusedDay: this.date.day()
268-
}
269-
}).appendTo( this.picker )
270-
.find( "button" ).button().end();
261+
this._createDatepicker();
262+
this.picker.find( "button" ).button();
271263

272264
if ( this.inline ) {
273265
this.picker.children().addClass( "ui-datepicker-inline" );
@@ -276,6 +268,163 @@ $.widget( "ui.datepicker", {
276268
this.picker.find( ".ui-datepicker" ).css( "display", "block" );
277269
this.grid = this.picker.find( ".ui-datepicker-calendar" );
278270
},
271+
_createDatepicker: function() {
272+
$( "<div>" )
273+
.addClass( "ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all" )
274+
.attr({
275+
role: "region",
276+
"aria-labelledby": this.id + "-title"
277+
})
278+
.html( this._buildHeader() + this._buildGrid() + this._buildButtons() )
279+
.appendTo( this.picker );
280+
},
281+
_buildHeader: function() {
282+
return "<div class='ui-datepicker-header ui-widget-header ui-helper-clearfix ui-corner-all'>" +
283+
this._buildPreviousLink() +
284+
this._buildNextLink() +
285+
this._buildTitlebar() +
286+
"</div>";
287+
},
288+
_buildPreviousLink: function() {
289+
var labels = Globalize.localize( "datepicker" );
290+
return "<a href='#' class='ui-datepicker-prev ui-corner-all' " +
291+
"title='" + labels.prevText + "'>" +
292+
"<span class='ui-icon ui-icon-circle-triangle-w'>" +
293+
labels.prevText +
294+
"</span>" +
295+
"</a>";
296+
},
297+
_buildNextLink: function() {
298+
var labels = Globalize.localize( "datepicker" );
299+
return "<a href='#' class='ui-datepicker-next ui-corner-all' " +
300+
"title='" + labels.nextText + "'>" +
301+
"<span class='ui-icon ui-icon-circle-triangle-e'>" +
302+
labels.nextText +
303+
"</span>" +
304+
"</a>";
305+
},
306+
_buildTitlebar: function() {
307+
var labels = Globalize.localize( "datepicker" );
308+
return "<div role='header' id='" + this.id + "-title'>" +
309+
"<div id='" + this.id + "-month-lbl' class='ui-datepicker-title'>" +
310+
this._buildTitle() +
311+
"</div>" +
312+
"<span class='ui-helper-hidden-accessible'>, " + labels.datePickerRole + "</span>" +
313+
"</div>";
314+
},
315+
_buildTitle: function() {
316+
return "<span class='ui-datepicker-month'>" +
317+
this.date.monthName() +
318+
"</span>" +
319+
"<span class='ui-datepicker-year'>" +
320+
this.date.year() +
321+
"</span>";
322+
},
323+
_buildGrid: function() {
324+
return "<table class='ui-datepicker-calendar' role='grid' aria-readonly='true' " +
325+
"aria-labelledby='" + this.id + "-month-lbl' tabindex='0' aria-activedescendant='" + this.id + "-" + this.date.day() + "'>" +
326+
this._buildGridHeading() +
327+
this._buildGridBody() +
328+
"</table>";
329+
},
330+
_buildGridHeading: function() {
331+
var cells = "",
332+
i = 0;
333+
for ( i; i < this.date.weekdays().length; i++ ) {
334+
cells += this._buildGridHeaderCell( this.date.weekdays()[i] );
335+
}
336+
return "<thead role='presentation'>" +
337+
"<tr role='row'>" + cells + "</tr>" +
338+
"</thead>";
339+
},
340+
_buildGridHeaderCell: function( day ) {
341+
return "<th role='columnheader' abbr='" + day.fullname + "' aria-label='" + day.fullname + "'>" +
342+
"<span title='" + day.fullname + "'>" +
343+
day.shortname +
344+
"</span>" +
345+
"</th>";
346+
},
347+
_buildGridBody: function() {
348+
var rows = "",
349+
i = 0;
350+
for ( i; i < this.date.days().length; i++ ) {
351+
rows += this._buildWeekRow( this.date.days()[i] );
352+
}
353+
return "<tbody role='presentation'>" + rows + "</tbody>";
354+
},
355+
_buildWeekRow: function( week ) {
356+
var cells = "",
357+
i = 0;
358+
for ( i; i < week.days.length; i++ ) {
359+
cells += this._buildDayCell( week.days[i] );
360+
}
361+
return "<tr role='row'>" + cells + "</tr>";
362+
},
363+
_buildDayCell: function( day ) {
364+
var contents = "";
365+
idAttribute = day.render ? ( "id=" + this.id + "-" + day.date ) : "",
366+
ariaSelectedAttribute = "aria-selected=" + ( day.current ? "true" : "false" ),
367+
ariaDisabledAttribute = day.selectable ? "" : "aria-disabled=true";
368+
369+
if ( day.render ) {
370+
if ( day.selectable ) {
371+
contents = this._buildDayLink( day );
372+
} else {
373+
contents = this._buildDayDisplay( day );
374+
}
375+
}
376+
377+
return "<td role='gridcell' " + idAttribute + " " + ariaSelectedAttribute + " " + ariaDisabledAttribute + ">" +
378+
contents +
379+
"</td>";
380+
},
381+
_buildDayLink: function( day ) {
382+
var link,
383+
classes = [ "ui-state-default" ],
384+
labels = Globalize.localize( "datepicker" );
385+
386+
if ( day.date == this.date.day() ) {
387+
classes.push( "ui-state-focus" )
388+
}
389+
if ( day.current ) {
390+
classes.push( "ui-state-active" );
391+
}
392+
if ( day.today ) {
393+
classes.push( "ui-state-highlight" );
394+
}
395+
if ( day.extraClasses ) {
396+
classes.push( day.extraClasses.split( "" ) );
397+
}
398+
399+
link = "<a href='#' tabindex='-1' data-timestamp='" + day.timestamp + "' class='" + classes.join( " " ) + "'>" +
400+
day.date + "</a>";
401+
if ( day.today ) {
402+
link += "<span class='ui-helper-hidden-accessible'>, " + labels.currentText + "</span>";
403+
}
404+
return link;
405+
},
406+
_buildDayDisplay: function( day ) {
407+
var classes = [];
408+
if ( day.current ) {
409+
classes.push( "ui-state-active" );
410+
}
411+
if ( day.today ) {
412+
classes.push( "ui-state-highlight" );
413+
}
414+
if ( day.extraClasses ) {
415+
classes.push( day.extraClasses.split( "" ) );
416+
}
417+
418+
return "<span class='" + classes.join( "" ) + "'>" +
419+
day.date + "</span>";
420+
},
421+
_buildButtons: function() {
422+
var labels = Globalize.localize( "datepicker" );
423+
return "<div class='ui-datepicker-buttonpane ui-widget-content'>" +
424+
"<button class='ui-datepicker-current'>" + labels.currentText + "</button>" +
425+
"<button class='ui-datepicker-close'>" + labels.closeText + "</button>" +
426+
"</div>";
427+
},
279428
_focusTrigger: function( event ) {
280429
suppressExpandOnFocus = true;
281430
this.element.focus();
@@ -284,19 +433,11 @@ $.widget( "ui.datepicker", {
284433
//determine which day gridcell to focus after refresh
285434
//TODO: Prevent disabled cells from being focused
286435
this.date.refresh();
287-
$(".ui-datepicker-title", this.picker).html(
288-
$("#ui-datepicker-title-tmpl").tmpl( {
289-
date: this.date
290-
}));
291-
var newGrid = $( this.options.gridTmpl ).tmpl( {
292-
date: this.date,
293-
labels: Globalize.localize( "datepicker" ),
294-
instance: {
295-
id: this.id,
296-
focusedDay: this.date.day()
297-
}
298-
});
436+
437+
$( ".ui-datepicker-title", this.picker ).html( this._buildTitle() );
438+
299439
// TODO fix me
440+
var newGrid = $( this._buildGrid() );
300441
this.grid = this.grid.replaceWith( newGrid );
301442
this.grid = newGrid;
302443
},

0 commit comments

Comments
 (0)