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