DateField.js
上传用户:dawnssy
上传日期:2022-08-06
资源大小:9345k
文件大小:14k
源码类别:

JavaScript

开发平台:

JavaScript

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