You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

460 lines
19 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-form-DateField-method-constructor'><span id='Ext-form-DateField'>/**
  19. </span></span> * @class Ext.form.DateField
  20. * @extends Ext.form.TriggerField
  21. * Provides a date input field with a {@link Ext.DatePicker} dropdown and automatic date validation.
  22. * @constructor
  23. * Create a new DateField
  24. * @param {Object} config
  25. * @xtype datefield
  26. */
  27. Ext.form.DateField = Ext.extend(Ext.form.TriggerField, {
  28. <span id='Ext-form-DateField-cfg-format'> /**
  29. </span> * @cfg {String} format
  30. * The default date format string which can be overriden for localization support. The format must be
  31. * valid according to {@link Date#parseDate} (defaults to &lt;tt&gt;'m/d/Y'&lt;/tt&gt;).
  32. */
  33. format : &quot;m/d/Y&quot;,
  34. <span id='Ext-form-DateField-cfg-altFormats'> /**
  35. </span> * @cfg {String} altFormats
  36. * Multiple date formats separated by &quot;&lt;tt&gt;|&lt;/tt&gt;&quot; to try when parsing a user input value and it
  37. * does not match the defined format (defaults to
  38. * &lt;tt&gt;'m/d/Y|n/j/Y|n/j/y|m/j/y|n/d/y|m/j/Y|n/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d|Y-m-d|n-j|n/j'&lt;/tt&gt;).
  39. */
  40. altFormats : &quot;m/d/Y|n/j/Y|n/j/y|m/j/y|n/d/y|m/j/Y|n/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d|Y-m-d|n-j|n/j&quot;,
  41. <span id='Ext-form-DateField-cfg-disabledDaysText'> /**
  42. </span> * @cfg {String} disabledDaysText
  43. * The tooltip to display when the date falls on a disabled day (defaults to &lt;tt&gt;'Disabled'&lt;/tt&gt;)
  44. */
  45. disabledDaysText : &quot;Disabled&quot;,
  46. <span id='Ext-form-DateField-cfg-disabledDatesText'> /**
  47. </span> * @cfg {String} disabledDatesText
  48. * The tooltip text to display when the date falls on a disabled date (defaults to &lt;tt&gt;'Disabled'&lt;/tt&gt;)
  49. */
  50. disabledDatesText : &quot;Disabled&quot;,
  51. <span id='Ext-form-DateField-cfg-minText'> /**
  52. </span> * @cfg {String} minText
  53. * The error text to display when the date in the cell is before &lt;tt&gt;{@link #minValue}&lt;/tt&gt; (defaults to
  54. * &lt;tt&gt;'The date in this field must be after {minValue}'&lt;/tt&gt;).
  55. */
  56. minText : &quot;The date in this field must be equal to or after {0}&quot;,
  57. <span id='Ext-form-DateField-cfg-maxText'> /**
  58. </span> * @cfg {String} maxText
  59. * The error text to display when the date in the cell is after &lt;tt&gt;{@link #maxValue}&lt;/tt&gt; (defaults to
  60. * &lt;tt&gt;'The date in this field must be before {maxValue}'&lt;/tt&gt;).
  61. */
  62. maxText : &quot;The date in this field must be equal to or before {0}&quot;,
  63. <span id='Ext-form-DateField-cfg-invalidText'> /**
  64. </span> * @cfg {String} invalidText
  65. * The error text to display when the date in the field is invalid (defaults to
  66. * &lt;tt&gt;'{value} is not a valid date - it must be in the format {format}'&lt;/tt&gt;).
  67. */
  68. invalidText : &quot;{0} is not a valid date - it must be in the format {1}&quot;,
  69. <span id='Ext-form-DateField-cfg-triggerClass'> /**
  70. </span> * @cfg {String} triggerClass
  71. * An additional CSS class used to style the trigger button. The trigger will always get the
  72. * class &lt;tt&gt;'x-form-trigger'&lt;/tt&gt; and &lt;tt&gt;triggerClass&lt;/tt&gt; will be &lt;b&gt;appended&lt;/b&gt; if specified
  73. * (defaults to &lt;tt&gt;'x-form-date-trigger'&lt;/tt&gt; which displays a calendar icon).
  74. */
  75. triggerClass : 'x-form-date-trigger',
  76. <span id='Ext-form-DateField-cfg-showToday'> /**
  77. </span> * @cfg {Boolean} showToday
  78. * &lt;tt&gt;false&lt;/tt&gt; to hide the footer area of the DatePicker containing the Today button and disable
  79. * the keyboard handler for spacebar that selects the current date (defaults to &lt;tt&gt;true&lt;/tt&gt;).
  80. */
  81. showToday : true,
  82. <span id='Ext-form-DateField-cfg-startDay'> /**
  83. </span> * @cfg {Number} startDay
  84. * Day index at which the week should begin, 0-based (defaults to 0, which is Sunday)
  85. */
  86. startDay : 0,
  87. <span id='Ext-form-DateField-cfg-minValue'> /**
  88. </span> * @cfg {Date/String} minValue
  89. * The minimum allowed date. Can be either a Javascript date object or a string date in a
  90. * valid format (defaults to null).
  91. */
  92. <span id='Ext-form-DateField-cfg-maxValue'> /**
  93. </span> * @cfg {Date/String} maxValue
  94. * The maximum allowed date. Can be either a Javascript date object or a string date in a
  95. * valid format (defaults to null).
  96. */
  97. <span id='Ext-form-DateField-cfg-disabledDays'> /**
  98. </span> * @cfg {Array} disabledDays
  99. * An array of days to disable, 0 based (defaults to null). Some examples:&lt;pre&gt;&lt;code&gt;
  100. // disable Sunday and Saturday:
  101. disabledDays: [0, 6]
  102. // disable weekdays:
  103. disabledDays: [1,2,3,4,5]
  104. * &lt;/code&gt;&lt;/pre&gt;
  105. */
  106. <span id='Ext-form-DateField-cfg-disabledDates'> /**
  107. </span> * @cfg {Array} disabledDates
  108. * An array of &quot;dates&quot; to disable, as strings. These strings will be used to build a dynamic regular
  109. * expression so they are very powerful. Some examples:&lt;pre&gt;&lt;code&gt;
  110. // disable these exact dates:
  111. disabledDates: [&quot;03/08/2003&quot;, &quot;09/16/2003&quot;]
  112. // disable these days for every year:
  113. disabledDates: [&quot;03/08&quot;, &quot;09/16&quot;]
  114. // only match the beginning (useful if you are using short years):
  115. disabledDates: [&quot;^03/08&quot;]
  116. // disable every day in March 2006:
  117. disabledDates: [&quot;03/../2006&quot;]
  118. // disable every day in every March:
  119. disabledDates: [&quot;^03&quot;]
  120. * &lt;/code&gt;&lt;/pre&gt;
  121. * Note that the format of the dates included in the array should exactly match the {@link #format} config.
  122. * In order to support regular expressions, if you are using a {@link #format date format} that has &quot;.&quot; in
  123. * it, you will have to escape the dot when restricting dates. For example: &lt;tt&gt;[&quot;03\\.08\\.03&quot;]&lt;/tt&gt;.
  124. */
  125. <span id='Ext-form-DateField-cfg-autoCreate'> /**
  126. </span> * @cfg {String/Object} autoCreate
  127. * A {@link Ext.DomHelper DomHelper element specification object}, or &lt;tt&gt;true&lt;/tt&gt; for the default element
  128. * specification object:&lt;pre&gt;&lt;code&gt;
  129. * autoCreate: {tag: &quot;input&quot;, type: &quot;text&quot;, size: &quot;10&quot;, autocomplete: &quot;off&quot;}
  130. * &lt;/code&gt;&lt;/pre&gt;
  131. */
  132. <span id='Ext-form-DateField-property-defaultAutoCreate'> // private
  133. </span> defaultAutoCreate : {tag: &quot;input&quot;, type: &quot;text&quot;, size: &quot;10&quot;, autocomplete: &quot;off&quot;},
  134. <span id='Ext-form-DateField-property-initTime'> // in the absence of a time value, a default value of 12 noon will be used
  135. </span> // (note: 12 noon was chosen because it steers well clear of all DST timezone changes)
  136. <span id='Ext-form-DateField-property-initTimeFormat'> initTime: '12', // 24 hour format
  137. </span>
  138. initTimeFormat: 'H',
  139. <span id='Ext-form-DateField-method-safeParse'> // PUBLIC -- to be documented
  140. </span> safeParse : function(value, format) {
  141. if (Date.formatContainsHourInfo(format)) {
  142. // if parse format contains hour information, no DST adjustment is necessary
  143. return Date.parseDate(value, format);
  144. } else {
  145. // set time to 12 noon, then clear the time
  146. var parsedDate = Date.parseDate(value + ' ' + this.initTime, format + ' ' + this.initTimeFormat);
  147. if (parsedDate) {
  148. return parsedDate.clearTime();
  149. }
  150. }
  151. },
  152. <span id='Ext-form-DateField-method-initComponent'> initComponent : function(){
  153. </span> Ext.form.DateField.superclass.initComponent.call(this);
  154. this.addEvents(
  155. <span id='Ext-form-DateField-event-select'> /**
  156. </span> * @event select
  157. * Fires when a date is selected via the date picker.
  158. * @param {Ext.form.DateField} this
  159. * @param {Date} date The date that was selected
  160. */
  161. 'select'
  162. );
  163. if(Ext.isString(this.minValue)){
  164. this.minValue = this.parseDate(this.minValue);
  165. }
  166. if(Ext.isString(this.maxValue)){
  167. this.maxValue = this.parseDate(this.maxValue);
  168. }
  169. this.disabledDatesRE = null;
  170. this.initDisabledDays();
  171. },
  172. <span id='Ext-form-DateField-method-initEvents'> initEvents: function() {
  173. </span> Ext.form.DateField.superclass.initEvents.call(this);
  174. this.keyNav = new Ext.KeyNav(this.el, {
  175. &quot;down&quot;: function(e) {
  176. this.onTriggerClick();
  177. },
  178. scope: this,
  179. forceKeyDown: true
  180. });
  181. },
  182. <span id='Ext-form-DateField-method-initDisabledDays'> // private
  183. </span> initDisabledDays : function(){
  184. if(this.disabledDates){
  185. var dd = this.disabledDates,
  186. len = dd.length - 1,
  187. re = &quot;(?:&quot;;
  188. Ext.each(dd, function(d, i){
  189. re += Ext.isDate(d) ? '^' + Ext.escapeRe(d.dateFormat(this.format)) + '$' : dd[i];
  190. if(i != len){
  191. re += '|';
  192. }
  193. }, this);
  194. this.disabledDatesRE = new RegExp(re + ')');
  195. }
  196. },
  197. <span id='Ext-form-DateField-method-setDisabledDates'> /**
  198. </span> * Replaces any existing disabled dates with new values and refreshes the DatePicker.
  199. * @param {Array} disabledDates An array of date strings (see the &lt;tt&gt;{@link #disabledDates}&lt;/tt&gt; config
  200. * for details on supported values) used to disable a pattern of dates.
  201. */
  202. setDisabledDates : function(dd){
  203. this.disabledDates = dd;
  204. this.initDisabledDays();
  205. if(this.menu){
  206. this.menu.picker.setDisabledDates(this.disabledDatesRE);
  207. }
  208. },
  209. <span id='Ext-form-DateField-method-setDisabledDays'> /**
  210. </span> * Replaces any existing disabled days (by index, 0-6) with new values and refreshes the DatePicker.
  211. * @param {Array} disabledDays An array of disabled day indexes. See the &lt;tt&gt;{@link #disabledDays}&lt;/tt&gt;
  212. * config for details on supported values.
  213. */
  214. setDisabledDays : function(dd){
  215. this.disabledDays = dd;
  216. if(this.menu){
  217. this.menu.picker.setDisabledDays(dd);
  218. }
  219. },
  220. <span id='Ext-form-DateField-method-setMinValue'> /**
  221. </span> * Replaces any existing &lt;tt&gt;{@link #minValue}&lt;/tt&gt; with the new value and refreshes the DatePicker.
  222. * @param {Date} value The minimum date that can be selected
  223. */
  224. setMinValue : function(dt){
  225. this.minValue = (Ext.isString(dt) ? this.parseDate(dt) : dt);
  226. if(this.menu){
  227. this.menu.picker.setMinDate(this.minValue);
  228. }
  229. },
  230. <span id='Ext-form-DateField-method-setMaxValue'> /**
  231. </span> * Replaces any existing &lt;tt&gt;{@link #maxValue}&lt;/tt&gt; with the new value and refreshes the DatePicker.
  232. * @param {Date} value The maximum date that can be selected
  233. */
  234. setMaxValue : function(dt){
  235. this.maxValue = (Ext.isString(dt) ? this.parseDate(dt) : dt);
  236. if(this.menu){
  237. this.menu.picker.setMaxDate(this.maxValue);
  238. }
  239. },
  240. <span id='Ext-form-DateField-method-getErrors'> /**
  241. </span> * Runs all of NumberFields validations and returns an array of any errors. Note that this first
  242. * runs TextField's validations, so the returned array is an amalgamation of all field errors.
  243. * The additional validation checks are testing that the date format is valid, that the chosen
  244. * date is within the min and max date constraints set, that the date chosen is not in the disabledDates
  245. * regex and that the day chosed is not one of the disabledDays.
  246. * @param {Mixed} value The value to get errors for (defaults to the current field value)
  247. * @return {Array} All validation errors for this field
  248. */
  249. getErrors: function(value) {
  250. var errors = Ext.form.DateField.superclass.getErrors.apply(this, arguments);
  251. value = this.formatDate(value || this.processValue(this.getRawValue()));
  252. if (value.length &lt; 1) { // if it's blank and textfield didn't flag it then it's valid
  253. return errors;
  254. }
  255. var svalue = value;
  256. value = this.parseDate(value);
  257. if (!value) {
  258. errors.push(String.format(this.invalidText, svalue, this.format));
  259. return errors;
  260. }
  261. var time = value.getTime();
  262. if (this.minValue &amp;&amp; time &lt; this.minValue.clearTime().getTime()) {
  263. errors.push(String.format(this.minText, this.formatDate(this.minValue)));
  264. }
  265. if (this.maxValue &amp;&amp; time &gt; this.maxValue.clearTime().getTime()) {
  266. errors.push(String.format(this.maxText, this.formatDate(this.maxValue)));
  267. }
  268. if (this.disabledDays) {
  269. var day = value.getDay();
  270. for(var i = 0; i &lt; this.disabledDays.length; i++) {
  271. if (day === this.disabledDays[i]) {
  272. errors.push(this.disabledDaysText);
  273. break;
  274. }
  275. }
  276. }
  277. var fvalue = this.formatDate(value);
  278. if (this.disabledDatesRE &amp;&amp; this.disabledDatesRE.test(fvalue)) {
  279. errors.push(String.format(this.disabledDatesText, fvalue));
  280. }
  281. return errors;
  282. },
  283. <span id='Ext-form-DateField-method-validateBlur'> // private
  284. </span> // Provides logic to override the default TriggerField.validateBlur which just returns true
  285. validateBlur : function(){
  286. return !this.menu || !this.menu.isVisible();
  287. },
  288. <span id='Ext-form-DateField-method-getValue'> /**
  289. </span> * Returns the current date value of the date field.
  290. * @return {Date} The date value
  291. */
  292. getValue : function(){
  293. return this.parseDate(Ext.form.DateField.superclass.getValue.call(this)) || &quot;&quot;;
  294. },
  295. <span id='Ext-form-DateField-method-setValue'> /**
  296. </span> * Sets the value of the date field. You can pass a date object or any string that can be
  297. * parsed into a valid date, using &lt;tt&gt;{@link #format}&lt;/tt&gt; as the date format, according
  298. * to the same rules as {@link Date#parseDate} (the default format used is &lt;tt&gt;&quot;m/d/Y&quot;&lt;/tt&gt;).
  299. * &lt;br /&gt;Usage:
  300. * &lt;pre&gt;&lt;code&gt;
  301. //All of these calls set the same date value (May 4, 2006)
  302. //Pass a date object:
  303. var dt = new Date('5/4/2006');
  304. dateField.setValue(dt);
  305. //Pass a date string (default format):
  306. dateField.setValue('05/04/2006');
  307. //Pass a date string (custom format):
  308. dateField.format = 'Y-m-d';
  309. dateField.setValue('2006-05-04');
  310. &lt;/code&gt;&lt;/pre&gt;
  311. * @param {String/Date} date The date or valid date string
  312. * @return {Ext.form.Field} this
  313. */
  314. setValue : function(date){
  315. return Ext.form.DateField.superclass.setValue.call(this, this.formatDate(this.parseDate(date)));
  316. },
  317. <span id='Ext-form-DateField-method-parseDate'> // private
  318. </span> parseDate : function(value) {
  319. if(!value || Ext.isDate(value)){
  320. return value;
  321. }
  322. var v = this.safeParse(value, this.format),
  323. af = this.altFormats,
  324. afa = this.altFormatsArray;
  325. if (!v &amp;&amp; af) {
  326. afa = afa || af.split(&quot;|&quot;);
  327. for (var i = 0, len = afa.length; i &lt; len &amp;&amp; !v; i++) {
  328. v = this.safeParse(value, afa[i]);
  329. }
  330. }
  331. return v;
  332. },
  333. <span id='Ext-form-DateField-method-onDestroy'> // private
  334. </span> onDestroy : function(){
  335. Ext.destroy(this.menu, this.keyNav);
  336. Ext.form.DateField.superclass.onDestroy.call(this);
  337. },
  338. <span id='Ext-form-DateField-method-formatDate'> // private
  339. </span> formatDate : function(date){
  340. return Ext.isDate(date) ? date.dateFormat(this.format) : date;
  341. },
  342. <span id='Ext-form-DateField-method-onTriggerClick'> /**
  343. </span> * @method onTriggerClick
  344. * @hide
  345. */
  346. <span id='Ext-form-DateField-method-onTriggerClick'> // private
  347. </span> // Implements the default empty TriggerField.onTriggerClick function to display the DatePicker
  348. onTriggerClick : function(){
  349. if(this.disabled){
  350. return;
  351. }
  352. if(this.menu == null){
  353. this.menu = new Ext.menu.DateMenu({
  354. hideOnClick: false,
  355. focusOnSelect: false
  356. });
  357. }
  358. this.onFocus();
  359. Ext.apply(this.menu.picker, {
  360. minDate : this.minValue,
  361. maxDate : this.maxValue,
  362. disabledDatesRE : this.disabledDatesRE,
  363. disabledDatesText : this.disabledDatesText,
  364. disabledDays : this.disabledDays,
  365. disabledDaysText : this.disabledDaysText,
  366. format : this.format,
  367. showToday : this.showToday,
  368. startDay: this.startDay,
  369. minText : String.format(this.minText, this.formatDate(this.minValue)),
  370. maxText : String.format(this.maxText, this.formatDate(this.maxValue))
  371. });
  372. this.menu.picker.setValue(this.getValue() || new Date());
  373. this.menu.show(this.el, &quot;tl-bl?&quot;);
  374. this.menuEvents('on');
  375. },
  376. <span id='Ext-form-DateField-method-menuEvents'> //private
  377. </span> menuEvents: function(method){
  378. this.menu[method]('select', this.onSelect, this);
  379. this.menu[method]('hide', this.onMenuHide, this);
  380. this.menu[method]('show', this.onFocus, this);
  381. },
  382. <span id='Ext-form-DateField-method-onSelect'> onSelect: function(m, d){
  383. </span> this.setValue(d);
  384. this.fireEvent('select', this, d);
  385. this.menu.hide();
  386. },
  387. <span id='Ext-form-DateField-method-onMenuHide'> onMenuHide: function(){
  388. </span> this.focus(false, 60);
  389. this.menuEvents('un');
  390. },
  391. <span id='Ext-form-DateField-method-beforeBlur'> // private
  392. </span> beforeBlur : function(){
  393. var v = this.parseDate(this.getRawValue());
  394. if(v){
  395. this.setValue(v);
  396. }
  397. }
  398. <span id='Ext-form-DateField-cfg-grow'> /**
  399. </span> * @cfg {Boolean} grow @hide
  400. */
  401. <span id='Ext-form-DateField-cfg-growMin'> /**
  402. </span> * @cfg {Number} growMin @hide
  403. */
  404. <span id='Ext-form-DateField-cfg-growMax'> /**
  405. </span> * @cfg {Number} growMax @hide
  406. */
  407. <span id='Ext-form-DateField-method-autoSize'> /**
  408. </span> * @hide
  409. * @method autoSize
  410. */
  411. });
  412. Ext.reg('datefield', Ext.form.DateField);
  413. </pre>
  414. </body>
  415. </html>