Roo/bootstrap/DateSplitField.js
[roojs1] / Roo / bootstrap / DateSplitField.js
1 /*
2  * - LGPL
3  *
4  * page DateSplitField.
5  * 
6  */
7
8
9 /**
10  * @class Roo.bootstrap.DateSplitField
11  * @extends Roo.bootstrap.Component
12  * Bootstrap DateSplitField class
13  * @cfg {string} fieldLabel - the label associated
14  * @cfg {Number} labelWidth set the width of label (0-12)
15  * @cfg {String} labelAlign (top|left)
16  * @cfg {Boolean} dayAllowBlank (true|false) default false
17  * @cfg {Boolean} monthAllowBlank (true|false) default false
18  * @cfg {Boolean} yearAllowBlank (true|false) default false
19  * @cfg {string} dayPlaceholder 
20  * @cfg {string} monthPlaceholder
21  * @cfg {string} yearPlaceholder
22  * @cfg {string} dayFormat default 'd'
23  * @cfg {string} monthFormat default 'm'
24  * @cfg {string} yearFormat default 'Y'
25
26  *     
27  * @constructor
28  * Create a new DateSplitField
29  * @param {Object} config The config object
30  */
31
32 Roo.bootstrap.DateSplitField = function(config){
33     Roo.bootstrap.DateSplitField.superclass.constructor.call(this, config);
34     
35     this.addEvents({
36         // raw events
37          /**
38          * @event years
39          * getting the data of years
40          * @param {Roo.bootstrap.DateSplitField} this
41          * @param {Object} years
42          */
43         "years" : true,
44         /**
45          * @event days
46          * getting the data of days
47          * @param {Roo.bootstrap.DateSplitField} this
48          * @param {Object} days
49          */
50         "days" : true,
51         /**
52          * @event invalid
53          * Fires after the field has been marked as invalid.
54          * @param {Roo.form.Field} this
55          * @param {String} msg The validation message
56          */
57         invalid : true,
58        /**
59          * @event valid
60          * Fires after the field has been validated with no errors.
61          * @param {Roo.form.Field} this
62          */
63         valid : true
64     });
65 };
66
67 Roo.extend(Roo.bootstrap.DateSplitField, Roo.bootstrap.Component,  {
68     
69     fieldLabel : '',
70     labelAlign : 'top',
71     labelWidth : 3,
72     dayAllowBlank : false,
73     monthAllowBlank : false,
74     yearAllowBlank : false,
75     dayPlaceholder : '',
76     monthPlaceholder : '',
77     yearPlaceholder : '',
78     dayFormat : 'd',
79     monthFormat : 'm',
80     yearFormat : 'Y',
81     isFormField : true,
82     
83     getAutoCreate : function()
84     {
85         var cfg = {
86             tag : 'div',
87             cls : 'row roo-date-split-field-group',
88             cn : [
89                 {
90                     tag : 'input',
91                     type : 'hidden',
92                     cls : 'form-hidden-field roo-date-split-field-group-value',
93                     name : this.name
94                 }
95             ]
96         }
97         
98         if(this.fieldLabel){
99             cfg.cn.push({
100                 tag : 'div',
101                 cls : 'column roo-date-split-field-label col-md-' + ((this.labelAlign == 'top') ? '12' : this.labelWidth),
102                 cn : [
103                     {
104                         tag : 'label',
105                         html : this.fieldLabel
106                     }
107                 ]
108             });
109         }
110         
111         Roo.each(['day', 'month', 'year'], function(t){
112             cfg.cn.push({
113                 tag : 'div',
114                 cls : 'column roo-date-split-field-' + t + ' col-md-' + ((this.labelAlign == 'top') ? '4' : ((12 - this.labelWidth) / 3))
115             });
116         }, this);
117         
118         return cfg;
119     },
120     
121     inputEl: function ()
122     {
123         return this.el.select('.roo-date-split-field-group-value', true).first();
124     },
125     
126     onRender : function(ct, position) 
127     {
128         var _this = this;
129         
130         Roo.bootstrap.NavProgressBar.superclass.onRender.call(this, ct, position);
131         
132         this.inputEl = this.el.select('.roo-date-split-field-group-value', true).first();
133         
134         this.dayField = new Roo.bootstrap.ComboBox({
135             allowBlank : this.dayAllowBlank,
136             alwaysQuery : true,
137             displayField : 'value',
138             editable : false,
139             fieldLabel : '',
140             forceSelection : true,
141             mode : 'local',
142             placeholder : this.dayPlaceholder,
143             selectOnFocus : true,
144             tpl : '<div class="select2-result"><b>{value}</b></div>',
145             triggerAction : 'all',
146             typeAhead : true,
147             valueField : 'value',
148             store : new Roo.data.SimpleStore({
149                 data : (function() {    
150                     var days = [];
151                     _this.fireEvent('days', _this, days);
152                     return days;
153                 })(),
154                 fields : [ 'value' ]
155             }),
156             listeners : {
157                 select : function (_self, record, index)
158                 {
159                     _this.validate();
160                 }
161             }
162         });
163
164         this.dayField.render(this.el.select('.roo-date-split-field-day', true).first(), null);
165         
166         this.monthField = new Roo.bootstrap.MonthField({
167             after : '<i class=\"fa fa-calendar\"></i>',
168             allowBlank : this.monthAllowBlank,
169             placeholder : this.monthPlaceholder,
170             readOnly : true,
171             listeners : {
172                 render : function (_self)
173                 {
174                     this.el.select('span.input-group-addon', true).first().on('click', function(e){
175                         e.preventDefault();
176                         _self.focus();
177                     });
178                 },
179                 select : function (_self, oldvalue, newvalue)
180                 {
181                     _this.setValue(_this.getValue());
182                     _this.validate();
183                 }
184             }
185         });
186         
187         this.monthField.render(this.el.select('.roo-date-split-field-month', true).first(), null);
188         
189         this.yearField = new Roo.bootstrap.ComboBox({
190             allowBlank : this.yearAllowBlank,
191             alwaysQuery : true,
192             displayField : 'value',
193             editable : false,
194             fieldLabel : '',
195             forceSelection : true,
196             mode : 'local',
197             placeholder : this.yearPlaceholder,
198             selectOnFocus : true,
199             tpl : '<div class="select2-result"><b>{value}</b></div>',
200             triggerAction : 'all',
201             typeAhead : true,
202             valueField : 'value',
203             store : new Roo.data.SimpleStore({
204                 data : (function() {
205                     var years = [];
206                     _this.fireEvent('years', _this, years);
207                     return years;
208                 })(),
209                 fields : [ 'value' ]
210             }),
211             listeners : {
212                 select : function (_self, record, index)
213                 {
214                     _this.setValue(_this.getValue());
215                     _this.validate();
216                 }
217             }
218         });
219
220         this.yearField.render(this.el.select('.roo-date-split-field-year', true).first(), null);
221     },
222     
223     setValue : function(v, format)
224     {
225         this.inputEl.dom.value = v;
226         
227         var f = format || (this.yearFormat + '-' + this.monthFormat + '-' + this.dayFormat);
228         
229         var d = Date.parseDate(v, f);
230         
231         if(!d){
232             return;
233         }
234         
235         this.dayField.setValue(d.format(this.dayFormat));
236         this.monthField.setValue(d.format(this.monthFormat));
237         this.yearField.setValue(d.format(this.yearFormat));
238         
239         this.validate();
240         
241         return;
242     },
243     
244     setDay : function(v)
245     {
246         this.dayField.setValue(v);
247         this.validate();
248         
249         return;
250     },
251     
252     setMonth : function(v)
253     {
254         this.monthField.setValue(v);
255         this.validate();
256         return;
257     },
258     
259     setYear : function(v)
260     {
261         this.yearField.setValue(v);
262         this.validate();
263         return;
264     },
265     
266     getValue : function()
267     {
268         var f = this.yearFormat + '-' + this.monthFormat + '-' + this.dayFormat;
269         
270         var date = this.yearField.getValue() + '-' + this.monthField.getValue() + '-' + this.dayField.getValue();
271         
272         return date;
273     },
274     
275     validate : function()
276     {
277         var d = this.dayField.validate();
278         var m = this.monthField.validate();
279         var y = this.yearField.validate();
280         
281         var valid = true;
282         
283         if(
284                 (!this.dayAllowBlank && !d) ||
285                 (!this.monthAllowBlank && !m) ||
286                 (!this.yearAllowBlank && !y)
287         ){
288             valid = false;
289         }
290         
291         if(this.dayAllowBlank && this.monthAllowBlank && this.yearAllowBlank){
292             return valid;
293         }
294         
295         if(valid){
296             this.markValid();
297             return valid;
298         }
299         
300         this.markInvalid();
301         
302         return valid;
303     },
304     
305     markValid : function()
306     {
307         
308         var label = this.el.select('label', true).first();
309         var icon = this.el.select('i.fa-star', true).first();
310
311         if(label && icon){
312             icon.remove();
313         }
314         
315         this.fireEvent('valid', this);
316     },
317     
318      /**
319      * Mark this field as invalid
320      * @param {String} msg The validation message
321      */
322     markInvalid : function(msg)
323     {
324         
325         var label = this.el.select('label', true).first();
326         var icon = this.el.select('i.fa-star', true).first();
327
328         if(label && !icon){
329             this.el.select('.roo-date-split-field-label', true).createChild({
330                 tag : 'i',
331                 cls : 'text-danger fa fa-lg fa-star',
332                 tooltip : 'This field is required',
333                 style : 'margin-right:5px;'
334             }, label, true);
335         }
336         
337         this.fireEvent('invalid', this, msg);
338     },
339     
340     clearInvalid : function()
341     {
342         var label = this.el.select('label', true).first();
343         var icon = this.el.select('i.fa-star', true).first();
344
345         if(label && icon){
346             icon.remove();
347         }
348         
349         this.fireEvent('valid', this);
350     },
351     
352     getName: function()
353     {
354         return this.name;
355     }
356     
357 });
358
359