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.update(new Date().clearTime());
281 resize : function() {
282 var sz = this.el.getSize();
284 this.el.select('.fc-day-header',true).setWidth(sz.width / 7);
285 this.el.select('.fc-day-content div',true).setHeight(34);
290 showPrevMonth : function(e){
291 this.update(this.activeDate.add("mo", -1));
293 showToday : function(e){
294 this.update(new Date().clearTime());
297 showNextMonth : function(e){
298 this.update(this.activeDate.add("mo", 1));
302 showPrevYear : function(){
303 this.update(this.activeDate.add("y", -1));
307 showNextYear : function(){
308 this.update(this.activeDate.add("y", 1));
313 update : function(date)
315 var vd = this.activeDate;
316 this.activeDate = date;
318 var t = date.getTime();
319 if(vd.getMonth() == date.getMonth() && vd.getFullYear() == date.getFullYear()){
320 Roo.log('using add remove');
321 this.cells.removeClass("fc-state-highlight");
322 this.cells.each(function(c){
323 if(c.dateValue == t){
324 c.addClass("fc-state-highlight");
325 setTimeout(function(){
326 try{c.dom.firstChild.focus();}catch(e){}
336 this.cells = this.el.select('.fc-day',true);
337 this.textNodes = this.el.query('.fc-day-number');
338 this.cells.addClassOnOver('fc-state-hover');
340 var days = date.getDaysInMonth();
342 var firstOfMonth = date.getFirstDateOfMonth();
343 var startingPos = firstOfMonth.getDay()-this.startDay;
345 if(startingPos < this.startDay){
349 var pm = date.add("mo", -1);
350 var prevStart = pm.getDaysInMonth()-startingPos;
352 var cells = this.cells.elements;
353 var textEls = this.textNodes;
356 // convert everything to numbers so it's fast
358 var d = (new Date(pm.getFullYear(), pm.getMonth(), prevStart)).clearTime();
359 var today = new Date().clearTime().getTime();
360 var sel = date.clearTime().getTime();
361 var min = this.minDate ? this.minDate.clearTime() : Number.NEGATIVE_INFINITY;
362 var max = this.maxDate ? this.maxDate.clearTime() : Number.POSITIVE_INFINITY;
363 var ddMatch = this.disabledDatesRE;
364 var ddText = this.disabledDatesText;
365 var ddays = this.disabledDays ? this.disabledDays.join("") : false;
366 var ddaysText = this.disabledDaysText;
367 var format = this.format;
369 var setCellClass = function(cal, cell){
375 cell.className += " fc-today";
376 cell.title = cal.todayText;
379 cell.className += " fc-state-highlight";
380 //setTimeout(function(){
381 // try{cell.firstChild.focus();}catch(e){}
386 cell.className = " fc-state-disabled";
387 cell.title = cal.minText;
391 cell.className = " fc-state-disabled";
392 cell.title = cal.maxText;
396 if(ddays.indexOf(d.getDay()) != -1){
397 cell.title = ddaysText;
398 cell.className = " fc-state-disabled";
401 if(ddMatch && format){
402 var fvalue = d.dateFormat(format);
403 if(ddMatch.test(fvalue)){
404 cell.title = ddText.replace("%0", fvalue);
405 cell.className = " fc-state-disabled";
408 if (!cell.initialClassName) {
409 cell.initialClassName = cell.dom.className;
411 cell.dom.className = cell.initialClassName + ' ' + cell.className;
415 for(; i < startingPos; i++) {
416 textEls[i].innerHTML = (++prevStart);
417 d.setDate(d.getDate()+1);
418 cells[i].className = "fc-past fc-other-month";
419 setCellClass(this, cells[i]);
421 for(; i < days; i++){
422 intDay = i - startingPos + 1;
423 textEls[i].innerHTML = (intDay);
424 d.setDate(d.getDate()+1);
425 cells[i].className = ''; // "x-date-active";
426 setCellClass(this, cells[i]);
431 textEls[i].innerHTML = (++extraDays);
432 d.setDate(d.getDate()+1);
433 cells[i].className = "fc-future fc-other-month";
434 setCellClass(this, cells[i]);
437 this.el.select('.fc-header-title h2',true).update(Date.monthNames[date.getMonth()] + " " + date.getFullYear());
439 var totalRows = Math.ceil((date.getDaysInMonth() + date.getFirstDateOfMonth().getDay()) / 7);
441 this.el.select('tr.fc-week.fc-prev-last',true).removeClass('fc-last');
442 this.el.select('tr.fc-week.fc-next-last',true).addClass('fc-last').show();
445 this.el.select('tr.fc-week.fc-last',true).removeClass('fc-last').addClass('fc-next-last').hide();
446 this.el.select('tr.fc-week.fc-prev-last',true).addClass('fc-last');
449 this.fireEvent('monthchange', this, date);
453 if(!this.internalRender){
454 var main = this.el.dom.firstChild;
455 var w = main.offsetWidth;
456 this.el.setWidth(w + this.el.getBorderWidth("lr"));
457 Roo.fly(main).setWidth(w);
458 this.internalRender = true;
459 // opera does not respect the auto grow header center column
460 // then, after it gets a width opera refuses to recalculate
461 // without a second pass
462 if(Roo.isOpera && !this.secondPass){
463 main.rows[0].cells[1].style.width = (w - (main.rows[0].cells[0].offsetWidth+main.rows[0].cells[2].offsetWidth)) + "px";
464 this.secondPass = true;
465 this.update.defer(10, this, [date]);
472 findCell : function(dt) {
473 dt = dt.clearTime().getTime();
475 this.cells.each(function(c){
476 //Roo.log("check " +c.dateValue + '?=' + dt);
477 if(c.dateValue == dt){
487 findCells : function(ev) {
488 var s = ev.start.clone().clearTime().getTime();
489 var e= ev.end.clone().clearTime().getTime();
491 this.cells.each(function(c){
492 //Roo.log("check " +c.dateValue + '<' + e + ' > ' + s);
506 findBestRow: function(cells)
510 for (var i =0 ; i < cells.length;i++) {
511 ret = Math.max(cells[i].rows || 0,ret);
514 //d.setDate(d.ev()+1);
519 addItem : function(ev)
521 // look for vertical location slot in
522 var cells = this.findCells(ev);
524 ev.row = this.findBestRow(cells);
526 // work out the location.
530 for(var i =0; i < cells.length; i++) {
538 if (crow.start.getY() == cells[i].getY()) {
555 for (var i = 0; i < cells.length;i++) {
556 cells[i].rows = Math.max(cells[i].rows || 0 , ev.row + 1 );
560 this.calevents.push(ev);
563 clearEvents: function() {
564 Roo.each(this.calevents, function(e) {
565 Roo.each(e.els, function(el) {
566 el.un('mouseenter' ,this.onEventEnter, this);
567 el.un('mouseleave' ,this.onEventLeave, this);
574 renderEvents: function()
577 // first make sure there is enough space..
578 this.cells.each(function(c) {
579 // Roo.log(c.select('.fc-day-content div',true).first(), Math.max(34, c.rows * 20));
580 c.select('.fc-day-content div',true).first().setHeight(Math.max(34, c.rows * 20));
583 for (var e = 0; e < this.calevents.length; e++) {
584 var ev = this.calevents[e];
585 var cells = ev.cells;
588 for(var i =0; i < rows.length; i++) {
591 // how many rows should it span..
594 cls : 'roo-dynamic fc-event fc-event-hori fc-event-draggable ui-draggable',
595 style : 'position: absolute', // left: 387px; width: 121px; top: 359px;
600 cls: 'fc-event-inner',
604 cls: 'fc-event-time',
605 html : cells.length > 1 ? '' : '7pm'
609 cls: 'fc-event-title',
610 html : String.format('{0}', ev.title)
617 cls: 'ui-resizable-handle ui-resizable-e',
618 html : '  '
624 cfg.cls += ' fc-event-start';
626 if ((i+1) == rows.length) {
627 cfg.cls += ' fc-event-end';
630 var ctr = this.el.select('.fc-event-container',true).first();
631 var cg = ctr.createChild(cfg);
633 cg.on('mouseenter' ,this.onEventEnter, this);
634 cg.on('mouseleave' ,this.onEventLeave, this);
638 var sbox = rows[i].start.select('.fc-day-content',true).first().getBox();
639 var ebox = rows[i].end.select('.fc-day-content',true).first().getBox();
641 cg.setXY([sbox.x +2, sbox.y +(ev.row * 20)]);
642 cg.setWidth(ebox.right - sbox.x -2);
649 onEventEnter: function (e, el,c,d) {
650 this.fireEvent('evententer', this, el);
653 onEventLeave: function (e, el,c,d) {
654 this.fireEvent('eventleave', this, el);
657 onLoad: function () {
659 if(this.store.getCount() > 0){
660 this.store.data.each(function(d){