DateTextBox.js 5.5 KB

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