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     
82     getAutoCreate : function()
83     {
84         var cfg = {
85             tag : 'div',
86             cls : 'row roo-date-split-field-group',
87             cn : [
88                 {
89                     tag : 'input',
90                     type : 'hidden',
91                     cls : 'form-hidden-field roo-date-split-field-group-value',
92                     name : this.name
93                 }
94             ]
95         }
96         
97         if(this.fieldLabel){
98             cfg.cn.push({
99                 tag : 'div',
100                 cls : 'column roo-date-split-field-label col-md-' + ((this.labelAlign == 'top') ? '12' : this.labelWidth),
101                 cn : [
102                     {
103                         tag : 'label',
104                         html : this.fieldLabel
105                     }
106                 ]
107             });
108         }
109         
110         Roo.each(['day', 'month', 'year'], function(t){
111             cfg.cn.push({
112                 tag : 'div',
113                 cls : 'column roo-date-split-field-' + t + ' col-md-' + ((this.labelAlign == 'top') ? '4' : ((12 - this.labelWidth) / 3))
114             });
115         }, this);
116         
117         return cfg;
118     },
119     
120     inputEl: function ()
121     {
122         return this.el.select('.roo-date-split-field-group-value', true).first();
123     },
124     
125     onRender : function(ct, position) 
126     {
127         var _this = this;
128         
129         Roo.bootstrap.NavProgressBar.superclass.onRender.call(this, ct, position);
130         
131         this.inputEl = this.el.select('.roo-date-split-field-group-value', true).first();
132         
133         this.dayField = new Roo.bootstrap.ComboBox({
134             allowBlank : this.dayAllowBlank,
135             alwaysQuery : true,
136             displayField : 'value',
137             editable : false,
138             fieldLabel : '',
139             forceSelection : true,
140             mode : 'local',
141             placeholder : this.dayPlaceholder,
142             selectOnFocus : true,
143             tpl : '<div class="select2-result"><b>{value}</b></div>',
144             triggerAction : 'all',
145             typeAhead : true,
146             valueField : 'value',
147             store : new Roo.data.SimpleStore({
148                 data : (function() {    
149                     var days = [];
150                     _this.fireEvent('days', _this, days);
151                     return days;
152                 })(),
153                 fields : [ 'value' ]
154             }),
155             listeners : {
156                 select : function (_self, record, index)
157                 {
158                     _this.validate();
159                 }
160             }
161         });
162
163         this.dayField.render(this.el.select('.roo-date-split-field-day', true).first(), null);
164         
165         this.monthField = new Roo.bootstrap.MonthField({
166             after : '<i class=\"fa fa-calendar\"></i>',
167             allowBlank : this.monthAllowBlank,
168             placeholder : this.monthPlaceholder,
169             readOnly : true,
170             listeners : {
171                 render : function (_self)
172                 {
173                     this.el.select('span.input-group-addon', true).first().on('click', function(e){
174                         e.preventDefault();
175                         _self.focus();
176                     });
177                 },
178                 select : function (_self, oldvalue, newvalue)
179                 {
180                     _this.validate();
181                 }
182             }
183         });
184         
185         this.monthField.render(this.el.select('.roo-date-split-field-month', true).first(), null);
186         
187         this.yearField = new Roo.bootstrap.ComboBox({
188             allowBlank : this.yearAllowBlank,
189             alwaysQuery : true,
190             displayField : 'value',
191             editable : false,
192             fieldLabel : '',
193             forceSelection : true,
194             mode : 'local',
195             placeholder : this.yearPlaceholder,
196             selectOnFocus : true,
197             tpl : '<div class="select2-result"><b>{value}</b></div>',
198             triggerAction : 'all',
199             typeAhead : true,
200             valueField : 'value',
201             store : new Roo.data.SimpleStore({
202                 data : (function() {
203                     var years = [];
204                     _this.fireEvent('years', _this, years);
205                     return years;
206                 })(),
207                 fields : [ 'value' ]
208             }),
209             listeners : {
210                 select : function (_self, record, index)
211                 {
212                     _this.validate();
213                 }
214             }
215         });
216
217         this.yearField.render(this.el.select('.roo-date-split-field-year', true).first(), null);
218     },
219     
220     setValue : function(v, format)
221     {
222         var f = format || (this.yearFormat + '-' + this.monthFormat + '-' + this.dayFormat);
223         
224         var d = Date.parseDate(v, f);
225         
226         this.dayField.setValue(d.format(this.dayFormat));
227         this.monthField.setValue(d.format(this.monthFormat));
228         this.yearField.setValue(d.format(this.yearFormat));
229         
230         return;
231     },
232     
233     setDay : function(v)
234     {
235         this.dayField.setValue(v);
236         
237         return;
238     },
239     
240     setMonth : function(v)
241     {
242         this.monthField.setValue(v);
243         
244         return;
245     },
246     
247     setYear : function(v)
248     {
249         this.yearField.setValue(v);
250         
251         return;
252     },
253     
254     getValue : function()
255     {
256         var f = this.yearFormat + '-' + this.monthFormat + '-' + this.dayFormat;
257         
258         var date = this.yearField.getValue() + '-' + this.monthField.getValue() + '-' + this.dayField.getValue();
259         
260         return date;
261     },
262     
263     validate : function()
264     {
265         var d = this.dayField.validate();
266         var m = this.monthField.validate();
267         var y = this.yearField.validate();
268         
269         var valid = true;
270         
271         if(
272                 (!this.dayAllowBlank && !d) ||
273                 (!this.monthAllowBlank && !m) ||
274                 (!this.yearAllowBlank && !y)
275         ){
276             valid = false;
277         }
278         
279         if(this.dayAllowBlank && this.monthAllowBlank && this.yearAllowBlank){
280             return valid;
281         }
282         
283         if(valid){
284             this.markValid();
285             return valid;
286         }
287         
288         this.markInvalid();
289         
290         return valid;
291     },
292     
293     markValid : function()
294     {
295         
296         var label = this.el.select('label', true).first();
297         var icon = this.el.select('i.fa-star', true).first();
298
299         if(label && icon){
300             icon.remove();
301         }
302         
303         this.fireEvent('valid', this);
304     },
305     
306      /**
307      * Mark this field as invalid
308      * @param {String} msg The validation message
309      */
310     markInvalid : function(msg)
311     {
312         
313         var label = this.el.select('label', true).first();
314         var icon = this.el.select('i.fa-star', true).first();
315
316         if(label && !icon){
317             this.el.select('.roo-date-split-field-label', true).createChild({
318                 tag : 'i',
319                 cls : 'text-danger fa fa-lg fa-star',
320                 tooltip : 'This field is required',
321                 style : 'margin-right:5px;'
322             }, label, true);
323         }
324         
325         this.fireEvent('invalid', this, msg);
326     },
327     
328     clearInvalid : function()
329     {
330         var label = this.el.select('label', true).first();
331         var icon = this.el.select('i.fa-star', true).first();
332
333         if(label && icon){
334             icon.remove();
335         }
336         
337         this.fireEvent('valid', this);
338     }
339     
340 });
341
342