4 * based on jquery fullcalendar
10 * @class Roo.bootstrap.Calendar
11 * @extends Roo.bootstrap.Component
12 * Bootstrap Calendar class
15 * Create a new Container
16 * @param {Object} config The config object
19 Roo.bootstrap.Calendar = function(config){
20 Roo.bootstrap.Calendar.superclass.constructor.call(this, config);
24 * Fires when a date is selected
25 * @param {DatePicker} this
26 * @param {Date} date The selected date
31 * Fires when the displayed month changes
32 * @param {DatePicker} this
33 * @param {Date} date The selected month
38 * Fires when mouse over an event
39 * @param {Calendar} this
40 * @param {event} Event
45 * Fires when the mouse leaves an
46 * @param {Calendar} this
55 Roo.extend(Roo.bootstrap.Calendar, Roo.bootstrap.Component, {
58 * @cfg {Number} startDay
59 * Day index at which the week should begin, 0-based (defaults to 0, which is Sunday)
63 getAutoCreate : function(){
66 fc_button = function(name, corner, style, content ) {
69 cls : 'fc-button fc-button-'+name+' fc-state-default ' +
71 'fc-corner-' + corner.split(' ').join(' fc-corner-') :
74 html : '<SPAN class="fc-text-'+style+ '">'+content +'</SPAN>',
89 cls : 'fc-header-left',
91 fc_button('prev', 'left', 'arrow', '‹' ),
92 fc_button('next', 'right', 'arrow', '›' ),
93 { tag: 'span', cls: 'fc-header-space' },
94 fc_button('today', 'left right', '', 'today' ) // neds state disabled..
102 cls : 'fc-header-center',
106 cls: 'fc-header-title',
109 html : 'month / year'
117 cls : 'fc-header-right',
119 /* fc_button('month', 'left', '', 'month' ),
120 fc_button('week', '', '', 'week' ),
121 fc_button('day', 'right', '', 'day' )
133 var cal_heads = function() {
135 // fixme - handle this.
137 for (var i =0; i < Date.dayNames.length; i++) {
138 var d = Date.dayNames[i];
141 cls : 'fc-day-header fc-' + d.substring(0,3).toLowerCase() + ' fc-widget-header',
142 html : d.substring(0,3)
146 ret[0].cls += ' fc-first';
147 ret[6].cls += ' fc-last';
150 var cal_cell = function(n) {
153 cls : 'fc-day fc-'+n + ' fc-widget-content', ///fc-other-month fc-past
158 cls: 'fc-day-number',
162 cls: 'fc-day-content',
166 style: 'position: relative;' // height: 17px;
178 var cal_rows = function() {
181 for (var r = 0; r < 6; r++) {
188 for (var i =0; i < Date.dayNames.length; i++) {
189 var d = Date.dayNames[i];
190 row.cn.push(cal_cell(d.substring(0,3).toLowerCase()));
193 row.cn[0].cls+=' fc-first';
194 row.cn[0].cn[0].style = 'min-height:90px';
195 row.cn[6].cls+=' fc-last';
199 ret[0].cls += ' fc-first';
200 ret[4].cls += ' fc-prev-last';
201 ret[5].cls += ' fc-last';
208 cls: 'fc-border-separate',
209 style : 'width:100%',
217 cls : 'fc-first fc-last',
236 style : "position: relative;",
239 cls : 'fc-view fc-view-month fc-grid',
240 style : 'position: relative',
244 cls : 'fc-event-container',
245 style : 'position:absolute;z-index:8;top:0;left:0;'
263 initEvents : function()
266 throw "can not find store for combo";
269 this.store = Roo.factory(this.store, Roo.data);
270 this.store.on('load', this.onLoad, this);
273 this.el.select('.fc-button-prev',true).on('click', this.showPrevMonth, this);
274 this.el.select('.fc-button-next',true).on('click', this.showNextMonth, this);
275 this.el.select('.fc-button-today',true).on('click', this.showToday, this);
276 this.el.select('.fc-button',true).addClassOnOver('fc-state-hover');
278 this.on('monthchange', this.onMonthChange, this);
280 this.update(new Date().clearTime());
283 resize : function() {
284 var sz = this.el.getSize();
286 this.el.select('.fc-day-header',true).setWidth(sz.width / 7);
287 this.el.select('.fc-day-content div',true).setHeight(34);
292 showPrevMonth : function(e){
293 this.update(this.activeDate.add("mo", -1));
295 showToday : function(e){
296 this.update(new Date().clearTime());
299 showNextMonth : function(e){
300 this.update(this.activeDate.add("mo", 1));
304 showPrevYear : function(){
305 this.update(this.activeDate.add("y", -1));
309 showNextYear : function(){
310 this.update(this.activeDate.add("y", 1));
315 update : function(date)
317 var vd = this.activeDate;
318 this.activeDate = date;
320 var t = date.getTime();
321 if(vd.getMonth() == date.getMonth() && vd.getFullYear() == date.getFullYear()){
322 Roo.log('using add remove');
323 this.cells.removeClass("fc-state-highlight");
324 this.cells.each(function(c){
325 if(c.dateValue == t){
326 c.addClass("fc-state-highlight");
327 setTimeout(function(){
328 try{c.dom.firstChild.focus();}catch(e){}
338 this.cells = this.el.select('.fc-day',true);
341 this.textNodes = this.el.query('.fc-day-number');
342 this.cells.addClassOnOver('fc-state-hover');
344 var days = date.getDaysInMonth();
346 var firstOfMonth = date.getFirstDateOfMonth();
347 var startingPos = firstOfMonth.getDay()-this.startDay;
349 if(startingPos < this.startDay){
353 var pm = date.add("mo", -1);
354 var prevStart = pm.getDaysInMonth()-startingPos;
356 var cells = this.cells.elements;
357 var textEls = this.textNodes;
360 // convert everything to numbers so it's fast
362 var d = (new Date(pm.getFullYear(), pm.getMonth(), prevStart)).clearTime();
363 var today = new Date().clearTime().getTime();
364 var sel = date.clearTime().getTime();
365 var min = this.minDate ? this.minDate.clearTime() : Number.NEGATIVE_INFINITY;
366 var max = this.maxDate ? this.maxDate.clearTime() : Number.POSITIVE_INFINITY;
367 var ddMatch = this.disabledDatesRE;
368 var ddText = this.disabledDatesText;
369 var ddays = this.disabledDays ? this.disabledDays.join("") : false;
370 var ddaysText = this.disabledDaysText;
371 var format = this.format;
373 var setCellClass = function(cal, cell){
379 cell.className += " fc-today";
380 cell.title = cal.todayText;
383 cell.className += " fc-state-highlight";
384 //setTimeout(function(){
385 // try{cell.firstChild.focus();}catch(e){}
390 cell.className = " fc-state-disabled";
391 cell.title = cal.minText;
395 cell.className = " fc-state-disabled";
396 cell.title = cal.maxText;
400 if(ddays.indexOf(d.getDay()) != -1){
401 cell.title = ddaysText;
402 cell.className = " fc-state-disabled";
405 if(ddMatch && format){
406 var fvalue = d.dateFormat(format);
407 if(ddMatch.test(fvalue)){
408 cell.title = ddText.replace("%0", fvalue);
409 cell.className = " fc-state-disabled";
412 if (!cell.initialClassName) {
413 cell.initialClassName = cell.dom.className;
415 cell.dom.className = cell.initialClassName + ' ' + cell.className;
419 for(; i < startingPos; i++) {
420 textEls[i].innerHTML = (++prevStart);
421 d.setDate(d.getDate()+1);
422 cells[i].className = "fc-past fc-other-month";
423 setCellClass(this, cells[i]);
425 for(; i < days; i++){
426 intDay = i - startingPos + 1;
427 textEls[i].innerHTML = (intDay);
428 d.setDate(d.getDate()+1);
429 cells[i].className = ''; // "x-date-active";
430 setCellClass(this, cells[i]);
435 textEls[i].innerHTML = (++extraDays);
436 d.setDate(d.getDate()+1);
437 cells[i].className = "fc-future fc-other-month";
438 setCellClass(this, cells[i]);
441 this.el.select('.fc-header-title h2',true).update(Date.monthNames[date.getMonth()] + " " + date.getFullYear());
443 var totalRows = Math.ceil((date.getDaysInMonth() + date.getFirstDateOfMonth().getDay()) / 7);
445 this.el.select('tr.fc-week.fc-prev-last',true).removeClass('fc-last');
446 this.el.select('tr.fc-week.fc-next-last',true).addClass('fc-last').show();
449 this.el.select('tr.fc-week.fc-last',true).removeClass('fc-last').addClass('fc-next-last').hide();
450 this.el.select('tr.fc-week.fc-prev-last',true).addClass('fc-last');
453 this.fireEvent('monthchange', this, date);
457 if(!this.internalRender){
458 var main = this.el.dom.firstChild;
459 var w = main.offsetWidth;
460 this.el.setWidth(w + this.el.getBorderWidth("lr"));
461 Roo.fly(main).setWidth(w);
462 this.internalRender = true;
463 // opera does not respect the auto grow header center column
464 // then, after it gets a width opera refuses to recalculate
465 // without a second pass
466 if(Roo.isOpera && !this.secondPass){
467 main.rows[0].cells[1].style.width = (w - (main.rows[0].cells[0].offsetWidth+main.rows[0].cells[2].offsetWidth)) + "px";
468 this.secondPass = true;
469 this.update.defer(10, this, [date]);
476 findCell : function(dt) {
477 dt = dt.clearTime().getTime();
479 this.cells.each(function(c){
480 //Roo.log("check " +c.dateValue + '?=' + dt);
481 if(c.dateValue == dt){
491 findCells : function(ev) {
492 var s = ev.start.clone().clearTime().getTime();
493 var e= ev.end.clone().clearTime().getTime();
495 this.cells.each(function(c){
496 //Roo.log("check " +c.dateValue + '<' + e + ' > ' + s);
510 findBestRow: function(cells)
514 for (var i =0 ; i < cells.length;i++) {
515 ret = Math.max(cells[i].rows || 0,ret);
518 //d.setDate(d.ev()+1);
523 addItem : function(ev)
525 // look for vertical location slot in
526 var cells = this.findCells(ev);
528 ev.row = this.findBestRow(cells);
530 // work out the location.
534 for(var i =0; i < cells.length; i++) {
542 if (crow.start.getY() == cells[i].getY()) {
560 for (var i = 0; i < cells.length;i++) {
561 cells[i].rows = Math.max(cells[i].rows || 0 , ev.row + 1 );
565 this.calevents.push(ev);
568 clearEvents: function() {
574 Roo.each(this.calevents, function(e) {
575 Roo.each(e.els, function(el) {
576 el.un('mouseenter' ,this.onEventEnter, this);
577 el.un('mouseleave' ,this.onEventLeave, this);
584 renderEvents: function()
586 // first make sure there is enough space..
587 this.cells.each(function(c) {
588 // Roo.log(c.select('.fc-day-content div',true).first(), Math.max(34, c.rows * 20));
589 c.select('.fc-day-content div',true).first().setHeight(Math.max(34, c.rows * 20));
592 for (var e = 0; e < this.calevents.length; e++) {
593 var ev = this.calevents[e];
594 var cells = ev.cells;
597 for(var i =0; i < rows.length; i++) {
600 // how many rows should it span..
603 cls : 'roo-dynamic fc-event fc-event-hori fc-event-draggable ui-draggable',
604 style : 'position: absolute', // left: 387px; width: 121px; top: 359px;
609 cls: 'fc-event-inner',
613 cls: 'fc-event-time',
614 html : cells.length > 1 ? '' : '7pm'
618 cls: 'fc-event-title',
619 html : String.format('{0}', ev.title)
626 cls: 'ui-resizable-handle ui-resizable-e',
627 html : '  '
633 cfg.cls += ' fc-event-start';
635 if ((i+1) == rows.length) {
636 cfg.cls += ' fc-event-end';
639 var ctr = this.el.select('.fc-event-container',true).first();
640 var cg = ctr.createChild(cfg);
642 cg.on('mouseenter' ,this.onEventEnter, this);
643 cg.on('mouseleave' ,this.onEventLeave, this);
646 var sbox = rows[i].start.select('.fc-day-content',true).first().getBox();
647 var ebox = rows[i].end.select('.fc-day-content',true).first().getBox();
649 cg.setXY([sbox.x +2, sbox.y +(ev.row * 20)]);
650 cg.setWidth(ebox.right - sbox.x -2);
657 onEventEnter: function (e, el,c,d) {
658 this.fireEvent('evententer', this, el);
661 onEventLeave: function (e, el,c,d) {
662 this.fireEvent('eventleave', this, el);
665 onMonthChange: function () {
669 onLoad: function () {
675 if(this.store.getCount() > 0){
676 this.store.data.each(function(d){
678 start: new Date(d.data.start_dt),
679 end : new Date(d.data.end_dt),
684 Roo.log(this.calevents);
685 // this.renderEvents();