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.setValue(_this.getValue());
160                     _this.validate();
161                 }
162             }
163         });
164
165         this.dayField.render(this.el.select('.roo-date-split-field-day', true).first(), null);
166         
167         this.monthField = new Roo.bootstrap.MonthField({
168             after : '<i class=\"fa fa-calendar\"></i>',
169             allowBlank : this.monthAllowBlank,
170             placeholder : this.monthPlaceholder,
171             readOnly : true,
172             listeners : {
173                 render : function (_self)
174                 {
175                     this.el.select('span.input-group-addon', true).first().on('click', function(e){
176                         e.preventDefault();
177                         _self.focus();
178                     });
179                 },
180                 select : function (_self, oldvalue, newvalue)
181                 {
182                     _this.setValue(_this.getValue());
183                     _this.validate();
184                 }
185             }
186         });
187         
188         this.monthField.render(this.el.select('.roo-date-split-field-month', true).first(), null);
189         
190         this.yearField = new Roo.bootstrap.ComboBox({
191             allowBlank : this.yearAllowBlank,
192             alwaysQuery : true,
193             displayField : 'value',
194             editable : false,
195             fieldLabel : '',
196             forceSelection : true,
197             mode : 'local',
198             placeholder : this.yearPlaceholder,
199             selectOnFocus : true,
200             tpl : '<div class="select2-result"><b>{value}</b></div>',
201             triggerAction : 'all',
202             typeAhead : true,
203             valueField : 'value',
204             store : new Roo.data.SimpleStore({
205                 data : (function() {
206                     var years = [];
207                     _this.fireEvent('years', _this, years);
208                     return years;
209                 })(),
210                 fields : [ 'value' ]
211             }),
212             listeners : {
213                 select : function (_self, record, index)
214                 {
215                     _this.setValue(_this.getValue());
216                     _this.validate();
217                 }
218             }
219         });
220
221         this.yearField.render(this.el.select('.roo-date-split-field-year', true).first(), null);
222     },
223     
224     setValue : function(v, format)
225     {
226         this.inputEl.dom.value = v;
227         
228         var f = format || (this.yearFormat + '-' + this.monthFormat + '-' + this.dayFormat);
229         
230         var d = Date.parseDate(v, f);
231         
232         if(!d){
233             this.validate();
234             return;
235         }
236         
237 //        this.setDay(d.format(this.dayFormat));
238 //        this.setMonth(d.format(this.monthFormat));
239 //        this.setYear(d.format(this.yearFormat));
240         
241         this.validate();
242         
243         return;
244     },
245     
246     setDay : function(v)
247     {
248         this.dayField.setValue(v);
249         return;
250     },
251     
252     setMonth : function(v)
253     {
254         this.monthField.setValue(v);
255         return;
256     },
257     
258     setYear : function(v)
259     {
260         this.yearField.setValue(v);
261         return;
262     },
263     
264     getValue : function()
265     {
266         var f = this.yearFormat + '-' + this.monthFormat + '-' + this.dayFormat;
267         
268         var date = this.yearField.getValue() + '-' + this.monthField.getValue() + '-' + this.dayField.getValue();
269         
270         return date;
271     },
272     
273     validate : function()
274     {
275         var d = this.dayField.validate();
276         var m = this.monthField.validate();
277         var y = this.yearField.validate();
278         
279         var valid = true;
280         
281         if(
282                 (!this.dayAllowBlank && !d) ||
283                 (!this.monthAllowBlank && !m) ||
284                 (!this.yearAllowBlank && !y)
285         ){
286             valid = false;
287         }
288         
289         if(this.dayAllowBlank && this.monthAllowBlank && this.yearAllowBlank){
290             return valid;
291         }
292         
293         if(valid){
294             this.markValid();
295             return valid;
296         }
297         
298         this.markInvalid();
299         
300         return valid;
301     },
302     
303     markValid : function()
304     {
305         
306         var label = this.el.select('label', true).first();
307         var icon = this.el.select('i.fa-star', true).first();
308
309         if(label && icon){
310             icon.remove();
311         }
312         
313         this.fireEvent('valid', this);
314     },
315     
316      /**
317      * Mark this field as invalid
318      * @param {String} msg The validation message
319      */
320     markInvalid : function(msg)
321     {
322         
323         var label = this.el.select('label', true).first();
324         var icon = this.el.select('i.fa-star', true).first();
325
326         if(label && !icon){
327             this.el.select('.roo-date-split-field-label', true).createChild({
328                 tag : 'i',
329                 cls : 'text-danger fa fa-lg fa-star',
330                 tooltip : 'This field is required',
331                 style : 'margin-right:5px;'
332             }, label, true);
333         }
334         
335         this.fireEvent('invalid', this, msg);
336     },
337     
338     clearInvalid : function()
339     {
340         var label = this.el.select('label', true).first();
341         var icon = this.el.select('i.fa-star', true).first();
342
343         if(label && icon){
344             icon.remove();
345         }
346         
347         this.fireEvent('valid', this);
348     },
349     
350     getName: function()
351     {
352         return this.name;
353     }
354     
355 });
356
357