Date4.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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-field-Date'>/**
  19. </span> * @docauthor Jason Johnston &lt;jason@sencha.com&gt;
  20. *
  21. * Provides a date input field with a {@link Ext.picker.Date date picker} dropdown and automatic date
  22. * validation.
  23. *
  24. * This field recognizes and uses the JavaScript Date object as its main {@link #value} type. In addition,
  25. * it recognizes string values which are parsed according to the {@link #format} and/or {@link #altFormats}
  26. * configs. These may be reconfigured to use date formats appropriate for the user's locale.
  27. *
  28. * The field may be limited to a certain range of dates by using the {@link #minValue}, {@link #maxValue},
  29. * {@link #disabledDays}, and {@link #disabledDates} config parameters. These configurations will be used both
  30. * in the field's validation, and in the date picker dropdown by preventing invalid dates from being selected.
  31. *
  32. * # Example usage
  33. *
  34. * @example
  35. * Ext.create('Ext.form.Panel', {
  36. * renderTo: Ext.getBody(),
  37. * width: 300,
  38. * bodyPadding: 10,
  39. * title: 'Dates',
  40. * items: [{
  41. * xtype: 'datefield',
  42. * anchor: '100%',
  43. * fieldLabel: 'From',
  44. * name: 'from_date',
  45. * maxValue: new Date() // limited to the current date or prior
  46. * }, {
  47. * xtype: 'datefield',
  48. * anchor: '100%',
  49. * fieldLabel: 'To',
  50. * name: 'to_date',
  51. * value: new Date() // defaults to today
  52. * }]
  53. * });
  54. *
  55. * # Date Formats Examples
  56. *
  57. * This example shows a couple of different date format parsing scenarios. Both use custom date format
  58. * configurations; the first one matches the configured `format` while the second matches the `altFormats`.
  59. *
  60. * @example
  61. * Ext.create('Ext.form.Panel', {
  62. * renderTo: Ext.getBody(),
  63. * width: 300,
  64. * bodyPadding: 10,
  65. * title: 'Dates',
  66. * items: [{
  67. * xtype: 'datefield',
  68. * anchor: '100%',
  69. * fieldLabel: 'Date',
  70. * name: 'date',
  71. * // The value matches the format; will be parsed and displayed using that format.
  72. * format: 'm d Y',
  73. * value: '2 4 1978'
  74. * }, {
  75. * xtype: 'datefield',
  76. * anchor: '100%',
  77. * fieldLabel: 'Date',
  78. * name: 'date',
  79. * // The value does not match the format, but does match an altFormat; will be parsed
  80. * // using the altFormat and displayed using the format.
  81. * format: 'm d Y',
  82. * altFormats: 'm,d,Y|m.d.Y',
  83. * value: '2.4.1978'
  84. * }]
  85. * });
  86. */
  87. Ext.define('Ext.form.field.Date', {
  88. extend:'Ext.form.field.Picker',
  89. alias: 'widget.datefield',
  90. requires: ['Ext.picker.Date'],
  91. alternateClassName: ['Ext.form.DateField', 'Ext.form.Date'],
  92. //&lt;locale&gt;
  93. <span id='Ext-form-field-Date-cfg-format'> /**
  94. </span> * @cfg {String} format
  95. * The default date format string which can be overriden for localization support. The format must be valid
  96. * according to {@link Ext.Date#parse}.
  97. */
  98. format : &quot;m/d/Y&quot;,
  99. //&lt;/locale&gt;
  100. //&lt;locale&gt;
  101. <span id='Ext-form-field-Date-cfg-altFormats'> /**
  102. </span> * @cfg {String} altFormats
  103. * Multiple date formats separated by &quot;|&quot; to try when parsing a user input value and it does not match the defined
  104. * format.
  105. */
  106. 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;,
  107. //&lt;/locale&gt;
  108. //&lt;locale&gt;
  109. <span id='Ext-form-field-Date-cfg-disabledDaysText'> /**
  110. </span> * @cfg {String} disabledDaysText
  111. * The tooltip to display when the date falls on a disabled day.
  112. */
  113. disabledDaysText : &quot;Disabled&quot;,
  114. //&lt;/locale&gt;
  115. //&lt;locale&gt;
  116. <span id='Ext-form-field-Date-cfg-disabledDatesText'> /**
  117. </span> * @cfg {String} disabledDatesText
  118. * The tooltip text to display when the date falls on a disabled date.
  119. */
  120. disabledDatesText : &quot;Disabled&quot;,
  121. //&lt;/locale&gt;
  122. //&lt;locale&gt;
  123. <span id='Ext-form-field-Date-cfg-minText'> /**
  124. </span> * @cfg {String} minText
  125. * The error text to display when the date in the cell is before {@link #minValue}.
  126. */
  127. minText : &quot;The date in this field must be equal to or after {0}&quot;,
  128. //&lt;/locale&gt;
  129. //&lt;locale&gt;
  130. <span id='Ext-form-field-Date-cfg-maxText'> /**
  131. </span> * @cfg {String} maxText
  132. * The error text to display when the date in the cell is after {@link #maxValue}.
  133. */
  134. maxText : &quot;The date in this field must be equal to or before {0}&quot;,
  135. //&lt;/locale&gt;
  136. //&lt;locale&gt;
  137. <span id='Ext-form-field-Date-cfg-invalidText'> /**
  138. </span> * @cfg {String} invalidText
  139. * The error text to display when the date in the field is invalid.
  140. */
  141. invalidText : &quot;{0} is not a valid date - it must be in the format {1}&quot;,
  142. //&lt;/locale&gt;
  143. <span id='Ext-form-field-Date-cfg-triggerCls'> /**
  144. </span> * @cfg {String} [triggerCls='x-form-date-trigger']
  145. * An additional CSS class used to style the trigger button. The trigger will always get the class 'x-form-trigger'
  146. * and triggerCls will be **appended** if specified (default class displays a calendar icon).
  147. */
  148. triggerCls : Ext.baseCSSPrefix + 'form-date-trigger',
  149. <span id='Ext-form-field-Date-cfg-showToday'> /**
  150. </span> * @cfg {Boolean} showToday
  151. * false to hide the footer area of the Date picker containing the Today button and disable the keyboard handler for
  152. * spacebar that selects the current date.
  153. */
  154. showToday : true,
  155. <span id='Ext-form-field-Date-cfg-minValue'> /**
  156. </span> * @cfg {Date/String} minValue
  157. * The minimum allowed date. Can be either a Javascript date object or a string date in a valid format.
  158. */
  159. <span id='Ext-form-field-Date-cfg-maxValue'> /**
  160. </span> * @cfg {Date/String} maxValue
  161. * The maximum allowed date. Can be either a Javascript date object or a string date in a valid format.
  162. */
  163. <span id='Ext-form-field-Date-cfg-disabledDays'> /**
  164. </span> * @cfg {Number[]} disabledDays
  165. * An array of days to disable, 0 based. Some examples:
  166. *
  167. * // disable Sunday and Saturday:
  168. * disabledDays: [0, 6]
  169. * // disable weekdays:
  170. * disabledDays: [1,2,3,4,5]
  171. */
  172. <span id='Ext-form-field-Date-cfg-disabledDates'> /**
  173. </span> * @cfg {String[]} disabledDates
  174. * An array of &quot;dates&quot; to disable, as strings. These strings will be used to build a dynamic regular expression so
  175. * they are very powerful. Some examples:
  176. *
  177. * // disable these exact dates:
  178. * disabledDates: [&quot;03/08/2003&quot;, &quot;09/16/2003&quot;]
  179. * // disable these days for every year:
  180. * disabledDates: [&quot;03/08&quot;, &quot;09/16&quot;]
  181. * // only match the beginning (useful if you are using short years):
  182. * disabledDates: [&quot;^03/08&quot;]
  183. * // disable every day in March 2006:
  184. * disabledDates: [&quot;03/../2006&quot;]
  185. * // disable every day in every March:
  186. * disabledDates: [&quot;^03&quot;]
  187. *
  188. * Note that the format of the dates included in the array should exactly match the {@link #format} config. In order
  189. * to support regular expressions, if you are using a {@link #format date format} that has &quot;.&quot; in it, you will have
  190. * to escape the dot when restricting dates. For example: `[&quot;03\\.08\\.03&quot;]`.
  191. */
  192. <span id='Ext-form-field-Date-cfg-submitFormat'> /**
  193. </span> * @cfg {String} submitFormat
  194. * The date format string which will be submitted to the server. The format must be valid according to
  195. * {@link Ext.Date#parse}.
  196. *
  197. * Defaults to {@link #format}.
  198. */
  199. <span id='Ext-form-field-Date-cfg-useStrict'> /**
  200. </span> * @cfg {Boolean} useStrict
  201. * True to enforce strict date parsing to prevent the default Javascript &quot;date rollover&quot;.
  202. * Defaults to the useStrict parameter set on Ext.Date
  203. * See {@link Ext.Date#parse}.
  204. */
  205. useStrict: undefined,
  206. // in the absence of a time value, a default value of 12 noon will be used
  207. // (note: 12 noon was chosen because it steers well clear of all DST timezone changes)
  208. initTime: '12', // 24 hour format
  209. initTimeFormat: 'H',
  210. matchFieldWidth: false,
  211. //&lt;locale&gt;
  212. <span id='Ext-form-field-Date-cfg-startDay'> /**
  213. </span> * @cfg {Number} [startDay=undefined]
  214. * Day index at which the week should begin, 0-based.
  215. *
  216. * Defaults to `0` (Sunday).
  217. */
  218. startDay: 0,
  219. //&lt;/locale&gt;
  220. initComponent : function(){
  221. var me = this,
  222. isString = Ext.isString,
  223. min, max;
  224. min = me.minValue;
  225. max = me.maxValue;
  226. if(isString(min)){
  227. me.minValue = me.parseDate(min);
  228. }
  229. if(isString(max)){
  230. me.maxValue = me.parseDate(max);
  231. }
  232. me.disabledDatesRE = null;
  233. me.initDisabledDays();
  234. me.callParent();
  235. },
  236. initValue: function() {
  237. var me = this,
  238. value = me.value;
  239. // If a String value was supplied, try to convert it to a proper Date
  240. if (Ext.isString(value)) {
  241. me.value = me.rawToValue(value);
  242. }
  243. me.callParent();
  244. },
  245. // private
  246. initDisabledDays : function(){
  247. if(this.disabledDates){
  248. var dd = this.disabledDates,
  249. len = dd.length - 1,
  250. re = &quot;(?:&quot;,
  251. d,
  252. dLen = dd.length,
  253. date;
  254. for (d = 0; d &lt; dLen; d++) {
  255. date = dd[d];
  256. re += Ext.isDate(date) ? '^' + Ext.String.escapeRegex(date.dateFormat(this.format)) + '$' : date;
  257. if (d !== len) {
  258. re += '|';
  259. }
  260. }
  261. this.disabledDatesRE = new RegExp(re + ')');
  262. }
  263. },
  264. <span id='Ext-form-field-Date-method-setDisabledDates'> /**
  265. </span> * Replaces any existing disabled dates with new values and refreshes the Date picker.
  266. * @param {String[]} disabledDates An array of date strings (see the {@link #disabledDates} config for details on
  267. * supported values) used to disable a pattern of dates.
  268. */
  269. setDisabledDates : function(dd){
  270. var me = this,
  271. picker = me.picker;
  272. me.disabledDates = dd;
  273. me.initDisabledDays();
  274. if (picker) {
  275. picker.setDisabledDates(me.disabledDatesRE);
  276. }
  277. },
  278. <span id='Ext-form-field-Date-method-setDisabledDays'> /**
  279. </span> * Replaces any existing disabled days (by index, 0-6) with new values and refreshes the Date picker.
  280. * @param {Number[]} disabledDays An array of disabled day indexes. See the {@link #disabledDays} config for details on
  281. * supported values.
  282. */
  283. setDisabledDays : function(dd){
  284. var picker = this.picker;
  285. this.disabledDays = dd;
  286. if (picker) {
  287. picker.setDisabledDays(dd);
  288. }
  289. },
  290. <span id='Ext-form-field-Date-method-setMinValue'> /**
  291. </span> * Replaces any existing {@link #minValue} with the new value and refreshes the Date picker.
  292. * @param {Date} value The minimum date that can be selected
  293. */
  294. setMinValue : function(dt){
  295. var me = this,
  296. picker = me.picker,
  297. minValue = (Ext.isString(dt) ? me.parseDate(dt) : dt);
  298. me.minValue = minValue;
  299. if (picker) {
  300. picker.minText = Ext.String.format(me.minText, me.formatDate(me.minValue));
  301. picker.setMinDate(minValue);
  302. }
  303. },
  304. <span id='Ext-form-field-Date-method-setMaxValue'> /**
  305. </span> * Replaces any existing {@link #maxValue} with the new value and refreshes the Date picker.
  306. * @param {Date} value The maximum date that can be selected
  307. */
  308. setMaxValue : function(dt){
  309. var me = this,
  310. picker = me.picker,
  311. maxValue = (Ext.isString(dt) ? me.parseDate(dt) : dt);
  312. me.maxValue = maxValue;
  313. if (picker) {
  314. picker.maxText = Ext.String.format(me.maxText, me.formatDate(me.maxValue));
  315. picker.setMaxDate(maxValue);
  316. }
  317. },
  318. <span id='Ext-form-field-Date-method-getErrors'> /**
  319. </span> * Runs all of Date's validations and returns an array of any errors. Note that this first runs Text's validations,
  320. * so the returned array is an amalgamation of all field errors. The additional validation checks are testing that
  321. * the date format is valid, that the chosen date is within the min and max date constraints set, that the date
  322. * chosen is not in the disabledDates regex and that the day chosed is not one of the disabledDays.
  323. * @param {Object} [value] The value to get errors for (defaults to the current field value)
  324. * @return {String[]} All validation errors for this field
  325. */
  326. getErrors: function(value) {
  327. var me = this,
  328. format = Ext.String.format,
  329. clearTime = Ext.Date.clearTime,
  330. errors = me.callParent(arguments),
  331. disabledDays = me.disabledDays,
  332. disabledDatesRE = me.disabledDatesRE,
  333. minValue = me.minValue,
  334. maxValue = me.maxValue,
  335. len = disabledDays ? disabledDays.length : 0,
  336. i = 0,
  337. svalue,
  338. fvalue,
  339. day,
  340. time;
  341. value = me.formatDate(value || me.processRawValue(me.getRawValue()));
  342. if (value === null || value.length &lt; 1) { // if it's blank and textfield didn't flag it then it's valid
  343. return errors;
  344. }
  345. svalue = value;
  346. value = me.parseDate(value);
  347. if (!value) {
  348. errors.push(format(me.invalidText, svalue, Ext.Date.unescapeFormat(me.format)));
  349. return errors;
  350. }
  351. time = value.getTime();
  352. if (minValue &amp;&amp; time &lt; clearTime(minValue).getTime()) {
  353. errors.push(format(me.minText, me.formatDate(minValue)));
  354. }
  355. if (maxValue &amp;&amp; time &gt; clearTime(maxValue).getTime()) {
  356. errors.push(format(me.maxText, me.formatDate(maxValue)));
  357. }
  358. if (disabledDays) {
  359. day = value.getDay();
  360. for(; i &lt; len; i++) {
  361. if (day === disabledDays[i]) {
  362. errors.push(me.disabledDaysText);
  363. break;
  364. }
  365. }
  366. }
  367. fvalue = me.formatDate(value);
  368. if (disabledDatesRE &amp;&amp; disabledDatesRE.test(fvalue)) {
  369. errors.push(format(me.disabledDatesText, fvalue));
  370. }
  371. return errors;
  372. },
  373. rawToValue: function(rawValue) {
  374. return this.parseDate(rawValue) || rawValue || null;
  375. },
  376. valueToRaw: function(value) {
  377. return this.formatDate(this.parseDate(value));
  378. },
  379. <span id='Ext-form-field-Date-method-setValue'> /**
  380. </span> * @method setValue
  381. * Sets the value of the date field. You can pass a date object or any string that can be parsed into a valid date,
  382. * using {@link #format} as the date format, according to the same rules as {@link Ext.Date#parse} (the default
  383. * format used is &quot;m/d/Y&quot;).
  384. *
  385. * Usage:
  386. *
  387. * //All of these calls set the same date value (May 4, 2006)
  388. *
  389. * //Pass a date object:
  390. * var dt = new Date('5/4/2006');
  391. * dateField.setValue(dt);
  392. *
  393. * //Pass a date string (default format):
  394. * dateField.setValue('05/04/2006');
  395. *
  396. * //Pass a date string (custom format):
  397. * dateField.format = 'Y-m-d';
  398. * dateField.setValue('2006-05-04');
  399. *
  400. * @param {String/Date} date The date or valid date string
  401. * @return {Ext.form.field.Date} this
  402. */
  403. <span id='Ext-form-field-Date-method-safeParse'> /**
  404. </span> * Attempts to parse a given string value using a given {@link Ext.Date#parse date format}.
  405. * @param {String} value The value to attempt to parse
  406. * @param {String} format A valid date format (see {@link Ext.Date#parse})
  407. * @return {Date} The parsed Date object, or null if the value could not be successfully parsed.
  408. */
  409. safeParse : function(value, format) {
  410. var me = this,
  411. utilDate = Ext.Date,
  412. result = null,
  413. strict = me.useStrict,
  414. parsedDate;
  415. if (utilDate.formatContainsHourInfo(format)) {
  416. // if parse format contains hour information, no DST adjustment is necessary
  417. result = utilDate.parse(value, format, strict);
  418. } else {
  419. // set time to 12 noon, then clear the time
  420. parsedDate = utilDate.parse(value + ' ' + me.initTime, format + ' ' + me.initTimeFormat, strict);
  421. if (parsedDate) {
  422. result = utilDate.clearTime(parsedDate);
  423. }
  424. }
  425. return result;
  426. },
  427. // @private
  428. getSubmitValue: function() {
  429. var format = this.submitFormat || this.format,
  430. value = this.getValue();
  431. return value ? Ext.Date.format(value, format) : '';
  432. },
  433. <span id='Ext-form-field-Date-method-parseDate'> /**
  434. </span> * @private
  435. */
  436. parseDate : function(value) {
  437. if(!value || Ext.isDate(value)){
  438. return value;
  439. }
  440. var me = this,
  441. val = me.safeParse(value, me.format),
  442. altFormats = me.altFormats,
  443. altFormatsArray = me.altFormatsArray,
  444. i = 0,
  445. len;
  446. if (!val &amp;&amp; altFormats) {
  447. altFormatsArray = altFormatsArray || altFormats.split('|');
  448. len = altFormatsArray.length;
  449. for (; i &lt; len &amp;&amp; !val; ++i) {
  450. val = me.safeParse(value, altFormatsArray[i]);
  451. }
  452. }
  453. return val;
  454. },
  455. // private
  456. formatDate : function(date){
  457. return Ext.isDate(date) ? Ext.Date.dateFormat(date, this.format) : date;
  458. },
  459. createPicker: function() {
  460. var me = this,
  461. format = Ext.String.format;
  462. return new Ext.picker.Date({
  463. pickerField: me,
  464. ownerCt: me.ownerCt,
  465. renderTo: document.body,
  466. floating: true,
  467. hidden: true,
  468. focusOnShow: true,
  469. minDate: me.minValue,
  470. maxDate: me.maxValue,
  471. disabledDatesRE: me.disabledDatesRE,
  472. disabledDatesText: me.disabledDatesText,
  473. disabledDays: me.disabledDays,
  474. disabledDaysText: me.disabledDaysText,
  475. format: me.format,
  476. showToday: me.showToday,
  477. startDay: me.startDay,
  478. minText: format(me.minText, me.formatDate(me.minValue)),
  479. maxText: format(me.maxText, me.formatDate(me.maxValue)),
  480. listeners: {
  481. scope: me,
  482. select: me.onSelect
  483. },
  484. keyNavConfig: {
  485. esc: function() {
  486. me.collapse();
  487. }
  488. }
  489. });
  490. },
  491. onSelect: function(m, d) {
  492. var me = this;
  493. me.setValue(d);
  494. me.fireEvent('select', me, d);
  495. me.collapse();
  496. },
  497. <span id='Ext-form-field-Date-method-onExpand'> /**
  498. </span> * @private
  499. * Sets the Date picker's value to match the current field value when expanding.
  500. */
  501. onExpand: function() {
  502. var value = this.getValue();
  503. this.picker.setValue(Ext.isDate(value) ? value : new Date());
  504. },
  505. <span id='Ext-form-field-Date-method-onCollapse'> /**
  506. </span> * @private
  507. * Focuses the field when collapsing the Date picker.
  508. */
  509. onCollapse: function() {
  510. this.focus(false, 60);
  511. },
  512. // private
  513. beforeBlur : function(){
  514. var me = this,
  515. v = me.parseDate(me.getRawValue()),
  516. focusTask = me.focusTask;
  517. if (focusTask) {
  518. focusTask.cancel();
  519. }
  520. if (v) {
  521. me.setValue(v);
  522. }
  523. }
  524. <span id='Ext-form-field-Date-cfg-grow'> /**
  525. </span> * @cfg {Boolean} grow
  526. * @private
  527. */
  528. <span id='Ext-form-field-Date-cfg-growMin'> /**
  529. </span> * @cfg {Number} growMin
  530. * @private
  531. */
  532. <span id='Ext-form-field-Date-cfg-growMax'> /**
  533. </span> * @cfg {Number} growMax
  534. * @private
  535. */
  536. <span id='Ext-form-field-Date-method-autoSize'> /**
  537. </span> * @method autoSize
  538. * @private
  539. */
  540. });
  541. </pre>
  542. </body>
  543. </html>