DateTextBox.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. define("dojox/form/DateTextBox", [
  2. "dojo/_base/kernel",
  3. "dojo/_base/lang",
  4. "dojo/dom-style",
  5. "dojox/widget/Calendar",
  6. "dojox/widget/CalendarViews",
  7. "dijit/form/_DateTimeTextBox",
  8. "dijit/form/TextBox",
  9. "dojo/_base/declare"
  10. ], function(kernel, lang, domStyle, Calendar, CalendarViews, _DateTimeTextBox, TextBox, declare){
  11. kernel.experimental("dojox.form.DateTextBox");
  12. /*=====
  13. _DateTimeTextBox = dijit.form._DateTimeTextBox;
  14. =====*/
  15. var DateTextBox = declare( "dojox.form.DateTextBox", _DateTimeTextBox,
  16. {
  17. // summary:
  18. // A validating, serializable, range-bound date text box with a popup calendar
  19. // popupClass: String
  20. // The popup widget to use. In this case, a calendar with Day, Month and Year views.
  21. popupClass: "dojox.widget.Calendar",
  22. _selector: "date",
  23. openDropDown: function(){
  24. this.inherited(arguments);
  25. domStyle.set(this.dropDown.domNode.parentNode, "position", "absolute");
  26. }
  27. }
  28. );
  29. declare( "dojox.form.DayTextBox", DateTextBox,
  30. {
  31. // summary:
  32. // A validating, serializable, range-bound date text box with a popup calendar that contains just months.
  33. // popupClass: String
  34. // The popup widget to use. In this case, a calendar with just a Month view.
  35. popupClass: "dojox.widget.DailyCalendar",
  36. parse: function(displayVal){
  37. return displayVal;
  38. },
  39. format: function(value){
  40. return value.getDate ? value.getDate() : value;
  41. },
  42. validator: function(value){
  43. var num = Number(value);
  44. var isInt = /(^-?\d\d*$)/.test(String(value));
  45. return value == "" || value == null || (isInt && num >= 1 && num <= 31);
  46. },
  47. _setValueAttr: function(value, priorityChange, formattedValue){
  48. if(value){
  49. if(value.getDate){
  50. value = value.getDate();
  51. }
  52. }
  53. TextBox.prototype._setValueAttr.call(this, value, priorityChange, formattedValue);
  54. },
  55. openDropDown: function(){
  56. this.inherited(arguments);
  57. this.dropDown.onValueSelected = lang.hitch(this, function(value){
  58. this.focus(); // focus the textbox before the popup closes to avoid reopening the popup
  59. setTimeout(lang.hitch(this, "closeDropDown"), 1); // allow focus time to take
  60. TextBox.prototype._setValueAttr.call(this, String(value.getDate()), true, String(value.getDate()));
  61. });
  62. }
  63. }
  64. );
  65. declare( "dojox.form.MonthTextBox", DateTextBox,
  66. {
  67. // summary:
  68. // A validating, serializable, range-bound date text box with a popup calendar that contains only years
  69. // popupClass: String
  70. // The popup widget to use. In this case, a calendar with just a Year view.
  71. popupClass: "dojox.widget.MonthlyCalendar",
  72. selector: "date",
  73. postMixInProperties: function(){
  74. this.inherited(arguments);
  75. this.constraints.datePattern = "MM";
  76. },
  77. format: function(value){
  78. if(!value && value !== 0){
  79. return 1;
  80. }
  81. if(value.getMonth){
  82. return value.getMonth() + 1;
  83. }
  84. return Number(value) + 1;
  85. },
  86. parse: function(value, constraints){
  87. return Number(value) - 1;
  88. },
  89. serialize: function(value, constraints){
  90. return String(value);
  91. },
  92. validator: function(value){
  93. var num = Number(value);
  94. var isInt = /(^-?\d\d*$)/.test(String(value));
  95. return value == "" || value == null || (isInt && num >= 1 && num <= 12);
  96. },
  97. _setValueAttr: function(value, priorityChange, formattedValue){
  98. if(value){
  99. if(value.getMonth){
  100. value = value.getMonth();
  101. }
  102. }
  103. TextBox.prototype._setValueAttr.call(this, value, priorityChange, formattedValue);
  104. },
  105. openDropDown: function(){
  106. this.inherited(arguments);
  107. this.dropDown.onValueSelected = lang.hitch(this, function(value){
  108. this.focus(); // focus the textbox before the popup closes to avoid reopening the popup
  109. setTimeout(lang.hitch(this, "closeDropDown"), 1); // allow focus time to take
  110. TextBox.prototype._setValueAttr.call(this, value, true, value);
  111. });
  112. }
  113. }
  114. );
  115. declare( "dojox.form.YearTextBox", DateTextBox,
  116. {
  117. // summary:
  118. // A validating, serializable, range-bound date text box with a popup calendar that contains only years
  119. popupClass: "dojox.widget.YearlyCalendar",
  120. format: function(value){
  121. //console.log('Year format ' + value);
  122. if(typeof value == "string"){
  123. return value;
  124. }
  125. else if(value.getFullYear){
  126. return value.getFullYear();
  127. }
  128. return value;
  129. },
  130. validator: function(value){
  131. return value == "" || value == null || /(^-?\d\d*$)/.test(String(value));
  132. },
  133. _setValueAttr: function(value, priorityChange, formattedValue){
  134. if(value){
  135. if(value.getFullYear){
  136. value = value.getFullYear();
  137. }
  138. }
  139. TextBox.prototype._setValueAttr.call(this, value, priorityChange, formattedValue);
  140. },
  141. openDropDown: function(){
  142. this.inherited(arguments);
  143. //console.log('yearly openDropDown and value = ' + this.get('value'));
  144. this.dropDown.onValueSelected = lang.hitch(this, function(value){
  145. this.focus(); // focus the textbox before the popup closes to avoid reopening the popup
  146. setTimeout(lang.hitch(this, "closeDropDown"), 1); // allow focus time to take
  147. TextBox.prototype._setValueAttr.call(this,value, true, value);
  148. });
  149. },
  150. parse: function(/*String*/value, /*dojo.date.locale.__FormatOptions*/constraints){
  151. return value || (this._isEmpty(value) ? null : undefined); // Date
  152. },
  153. filter: function(val){
  154. if(val && val.getFullYear){
  155. return val.getFullYear().toString();
  156. }
  157. return this.inherited(arguments);
  158. }
  159. }
  160. );
  161. return DateTextBox;
  162. });