Roo/grid/Calendar.js
[roojs1] / Roo / grid / Calendar.js
1 /*
2   
3  * Licence LGPL
4  
5  */
6  
7 /**
8  * @class Roo.grid.Calendar
9  * @extends Roo.util.Grid
10  * This class extends the Grid to provide a calendar widget
11  * <br><br>Usage:<pre><code>
12  var grid = new Roo.grid.Calendar("my-container-id", {
13      ds: myDataStore,
14      cm: myColModel,
15      selModel: mySelectionModel,
16      autoSizeColumns: true,
17      monitorWindowResize: false,
18      trackMouseOver: true
19      eventstore : real data store..
20  });
21  // set any options
22  grid.render();
23   
24   * @constructor
25  * @param {String/HTMLElement/Roo.Element} container The element into which this grid will be rendered -
26  * The container MUST have some type of size defined for the grid to fill. The container will be
27  * automatically set to position relative if it isn't already.
28  * @param {Object} config A config object that sets properties on this grid.
29  */
30 Roo.grid.Calendar = function(container, config){
31         // initialize the container
32         this.container = Roo.get(container);
33         this.container.update("");
34         this.container.setStyle("overflow", "hidden");
35     this.container.addClass('x-grid-container');
36
37     this.id = this.container.id;
38
39     Roo.apply(this, config);
40     // check and correct shorthanded configs
41     
42     var rows = [];
43     var d =1;
44     for (var r = 0;r < 6;r++) {
45         
46         rows[r]=[];
47         for (var c =0;c < 7;c++) {
48             rows[r][c]= '';
49         }
50     }
51     if (this.eventStore) {
52         this.eventStore= Roo.factory(this.eventStore, Roo.data);
53         this.eventStore.on('load',this.onLoad, this);
54         this.eventStore.on('beforeload',this.clearEvents, this);
55          
56     }
57     
58     this.dataSource = new Roo.data.Store({
59             proxy: new Roo.data.MemoryProxy(rows),
60             reader: new Roo.data.ArrayReader({}, [
61                    'weekday0', 'weekday1', 'weekday2', 'weekday3', 'weekday4', 'weekday5', 'weekday6' ])
62     });
63
64     this.dataSource.load();
65     this.ds = this.dataSource;
66     this.ds.xmodule = this.xmodule || false;
67     
68     
69     var cellRender = function(v,x,r)
70     {
71         return String.format(
72             '<div class="fc-day  fc-widget-content"><div>' +
73                 '<div class="fc-event-container"></div>' +
74                 '<div class="fc-day-number">{0}</div>'+
75                 
76                 '<div class="fc-day-content"><div style="position:relative"></div></div>' +
77             '</div></div>', v);
78     
79     }
80     
81     
82     this.colModel = new Roo.grid.ColumnModel( [
83         {
84             xtype: 'ColumnModel',
85             xns: Roo.grid,
86             dataIndex : 'weekday0',
87             header : 'Sunday',
88             renderer : cellRender
89         },
90         {
91             xtype: 'ColumnModel',
92             xns: Roo.grid,
93             dataIndex : 'weekday1',
94             header : 'Monday',
95             renderer : cellRender
96         },
97         {
98             xtype: 'ColumnModel',
99             xns: Roo.grid,
100             dataIndex : 'weekday2',
101             header : 'Tuesday',
102             renderer : cellRender
103         },
104         {
105             xtype: 'ColumnModel',
106             xns: Roo.grid,
107             dataIndex : 'weekday3',
108             header : 'Wednesday',
109             renderer : cellRender
110         },
111         {
112             xtype: 'ColumnModel',
113             xns: Roo.grid,
114             dataIndex : 'weekday4',
115             header : 'Thursday',
116             renderer : cellRender
117         },
118         {
119             xtype: 'ColumnModel',
120             xns: Roo.grid,
121             dataIndex : 'weekday5',
122             header : 'Friday',
123             renderer : cellRender
124         },
125         {
126             xtype: 'ColumnModel',
127             xns: Roo.grid,
128             dataIndex : 'weekday6',
129             header : 'Saturday',
130             renderer : cellRender
131         }
132     ]);
133     this.cm = this.colModel;
134     this.cm.xmodule = this.xmodule || false;
135  
136         
137           
138     //this.selModel = new Roo.grid.CellSelectionModel();
139     //this.sm = this.selModel;
140     //this.selModel.init(this);
141     
142     
143     if(this.width){
144         this.container.setWidth(this.width);
145     }
146
147     if(this.height){
148         this.container.setHeight(this.height);
149     }
150     /** @private */
151         this.addEvents({
152         // raw events
153         /**
154          * @event click
155          * The raw click event for the entire grid.
156          * @param {Roo.EventObject} e
157          */
158         "click" : true,
159         /**
160          * @event dblclick
161          * The raw dblclick event for the entire grid.
162          * @param {Roo.EventObject} e
163          */
164         "dblclick" : true,
165         /**
166          * @event contextmenu
167          * The raw contextmenu event for the entire grid.
168          * @param {Roo.EventObject} e
169          */
170         "contextmenu" : true,
171         /**
172          * @event mousedown
173          * The raw mousedown event for the entire grid.
174          * @param {Roo.EventObject} e
175          */
176         "mousedown" : true,
177         /**
178          * @event mouseup
179          * The raw mouseup event for the entire grid.
180          * @param {Roo.EventObject} e
181          */
182         "mouseup" : true,
183         /**
184          * @event mouseover
185          * The raw mouseover event for the entire grid.
186          * @param {Roo.EventObject} e
187          */
188         "mouseover" : true,
189         /**
190          * @event mouseout
191          * The raw mouseout event for the entire grid.
192          * @param {Roo.EventObject} e
193          */
194         "mouseout" : true,
195         /**
196          * @event keypress
197          * The raw keypress event for the entire grid.
198          * @param {Roo.EventObject} e
199          */
200         "keypress" : true,
201         /**
202          * @event keydown
203          * The raw keydown event for the entire grid.
204          * @param {Roo.EventObject} e
205          */
206         "keydown" : true,
207
208         // custom events
209
210         /**
211          * @event cellclick
212          * Fires when a cell is clicked
213          * @param {Grid} this
214          * @param {Number} rowIndex
215          * @param {Number} columnIndex
216          * @param {Roo.EventObject} e
217          */
218         "cellclick" : true,
219         /**
220          * @event celldblclick
221          * Fires when a cell is double clicked
222          * @param {Grid} this
223          * @param {Number} rowIndex
224          * @param {Number} columnIndex
225          * @param {Roo.EventObject} e
226          */
227         "celldblclick" : true,
228         /**
229          * @event rowclick
230          * Fires when a row is clicked
231          * @param {Grid} this
232          * @param {Number} rowIndex
233          * @param {Roo.EventObject} e
234          */
235         "rowclick" : true,
236         /**
237          * @event rowdblclick
238          * Fires when a row is double clicked
239          * @param {Grid} this
240          * @param {Number} rowIndex
241          * @param {Roo.EventObject} e
242          */
243         "rowdblclick" : true,
244         /**
245          * @event headerclick
246          * Fires when a header is clicked
247          * @param {Grid} this
248          * @param {Number} columnIndex
249          * @param {Roo.EventObject} e
250          */
251         "headerclick" : true,
252         /**
253          * @event headerdblclick
254          * Fires when a header cell is double clicked
255          * @param {Grid} this
256          * @param {Number} columnIndex
257          * @param {Roo.EventObject} e
258          */
259         "headerdblclick" : true,
260         /**
261          * @event rowcontextmenu
262          * Fires when a row is right clicked
263          * @param {Grid} this
264          * @param {Number} rowIndex
265          * @param {Roo.EventObject} e
266          */
267         "rowcontextmenu" : true,
268         /**
269          * @event cellcontextmenu
270          * Fires when a cell is right clicked
271          * @param {Grid} this
272          * @param {Number} rowIndex
273          * @param {Number} cellIndex
274          * @param {Roo.EventObject} e
275          */
276          "cellcontextmenu" : true,
277         /**
278          * @event headercontextmenu
279          * Fires when a header is right clicked
280          * @param {Grid} this
281          * @param {Number} columnIndex
282          * @param {Roo.EventObject} e
283          */
284         "headercontextmenu" : true,
285         /**
286          * @event bodyscroll
287          * Fires when the body element is scrolled
288          * @param {Number} scrollLeft
289          * @param {Number} scrollTop
290          */
291         "bodyscroll" : true,
292         /**
293          * @event columnresize
294          * Fires when the user resizes a column
295          * @param {Number} columnIndex
296          * @param {Number} newSize
297          */
298         "columnresize" : true,
299         /**
300          * @event columnmove
301          * Fires when the user moves a column
302          * @param {Number} oldIndex
303          * @param {Number} newIndex
304          */
305         "columnmove" : true,
306         /**
307          * @event startdrag
308          * Fires when row(s) start being dragged
309          * @param {Grid} this
310          * @param {Roo.GridDD} dd The drag drop object
311          * @param {event} e The raw browser event
312          */
313         "startdrag" : true,
314         /**
315          * @event enddrag
316          * Fires when a drag operation is complete
317          * @param {Grid} this
318          * @param {Roo.GridDD} dd The drag drop object
319          * @param {event} e The raw browser event
320          */
321         "enddrag" : true,
322         /**
323          * @event dragdrop
324          * Fires when dragged row(s) are dropped on a valid DD target
325          * @param {Grid} this
326          * @param {Roo.GridDD} dd The drag drop object
327          * @param {String} targetId The target drag drop object
328          * @param {event} e The raw browser event
329          */
330         "dragdrop" : true,
331         /**
332          * @event dragover
333          * Fires while row(s) are being dragged. "targetId" is the id of the Yahoo.util.DD object the selected rows are being dragged over.
334          * @param {Grid} this
335          * @param {Roo.GridDD} dd The drag drop object
336          * @param {String} targetId The target drag drop object
337          * @param {event} e The raw browser event
338          */
339         "dragover" : true,
340         /**
341          * @event dragenter
342          *  Fires when the dragged row(s) first cross another DD target while being dragged
343          * @param {Grid} this
344          * @param {Roo.GridDD} dd The drag drop object
345          * @param {String} targetId The target drag drop object
346          * @param {event} e The raw browser event
347          */
348         "dragenter" : true,
349         /**
350          * @event dragout
351          * Fires when the dragged row(s) leave another DD target while being dragged
352          * @param {Grid} this
353          * @param {Roo.GridDD} dd The drag drop object
354          * @param {String} targetId The target drag drop object
355          * @param {event} e The raw browser event
356          */
357         "dragout" : true,
358         /**
359          * @event rowclass
360          * Fires when a row is rendered, so you can change add a style to it.
361          * @param {GridView} gridview   The grid view
362          * @param {Object} rowcfg   contains record  rowIndex and rowClass - set rowClass to add a style.
363          */
364         'rowclass' : true,
365
366         /**
367          * @event render
368          * Fires when the grid is rendered
369          * @param {Grid} grid
370          */
371         'render' : true,
372             /**
373              * @event select
374              * Fires when a date is selected
375              * @param {DatePicker} this
376              * @param {Date} date The selected date
377              */
378         'select': true,
379         /**
380              * @event monthchange
381              * Fires when the displayed month changes 
382              * @param {DatePicker} this
383              * @param {Date} date The selected month
384              */
385         'monthchange': true,
386         /**
387              * @event evententer
388              * Fires when mouse over an event
389              * @param {Calendar} this
390              * @param {event} Event
391              */
392         'evententer': true,
393         /**
394              * @event eventleave
395              * Fires when the mouse leaves an
396              * @param {Calendar} this
397              * @param {event}
398              */
399         'eventleave': true,
400         /**
401              * @event eventclick
402              * Fires when the mouse click an
403              * @param {Calendar} this
404              * @param {event}
405              */
406         'eventclick': true
407     });
408
409     Roo.grid.Grid.superclass.constructor.call(this);
410     this.on('render', function() {
411         this.view.el.addClass('x-grid-cal'); 
412         
413         (function() { this.setDate(new Date()); }).defer(100,this); //default today..
414
415     },this);
416     
417     if (!Roo.grid.Calendar.style) {
418         Roo.grid.Calendar.style = Roo.util.CSS.createStyleSheet({
419             
420             
421             '.x-grid-cal .x-grid-col' :  {
422                 height: 'auto !important',
423                 'vertical-align': 'top'
424             },
425             '.x-grid-cal  .fc-event-hori' : {
426                 height: '14px'
427             }
428              
429             
430         }, Roo.id());
431     }
432
433     
434     
435 };
436 Roo.extend(Roo.grid.Calendar, Roo.grid.Grid, {
437     /**
438      * @cfg {Store} eventStore The store that loads events.
439      */
440     eventStore : 25,
441
442      
443     activeDate : false,
444     startDay : 0,
445     autoWidth : true,
446     monitorWindowResize : false,
447
448     
449     resizeColumns : function() {
450         var col = (this.view.el.getWidth() / 7) - 3;
451         // loop through cols, and setWidth
452         for(var i =0 ; i < 7 ; i++){
453             this.cm.setColumnWidth(i, col);
454         }
455     },
456      setDate :function(date) {
457         
458         Roo.log('setDate?');
459         
460         this.resizeColumns();
461         var vd = this.activeDate;
462         this.activeDate = date;
463 //        if(vd && this.el){
464 //            var t = date.getTime();
465 //            if(vd.getMonth() == date.getMonth() && vd.getFullYear() == date.getFullYear()){
466 //                Roo.log('using add remove');
467 //                
468 //                this.fireEvent('monthchange', this, date);
469 //                
470 //                this.cells.removeClass("fc-state-highlight");
471 //                this.cells.each(function(c){
472 //                   if(c.dateValue == t){
473 //                       c.addClass("fc-state-highlight");
474 //                       setTimeout(function(){
475 //                            try{c.dom.firstChild.focus();}catch(e){}
476 //                       }, 50);
477 //                       return false;
478 //                   }
479 //                   return true;
480 //                });
481 //                return;
482 //            }
483 //        }
484         
485         var days = date.getDaysInMonth();
486         
487         var firstOfMonth = date.getFirstDateOfMonth();
488         var startingPos = firstOfMonth.getDay()-this.startDay;
489         
490         if(startingPos < this.startDay){
491             startingPos += 7;
492         }
493         
494         var pm = date.add(Date.MONTH, -1);
495         var prevStart = pm.getDaysInMonth()-startingPos;
496 //        
497         
498         
499         this.cells = this.view.el.select('.x-grid-row .x-grid-col',true);
500         
501         this.textNodes = this.view.el.query('.x-grid-row .x-grid-col .x-grid-cell-text');
502         //this.cells.addClassOnOver('fc-state-hover');
503         
504         var cells = this.cells.elements;
505         var textEls = this.textNodes;
506         
507         //Roo.each(cells, function(cell){
508         //    cell.removeClass([ 'fc-past', 'fc-other-month', 'fc-future', 'fc-state-highlight', 'fc-state-disabled']);
509         //});
510         
511         days += startingPos;
512
513         // convert everything to numbers so it's fast
514         var day = 86400000;
515         var d = (new Date(pm.getFullYear(), pm.getMonth(), prevStart)).clearTime();
516         //Roo.log(d);
517         //Roo.log(pm);
518         //Roo.log(prevStart);
519         
520         var today = new Date().clearTime().getTime();
521         var sel = date.clearTime().getTime();
522         var min = this.minDate ? this.minDate.clearTime() : Number.NEGATIVE_INFINITY;
523         var max = this.maxDate ? this.maxDate.clearTime() : Number.POSITIVE_INFINITY;
524         var ddMatch = this.disabledDatesRE;
525         var ddText = this.disabledDatesText;
526         var ddays = this.disabledDays ? this.disabledDays.join("") : false;
527         var ddaysText = this.disabledDaysText;
528         var format = this.format;
529         
530         var setCellClass = function(cal, cell){
531             
532             //Roo.log('set Cell Class');
533             cell.title = "";
534             var t = d.getTime();
535             
536             //Roo.log(d);
537             
538             
539             cell.dateValue = t;
540             if(t == today){
541                 cell.className += " fc-today";
542                 cell.className += " fc-state-highlight";
543                 cell.title = cal.todayText;
544             }
545             if(t == sel){
546                 // disable highlight in other month..
547                 cell.className += " fc-state-highlight";
548                 
549             }
550             // disabling
551             if(t < min) {
552                 //cell.className = " fc-state-disabled";
553                 cell.title = cal.minText;
554                 return;
555             }
556             if(t > max) {
557                 //cell.className = " fc-state-disabled";
558                 cell.title = cal.maxText;
559                 return;
560             }
561             if(ddays){
562                 if(ddays.indexOf(d.getDay()) != -1){
563                     // cell.title = ddaysText;
564                    // cell.className = " fc-state-disabled";
565                 }
566             }
567             if(ddMatch && format){
568                 var fvalue = d.dateFormat(format);
569                 if(ddMatch.test(fvalue)){
570                     cell.title = ddText.replace("%0", fvalue);
571                    cell.className = " fc-state-disabled";
572                 }
573             }
574             
575             if (!cell.initialClassName) {
576                 cell.initialClassName = cell.dom.className;
577             }
578             
579             cell.dom.className = cell.initialClassName  + ' ' +  cell.className;
580         };
581
582         var i = 0;
583         
584         for(; i < startingPos; i++) {
585             cells[i].dayName =  (++prevStart);
586             Roo.log(textEls[i]);
587             d.setDate(d.getDate()+1);
588             
589             //cells[i].className = "fc-past fc-other-month";
590             setCellClass(this, cells[i]);
591         }
592         
593         var intDay = 0;
594         
595         for(; i < days; i++){
596             intDay = i - startingPos + 1;
597             cells[i].dayName =  (intDay);
598             d.setDate(d.getDate()+1);
599             
600             cells[i].className = ''; // "x-date-active";
601             setCellClass(this, cells[i]);
602         }
603         var extraDays = 0;
604         
605         for(; i < 42; i++) {
606             //textEls[i].innerHTML = (++extraDays);
607             
608             d.setDate(d.getDate()+1);
609             cells[i].dayName = (++extraDays);
610             cells[i].className = "fc-future fc-other-month";
611             setCellClass(this, cells[i]);
612         }
613         
614         //this.el.select('.fc-header-title h2',true).update(Date.monthNames[date.getMonth()] + " " + date.getFullYear());
615         
616         var totalRows = Math.ceil((date.getDaysInMonth() + date.getFirstDateOfMonth().getDay()) / 7);
617         
618         // this will cause all the cells to mis
619         var rows= [];
620         var i =0;
621         for (var r = 0;r < 6;r++) {
622             for (var c =0;c < 7;c++) {
623                 this.ds.getAt(r).set('weekday' + c ,cells[i++].dayName );
624             }    
625         }
626         
627         this.cells = this.view.el.select('.x-grid-row .x-grid-col',true);
628         for(i=0;i<cells.length;i++) {
629             
630             this.cells.elements[i].dayName = cells[i].dayName ;
631             this.cells.elements[i].className = cells[i].className;
632             this.cells.elements[i].initialClassName = cells[i].initialClassName ;
633             this.cells.elements[i].title = cells[i].title ;
634             this.cells.elements[i].dateValue = cells[i].dateValue ;
635         }
636         
637         
638         
639         
640         //this.el.select('tr.fc-week.fc-prev-last',true).removeClass('fc-last');
641         //this.el.select('tr.fc-week.fc-next-last',true).addClass('fc-last').show();
642         
643         ////if(totalRows != 6){
644             //this.el.select('tr.fc-week.fc-last',true).removeClass('fc-last').addClass('fc-next-last').hide();
645            // this.el.select('tr.fc-week.fc-prev-last',true).addClass('fc-last');
646        // }
647         
648         this.fireEvent('monthchange', this, date);
649         
650         
651     },
652  /**
653      * Returns the grid's SelectionModel.
654      * @return {SelectionModel}
655      */
656     getSelectionModel : function(){
657         if(!this.selModel){
658             this.selModel = new Roo.grid.CellSelectionModel();
659         }
660         return this.selModel;
661     },
662
663     load: function() {
664         this.eventStore.load()
665         
666         
667         
668     },
669     
670     findCell : function(dt) {
671         dt = dt.clearTime().getTime();
672         var ret = false;
673         this.cells.each(function(c){
674             //Roo.log("check " +c.dateValue + '?=' + dt);
675             if(c.dateValue == dt){
676                 ret = c;
677                 return false;
678             }
679             return true;
680         });
681         
682         return ret;
683     },
684     
685     findCells : function(rec) {
686         var s = rec.data.start_dt.clone().clearTime().getTime();
687        // Roo.log(s);
688         var e= rec.data.end_dt.clone().clearTime().getTime();
689        // Roo.log(e);
690         var ret = [];
691         this.cells.each(function(c){
692              ////Roo.log("check " +c.dateValue + '<' + e + ' > ' + s);
693             
694             if(c.dateValue > e){
695                 return ;
696             }
697             if(c.dateValue < s){
698                 return ;
699             }
700             ret.push(c);
701         });
702         
703         return ret;    
704     },
705     
706     findBestRow: function(cells)
707     {
708         var ret = 0;
709         
710         for (var i =0 ; i < cells.length;i++) {
711             ret  = Math.max(cells[i].rows || 0,ret);
712         }
713         return ret;
714         
715     },
716     
717     
718     addItem : function(rec)
719     {
720         // look for vertical location slot in
721         var cells = this.findCells(rec);
722         
723         rec.row = this.findBestRow(cells);
724         
725         // work out the location.
726         
727         var crow = false;
728         var rows = [];
729         for(var i =0; i < cells.length; i++) {
730             if (!crow) {
731                 crow = {
732                     start : cells[i],
733                     end :  cells[i]
734                 };
735                 continue;
736             }
737             if (crow.start.getY() == cells[i].getY()) {
738                 // on same row.
739                 crow.end = cells[i];
740                 continue;
741             }
742             // different row.
743             rows.push(crow);
744             crow = {
745                 start: cells[i],
746                 end : cells[i]
747             };
748             
749         }
750         
751         rows.push(crow);
752         rec.els = [];
753         rec.rows = rows;
754         rec.cells = cells;
755         for (var i = 0; i < cells.length;i++) {
756             cells[i].rows = Math.max(cells[i].rows || 0 , rec.row + 1 );
757             
758         }
759         
760         
761     },
762     
763     clearEvents: function() {
764         
765         if (!this.eventStore.getCount()) {
766             return;
767         }
768         // reset number of rows in cells.
769         Roo.each(this.cells.elements, function(c){
770             c.rows = 0;
771         });
772         
773         this.eventStore.each(function(e) {
774             Roo.each(e.els, function(el) {
775                 el.un('mouseenter' ,this.onEventEnter, this);
776                 el.un('mouseleave' ,this.onEventLeave, this);
777                 el.remove();
778             },this);
779             e.els = [];
780         },this);
781         
782     },
783     
784     
785     
786     
787     renderEvents: function()
788     {   
789         // first make sure there is enough space..
790         
791         if (!this.eventTmpl) {
792             this.eventTmpl = new Roo.Template(
793                 '<div class="roo-dynamic fc-event fc-event-hori fc-event-draggable ui-draggable {fccls} {cls}"  style="position: absolute" unselectable="on">' +
794                     '<div class="fc-event-inner">' +
795                         '<span class="fc-event-time">{time}</span>', +
796                         '<span class="fc-event-title" qtip="{qtip}">{title}</span>', +
797                     '</div>' +
798                     '<div class="ui-resizable-heandle ui-resizable-e">&nbsp;&nbsp;&nbsp;</div>' +
799                 '</div>'
800             );
801                 
802         }
803                
804         
805         
806         this.cells.each(function(c) {
807             Roo.log(c.select('.fc-day-content div',true).first());
808             c.select('.fc-day-content div',true).first().setHeight(Math.max(34, (c.rows || 1) * 20));
809         });
810         
811         var ctr = this.view.el.select('.fc-event-container',true).first();
812         
813         var cls;
814         this.eventStore.each(function(ev){
815             
816             ev.els = [];
817             var cells = ev.cells;
818             var rows = ev.rows;
819             var rec = ev.rec;
820             
821             for(var i =0; i < rows.length; i++) {
822                 
823                 cls = '';
824                 if (i == 0) {
825                     cls += ' fc-event-start';
826                 }
827                 if ((i+1) == rows.length) {
828                     cls += ' fc-event-end';
829                 }
830                 
831                 
832                 // how many rows should it span..
833                 var cg = this.eventTmpl.append(ctr,Roo.apply({
834                     fccls : cls
835                     
836                 }, rec.data) , true);
837                 
838                 
839                 cg.on('mouseenter' ,this.onEventEnter, this, ev);
840                 cg.on('mouseleave' ,this.onEventLeave, this, ev);
841                 cg.on('click', this.onEventClick, this, ev);
842                 
843                 ev.els.push(cg);
844                 
845                 var sbox = rows[i].start.select('.fc-day-content',true).first().getBox();
846                 var ebox = rows[i].end.select('.fc-day-content',true).first().getBox();
847                 //Roo.log(cg);
848                  
849                 cg.setXY([sbox.x +2, sbox.y +(ev.row * 20)]);    
850                 cg.setWidth(ebox.right - sbox.x -2);
851             }
852             
853             
854         }, this);
855         this.view.layout();
856         
857     },
858     
859     onEventEnter: function (e, el,event,d) {
860         this.fireEvent('evententer', this, el, event);
861     },
862     
863     onEventLeave: function (e, el,event,d) {
864         this.fireEvent('eventleave', this, el, event);
865     },
866     
867     onEventClick: function (e, el,event,d) {
868         this.fireEvent('eventclick', this, el, event);
869     },
870     
871     onMonthChange: function () {
872         this.store.load();
873     },
874     
875     onLoad: function () {
876         
877         this.clearEvents();
878         //Roo.log('calendar onload');
879 //         
880         if(this.eventStore.getCount() > 0){
881             
882            
883             
884             this.eventStore.each(function(d){
885                 
886                 
887                 // FIXME..
888                 var add =   d.data;
889                 if (typeof(add.end_dt) == 'undefined')  {
890                     Roo.log("Missing End time in calendar data: ");
891                     Roo.log(d);
892                     return;
893                 }
894                 if (typeof(add.start_dt) == 'undefined')  {
895                     Roo.log("Missing Start time in calendar data: ");
896                     Roo.log(d);
897                     return;
898                 }
899                 add.start_dt = typeof(add.start_dt) == 'string' ? Date.parseDate(add.start_dt,'Y-m-d H:i:s') : add.start_dt,
900                 add.end_dt = typeof(add.end_dt) == 'string' ? Date.parseDate(add.end_dt,'Y-m-d H:i:s') : add.end_dt,
901                 add.id = add.id || d.id;
902                 add.title = add.title || '??';
903                 
904                 this.addItem(d);
905                 
906              
907             },this);
908         }
909         
910         this.renderEvents();
911     }
912     
913
914 });
915 /*
916  grid : {
917                 xtype: 'Grid',
918                 xns: Roo.grid,
919                 listeners : {
920                     render : function ()
921                     {
922                         _this.grid = this;
923                         
924                         if (!this.view.el.hasClass('course-timesheet')) {
925                             this.view.el.addClass('course-timesheet');
926                         }
927                         if (this.tsStyle) {
928                             this.ds.load({});
929                             return; 
930                         }
931                         Roo.log('width');
932                         Roo.log(_this.grid.view.el.getWidth());
933                         
934                         
935                         this.tsStyle =  Roo.util.CSS.createStyleSheet({
936                             '.course-timesheet .x-grid-row' : {
937                                 height: '80px'
938                             },
939                             '.x-grid-row td' : {
940                                 'vertical-align' : 0
941                             },
942                             '.course-edit-link' : {
943                                 'color' : 'blue',
944                                 'text-overflow' : 'ellipsis',
945                                 'overflow' : 'hidden',
946                                 'white-space' : 'nowrap',
947                                 'cursor' : 'pointer'
948                             },
949                             '.sub-link' : {
950                                 'color' : 'green'
951                             },
952                             '.de-act-sup-link' : {
953                                 'color' : 'purple',
954                                 'text-decoration' : 'line-through'
955                             },
956                             '.de-act-link' : {
957                                 'color' : 'red',
958                                 'text-decoration' : 'line-through'
959                             },
960                             '.course-timesheet .course-highlight' : {
961                                 'border-top-style': 'dashed !important',
962                                 'border-bottom-bottom': 'dashed !important'
963                             },
964                             '.course-timesheet .course-item' : {
965                                 'font-family'   : 'tahoma, arial, helvetica',
966                                 'font-size'     : '11px',
967                                 'overflow'      : 'hidden',
968                                 'padding-left'  : '10px',
969                                 'padding-right' : '10px',
970                                 'padding-top' : '10px' 
971                             }
972                             
973                         }, Roo.id());
974                                 this.ds.load({});
975                     }
976                 },
977                 autoWidth : true,
978                 monitorWindowResize : false,
979                 cellrenderer : function(v,x,r)
980                 {
981                     return v;
982                 },
983                 sm : {
984                     xtype: 'CellSelectionModel',
985                     xns: Roo.grid
986                 },
987                 dataSource : {
988                     xtype: 'Store',
989                     xns: Roo.data,
990                     listeners : {
991                         beforeload : function (_self, options)
992                         {
993                             options.params = options.params || {};
994                             options.params._month = _this.monthField.getValue();
995                             options.params.limit = 9999;
996                             options.params['sort'] = 'when_dt';    
997                             options.params['dir'] = 'ASC';    
998                             this.proxy.loadResponse = this.loadResponse;
999                             Roo.log("load?");
1000                             //this.addColumns();
1001                         },
1002                         load : function (_self, records, options)
1003                         {
1004                             _this.grid.view.el.select('.course-edit-link', true).on('click', function() {
1005                                 // if you click on the translation.. you can edit it...
1006                                 var el = Roo.get(this);
1007                                 var id = el.dom.getAttribute('data-id');
1008                                 var d = el.dom.getAttribute('data-date');
1009                                 var t = el.dom.getAttribute('data-time');
1010                                 //var id = this.child('span').dom.textContent;
1011                                 
1012                                 //Roo.log(this);
1013                                 Pman.Dialog.CourseCalendar.show({
1014                                     id : id,
1015                                     when_d : d,
1016                                     when_t : t,
1017                                     productitem_active : id ? 1 : 0
1018                                 }, function() {
1019                                     _this.grid.ds.load({});
1020                                 });
1021                            
1022                            });
1023                            
1024                            _this.panel.fireEvent('resize', [ '', '' ]);
1025                         }
1026                     },
1027                     loadResponse : function(o, success, response){
1028                             // this is overridden on before load..
1029                             
1030                             Roo.log("our code?");       
1031                             //Roo.log(success);
1032                             //Roo.log(response)
1033                             delete this.activeRequest;
1034                             if(!success){
1035                                 this.fireEvent("loadexception", this, o, response);
1036                                 o.request.callback.call(o.request.scope, null, o.request.arg, false);
1037                                 return;
1038                             }
1039                             var result;
1040                             try {
1041                                 result = o.reader.read(response);
1042                             }catch(e){
1043                                 Roo.log("load exception?");
1044                                 this.fireEvent("loadexception", this, o, response, e);
1045                                 o.request.callback.call(o.request.scope, null, o.request.arg, false);
1046                                 return;
1047                             }
1048                             Roo.log("ready...");        
1049                             // loop through result.records;
1050                             // and set this.tdate[date] = [] << array of records..
1051                             _this.tdata  = {};
1052                             Roo.each(result.records, function(r){
1053                                 //Roo.log(r.data);
1054                                 if(typeof(_this.tdata[r.data.when_dt.format('j')]) == 'undefined'){
1055                                     _this.tdata[r.data.when_dt.format('j')] = [];
1056                                 }
1057                                 _this.tdata[r.data.when_dt.format('j')].push(r.data);
1058                             });
1059                             
1060                             //Roo.log(_this.tdata);
1061                             
1062                             result.records = [];
1063                             result.totalRecords = 6;
1064                     
1065                             // let's generate some duumy records for the rows.
1066                             //var st = _this.dateField.getValue();
1067                             
1068                             // work out monday..
1069                             //st = st.add(Date.DAY, -1 * st.format('w'));
1070                             
1071                             var date = Date.parseDate(_this.monthField.getValue(), "Y-m-d");
1072                             
1073                             var firstOfMonth = date.getFirstDayOfMonth();
1074                             var days = date.getDaysInMonth();
1075                             var d = 1;
1076                             var firstAdded = false;
1077                             for (var i = 0; i < result.totalRecords ; i++) {
1078                                 //var d= st.add(Date.DAY, i);
1079                                 var row = {};
1080                                 var added = 0;
1081                                 for(var w = 0 ; w < 7 ; w++){
1082                                     if(!firstAdded && firstOfMonth != w){
1083                                         continue;
1084                                     }
1085                                     if(d > days){
1086                                         continue;
1087                                     }
1088                                     firstAdded = true;
1089                                     var dd = (d > 0 && d < 10) ? "0"+d : d;
1090                                     row['weekday'+w] = String.format(
1091                                                     '<span style="font-size: 16px;"><b>{0}</b></span>'+
1092                                                     '<span class="course-edit-link" style="color:blue;" data-id="0" data-date="{1}"> Add New</span>',
1093                                                     d,
1094                                                     date.format('Y-m-')+dd
1095                                                 );
1096                                     added++;
1097                                     if(typeof(_this.tdata[d]) != 'undefined'){
1098                                         Roo.each(_this.tdata[d], function(r){
1099                                             var is_sub = '';
1100                                             var deactive = '';
1101                                             var id = r.id;
1102                                             var desc = (r.productitem_id_descrip) ? r.productitem_id_descrip : '';
1103                                             if(r.parent_id*1>0){
1104                                                 is_sub = (r.productitem_id_visible*1 < 1) ? 'de-act-sup-link' :'sub-link';
1105                                                 id = r.parent_id;
1106                                             }
1107                                             if(r.productitem_id_visible*1 < 1 && r.parent_id*1 < 1){
1108                                                 deactive = 'de-act-link';
1109                                             }
1110                                             
1111                                             row['weekday'+w] += String.format(
1112                                                     '<br /><span class="course-edit-link {3} {4}" qtip="{5}" data-id="{0}">{2} - {1}</span>',
1113                                                     id, //0
1114                                                     r.product_id_name, //1
1115                                                     r.when_dt.format('h:ia'), //2
1116                                                     is_sub, //3
1117                                                     deactive, //4
1118                                                     desc // 5
1119                                             );
1120                                         });
1121                                     }
1122                                     d++;
1123                                 }
1124                                 
1125                                 // only do this if something added..
1126                                 if(added > 0){ 
1127                                     result.records.push(_this.grid.dataSource.reader.newRow(row));
1128                                 }
1129                                 
1130                                 
1131                                 // push it twice. (second one with an hour..
1132                                 
1133                             }
1134                             //Roo.log(result);
1135                             this.fireEvent("load", this, o, o.request.arg);
1136                             o.request.callback.call(o.request.scope, result, o.request.arg, true);
1137                         },
1138                     sortInfo : {field: 'when_dt', direction : 'ASC' },
1139                     proxy : {
1140                         xtype: 'HttpProxy',
1141                         xns: Roo.data,
1142                         method : 'GET',
1143                         url : baseURL + '/Roo/Shop_course.php'
1144                     },
1145                     reader : {
1146                         xtype: 'JsonReader',
1147                         xns: Roo.data,
1148                         id : 'id',
1149                         fields : [
1150                             {
1151                                 'name': 'id',
1152                                 'type': 'int'
1153                             },
1154                             {
1155                                 'name': 'when_dt',
1156                                 'type': 'string'
1157                             },
1158                             {
1159                                 'name': 'end_dt',
1160                                 'type': 'string'
1161                             },
1162                             {
1163                                 'name': 'parent_id',
1164                                 'type': 'int'
1165                             },
1166                             {
1167                                 'name': 'product_id',
1168                                 'type': 'int'
1169                             },
1170                             {
1171                                 'name': 'productitem_id',
1172                                 'type': 'int'
1173                             },
1174                             {
1175                                 'name': 'guid',
1176                                 'type': 'int'
1177                             }
1178                         ]
1179                     }
1180                 },
1181                 toolbar : {
1182                     xtype: 'Toolbar',
1183                     xns: Roo,
1184                     items : [
1185                         {
1186                             xtype: 'Button',
1187                             xns: Roo.Toolbar,
1188                             listeners : {
1189                                 click : function (_self, e)
1190                                 {
1191                                     var sd = Date.parseDate(_this.monthField.getValue(), "Y-m-d");
1192                                     sd.setMonth(sd.getMonth()-1);
1193                                     _this.monthField.setValue(sd.format('Y-m-d'));
1194                                     _this.grid.ds.load({});
1195                                 }
1196                             },
1197                             text : "Back"
1198                         },
1199                         {
1200                             xtype: 'Separator',
1201                             xns: Roo.Toolbar
1202                         },
1203                         {
1204                             xtype: 'MonthField',
1205                             xns: Roo.form,
1206                             listeners : {
1207                                 render : function (_self)
1208                                 {
1209                                     _this.monthField = _self;
1210                                    // _this.monthField.set  today
1211                                 },
1212                                 select : function (combo, date)
1213                                 {
1214                                     _this.grid.ds.load({});
1215                                 }
1216                             },
1217                             value : (function() { return new Date(); })()
1218                         },
1219                         {
1220                             xtype: 'Separator',
1221                             xns: Roo.Toolbar
1222                         },
1223                         {
1224                             xtype: 'TextItem',
1225                             xns: Roo.Toolbar,
1226                             text : "Blue: in-active, green: in-active sup-event, red: de-active, purple: de-active sup-event"
1227                         },
1228                         {
1229                             xtype: 'Fill',
1230                             xns: Roo.Toolbar
1231                         },
1232                         {
1233                             xtype: 'Button',
1234                             xns: Roo.Toolbar,
1235                             listeners : {
1236                                 click : function (_self, e)
1237                                 {
1238                                     var sd = Date.parseDate(_this.monthField.getValue(), "Y-m-d");
1239                                     sd.setMonth(sd.getMonth()+1);
1240                                     _this.monthField.setValue(sd.format('Y-m-d'));
1241                                     _this.grid.ds.load({});
1242                                 }
1243                             },
1244                             text : "Next"
1245                         }
1246                     ]
1247                 },
1248                  
1249             }
1250         };
1251         
1252         */