_DateTimeTextBox.js 10 KB

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