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               (function() { this.setDate(new Date()); }).defer(100,this); //default today..
411
412     });
413     
414 };
415 Roo.extend(Roo.grid.Calendar, Roo.grid.Grid, {
416     /**
417      * @cfg {Store} eventStore The store that loads events.
418      */
419     eventStore : 25,
420
421     
422     activeDate : false,
423     startDay : 0,
424     autoWidth : true,
425     monitorWindowResize : false,
426
427     
428     resizeColumns : function() {
429         var col = (this.view.el.getWidth() / 7) - 3;
430         // loop through cols, and setWidth
431         for(var i =0 ; i < 7 ; i++){
432             this.cm.setColumnWidth(i, col);
433         }
434     },
435      setDate :function(date) {
436           this.resizeColumns();
437         var vd = this.activeDate;
438         this.activeDate = date;
439 //        if(vd && this.el){
440 //            var t = date.getTime();
441 //            if(vd.getMonth() == date.getMonth() && vd.getFullYear() == date.getFullYear()){
442 //                Roo.log('using add remove');
443 //                
444 //                this.fireEvent('monthchange', this, date);
445 //                
446 //                this.cells.removeClass("fc-state-highlight");
447 //                this.cells.each(function(c){
448 //                   if(c.dateValue == t){
449 //                       c.addClass("fc-state-highlight");
450 //                       setTimeout(function(){
451 //                            try{c.dom.firstChild.focus();}catch(e){}
452 //                       }, 50);
453 //                       return false;
454 //                   }
455 //                   return true;
456 //                });
457 //                return;
458 //            }
459 //        }
460         
461         var days = date.getDaysInMonth();
462         
463         var firstOfMonth = date.getFirstDateOfMonth();
464         var startingPos = firstOfMonth.getDay()-this.startDay;
465         
466         if(startingPos < this.startDay){
467             startingPos += 7;
468         }
469         
470         var pm = date.add(Date.MONTH, -1);
471         var prevStart = pm.getDaysInMonth()-startingPos;
472 //        
473         
474         
475         this.cells = this.view.el.select('.x-grid-row .x-grid-col',true);
476         
477         this.textNodes = this.view.el.query('.x-grid-row .x-grid-col .x-grid-cell-text');
478         //this.cells.addClassOnOver('fc-state-hover');
479         
480         var cells = this.cells.elements;
481         var textEls = this.textNodes;
482         
483         //Roo.each(cells, function(cell){
484         //    cell.removeClass([ 'fc-past', 'fc-other-month', 'fc-future', 'fc-state-highlight', 'fc-state-disabled']);
485         //});
486         
487         days += startingPos;
488
489         // convert everything to numbers so it's fast
490         var day = 86400000;
491         var d = (new Date(pm.getFullYear(), pm.getMonth(), prevStart)).clearTime();
492         //Roo.log(d);
493         //Roo.log(pm);
494         //Roo.log(prevStart);
495         
496         var today = new Date().clearTime().getTime();
497         var sel = date.clearTime().getTime();
498         var min = this.minDate ? this.minDate.clearTime() : Number.NEGATIVE_INFINITY;
499         var max = this.maxDate ? this.maxDate.clearTime() : Number.POSITIVE_INFINITY;
500         var ddMatch = this.disabledDatesRE;
501         var ddText = this.disabledDatesText;
502         var ddays = this.disabledDays ? this.disabledDays.join("") : false;
503         var ddaysText = this.disabledDaysText;
504         var format = this.format;
505         
506         var setCellClass = function(cal, cell){
507             
508             //Roo.log('set Cell Class');
509             cell.title = "";
510             var t = d.getTime();
511             
512             //Roo.log(d);
513             
514             cell.dateValue = t;
515             if(t == today){
516                // cell.className += " fc-today";
517                // cell.className += " fc-state-highlight";
518                 cell.title = cal.todayText;
519             }
520             if(t == sel){
521                 // disable highlight in other month..
522                 //cell.className += " fc-state-highlight";
523                 
524             }
525             // disabling
526             if(t < min) {
527                 //cell.className = " fc-state-disabled";
528                 cell.title = cal.minText;
529                 return;
530             }
531             if(t > max) {
532                 //cell.className = " fc-state-disabled";
533                 cell.title = cal.maxText;
534                 return;
535             }
536             if(ddays){
537                 if(ddays.indexOf(d.getDay()) != -1){
538                     // cell.title = ddaysText;
539                    // cell.className = " fc-state-disabled";
540                 }
541             }
542             if(ddMatch && format){
543                 var fvalue = d.dateFormat(format);
544                 if(ddMatch.test(fvalue)){
545                     cell.title = ddText.replace("%0", fvalue);
546                    // cell.className = " fc-state-disabled";
547                 }
548             }
549             
550             if (!cell.initialClassName) {
551                 //cell.initialClassName = cell.dom.className;
552             }
553             
554             cell.dom.className = cell.initialClassName  + ' ' +  cell.className;
555         };
556
557         var i = 0;
558         
559         for(; i < startingPos; i++) {
560             cells[i].dayName =  (++prevStart);
561             Roo.log(textEls[i]);
562             d.setDate(d.getDate()+1);
563             
564             //cells[i].className = "fc-past fc-other-month";
565             setCellClass(this, cells[i]);
566         }
567         
568         var intDay = 0;
569         
570         for(; i < days; i++){
571             intDay = i - startingPos + 1;
572             cells[i].dayName =  (intDay);
573             d.setDate(d.getDate()+1);
574             
575             cells[i].className = ''; // "x-date-active";
576             setCellClass(this, cells[i]);
577         }
578         var extraDays = 0;
579         
580         for(; i < 42; i++) {
581             //textEls[i].innerHTML = (++extraDays);
582             
583             d.setDate(d.getDate()+1);
584             cells[i].dayName = (++extraDays);
585             cells[i].className = "fc-future fc-other-month";
586             setCellClass(this, cells[i]);
587         }
588         
589         //this.el.select('.fc-header-title h2',true).update(Date.monthNames[date.getMonth()] + " " + date.getFullYear());
590         
591         var totalRows = Math.ceil((date.getDaysInMonth() + date.getFirstDateOfMonth().getDay()) / 7);
592         
593         var rows= [];
594         var i =0;
595         for (var r = 0;r < 6;r++) {
596             for (var c =0;c < 7;c++) {
597                 this.ds.getAt(r).set('weekday' + c ,cells[i++].dayName );
598             }
599             
600         }
601         
602         
603         //this.el.select('tr.fc-week.fc-prev-last',true).removeClass('fc-last');
604         //this.el.select('tr.fc-week.fc-next-last',true).addClass('fc-last').show();
605         
606         ////if(totalRows != 6){
607             //this.el.select('tr.fc-week.fc-last',true).removeClass('fc-last').addClass('fc-next-last').hide();
608            // this.el.select('tr.fc-week.fc-prev-last',true).addClass('fc-last');
609        // }
610         
611         this.fireEvent('monthchange', this, date);
612         
613         
614     },
615  /**
616      * Returns the grid's SelectionModel.
617      * @return {SelectionModel}
618      */
619     getSelectionModel : function(){
620         if(!this.selModel){
621             this.selModel = new Roo.grid.CellSelectionModel();
622         }
623         return this.selModel;
624     },
625
626     load: function() {
627         this.eventStore.load()
628         
629         
630         
631     },
632     
633     findCell : function(dt) {
634         dt = dt.clearTime().getTime();
635         var ret = false;
636         this.cells.each(function(c){
637             //Roo.log("check " +c.dateValue + '?=' + dt);
638             if(c.dateValue == dt){
639                 ret = c;
640                 return false;
641             }
642             return true;
643         });
644         
645         return ret;
646     },
647     
648     findCells : function(ev) {
649         var s = ev.start_dt.clone().clearTime().getTime();
650        // Roo.log(s);
651         var e= ev.end_dt.clone().clearTime().getTime();
652        // Roo.log(e);
653         var ret = [];
654         this.cells.each(function(c){
655              ////Roo.log("check " +c.dateValue + '<' + e + ' > ' + s);
656             
657             if(c.dateValue > e){
658                 return ;
659             }
660             if(c.dateValue < s){
661                 return ;
662             }
663             ret.push(c);
664         });
665         
666         return ret;    
667     },
668     
669     findBestRow: function(cells)
670     {
671         var ret = 0;
672         
673         for (var i =0 ; i < cells.length;i++) {
674             ret  = Math.max(cells[i].rows || 0,ret);
675         }
676         return ret;
677         
678     },
679     
680     
681     addItem : function(ev)
682     {
683         // look for vertical location slot in
684         var cells = this.findCells(ev);
685         
686         ev.row = this.findBestRow(cells);
687         
688         // work out the location.
689         
690         var crow = false;
691         var rows = [];
692         for(var i =0; i < cells.length; i++) {
693             if (!crow) {
694                 crow = {
695                     start : cells[i],
696                     end :  cells[i]
697                 };
698                 continue;
699             }
700             if (crow.start.getY() == cells[i].getY()) {
701                 // on same row.
702                 crow.end = cells[i];
703                 continue;
704             }
705             // different row.
706             rows.push(crow);
707             crow = {
708                 start: cells[i],
709                 end : cells[i]
710             };
711             
712         }
713         
714         rows.push(crow);
715         ev.els = [];
716         ev.rows = rows;
717         ev.cells = cells;
718         for (var i = 0; i < cells.length;i++) {
719             cells[i].rows = Math.max(cells[i].rows || 0 , ev.row + 1 );
720             
721         }
722         
723         this.calevents.push(ev);
724     },
725     
726     clearEvents: function() {
727         
728         if(!this.calevents){
729             return;
730         }
731         
732         Roo.each(this.cells.elements, function(c){
733             c.rows = 0;
734         });
735         
736         Roo.each(this.calevents, function(e) {
737             Roo.each(e.els, function(el) {
738                 el.un('mouseenter' ,this.onEventEnter, this);
739                 el.un('mouseleave' ,this.onEventLeave, this);
740                 el.remove();
741             },this);
742         },this);
743         
744     },
745     
746     renderEvents: function()
747     {   
748         // first make sure there is enough space..
749         
750         this.cells.each(function(c) {
751             c.select('.fc-day-content div',true).first().setHeight(Math.max(34, c.rows * 20));
752         });
753         
754         for (var e = 0; e < this.calevents.length; e++) {
755             var ev = this.calevents[e];
756             var cells = ev.cells;
757             var rows = ev.rows;
758             
759             for(var i =0; i < rows.length; i++) {
760                 
761                  
762                 // how many rows should it span..
763                 
764                 var  cfg = {
765                     cls : 'roo-dynamic fc-event fc-event-hori fc-event-draggable ui-draggable',
766                     style : 'position: absolute', // left: 387px; width: 121px; top: 359px;
767                     
768                     unselectable : "on",
769                     cn : [
770                         {
771                             cls: 'fc-event-inner',
772                             cn : [
773                                 {
774                                   tag:'span',
775                                   cls: 'fc-event-time',
776                                   html : cells.length > 1 ? '' : ev.start_dt.format('h:ia')
777                                 },
778                                 {
779                                   tag:'span',
780                                   cls: 'fc-event-title',
781                                   html : String.format('{0}', ev.title)
782                                 }
783                                 
784                                 
785                             ]
786                         },
787                         {
788                             cls: 'ui-resizable-handle ui-resizable-e',
789                             html : '&nbsp;&nbsp;&nbsp'
790                         }
791                         
792                     ]
793                 };
794                 if (i == 0) {
795                     cfg.cls += ' fc-event-start';
796                 }
797                 if ((i+1) == rows.length) {
798                     cfg.cls += ' fc-event-end';
799                 }
800                 
801                 var ctr = this.el.select('.fc-event-container',true).first();
802                 var cg = ctr.createChild(cfg);
803                 
804                 cg.on('mouseenter' ,this.onEventEnter, this, ev);
805                 cg.on('mouseleave' ,this.onEventLeave, this, ev);
806                 cg.on('click', this.onEventClick, this, ev);
807                 
808                 ev.els.push(cg);
809                 
810                 var sbox = rows[i].start.select('.fc-day-content',true).first().getBox();
811                 var ebox = rows[i].end.select('.fc-day-content',true).first().getBox();
812                 //Roo.log(cg);
813                 cg.setXY([sbox.x +2, sbox.y +(ev.row * 20)]);    
814                 cg.setWidth(ebox.right - sbox.x -2);
815             }
816             
817             
818         }
819         
820     },
821     
822     onEventEnter: function (e, el,event,d) {
823         this.fireEvent('evententer', this, el, event);
824     },
825     
826     onEventLeave: function (e, el,event,d) {
827         this.fireEvent('eventleave', this, el, event);
828     },
829     
830     onEventClick: function (e, el,event,d) {
831         this.fireEvent('eventclick', this, el, event);
832     },
833     
834     onMonthChange: function () {
835         this.store.load();
836     },
837     
838     onLoad: function () {
839         
840         this.clearEvents();
841         //Roo.log('calendar onload');
842 //        
843         this.calevents = [];
844          
845         if(this.eventStore.getCount() > 0){
846             this.eventStore.data.each(function(d){
847                 
848                 
849                 // FIXME..
850                 var add = Roo.apply({}, d.data);
851                 if (typeof(add.end_dt) == 'undefined')  {
852                     Roo.log("Missing End time in calendar data: ");
853                     Roo.log(d);
854                     return;
855                 }
856                 if (typeof(add.start_dt) == 'undefined')  {
857                     Roo.log("Missing Start time in calendar data: ");
858                     Roo.log(d);
859                     return;
860                 }
861                 add.start_dt = typeof(add.start_dt) == 'string' ? Date.parseDate(add.start_dt,'Y-m-d H:i:s') : add.start_dt,
862                 add.end_dt = typeof(add.end_dt) == 'string' ? Date.parseDate(add.end_dt,'Y-m-d H:i:s') : add.end_dt,
863                 add.id = add.id || d.id;
864                 add.title = add.title || '??';
865                 this.addItem(add);
866                 
867                 // other than the 'required' coluns, we should just pass the data from the store.
868                 
869                 
870                /*cal.addItem({
871                     id : d.data.id,
872                     start: new Date(d.data.start_dt),
873                     end : new Date(d.data.end_dt),
874                     time : d.data.start_time,
875                     title : d.data.title,
876                     description : d.data.description,
877                     venue : d.data.venue
878                 });*/
879             },this);
880         }
881         
882         this.renderEvents();
883     }
884     
885
886 });
887 /*
888  grid : {
889                 xtype: 'Grid',
890                 xns: Roo.grid,
891                 listeners : {
892                     render : function ()
893                     {
894                         _this.grid = this;
895                         
896                         if (!this.view.el.hasClass('course-timesheet')) {
897                             this.view.el.addClass('course-timesheet');
898                         }
899                         if (this.tsStyle) {
900                             this.ds.load({});
901                             return; 
902                         }
903                         Roo.log('width');
904                         Roo.log(_this.grid.view.el.getWidth());
905                         
906                         
907                         this.tsStyle =  Roo.util.CSS.createStyleSheet({
908                             '.course-timesheet .x-grid-row' : {
909                                 height: '80px'
910                             },
911                             '.x-grid-row td' : {
912                                 'vertical-align' : 0
913                             },
914                             '.course-edit-link' : {
915                                 'color' : 'blue',
916                                 'text-overflow' : 'ellipsis',
917                                 'overflow' : 'hidden',
918                                 'white-space' : 'nowrap',
919                                 'cursor' : 'pointer'
920                             },
921                             '.sub-link' : {
922                                 'color' : 'green'
923                             },
924                             '.de-act-sup-link' : {
925                                 'color' : 'purple',
926                                 'text-decoration' : 'line-through'
927                             },
928                             '.de-act-link' : {
929                                 'color' : 'red',
930                                 'text-decoration' : 'line-through'
931                             },
932                             '.course-timesheet .course-highlight' : {
933                                 'border-top-style': 'dashed !important',
934                                 'border-bottom-bottom': 'dashed !important'
935                             },
936                             '.course-timesheet .course-item' : {
937                                 'font-family'   : 'tahoma, arial, helvetica',
938                                 'font-size'     : '11px',
939                                 'overflow'      : 'hidden',
940                                 'padding-left'  : '10px',
941                                 'padding-right' : '10px',
942                                 'padding-top' : '10px' 
943                             }
944                             
945                         }, Roo.id());
946                                 this.ds.load({});
947                     }
948                 },
949                 autoWidth : true,
950                 monitorWindowResize : false,
951                 cellrenderer : function(v,x,r)
952                 {
953                     return v;
954                 },
955                 sm : {
956                     xtype: 'CellSelectionModel',
957                     xns: Roo.grid
958                 },
959                 dataSource : {
960                     xtype: 'Store',
961                     xns: Roo.data,
962                     listeners : {
963                         beforeload : function (_self, options)
964                         {
965                             options.params = options.params || {};
966                             options.params._month = _this.monthField.getValue();
967                             options.params.limit = 9999;
968                             options.params['sort'] = 'when_dt';    
969                             options.params['dir'] = 'ASC';    
970                             this.proxy.loadResponse = this.loadResponse;
971                             Roo.log("load?");
972                             //this.addColumns();
973                         },
974                         load : function (_self, records, options)
975                         {
976                             _this.grid.view.el.select('.course-edit-link', true).on('click', function() {
977                                 // if you click on the translation.. you can edit it...
978                                 var el = Roo.get(this);
979                                 var id = el.dom.getAttribute('data-id');
980                                 var d = el.dom.getAttribute('data-date');
981                                 var t = el.dom.getAttribute('data-time');
982                                 //var id = this.child('span').dom.textContent;
983                                 
984                                 //Roo.log(this);
985                                 Pman.Dialog.CourseCalendar.show({
986                                     id : id,
987                                     when_d : d,
988                                     when_t : t,
989                                     productitem_active : id ? 1 : 0
990                                 }, function() {
991                                     _this.grid.ds.load({});
992                                 });
993                            
994                            });
995                            
996                            _this.panel.fireEvent('resize', [ '', '' ]);
997                         }
998                     },
999                     loadResponse : function(o, success, response){
1000                             // this is overridden on before load..
1001                             
1002                             Roo.log("our code?");       
1003                             //Roo.log(success);
1004                             //Roo.log(response)
1005                             delete this.activeRequest;
1006                             if(!success){
1007                                 this.fireEvent("loadexception", this, o, response);
1008                                 o.request.callback.call(o.request.scope, null, o.request.arg, false);
1009                                 return;
1010                             }
1011                             var result;
1012                             try {
1013                                 result = o.reader.read(response);
1014                             }catch(e){
1015                                 Roo.log("load exception?");
1016                                 this.fireEvent("loadexception", this, o, response, e);
1017                                 o.request.callback.call(o.request.scope, null, o.request.arg, false);
1018                                 return;
1019                             }
1020                             Roo.log("ready...");        
1021                             // loop through result.records;
1022                             // and set this.tdate[date] = [] << array of records..
1023                             _this.tdata  = {};
1024                             Roo.each(result.records, function(r){
1025                                 //Roo.log(r.data);
1026                                 if(typeof(_this.tdata[r.data.when_dt.format('j')]) == 'undefined'){
1027                                     _this.tdata[r.data.when_dt.format('j')] = [];
1028                                 }
1029                                 _this.tdata[r.data.when_dt.format('j')].push(r.data);
1030                             });
1031                             
1032                             //Roo.log(_this.tdata);
1033                             
1034                             result.records = [];
1035                             result.totalRecords = 6;
1036                     
1037                             // let's generate some duumy records for the rows.
1038                             //var st = _this.dateField.getValue();
1039                             
1040                             // work out monday..
1041                             //st = st.add(Date.DAY, -1 * st.format('w'));
1042                             
1043                             var date = Date.parseDate(_this.monthField.getValue(), "Y-m-d");
1044                             
1045                             var firstOfMonth = date.getFirstDayOfMonth();
1046                             var days = date.getDaysInMonth();
1047                             var d = 1;
1048                             var firstAdded = false;
1049                             for (var i = 0; i < result.totalRecords ; i++) {
1050                                 //var d= st.add(Date.DAY, i);
1051                                 var row = {};
1052                                 var added = 0;
1053                                 for(var w = 0 ; w < 7 ; w++){
1054                                     if(!firstAdded && firstOfMonth != w){
1055                                         continue;
1056                                     }
1057                                     if(d > days){
1058                                         continue;
1059                                     }
1060                                     firstAdded = true;
1061                                     var dd = (d > 0 && d < 10) ? "0"+d : d;
1062                                     row['weekday'+w] = String.format(
1063                                                     '<span style="font-size: 16px;"><b>{0}</b></span>'+
1064                                                     '<span class="course-edit-link" style="color:blue;" data-id="0" data-date="{1}"> Add New</span>',
1065                                                     d,
1066                                                     date.format('Y-m-')+dd
1067                                                 );
1068                                     added++;
1069                                     if(typeof(_this.tdata[d]) != 'undefined'){
1070                                         Roo.each(_this.tdata[d], function(r){
1071                                             var is_sub = '';
1072                                             var deactive = '';
1073                                             var id = r.id;
1074                                             var desc = (r.productitem_id_descrip) ? r.productitem_id_descrip : '';
1075                                             if(r.parent_id*1>0){
1076                                                 is_sub = (r.productitem_id_visible*1 < 1) ? 'de-act-sup-link' :'sub-link';
1077                                                 id = r.parent_id;
1078                                             }
1079                                             if(r.productitem_id_visible*1 < 1 && r.parent_id*1 < 1){
1080                                                 deactive = 'de-act-link';
1081                                             }
1082                                             
1083                                             row['weekday'+w] += String.format(
1084                                                     '<br /><span class="course-edit-link {3} {4}" qtip="{5}" data-id="{0}">{2} - {1}</span>',
1085                                                     id, //0
1086                                                     r.product_id_name, //1
1087                                                     r.when_dt.format('h:ia'), //2
1088                                                     is_sub, //3
1089                                                     deactive, //4
1090                                                     desc // 5
1091                                             );
1092                                         });
1093                                     }
1094                                     d++;
1095                                 }
1096                                 
1097                                 // only do this if something added..
1098                                 if(added > 0){ 
1099                                     result.records.push(_this.grid.dataSource.reader.newRow(row));
1100                                 }
1101                                 
1102                                 
1103                                 // push it twice. (second one with an hour..
1104                                 
1105                             }
1106                             //Roo.log(result);
1107                             this.fireEvent("load", this, o, o.request.arg);
1108                             o.request.callback.call(o.request.scope, result, o.request.arg, true);
1109                         },
1110                     sortInfo : {field: 'when_dt', direction : 'ASC' },
1111                     proxy : {
1112                         xtype: 'HttpProxy',
1113                         xns: Roo.data,
1114                         method : 'GET',
1115                         url : baseURL + '/Roo/Shop_course.php'
1116                     },
1117                     reader : {
1118                         xtype: 'JsonReader',
1119                         xns: Roo.data,
1120                         id : 'id',
1121                         fields : [
1122                             {
1123                                 'name': 'id',
1124                                 'type': 'int'
1125                             },
1126                             {
1127                                 'name': 'when_dt',
1128                                 'type': 'string'
1129                             },
1130                             {
1131                                 'name': 'end_dt',
1132                                 'type': 'string'
1133                             },
1134                             {
1135                                 'name': 'parent_id',
1136                                 'type': 'int'
1137                             },
1138                             {
1139                                 'name': 'product_id',
1140                                 'type': 'int'
1141                             },
1142                             {
1143                                 'name': 'productitem_id',
1144                                 'type': 'int'
1145                             },
1146                             {
1147                                 'name': 'guid',
1148                                 'type': 'int'
1149                             }
1150                         ]
1151                     }
1152                 },
1153                 toolbar : {
1154                     xtype: 'Toolbar',
1155                     xns: Roo,
1156                     items : [
1157                         {
1158                             xtype: 'Button',
1159                             xns: Roo.Toolbar,
1160                             listeners : {
1161                                 click : function (_self, e)
1162                                 {
1163                                     var sd = Date.parseDate(_this.monthField.getValue(), "Y-m-d");
1164                                     sd.setMonth(sd.getMonth()-1);
1165                                     _this.monthField.setValue(sd.format('Y-m-d'));
1166                                     _this.grid.ds.load({});
1167                                 }
1168                             },
1169                             text : "Back"
1170                         },
1171                         {
1172                             xtype: 'Separator',
1173                             xns: Roo.Toolbar
1174                         },
1175                         {
1176                             xtype: 'MonthField',
1177                             xns: Roo.form,
1178                             listeners : {
1179                                 render : function (_self)
1180                                 {
1181                                     _this.monthField = _self;
1182                                    // _this.monthField.set  today
1183                                 },
1184                                 select : function (combo, date)
1185                                 {
1186                                     _this.grid.ds.load({});
1187                                 }
1188                             },
1189                             value : (function() { return new Date(); })()
1190                         },
1191                         {
1192                             xtype: 'Separator',
1193                             xns: Roo.Toolbar
1194                         },
1195                         {
1196                             xtype: 'TextItem',
1197                             xns: Roo.Toolbar,
1198                             text : "Blue: in-active, green: in-active sup-event, red: de-active, purple: de-active sup-event"
1199                         },
1200                         {
1201                             xtype: 'Fill',
1202                             xns: Roo.Toolbar
1203                         },
1204                         {
1205                             xtype: 'Button',
1206                             xns: Roo.Toolbar,
1207                             listeners : {
1208                                 click : function (_self, e)
1209                                 {
1210                                     var sd = Date.parseDate(_this.monthField.getValue(), "Y-m-d");
1211                                     sd.setMonth(sd.getMonth()+1);
1212                                     _this.monthField.setValue(sd.format('Y-m-d'));
1213                                     _this.grid.ds.load({});
1214                                 }
1215                             },
1216                             text : "Next"
1217                         }
1218                     ]
1219                 },
1220                  
1221             }
1222         };
1223         
1224         */