| 1 |
define([ |
| 2 |
"dojo/_base/array", |
| 3 |
"dojo/_base/declare", |
| 4 |
"dojo/cldr/supplemental", |
| 5 |
"dojo/date", |
| 6 |
"dojo/date/locale", |
| 7 |
"dojo/date/stamp", |
| 8 |
"dojo/dom", |
| 9 |
"dojo/dom-class", |
| 10 |
"dojo/dom-attr", |
| 11 |
"dojo/_base/lang", |
| 12 |
"dojo/on", |
| 13 |
"dojo/sniff", |
| 14 |
"dojo/string", |
| 15 |
"./_WidgetBase", |
| 16 |
"./_TemplatedMixin", |
| 17 |
"dojo/text!./templates/Calendar.html", |
| 18 |
"./a11yclick", |
| 19 |
"./hccss" |
| 20 |
], function(array, declare, cldrSupplemental, date, locale, stamp, dom, domClass, domAttr, lang, on, has, string, _WidgetBase, _TemplatedMixin, template){ |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
var CalendarLite = declare("dijit.CalendarLite", [_WidgetBase, _TemplatedMixin], { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
templateString: template, |
| 48 |
|
| 49 |
|
| 50 |
dowTemplateString: '<th class="dijitReset dijitCalendarDayLabelTemplate" role="columnheader" scope="col"><span class="dijitCalendarDayLabel">${d}</span></th>', |
| 51 |
|
| 52 |
|
| 53 |
dateTemplateString: '<td class="dijitReset" role="gridcell" data-dojo-attach-point="dateCells"><span class="dijitCalendarDateLabel" data-dojo-attach-point="dateLabels"></span></td>', |
| 54 |
weekTemplateString: '<tr class="dijitReset dijitCalendarWeekTemplate" role="row">${d}${d}${d}${d}${d}${d}${d}</tr>', |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
value: new Date(""), |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
datePackage: "", |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
dayWidth: "narrow", |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
tabIndex: "0", |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
dayOffset: -1, |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
currentFocus: new Date(), |
| 88 |
|
| 89 |
|
| 90 |
_setSummaryAttr: "gridNode", |
| 91 |
|
| 92 |
baseClass: "dijitCalendar dijitCalendarLite", |
| 93 |
|
| 94 |
_isValidDate: function( value){ |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
return value && !isNaN(value) && typeof value == "object" && |
| 101 |
value.toString() != this.constructor.prototype.value.toString(); |
| 102 |
}, |
| 103 |
|
| 104 |
_getValueAttr: function(){ |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
var storedVal = this._get("value"); |
| 110 |
if(storedVal && !isNaN(storedVal)){ |
| 111 |
var value = new this.dateClassObj(storedVal); |
| 112 |
value.setHours(0, 0, 0, 0); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
if(value.getDate() < storedVal.getDate()){ |
| 117 |
value = this.dateModule.add(value, "hour", 1); |
| 118 |
} |
| 119 |
return value; |
| 120 |
}else{ |
| 121 |
return null; |
| 122 |
} |
| 123 |
}, |
| 124 |
|
| 125 |
_setValueAttr: function( value, priorityChange){ |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
if(typeof value == "string"){ |
| 136 |
value = stamp.fromISOString(value); |
| 137 |
} |
| 138 |
value = this._patchDate(value); |
| 139 |
|
| 140 |
if(this._isValidDate(value) && !this.isDisabledDate(value, this.lang)){ |
| 141 |
this._set("value", value); |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
this.set("currentFocus", value); |
| 146 |
|
| 147 |
|
| 148 |
this._markSelectedDates([value]); |
| 149 |
|
| 150 |
if(this._created && (priorityChange || typeof priorityChange == "undefined")){ |
| 151 |
this.onChange(this.get('value')); |
| 152 |
} |
| 153 |
}else{ |
| 154 |
|
| 155 |
this._set("value", null); |
| 156 |
this._markSelectedDates([]); |
| 157 |
} |
| 158 |
}, |
| 159 |
|
| 160 |
_patchDate: function( value){ |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
if(value || value === 0){ |
| 165 |
value = new this.dateClassObj(value); |
| 166 |
value.setHours(1, 0, 0, 0); |
| 167 |
} |
| 168 |
return value; |
| 169 |
}, |
| 170 |
|
| 171 |
_setText: function(node, text){ |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
while(node.firstChild){ |
| 178 |
node.removeChild(node.firstChild); |
| 179 |
} |
| 180 |
node.appendChild(node.ownerDocument.createTextNode(text)); |
| 181 |
}, |
| 182 |
|
| 183 |
_populateGrid: function(){ |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
var month = new this.dateClassObj(this.currentFocus); |
| 191 |
month.setDate(1); |
| 192 |
month = this._patchDate(month); |
| 193 |
|
| 194 |
var firstDay = month.getDay(), |
| 195 |
daysInMonth = this.dateModule.getDaysInMonth(month), |
| 196 |
daysInPreviousMonth = this.dateModule.getDaysInMonth(this.dateModule.add(month, "month", -1)), |
| 197 |
today = new this.dateClassObj(), |
| 198 |
dayOffset = this.dayOffset >= 0 ? this.dayOffset : cldrSupplemental.getFirstDayOfWeek(this.lang); |
| 199 |
if(dayOffset > firstDay){ |
| 200 |
dayOffset -= 7; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
if(!this.summary){ |
| 205 |
var monthNames = this.dateLocaleModule.getNames('months', 'wide', 'standAlone', this.lang, month) |
| 206 |
this.gridNode.setAttribute("summary", monthNames[month.getMonth()]); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
this._date2cell = {}; |
| 211 |
|
| 212 |
|
| 213 |
array.forEach(this.dateCells, function(template, idx){ |
| 214 |
var i = idx + dayOffset; |
| 215 |
var date = new this.dateClassObj(month), |
| 216 |
number, clazz = "dijitCalendar", adj = 0; |
| 217 |
|
| 218 |
if(i < firstDay){ |
| 219 |
number = daysInPreviousMonth - firstDay + i + 1; |
| 220 |
adj = -1; |
| 221 |
clazz += "Previous"; |
| 222 |
}else if(i >= (firstDay + daysInMonth)){ |
| 223 |
number = i - firstDay - daysInMonth + 1; |
| 224 |
adj = 1; |
| 225 |
clazz += "Next"; |
| 226 |
}else{ |
| 227 |
number = i - firstDay + 1; |
| 228 |
clazz += "Current"; |
| 229 |
} |
| 230 |
|
| 231 |
if(adj){ |
| 232 |
date = this.dateModule.add(date, "month", adj); |
| 233 |
} |
| 234 |
date.setDate(number); |
| 235 |
|
| 236 |
if(!this.dateModule.compare(date, today, "date")){ |
| 237 |
clazz = "dijitCalendarCurrentDate " + clazz; |
| 238 |
} |
| 239 |
|
| 240 |
if(this.isDisabledDate(date, this.lang)){ |
| 241 |
clazz = "dijitCalendarDisabledDate " + clazz; |
| 242 |
template.setAttribute("aria-disabled", "true"); |
| 243 |
}else{ |
| 244 |
clazz = "dijitCalendarEnabledDate " + clazz; |
| 245 |
template.removeAttribute("aria-disabled"); |
| 246 |
template.setAttribute("aria-selected", "false"); |
| 247 |
} |
| 248 |
|
| 249 |
var clazz2 = this.getClassForDate(date, this.lang); |
| 250 |
if(clazz2){ |
| 251 |
clazz = clazz2 + " " + clazz; |
| 252 |
} |
| 253 |
|
| 254 |
template.className = clazz + "Month dijitCalendarDateTemplate"; |
| 255 |
|
| 256 |
|
| 257 |
var dateVal = date.valueOf(); |
| 258 |
this._date2cell[dateVal] = template; |
| 259 |
template.dijitDateValue = dateVal; |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
var localizedDate = date.getDateLocalized ? date.getDateLocalized(this.lang) : date.getDate() |
| 264 |
this._setText(this.dateLabels[idx], localizedDate); |
| 265 |
domAttr.set(template, 'aria-label', locale.format(date, { |
| 266 |
selector: 'date', |
| 267 |
formatLength: 'long' |
| 268 |
})); |
| 269 |
}, this); |
| 270 |
}, |
| 271 |
|
| 272 |
_populateControls: function(){ |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
var month = new this.dateClassObj(this.currentFocus); |
| 279 |
month.setDate(1); |
| 280 |
|
| 281 |
|
| 282 |
this.monthWidget.set("month", month); |
| 283 |
|
| 284 |
var y = month.getFullYear() - 1; |
| 285 |
var d = new this.dateClassObj(); |
| 286 |
array.forEach(["previous", "current", "next"], function(name){ |
| 287 |
d.setFullYear(y++); |
| 288 |
this._setText(this[name + "YearLabelNode"], |
| 289 |
this.dateLocaleModule.format(d, {selector: 'year', locale: this.lang})); |
| 290 |
}, this); |
| 291 |
}, |
| 292 |
|
| 293 |
goToToday: function(){ |
| 294 |
|
| 295 |
|
| 296 |
this.set('value', new this.dateClassObj()); |
| 297 |
}, |
| 298 |
|
| 299 |
constructor: function(params ){ |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
this.dateModule = params.datePackage ? lang.getObject(params.datePackage, false) : date; |
| 310 |
this.dateClassObj = this.dateModule.Date || Date; |
| 311 |
this.dateLocaleModule = params.datePackage ? lang.getObject(params.datePackage + ".locale", false) : locale; |
| 312 |
}, |
| 313 |
|
| 314 |
_createMonthWidget: function(){ |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
return CalendarLite._MonthWidget({ |
| 319 |
id: this.id + "_mddb", |
| 320 |
lang: this.lang, |
| 321 |
dateLocaleModule: this.dateLocaleModule |
| 322 |
}, this.monthNode); |
| 323 |
}, |
| 324 |
|
| 325 |
buildRendering: function(){ |
| 326 |
|
| 327 |
var d = this.dowTemplateString, |
| 328 |
dayNames = this.dateLocaleModule.getNames('days', this.dayWidth, 'standAlone', this.lang), |
| 329 |
dayOffset = this.dayOffset >= 0 ? this.dayOffset : cldrSupplemental.getFirstDayOfWeek(this.lang); |
| 330 |
this.dayCellsHtml = string.substitute([d, d, d, d, d, d, d].join(""), {d: ""}, function(){ |
| 331 |
return dayNames[dayOffset++ % 7]; |
| 332 |
}); |
| 333 |
|
| 334 |
|
| 335 |
var r = string.substitute(this.weekTemplateString, {d: this.dateTemplateString}); |
| 336 |
this.dateRowsHtml = [r, r, r, r, r, r].join(""); |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
this.dateCells = []; |
| 341 |
this.dateLabels = []; |
| 342 |
this.inherited(arguments); |
| 343 |
|
| 344 |
dom.setSelectable(this.domNode, false); |
| 345 |
|
| 346 |
var dateObj = new this.dateClassObj(this.currentFocus); |
| 347 |
|
| 348 |
this.monthWidget = this._createMonthWidget(); |
| 349 |
|
| 350 |
this.set('currentFocus', dateObj, false); |
| 351 |
}, |
| 352 |
|
| 353 |
postCreate: function(){ |
| 354 |
this.inherited(arguments); |
| 355 |
this._connectControls(); |
| 356 |
}, |
| 357 |
|
| 358 |
_connectControls: function(){ |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
var connect = lang.hitch(this, function(nodeProp, part, amount){ |
| 365 |
this[nodeProp].dojoClick = true; |
| 366 |
return on(this[nodeProp], "click", lang.hitch(this, function(){ |
| 367 |
this._setCurrentFocusAttr(this.dateModule.add(this.currentFocus, part, amount)); |
| 368 |
})); |
| 369 |
}); |
| 370 |
|
| 371 |
this.own( |
| 372 |
connect("incrementMonth", "month", 1), |
| 373 |
connect("decrementMonth", "month", -1), |
| 374 |
connect("nextYearLabelNode", "year", 1), |
| 375 |
connect("previousYearLabelNode", "year", -1) |
| 376 |
); |
| 377 |
}, |
| 378 |
|
| 379 |
_setCurrentFocusAttr: function( date, forceFocus){ |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
var oldFocus = this.currentFocus, |
| 390 |
oldCell = this._getNodeByDate(oldFocus); |
| 391 |
date = this._patchDate(date); |
| 392 |
|
| 393 |
this._set("currentFocus", date); |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
if(!this._date2cell || this.dateModule.difference(oldFocus, date, "month") != 0){ |
| 398 |
this._populateGrid(); |
| 399 |
this._populateControls(); |
| 400 |
this._markSelectedDates([this.value]); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
var newCell = this._getNodeByDate(date); |
| 405 |
newCell.setAttribute("tabIndex", this.tabIndex); |
| 406 |
if(this.focused || forceFocus){ |
| 407 |
newCell.focus(); |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
if(oldCell && oldCell != newCell){ |
| 412 |
if(has("webkit")){ |
| 413 |
oldCell.setAttribute("tabIndex", "-1"); |
| 414 |
}else{ |
| 415 |
oldCell.removeAttribute("tabIndex"); |
| 416 |
} |
| 417 |
} |
| 418 |
}, |
| 419 |
|
| 420 |
focus: function(){ |
| 421 |
|
| 422 |
|
| 423 |
this._setCurrentFocusAttr(this.currentFocus, true); |
| 424 |
}, |
| 425 |
|
| 426 |
_onDayClick: function( evt){ |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
evt.stopPropagation(); |
| 432 |
evt.preventDefault(); |
| 433 |
for(var node = evt.target; node && !node.dijitDateValue && node.dijitDateValue !== 0; node = node.parentNode){ |
| 434 |
; |
| 435 |
} |
| 436 |
if(node && !domClass.contains(node, "dijitCalendarDisabledDate")){ |
| 437 |
this.set('value', node.dijitDateValue); |
| 438 |
} |
| 439 |
}, |
| 440 |
|
| 441 |
_getNodeByDate: function( value){ |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
value = this._patchDate(value); |
| 446 |
return value && this._date2cell ? this._date2cell[value.valueOf()] : null; |
| 447 |
}, |
| 448 |
|
| 449 |
_markSelectedDates: function( dates){ |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
function mark( selected, cell){ |
| 457 |
domClass.toggle(cell, "dijitCalendarSelectedDate", selected); |
| 458 |
cell.setAttribute("aria-selected", selected ? "true" : "false"); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
array.forEach(this._selectedCells || [], lang.partial(mark, false)); |
| 463 |
|
| 464 |
|
| 465 |
this._selectedCells = array.filter(array.map(dates, this._getNodeByDate, this), function(n){ |
| 466 |
return n; |
| 467 |
}); |
| 468 |
array.forEach(this._selectedCells, lang.partial(mark, true)); |
| 469 |
}, |
| 470 |
|
| 471 |
onChange: function( ){ |
| 472 |
|
| 473 |
|
| 474 |
}, |
| 475 |
|
| 476 |
isDisabledDate: function(){ |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
}, |
| 487 |
|
| 488 |
getClassForDate: function(){ |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
} |
| 501 |
}); |
| 502 |
|
| 503 |
CalendarLite._MonthWidget = declare("dijit.CalendarLite._MonthWidget", _WidgetBase, { |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
_setMonthAttr: function(month){ |
| 515 |
|
| 516 |
|
| 517 |
var monthNames = this.dateLocaleModule.getNames('months', 'wide', 'standAlone', this.lang, month), |
| 518 |
spacer = |
| 519 |
(has("ie") == 6 ? "" : "<div class='dijitSpacer'>" + |
| 520 |
array.map(monthNames,function(s){ |
| 521 |
return "<div>" + s + "</div>"; |
| 522 |
}).join("") + "</div>"); |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
this.domNode.innerHTML = |
| 528 |
spacer + |
| 529 |
"<div class='dijitCalendarMonthLabel dijitCalendarCurrentMonthLabel'>" + |
| 530 |
monthNames[month.getMonth()] + "</div>"; |
| 531 |
} |
| 532 |
}); |
| 533 |
|
| 534 |
return CalendarLite; |
| 535 |
}); |