03a085bb9579541355f254a68d8f8974b6b34066
[roojs1] / Roo / form / MonthField.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11  
12 /**
13  * @class Roo.form.MonthField
14  * @extends Roo.form.TriggerField
15  * Provides a date input field with a {@link Roo.DatePicker} dropdown and automatic date validation.
16 * @constructor
17 * Create a new MonthField
18 * @param {Object} config
19  */
20 Roo.form.MonthField = function(config){
21     Roo.log(this);
22     Roo.form.MonthField.superclass.constructor.call(this, config);
23     
24       this.addEvents({
25          
26         /**
27          * @event select
28          * Fires when a date is selected
29              * @param {Roo.form.DateField} combo This combo box
30              * @param {Date} date The date selected
31              */
32         'select' : true
33          
34     });
35     
36     
37     if(typeof this.minValue == "string") this.minValue = this.parseDate(this.minValue);
38     if(typeof this.maxValue == "string") this.maxValue = this.parseDate(this.maxValue);
39     this.ddMatch = null;
40     if(this.disabledDates){
41         var dd = this.disabledDates;
42         var re = "(?:";
43         for(var i = 0; i < dd.length; i++){
44             re += dd[i];
45             if(i != dd.length-1) re += "|";
46         }
47         this.ddMatch = new RegExp(re + ")");
48     }
49 };
50
51 Roo.extend(Roo.form.MonthField, Roo.form.TriggerField,  {
52     /**
53      * @cfg {String} format
54      * The default date format string which can be overriden for localization support.  The format must be
55      * valid according to {@link Date#parseDate} (defaults to 'm/d/y').
56      */
57     format : "M-Y",
58     /**
59      * @cfg {String} altFormats
60      * Multiple date formats separated by "|" to try when parsing a user input value and it doesn't match the defined
61      * format (defaults to 'm/d/Y|m-d-y|m-d-Y|m/d|m-d|d').
62      */
63     altFormats : "m/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d",
64     /**
65      * @cfg {Array} disabledDays
66      * An array of days to disable, 0 based. For example, [0, 6] disables Sunday and Saturday (defaults to null).
67      */
68     disabledDays : null,
69     /**
70      * @cfg {String} disabledDaysText
71      * The tooltip to display when the date falls on a disabled day (defaults to 'Disabled')
72      */
73     disabledDaysText : "Disabled",
74     /**
75      * @cfg {Array} disabledDates
76      * An array of "dates" to disable, as strings. These strings will be used to build a dynamic regular
77      * expression so they are very powerful. Some examples:
78      * <ul>
79      * <li>["03/08/2003", "09/16/2003"] would disable those exact dates</li>
80      * <li>["03/08", "09/16"] would disable those days for every year</li>
81      * <li>["^03/08"] would only match the beginning (useful if you are using short years)</li>
82      * <li>["03/../2006"] would disable every day in March 2006</li>
83      * <li>["^03"] would disable every day in every March</li>
84      * </ul>
85      * In order to support regular expressions, if you are using a date format that has "." in it, you will have to
86      * escape the dot when restricting dates. For example: ["03\\.08\\.03"].
87      */
88     disabledDates : null,
89     /**
90      * @cfg {String} disabledDatesText
91      * The tooltip text to display when the date falls on a disabled date (defaults to 'Disabled')
92      */
93     disabledDatesText : "Disabled",
94     /**
95      * @cfg {Date/String} minValue
96      * The minimum allowed date. Can be either a Javascript date object or a string date in a
97      * valid format (defaults to null).
98      */
99     minValue : null,
100     /**
101      * @cfg {Date/String} maxValue
102      * The maximum allowed date. Can be either a Javascript date object or a string date in a
103      * valid format (defaults to null).
104      */
105     maxValue : null,
106     /**
107      * @cfg {String} minText
108      * The error text to display when the date in the cell is before minValue (defaults to
109      * 'The date in this field must be after {minValue}').
110      */
111     minText : "The date in this field must be equal to or after {0}",
112     /**
113      * @cfg {String} maxText
114      * The error text to display when the date in the cell is after maxValue (defaults to
115      * 'The date in this field must be before {maxValue}').
116      */
117     maxText : "The date in this field must be equal to or before {0}",
118     /**
119      * @cfg {String} invalidText
120      * The error text to display when the date in the field is invalid (defaults to
121      * '{value} is not a valid date - it must be in the format {format}').
122      */
123     invalidText : "{0} is not a valid date - it must be in the format {1}",
124     /**
125      * @cfg {String} triggerClass
126      * An additional CSS class used to style the trigger button.  The trigger will always get the
127      * class 'x-form-trigger' and triggerClass will be <b>appended</b> if specified (defaults to 'x-form-date-trigger'
128      * which displays a calendar icon).
129      */
130     triggerClass : 'x-form-date-trigger',
131     
132
133     /**
134      * @cfg {Boolean} useIso
135      * if enabled, then the date field will use a hidden field to store the 
136      * real value as iso formated date. default (false)
137      */ 
138     useIso : false,
139     /**
140      * @cfg {String/Object} autoCreate
141      * A DomHelper element spec, or true for a default element spec (defaults to
142      * {tag: "input", type: "text", size: "10", autocomplete: "off"})
143      */ 
144     // private
145     defaultAutoCreate : {tag: "input", type: "text", size: "10", autocomplete: "off"},
146     
147     // private
148     hiddenField: false,
149     
150     onRender : function(ct, position)
151     {
152         Roo.form.MonthField.superclass.onRender.call(this, ct, position);
153         if (this.useIso) {
154             this.el.dom.removeAttribute('name'); 
155             this.hiddenField = this.el.insertSibling({ tag:'input', type:'hidden', name: this.name },
156                     'before', true);
157             this.hiddenField.value = this.value ? this.formatDate(this.value, 'Y-m-d') : '';
158             // prevent input submission
159             this.hiddenName = this.name;
160         }
161             
162             
163     },
164     
165     // private
166     validateValue : function(value)
167     {
168         value = this.formatDate(value);
169         if(!Roo.form.DateField.superclass.validateValue.call(this, value)){
170             return false;
171         }
172         if(value.length < 1){ // if it's blank and textfield didn't flag it then it's valid
173              return true;
174         }
175         var svalue = value;
176         value = this.parseDate(value);
177         if(!value){
178             this.markInvalid(String.format(this.invalidText, svalue, this.format));
179             return false;
180         }
181         var time = value.getTime();
182         if(this.minValue && time < this.minValue.getTime()){
183             this.markInvalid(String.format(this.minText, this.formatDate(this.minValue)));
184             return false;
185         }
186         if(this.maxValue && time > this.maxValue.getTime()){
187             this.markInvalid(String.format(this.maxText, this.formatDate(this.maxValue)));
188             return false;
189         }
190         if(this.disabledDays){
191             var day = value.getDay();
192             for(var i = 0; i < this.disabledDays.length; i++) {
193                 if(day === this.disabledDays[i]){
194                     this.markInvalid(this.disabledDaysText);
195                     return false;
196                 }
197             }
198         }
199         var fvalue = this.formatDate(value);
200         if(this.ddMatch && this.ddMatch.test(fvalue)){
201             this.markInvalid(String.format(this.disabledDatesText, fvalue));
202             return false;
203         }
204         return true;
205     },
206
207     // private
208     // Provides logic to override the default TriggerField.validateBlur which just returns true
209     validateBlur : function(){
210         return !this.menu || !this.menu.isVisible();
211     },
212
213     /**
214      * Returns the current date value of the date field.
215      * @return {Date} The date value
216      */
217     getValue : function(){
218         
219         return  this.hiddenField ?
220                 this.hiddenField.value :
221                 this.parseDate(Roo.form.DateField.superclass.getValue.call(this)) || "";
222     },
223
224     /**
225      * Sets the value of the date field.  You can pass a date object or any string that can be parsed into a valid
226      * date, using DateField.format as the date format, according to the same rules as {@link Date#parseDate}
227      * (the default format used is "m/d/y").
228      * <br />Usage:
229      * <pre><code>
230 //All of these calls set the same date value (May 4, 2006)
231
232 //Pass a date object:
233 var dt = new Date('5/4/06');
234 dateField.setValue(dt);
235
236 //Pass a date string (default format):
237 dateField.setValue('5/4/06');
238
239 //Pass a date string (custom format):
240 dateField.format = 'Y-m-d';
241 dateField.setValue('2006-5-4');
242 </code></pre>
243      * @param {String/Date} date The date or valid date string
244      */
245     setValue : function(date){
246         if (this.hiddenField) {
247             this.hiddenField.value = this.formatDate(this.parseDate(date), 'Y-m-d');
248         }
249         Roo.form.DateField.superclass.setValue.call(this, this.formatDate(this.parseDate(date)));
250     },
251
252     // private
253     parseDate : function(value){
254         if(!value || value instanceof Date){
255             return value;
256         }
257         var v = Date.parseDate(value, this.format);
258         if(!v && this.altFormats){
259             if(!this.altFormatsArray){
260                 this.altFormatsArray = this.altFormats.split("|");
261             }
262             for(var i = 0, len = this.altFormatsArray.length; i < len && !v; i++){
263                 v = Date.parseDate(value, this.altFormatsArray[i]);
264             }
265         }
266         return v;
267     },
268
269     // private
270     formatDate : function(date, fmt){
271         return (!date || !(date instanceof Date)) ?
272                date : date.dateFormat(fmt || this.format);
273     },
274
275     // private
276     menuListeners : {
277         select: function(m, d){
278             this.setValue(d);
279             this.fireEvent('select', this, d);
280         },
281         show : function(){ // retain focus styling
282             this.onFocus();
283         },
284         hide : function(){
285             this.focus.defer(10, this);
286             var ml = this.menuListeners;
287             this.menu.un("select", ml.select,  this);
288             this.menu.un("show", ml.show,  this);
289             this.menu.un("hide", ml.hide,  this);
290         }
291     },
292     // private
293     // Implements the default empty TriggerField.onTriggerClick function to display the DatePicker
294     onTriggerClick : function(){
295         if(this.disabled){
296             return;
297         }
298         if(this.menu == null){
299             this.menu = new Roo.menu.DateMenu();
300         }
301         
302         
303         this.menu.on(Roo.apply({}, this.menuListeners, {
304             scope:this
305         }));
306         this.menu.picker.setValue(this.getValue() || new Date());
307         this.menu.show(this.el, "tl-bl?");
308     },
309
310     beforeBlur : function(){
311         var v = this.parseDate(this.getRawValue());
312         if(v){
313             this.setValue(v);
314         }
315     }
316
317     /** @cfg {Boolean} grow @hide */
318     /** @cfg {Number} growMin @hide */
319     /** @cfg {Number} growMax @hide */
320     /**
321      * @hide
322      * @method autoSize
323      */
324 });