SpinWheelDatePicker.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. define("dojox/mobile/SpinWheelDatePicker", [
  2. "dojo/_base/declare",
  3. "dojo/dom-class",
  4. "dojo/date",
  5. "dojo/date/locale",
  6. "./SpinWheel",
  7. "./SpinWheelSlot"
  8. ], function(declare, domClass, ddate, datelocale, SpinWheel, SpinWheelSlot){
  9. /*=====
  10. var SpinWheel = dojox.mobile.SpinWheel;
  11. var SpinWheelSlot = dojox.mobile.SpinWheelSlot;
  12. =====*/
  13. // module:
  14. // dojox/mobile/SpinWheelDatePicker
  15. // summary:
  16. // A SpinWheel-based date picker widget.
  17. //TODO: the api doc parser seems to fail if the 1st arg for declare (=class name) is missing..
  18. var SpinWheelYearSlot = declare(/*===== "dojox.mobile.SpinWheelYearSlot", =====*/ SpinWheelSlot, {
  19. buildRendering: function(){
  20. this.labels = [];
  21. if(this.labelFrom !== this.labelTo){
  22. var dtA = new Date(this.labelFrom, 0, 1);
  23. var i, idx;
  24. for(i = this.labelFrom, idx = 0; i <= this.labelTo; i++, idx++){
  25. dtA.setFullYear(i);
  26. this.labels.push(datelocale.format(dtA, {datePattern:"yyyy", selector:"date"}));
  27. }
  28. }
  29. this.inherited(arguments);
  30. }
  31. });
  32. var SpinWheelMonthSlot = declare(/*===== "dojox.mobile.SpinWheelMonthSlot", =====*/ SpinWheelSlot, {
  33. buildRendering: function(){
  34. this.labels = [];
  35. var dtA = new Date(2000, 0, 1);
  36. var monthStr;
  37. for(var i = 0; i < 12; i++){
  38. dtA.setMonth(i);
  39. monthStr = datelocale.format(dtA, {datePattern:"MMM", selector:"date"});
  40. this.labels.push(monthStr);
  41. }
  42. this.inherited(arguments);
  43. }
  44. });
  45. var SpinWheelDaySlot = declare(/*===== "dojox.mobile.SpinWheelDaySlot", =====*/ SpinWheelSlot, {
  46. });
  47. return declare("dojox.mobile.SpinWheelDatePicker", SpinWheel, {
  48. // summary:
  49. // A SpinWheel-based date picker widget.
  50. // description:
  51. // SpinWheelDatePicker is a date picker widget. It is a subclass of
  52. // dojox.mobile.SpinWheel. It has the year, month, and day slots.
  53. slotClasses: [
  54. SpinWheelYearSlot,
  55. SpinWheelMonthSlot,
  56. SpinWheelDaySlot
  57. ],
  58. slotProps: [
  59. {labelFrom:1970, labelTo:2038},
  60. {},
  61. {labelFrom:1, labelTo:31}
  62. ],
  63. buildRendering: function(){
  64. this.inherited(arguments);
  65. domClass.add(this.domNode, "mblSpinWheelDatePicker");
  66. this.connect(this.slots[1], "onFlickAnimationEnd", "onMonthSet");
  67. this.connect(this.slots[2], "onFlickAnimationEnd", "onDaySet");
  68. },
  69. reset: function(){
  70. // summary:
  71. // Goes to today.
  72. var slots = this.slots;
  73. var now = new Date();
  74. var monthStr = datelocale.format(now, {datePattern:"MMM", selector:"date"});
  75. this.setValue([now.getFullYear(), monthStr, now.getDate()]);
  76. },
  77. onMonthSet: function(){
  78. // summary:
  79. // A handler called when the month value is changed.
  80. var daysInMonth = this.onDaySet();
  81. var disableValuesTable = {28:[29,30,31], 29:[30,31], 30:[31], 31:[]};
  82. this.slots[2].disableValues(disableValuesTable[daysInMonth]);
  83. },
  84. onDaySet: function(){
  85. // summary:
  86. // A handler called when the day value is changed.
  87. var y = this.slots[0].getValue();
  88. var m = this.slots[1].getValue();
  89. var newMonth = datelocale.parse(y+"/"+m, {datePattern:'yyyy/MMM', selector:'date'});
  90. var daysInMonth = ddate.getDaysInMonth(newMonth);
  91. var d = this.slots[2].getValue();
  92. if(daysInMonth < d){
  93. this.slots[2].setValue(daysInMonth);
  94. }
  95. return daysInMonth;
  96. }
  97. });
  98. });