9 * @class Roo.bootstrap.MonthField
10 * @extends Roo.bootstrap.Input
11 * Bootstrap MonthField class
13 * @cfg {String} language default en
16 * Create a new MonthField
17 * @param {Object} config The config object
20 Roo.bootstrap.MonthField = function(config){
21 Roo.bootstrap.MonthField.superclass.constructor.call(this, config);
26 * Fires when this field show.
27 * @param {Roo.bootstrap.MonthField} this
28 * @param {Mixed} date The date value
33 * Fires when this field hide.
34 * @param {Roo.bootstrap.MonthField} this
35 * @param {Mixed} date The date value
40 * Fires when select a date.
41 * @param {Roo.bootstrap.MonthField} this
42 * @param {Mixed} date The date value
48 Roo.extend(Roo.bootstrap.MonthField, Roo.bootstrap.Input, {
52 onRender: function(ct, position)
55 Roo.bootstrap.MonthField.superclass.onRender.call(this, ct, position);
57 this.language = this.language || 'en';
58 this.language = this.language in Roo.bootstrap.MonthField.dates ? this.language : this.language.split('-')[0];
59 this.language = this.language in Roo.bootstrap.MonthField.dates ? this.language : "en";
61 this.isRTL = Roo.bootstrap.MonthField.dates[this.language].rtl || false;
62 this.format = this.format || 'F';
63 this.isInline = false;
65 this.component = this.el.select('.add-on', true).first() || false;
66 this.component = (this.component && this.component.length === 0) ? false : this.component;
67 this.hasInput = this.component && this.inputEL().length;
72 this.pickerEl = Roo.get(document.body).createChild(Roo.bootstrap.MonthField.template);
74 this.picker().setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
76 this.picker().on('mousedown', this.onMousedown, this);
77 this.picker().on('click', this.onClick, this);
79 this.picker().addClass('datepicker-dropdown');
81 this.startViewMode = this.viewMode;
83 Roo.each(this.picker().select('tbody > tr > td', true).elements, function(v){
84 v.setStyle('width', '189px');
98 var d = new Date(this.parseDate(v) ).clearTime();
100 if(isNaN(d.getTime())){
101 this.date = this.viewDate = '';
102 Roo.bootstrap.DateField.superclass.setValue.call(this, '');
106 v = this.formatDate(d);
108 Roo.bootstrap.DateField.superclass.setValue.call(this, v);
110 this.date = new Date(d.getTime() - d.getTimezoneOffset()*60000);
114 this.fireEvent('select', this, this.date);
123 var target = e.getTarget();
125 if(target.nodeName.toLowerCase() === 'i'){
126 target = Roo.get(target).dom.parentNode;
129 var nodeName = target.nodeName;
130 var className = target.className;
131 var html = target.innerHTML;
133 if(nodeName.toLowerCase() != 'span' || className.indexOf('disabled') > -1 || className.indexOf('month') == -1){
136 this.viewDate.setUTCDate(1);
137 this.viewDate.setUTCMonth(Roo.bootstrap.DateField.dates[this.language].monthsShort.indexOf(html));
139 this.setValue(this.formatDate(this.viewDate));
146 return this.pickerEl;
147 // return this.el.select('.datepicker', true).first();
150 fillMonths: function()
153 var months = this.picker().select('>.datepicker-months td', true).first();
155 months.dom.innerHTML = '';
161 html: Roo.bootstrap.DateField.dates[this.language].monthsShort[i++]
164 months.createChild(month);
171 this.date = (typeof(this.date) === 'undefined' || ((typeof(this.date) === 'string') && !this.date.length)) ? this.UTCToday() : (typeof(this.date) === 'string') ? this.parseDate(this.date) : this.date;
173 if (this.date < this.startDate) {
174 this.viewDate = new Date(this.startDate);
175 } else if (this.date > this.endDate) {
176 this.viewDate = new Date(this.endDate);
178 this.viewDate = new Date(this.date);
191 if(this.isInline) return;
193 this.picker().removeClass(['bottom', 'top']);
195 if((Roo.lib.Dom.getViewHeight() + Roo.get(document.body).getScroll().top) - (this.inputEl().getBottom() + this.picker().getHeight()) < 0){
197 * place to the top of element!
201 this.picker().addClass('top');
202 this.picker().setTop(this.inputEl().getTop() - this.picker().getHeight()).setLeft(this.inputEl().getLeft());
207 this.picker().addClass('bottom');
209 this.picker().setTop(this.inputEl().getBottom()).setLeft(this.inputEl().getLeft());
212 parseDate : function(value)
214 if(!value || value instanceof Date){
217 var v = Date.parseDate(value, this.format);
218 if (!v && (this.useIso || value.match(/^(\d{4})-0?(\d+)-0?(\d+)/))) {
219 v = Date.parseDate(value, 'Y-m-d');
221 if(!v && this.altFormats){
222 if(!this.altFormatsArray){
223 this.altFormatsArray = this.altFormats.split("|");
225 for(var i = 0, len = this.altFormatsArray.length; i < len && !v; i++){
226 v = Date.parseDate(value, this.altFormatsArray[i]);
232 formatDate : function(date, fmt)
234 return (!date || !(date instanceof Date)) ?
235 date : date.dateFormat(fmt || this.format);
240 Roo.bootstrap.DateField.superclass.onFocus.call(this);
246 Roo.bootstrap.DateField.superclass.onBlur.call(this);
248 var d = this.inputEl().getValue();
257 this.picker().show();
261 this.fireEvent('show', this, this.date);
266 if(this.isInline) return;
267 this.picker().hide();
268 this.viewMode = this.startViewMode;
271 this.fireEvent('hide', this, this.date);
275 onMousedown: function(e)
283 Roo.bootstrap.DateField.superclass.keyup.call(this);
289 return this.formatDate(this.date);
294 if (!this.picker().isVisible()){
295 if (e.keyCode == 27) // allow escape to hide and re-show picker
300 var dateChanged = false,
302 newDate, newViewDate;
311 if (!this.keyboardNavigation) break;
312 dir = e.keyCode == 37 ? -1 : 1;
315 newDate = this.moveYear(this.date, dir);
316 newViewDate = this.moveYear(this.viewDate, dir);
317 } else if (e.shiftKey){
318 newDate = this.moveMonth(this.date, dir);
319 newViewDate = this.moveMonth(this.viewDate, dir);
321 newDate = new Date(this.date);
322 newDate.setUTCDate(this.date.getUTCDate() + dir);
323 newViewDate = new Date(this.viewDate);
324 newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir);
326 if (this.dateWithinRange(newDate)){
328 this.viewDate = newViewDate;
329 this.setValue(this.formatDate(this.date));
337 if (!this.keyboardNavigation) break;
338 dir = e.keyCode == 38 ? -1 : 1;
340 newDate = this.moveYear(this.date, dir);
341 newViewDate = this.moveYear(this.viewDate, dir);
342 } else if (e.shiftKey){
343 newDate = this.moveMonth(this.date, dir);
344 newViewDate = this.moveMonth(this.viewDate, dir);
346 newDate = new Date(this.date);
347 newDate.setUTCDate(this.date.getUTCDate() + dir * 7);
348 newViewDate = new Date(this.viewDate);
349 newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir * 7);
351 if (this.dateWithinRange(newDate)){
353 this.viewDate = newViewDate;
354 this.setValue(this.formatDate(this.date));
361 this.setValue(this.formatDate(this.date));
366 this.setValue(this.formatDate(this.date));
379 setStartDate: function(startDate)
381 this.startDate = startDate || -Infinity;
382 if (this.startDate !== -Infinity) {
383 this.startDate = this.parseDate(this.startDate);
386 this.updateNavArrows();
389 setEndDate: function(endDate)
391 this.endDate = endDate || Infinity;
392 if (this.endDate !== Infinity) {
393 this.endDate = this.parseDate(this.endDate);
396 this.updateNavArrows();
399 setDaysOfWeekDisabled: function(daysOfWeekDisabled)
401 this.daysOfWeekDisabled = daysOfWeekDisabled || [];
402 if (typeof(this.daysOfWeekDisabled) !== 'object') {
403 this.daysOfWeekDisabled = this.daysOfWeekDisabled.split(/,\s*/);
405 this.daysOfWeekDisabled = this.daysOfWeekDisabled.map(function (d) {
406 return parseInt(d, 10);
409 this.updateNavArrows();
412 updateNavArrows: function()
418 var d = new Date(this.viewDate),
419 year = d.getUTCFullYear(),
420 month = d.getUTCMonth();
422 Roo.each(this.picker().select('.prev', true).elements, function(v){
424 switch (this.viewMode) {
427 if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear() && month <= this.startDate.getUTCMonth()) {
433 if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()) {
440 Roo.each(this.picker().select('.next', true).elements, function(v){
442 switch (this.viewMode) {
445 if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear() && month >= this.endDate.getUTCMonth()) {
451 if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()) {
459 moveMonth: function(date, dir)
461 if (!dir) return date;
462 var new_date = new Date(date.valueOf()),
463 day = new_date.getUTCDate(),
464 month = new_date.getUTCMonth(),
467 dir = dir > 0 ? 1 : -1;
470 // If going back one month, make sure month is not current month
471 // (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
473 return new_date.getUTCMonth() == month;
475 // If going forward one month, make sure month is as expected
476 // (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
478 return new_date.getUTCMonth() != new_month;
480 new_month = month + dir;
481 new_date.setUTCMonth(new_month);
482 // Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
483 if (new_month < 0 || new_month > 11)
484 new_month = (new_month + 12) % 12;
486 // For magnitudes >1, move one month at a time...
487 for (var i=0; i<mag; i++)
488 // ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
489 new_date = this.moveMonth(new_date, dir);
490 // ...then reset the day, keeping it in the new month
491 new_month = new_date.getUTCMonth();
492 new_date.setUTCDate(day);
494 return new_month != new_date.getUTCMonth();
497 // Common date-resetting loop -- if date is beyond end of month, make it
500 new_date.setUTCDate(--day);
501 new_date.setUTCMonth(new_month);
506 moveYear: function(date, dir)
508 return this.moveMonth(date, dir*12);
511 dateWithinRange: function(date)
513 return date >= this.startDate && date <= this.endDate;
519 this.picker().remove();
524 Roo.apply(Roo.bootstrap.DateField, {
535 html: '<i class="fa fa-arrow-left"/>'
545 html: '<i class="fa fa-arrow-right"/>'
587 days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
588 daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
589 daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
590 months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
591 monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
614 Roo.apply(Roo.bootstrap.DateField, {
618 cls: 'datepicker dropdown-menu roo-dynamic',
622 cls: 'datepicker-days',
626 cls: 'table-condensed',
628 Roo.bootstrap.DateField.head,
632 Roo.bootstrap.DateField.footer
639 cls: 'datepicker-months',
643 cls: 'table-condensed',
645 Roo.bootstrap.DateField.head,
646 Roo.bootstrap.DateField.content,
647 Roo.bootstrap.DateField.footer
654 cls: 'datepicker-years',
658 cls: 'table-condensed',
660 Roo.bootstrap.DateField.head,
661 Roo.bootstrap.DateField.content,
662 Roo.bootstrap.DateField.footer