_DateTimeTextBox.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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["dijit.form._DateTimeTextBox"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dijit.form._DateTimeTextBox"] = true;
  8. dojo.provide("dijit.form._DateTimeTextBox");
  9. dojo.require("dojo.date");
  10. dojo.require("dojo.date.locale");
  11. dojo.require("dojo.date.stamp");
  12. dojo.require("dijit.form.ValidationTextBox");
  13. dojo.require("dijit._HasDropDown");
  14. new Date("X"); // workaround for #11279, new Date("") == NaN
  15. /*=====
  16. dojo.declare(
  17. "dijit.form._DateTimeTextBox.__Constraints",
  18. [dijit.form.RangeBoundTextBox.__Constraints, dojo.date.locale.__FormatOptions], {
  19. // summary:
  20. // Specifies both the rules on valid/invalid values (first/last date/time allowed),
  21. // and also formatting options for how the date/time is displayed.
  22. // example:
  23. // To restrict to dates within 2004, displayed in a long format like "December 25, 2005":
  24. // | {min:'2004-01-01',max:'2004-12-31', formatLength:'long'}
  25. });
  26. =====*/
  27. dojo.declare(
  28. "dijit.form._DateTimeTextBox",
  29. [ dijit.form.RangeBoundTextBox, dijit._HasDropDown ],
  30. {
  31. // summary:
  32. // Base class for validating, serializable, range-bound date or time text box.
  33. templateString: dojo.cache("dijit.form", "templates/DropDownBox.html", "<div class=\"dijit dijitReset dijitInlineTable dijitLeft\"\n\tid=\"widget_${id}\"\n\trole=\"combobox\"\n\t><div class='dijitReset dijitRight dijitButtonNode dijitArrowButton dijitDownArrowButton dijitArrowButtonContainer'\n\t\tdojoAttachPoint=\"_buttonNode, _popupStateNode\" role=\"presentation\"\n\t\t><input class=\"dijitReset dijitInputField dijitArrowButtonInner\" value=\"&#9660; \" type=\"text\" tabIndex=\"-1\" readonly=\"readonly\" role=\"presentation\"\n\t\t\t${_buttonInputDisabled}\n\t/></div\n\t><div class='dijitReset dijitValidationContainer'\n\t\t><input class=\"dijitReset dijitInputField dijitValidationIcon dijitValidationInner\" value=\"&#935; \" type=\"text\" tabIndex=\"-1\" readonly=\"readonly\" role=\"presentation\"\n\t/></div\n\t><div class=\"dijitReset dijitInputField dijitInputContainer\"\n\t\t><input class='dijitReset dijitInputInner' ${!nameAttrSetting} type=\"text\" autocomplete=\"off\"\n\t\t\tdojoAttachPoint=\"textbox,focusNode\" role=\"textbox\" aria-haspopup=\"true\"\n\t/></div\n></div>\n"),
  34. // hasDownArrow: [const] Boolean
  35. // Set this textbox to display a down arrow button, to open the drop down list.
  36. hasDownArrow: true,
  37. // openOnClick: [const] Boolean
  38. // Set to true to open drop down upon clicking anywhere on the textbox.
  39. openOnClick: true,
  40. /*=====
  41. // constraints: dijit.form._DateTimeTextBox.__Constraints
  42. // Despite the name, this parameter specifies both constraints on the input
  43. // (including starting/ending dates/times allowed) as well as
  44. // formatting options like whether the date is displayed in long (ex: December 25, 2005)
  45. // or short (ex: 12/25/2005) format. See `dijit.form._DateTimeTextBox.__Constraints` for details.
  46. constraints: {},
  47. ======*/
  48. // Override ValidationTextBox.regExpGen().... we use a reg-ex generating function rather
  49. // than a straight regexp to deal with locale (plus formatting options too?)
  50. regExpGen: dojo.date.locale.regexp,
  51. // datePackage: String
  52. // JavaScript namespace to find calendar routines. Uses Gregorian calendar routines
  53. // at dojo.date, by default.
  54. datePackage: "dojo.date",
  55. // Override _FormWidget.compare() to work for dates/times
  56. compare: function(/*Date*/ val1, /*Date*/ val2){
  57. var isInvalid1 = this._isInvalidDate(val1);
  58. var isInvalid2 = this._isInvalidDate(val2);
  59. return isInvalid1 ? (isInvalid2 ? 0 : -1) : (isInvalid2 ? 1 : dojo.date.compare(val1, val2, this._selector));
  60. },
  61. // flag to _HasDropDown to make drop down Calendar width == <input> width
  62. forceWidth: true,
  63. format: function(/*Date*/ value, /*dojo.date.locale.__FormatOptions*/ constraints){
  64. // summary:
  65. // Formats the value as a Date, according to specified locale (second argument)
  66. // tags:
  67. // protected
  68. if(!value){ return ''; }
  69. return this.dateLocaleModule.format(value, constraints);
  70. },
  71. "parse": function(/*String*/ value, /*dojo.date.locale.__FormatOptions*/ constraints){
  72. // summary:
  73. // Parses as string as a Date, according to constraints
  74. // tags:
  75. // protected
  76. return this.dateLocaleModule.parse(value, constraints) || (this._isEmpty(value) ? null : undefined); // Date
  77. },
  78. // Overrides ValidationTextBox.serialize() to serialize a date in canonical ISO format.
  79. serialize: function(/*anything*/ val, /*Object?*/ options){
  80. if(val.toGregorian){
  81. val = val.toGregorian();
  82. }
  83. return dojo.date.stamp.toISOString(val, options);
  84. },
  85. // dropDownDefaultValue: Date
  86. // The default value to focus in the popupClass widget when the textbox value is empty.
  87. dropDownDefaultValue : new Date(),
  88. // value: Date
  89. // The value of this widget as a JavaScript Date object. Use get("value") / set("value", val) to manipulate.
  90. // When passed to the parser in markup, must be specified according to `dojo.date.stamp.fromISOString`
  91. value: new Date(""), // value.toString()="NaN"
  92. _blankValue: null, // used by filter() when the textbox is blank
  93. // popupClass: [protected extension] String
  94. // Name of the popup widget class used to select a date/time.
  95. // Subclasses should specify this.
  96. popupClass: "", // default is no popup = text only
  97. // _selector: [protected extension] String
  98. // Specifies constraints.selector passed to dojo.date functions, should be either
  99. // "date" or "time".
  100. // Subclass must specify this.
  101. _selector: "",
  102. constructor: function(/*Object*/ args){
  103. var dateClass = args.datePackage ? args.datePackage + ".Date" : "Date";
  104. this.dateClassObj = dojo.getObject(dateClass, false);
  105. this.value = new this.dateClassObj("");
  106. this.datePackage = args.datePackage || this.datePackage;
  107. this.dateLocaleModule = dojo.getObject(this.datePackage + ".locale", false);
  108. this.regExpGen = this.dateLocaleModule.regexp;
  109. this._invalidDate = dijit.form._DateTimeTextBox.prototype.value.toString();
  110. },
  111. buildRendering: function(){
  112. this.inherited(arguments);
  113. if(!this.hasDownArrow){
  114. this._buttonNode.style.display = "none";
  115. }
  116. // If openOnClick is true, we basically just want to treat the whole widget as the
  117. // button. We need to do that also if the actual drop down button will be hidden,
  118. // so that there's a mouse method for opening the drop down.
  119. if(this.openOnClick || !this.hasDownArrow){
  120. this._buttonNode = this.domNode;
  121. this.baseClass += " dijitComboBoxOpenOnClick";
  122. }
  123. },
  124. _setConstraintsAttr: function(/*Object*/ constraints){
  125. constraints.selector = this._selector;
  126. constraints.fullYear = true; // see #5465 - always format with 4-digit years
  127. var fromISO = dojo.date.stamp.fromISOString;
  128. if(typeof constraints.min == "string"){ constraints.min = fromISO(constraints.min); }
  129. if(typeof constraints.max == "string"){ constraints.max = fromISO(constraints.max); }
  130. this.inherited(arguments);
  131. },
  132. _isInvalidDate: function(/*Date*/ value){
  133. // summary:
  134. // Runs various tests on the value, checking for invalid conditions
  135. // tags:
  136. // private
  137. return !value || isNaN(value) || typeof value != "object" || value.toString() == this._invalidDate;
  138. },
  139. _setValueAttr: function(/*Date|String*/ value, /*Boolean?*/ priorityChange, /*String?*/ formattedValue){
  140. // summary:
  141. // Sets the date on this textbox. Note: value can be a JavaScript Date literal or a string to be parsed.
  142. if(value !== undefined){
  143. if(typeof value == "string"){
  144. value = dojo.date.stamp.fromISOString(value);
  145. }
  146. if(this._isInvalidDate(value)){
  147. value = null;
  148. }
  149. if(value instanceof Date && !(this.dateClassObj instanceof Date)){
  150. value = new this.dateClassObj(value);
  151. }
  152. }
  153. this.inherited(arguments);
  154. if(this.value instanceof Date){
  155. this.filterString = "";
  156. }
  157. if(this.dropDown){
  158. this.dropDown.set('value', value, false);
  159. }
  160. },
  161. _set: function(attr, value){
  162. // Avoid spurious watch() notifications when value is changed to new Date object w/the same value
  163. if(attr == "value" && this.value instanceof Date && this.compare(value, this.value) == 0){
  164. return;
  165. }
  166. this.inherited(arguments);
  167. },
  168. _setDropDownDefaultValueAttr: function(/*Date*/ val){
  169. if(this._isInvalidDate(val)){
  170. // convert null setting into today's date, since there needs to be *some* default at all times.
  171. val = new this.dateClassObj();
  172. }
  173. this.dropDownDefaultValue = val;
  174. },
  175. openDropDown: function(/*Function*/ callback){
  176. // rebuild drop down every time, so that constraints get copied (#6002)
  177. if(this.dropDown){
  178. this.dropDown.destroy();
  179. }
  180. var PopupProto = dojo.getObject(this.popupClass, false),
  181. textBox = this,
  182. value = this.get("value");
  183. this.dropDown = new PopupProto({
  184. onChange: function(value){
  185. // this will cause InlineEditBox and other handlers to do stuff so make sure it's last
  186. textBox.set('value', value, true);
  187. },
  188. id: this.id + "_popup",
  189. dir: textBox.dir,
  190. lang: textBox.lang,
  191. value: value,
  192. currentFocus: !this._isInvalidDate(value) ? value : this.dropDownDefaultValue,
  193. constraints: textBox.constraints,
  194. filterString: textBox.filterString, // for TimeTextBox, to filter times shown
  195. datePackage: textBox.datePackage,
  196. isDisabledDate: function(/*Date*/ date){
  197. // summary:
  198. // disables dates outside of the min/max of the _DateTimeTextBox
  199. return !textBox.rangeCheck(date, textBox.constraints);
  200. }
  201. });
  202. this.inherited(arguments);
  203. },
  204. _getDisplayedValueAttr: function(){
  205. return this.textbox.value;
  206. },
  207. _setDisplayedValueAttr: function(/*String*/ value, /*Boolean?*/ priorityChange){
  208. this._setValueAttr(this.parse(value, this.constraints), priorityChange, value);
  209. }
  210. }
  211. );
  212. }