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", {
15 selModel: mySelectionModel,
16 autoSizeColumns: true,
17 monitorWindowResize: false,
19 eventstore : real data store..
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.
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');
37 this.id = this.container.id;
39 Roo.apply(this, config);
40 // check and correct shorthanded configs
44 for (var r = 0;r < 6;r++) {
47 for (var c =0;c < 7;c++) {
51 if (this.eventStore) {
52 this.eventStore= Roo.factory(this.eventStore, Roo.data);
53 this.eventStore.on('load',this.onLoad, this);
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' ])
63 this.dataSource.load();
64 this.ds = this.dataSource;
65 this.ds.xmodule = this.xmodule || false;
68 var cellRender = function(v,x,r)
71 '<div class="fc-day fc-widget-content"><div>' +
72 '<div class="fc-event-container"></div>' +
73 '<div class="fc-day-number">{0}</div>'+
75 '<div class="fc-day-content"><div style="position:relative"></div></div>' +
81 this.colModel = new Roo.grid.ColumnModel( [
85 dataIndex : 'weekday0',
92 dataIndex : 'weekday1',
99 dataIndex : 'weekday2',
101 renderer : cellRender
104 xtype: 'ColumnModel',
106 dataIndex : 'weekday3',
107 header : 'Wednesday',
108 renderer : cellRender
111 xtype: 'ColumnModel',
113 dataIndex : 'weekday4',
115 renderer : cellRender
118 xtype: 'ColumnModel',
120 dataIndex : 'weekday5',
122 renderer : cellRender
125 xtype: 'ColumnModel',
127 dataIndex : 'weekday6',
129 renderer : cellRender
132 this.cm = this.colModel;
133 this.cm.xmodule = this.xmodule || false;
137 //this.selModel = new Roo.grid.CellSelectionModel();
138 //this.sm = this.selModel;
139 //this.selModel.init(this);
143 this.container.setWidth(this.width);
147 this.container.setHeight(this.height);
154 * The raw click event for the entire grid.
155 * @param {Roo.EventObject} e
160 * The raw dblclick event for the entire grid.
161 * @param {Roo.EventObject} e
166 * The raw contextmenu event for the entire grid.
167 * @param {Roo.EventObject} e
169 "contextmenu" : true,
172 * The raw mousedown event for the entire grid.
173 * @param {Roo.EventObject} e
178 * The raw mouseup event for the entire grid.
179 * @param {Roo.EventObject} e
184 * The raw mouseover event for the entire grid.
185 * @param {Roo.EventObject} e
190 * The raw mouseout event for the entire grid.
191 * @param {Roo.EventObject} e
196 * The raw keypress event for the entire grid.
197 * @param {Roo.EventObject} e
202 * The raw keydown event for the entire grid.
203 * @param {Roo.EventObject} e
211 * Fires when a cell is clicked
213 * @param {Number} rowIndex
214 * @param {Number} columnIndex
215 * @param {Roo.EventObject} e
219 * @event celldblclick
220 * Fires when a cell is double clicked
222 * @param {Number} rowIndex
223 * @param {Number} columnIndex
224 * @param {Roo.EventObject} e
226 "celldblclick" : true,
229 * Fires when a row is clicked
231 * @param {Number} rowIndex
232 * @param {Roo.EventObject} e
237 * Fires when a row is double clicked
239 * @param {Number} rowIndex
240 * @param {Roo.EventObject} e
242 "rowdblclick" : true,
245 * Fires when a header is clicked
247 * @param {Number} columnIndex
248 * @param {Roo.EventObject} e
250 "headerclick" : true,
252 * @event headerdblclick
253 * Fires when a header cell is double clicked
255 * @param {Number} columnIndex
256 * @param {Roo.EventObject} e
258 "headerdblclick" : true,
260 * @event rowcontextmenu
261 * Fires when a row is right clicked
263 * @param {Number} rowIndex
264 * @param {Roo.EventObject} e
266 "rowcontextmenu" : true,
268 * @event cellcontextmenu
269 * Fires when a cell is right clicked
271 * @param {Number} rowIndex
272 * @param {Number} cellIndex
273 * @param {Roo.EventObject} e
275 "cellcontextmenu" : true,
277 * @event headercontextmenu
278 * Fires when a header is right clicked
280 * @param {Number} columnIndex
281 * @param {Roo.EventObject} e
283 "headercontextmenu" : true,
286 * Fires when the body element is scrolled
287 * @param {Number} scrollLeft
288 * @param {Number} scrollTop
292 * @event columnresize
293 * Fires when the user resizes a column
294 * @param {Number} columnIndex
295 * @param {Number} newSize
297 "columnresize" : true,
300 * Fires when the user moves a column
301 * @param {Number} oldIndex
302 * @param {Number} newIndex
307 * Fires when row(s) start being dragged
309 * @param {Roo.GridDD} dd The drag drop object
310 * @param {event} e The raw browser event
315 * Fires when a drag operation is complete
317 * @param {Roo.GridDD} dd The drag drop object
318 * @param {event} e The raw browser event
323 * Fires when dragged row(s) are dropped on a valid DD target
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
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.
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
341 * Fires when the dragged row(s) first cross another DD target while being dragged
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
350 * Fires when the dragged row(s) leave another DD target while being dragged
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
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.
367 * Fires when the grid is rendered
373 * Fires when a date is selected
374 * @param {DatePicker} this
375 * @param {Date} date The selected date
380 * Fires when the displayed month changes
381 * @param {DatePicker} this
382 * @param {Date} date The selected month
387 * Fires when mouse over an event
388 * @param {Calendar} this
389 * @param {event} Event
394 * Fires when the mouse leaves an
395 * @param {Calendar} this
401 * Fires when the mouse click an
402 * @param {Calendar} this
408 Roo.grid.Grid.superclass.constructor.call(this);
409 this.on('render', function() {
410 this.view.el.addClass('x-grid-cal');
412 (function() { this.setDate(new Date()); }).defer(100,this); //default today..
417 Roo.extend(Roo.grid.Calendar, Roo.grid.Grid, {
419 * @cfg {Store} eventStore The store that loads events.
427 monitorWindowResize : false,
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);
437 setDate :function(date) {
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');
449 // this.fireEvent('monthchange', this, date);
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){}
466 var days = date.getDaysInMonth();
468 var firstOfMonth = date.getFirstDateOfMonth();
469 var startingPos = firstOfMonth.getDay()-this.startDay;
471 if(startingPos < this.startDay){
475 var pm = date.add(Date.MONTH, -1);
476 var prevStart = pm.getDaysInMonth()-startingPos;
480 this.cells = this.view.el.select('.x-grid-row .x-grid-col',true);
482 this.textNodes = this.view.el.query('.x-grid-row .x-grid-col .x-grid-cell-text');
483 //this.cells.addClassOnOver('fc-state-hover');
485 var cells = this.cells.elements;
486 var textEls = this.textNodes;
488 //Roo.each(cells, function(cell){
489 // cell.removeClass([ 'fc-past', 'fc-other-month', 'fc-future', 'fc-state-highlight', 'fc-state-disabled']);
494 // convert everything to numbers so it's fast
496 var d = (new Date(pm.getFullYear(), pm.getMonth(), prevStart)).clearTime();
499 //Roo.log(prevStart);
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;
511 var setCellClass = function(cal, cell){
513 //Roo.log('set Cell Class');
522 cell.className += " fc-today";
523 cell.className += " fc-state-highlight";
524 cell.title = cal.todayText;
527 // disable highlight in other month..
528 cell.className += " fc-state-highlight";
533 //cell.className = " fc-state-disabled";
534 cell.title = cal.minText;
538 //cell.className = " fc-state-disabled";
539 cell.title = cal.maxText;
543 if(ddays.indexOf(d.getDay()) != -1){
544 // cell.title = ddaysText;
545 // cell.className = " fc-state-disabled";
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";
556 if (!cell.initialClassName) {
557 cell.initialClassName = cell.dom.className;
560 cell.dom.className = cell.initialClassName + ' ' + cell.className;
565 for(; i < startingPos; i++) {
566 cells[i].dayName = (++prevStart);
568 d.setDate(d.getDate()+1);
570 //cells[i].className = "fc-past fc-other-month";
571 setCellClass(this, cells[i]);
576 for(; i < days; i++){
577 intDay = i - startingPos + 1;
578 cells[i].dayName = (intDay);
579 d.setDate(d.getDate()+1);
581 cells[i].className = ''; // "x-date-active";
582 setCellClass(this, cells[i]);
587 //textEls[i].innerHTML = (++extraDays);
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]);
595 //this.el.select('.fc-header-title h2',true).update(Date.monthNames[date.getMonth()] + " " + date.getFullYear());
597 var totalRows = Math.ceil((date.getDaysInMonth() + date.getFirstDateOfMonth().getDay()) / 7);
599 // this will cause all the cells to mis
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 );
608 this.cells = this.view.el.select('.x-grid-row .x-grid-col',true);
609 for(i=0;i<cells.length;i++) {
611 this.cells.elements[i].dayName = cells[i].dayName ;
612 this.cells.elements[i].className = cells[i].className;
613 this.cells.elements[i].initialClassName = cells[i].initialClassName ;
614 this.cells.elements[i].title = cells[i].title ;
615 this.cells.elements[i].dateValue = cells[i].dateValue ;
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();
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');
629 this.fireEvent('monthchange', this, date);
634 * Returns the grid's SelectionModel.
635 * @return {SelectionModel}
637 getSelectionModel : function(){
639 this.selModel = new Roo.grid.CellSelectionModel();
641 return this.selModel;
645 this.eventStore.load()
651 findCell : function(dt) {
652 dt = dt.clearTime().getTime();
654 this.cells.each(function(c){
655 //Roo.log("check " +c.dateValue + '?=' + dt);
656 if(c.dateValue == dt){
666 findCells : function(ev) {
667 var s = ev.start_dt.clone().clearTime().getTime();
669 var e= ev.end_dt.clone().clearTime().getTime();
672 this.cells.each(function(c){
673 ////Roo.log("check " +c.dateValue + '<' + e + ' > ' + s);
687 findBestRow: function(cells)
691 for (var i =0 ; i < cells.length;i++) {
692 ret = Math.max(cells[i].rows || 0,ret);
699 addItem : function(ev)
701 // look for vertical location slot in
702 var cells = this.findCells(ev);
704 ev.row = this.findBestRow(cells);
706 // work out the location.
710 for(var i =0; i < cells.length; i++) {
718 if (crow.start.getY() == cells[i].getY()) {
736 for (var i = 0; i < cells.length;i++) {
737 cells[i].rows = Math.max(cells[i].rows || 0 , ev.row + 1 );
741 this.calevents.push(ev);
744 clearEvents: function() {
750 Roo.each(this.cells.elements, function(c){
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);
764 renderEvents: function()
766 // first make sure there is enough space..
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));
773 for (var e = 0; e < this.calevents.length; e++) {
774 var ev = this.calevents[e];
775 var cells = ev.cells;
778 for(var i =0; i < rows.length; i++) {
781 // how many rows should it span..
784 cls : 'roo-dynamic fc-event fc-event-hori fc-event-draggable ui-draggable',
785 style : 'position: absolute', // left: 387px; width: 121px; top: 359px;
790 cls: 'fc-event-inner',
794 cls: 'fc-event-time',
795 html : cells.length > 1 ? '' : ev.start_dt.format('h:ia')
799 cls: 'fc-event-title',
800 html : String.format('{0}', ev.title)
807 cls: 'ui-resizable-handle ui-resizable-e',
808 html : '  '
814 cfg.cls += ' fc-event-start';
816 if ((i+1) == rows.length) {
817 cfg.cls += ' fc-event-end';
820 var ctr = this.view.el.select('.fc-event-container',true).first();
821 var cg = ctr.createChild(cfg);
823 cg.on('mouseenter' ,this.onEventEnter, this, ev);
824 cg.on('mouseleave' ,this.onEventLeave, this, ev);
825 cg.on('click', this.onEventClick, this, ev);
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();
835 cg.setXY([sbox.x +2, sbox.y +(ev.row * 20)]);
836 cg.setWidth(ebox.right - sbox.x -2);
845 onEventEnter: function (e, el,event,d) {
846 this.fireEvent('evententer', this, el, event);
849 onEventLeave: function (e, el,event,d) {
850 this.fireEvent('eventleave', this, el, event);
853 onEventClick: function (e, el,event,d) {
854 this.fireEvent('eventclick', this, el, event);
857 onMonthChange: function () {
861 onLoad: function () {
864 //Roo.log('calendar onload');
868 if(this.eventStore.getCount() > 0){
872 this.eventStore.data.each(function(d){
879 var add = Roo.apply({}, d.data);
880 if (typeof(add.end_dt) == 'undefined') {
881 Roo.log("Missing End time in calendar data: ");
885 if (typeof(add.start_dt) == 'undefined') {
886 Roo.log("Missing Start time in calendar data: ");
890 add.start_dt = typeof(add.start_dt) == 'string' ? Date.parseDate(add.start_dt,'Y-m-d H:i:s') : add.start_dt,
891 add.end_dt = typeof(add.end_dt) == 'string' ? Date.parseDate(add.end_dt,'Y-m-d H:i:s') : add.end_dt,
892 add.id = add.id || d.id;
893 add.title = add.title || '??';
897 // other than the 'required' coluns, we should just pass the data from the store.
902 start: new Date(d.data.start_dt),
903 end : new Date(d.data.end_dt),
904 time : d.data.start_time,
905 title : d.data.title,
906 description : d.data.description,
926 if (!this.view.el.hasClass('course-timesheet')) {
927 this.view.el.addClass('course-timesheet');
934 Roo.log(_this.grid.view.el.getWidth());
937 this.tsStyle = Roo.util.CSS.createStyleSheet({
938 '.course-timesheet .x-grid-row' : {
944 '.course-edit-link' : {
946 'text-overflow' : 'ellipsis',
947 'overflow' : 'hidden',
948 'white-space' : 'nowrap',
954 '.de-act-sup-link' : {
956 'text-decoration' : 'line-through'
960 'text-decoration' : 'line-through'
962 '.course-timesheet .course-highlight' : {
963 'border-top-style': 'dashed !important',
964 'border-bottom-bottom': 'dashed !important'
966 '.course-timesheet .course-item' : {
967 'font-family' : 'tahoma, arial, helvetica',
968 'font-size' : '11px',
969 'overflow' : 'hidden',
970 'padding-left' : '10px',
971 'padding-right' : '10px',
972 'padding-top' : '10px'
980 monitorWindowResize : false,
981 cellrenderer : function(v,x,r)
986 xtype: 'CellSelectionModel',
993 beforeload : function (_self, options)
995 options.params = options.params || {};
996 options.params._month = _this.monthField.getValue();
997 options.params.limit = 9999;
998 options.params['sort'] = 'when_dt';
999 options.params['dir'] = 'ASC';
1000 this.proxy.loadResponse = this.loadResponse;
1002 //this.addColumns();
1004 load : function (_self, records, options)
1006 _this.grid.view.el.select('.course-edit-link', true).on('click', function() {
1007 // if you click on the translation.. you can edit it...
1008 var el = Roo.get(this);
1009 var id = el.dom.getAttribute('data-id');
1010 var d = el.dom.getAttribute('data-date');
1011 var t = el.dom.getAttribute('data-time');
1012 //var id = this.child('span').dom.textContent;
1015 Pman.Dialog.CourseCalendar.show({
1019 productitem_active : id ? 1 : 0
1021 _this.grid.ds.load({});
1026 _this.panel.fireEvent('resize', [ '', '' ]);
1029 loadResponse : function(o, success, response){
1030 // this is overridden on before load..
1032 Roo.log("our code?");
1035 delete this.activeRequest;
1037 this.fireEvent("loadexception", this, o, response);
1038 o.request.callback.call(o.request.scope, null, o.request.arg, false);
1043 result = o.reader.read(response);
1045 Roo.log("load exception?");
1046 this.fireEvent("loadexception", this, o, response, e);
1047 o.request.callback.call(o.request.scope, null, o.request.arg, false);
1050 Roo.log("ready...");
1051 // loop through result.records;
1052 // and set this.tdate[date] = [] << array of records..
1054 Roo.each(result.records, function(r){
1056 if(typeof(_this.tdata[r.data.when_dt.format('j')]) == 'undefined'){
1057 _this.tdata[r.data.when_dt.format('j')] = [];
1059 _this.tdata[r.data.when_dt.format('j')].push(r.data);
1062 //Roo.log(_this.tdata);
1064 result.records = [];
1065 result.totalRecords = 6;
1067 // let's generate some duumy records for the rows.
1068 //var st = _this.dateField.getValue();
1070 // work out monday..
1071 //st = st.add(Date.DAY, -1 * st.format('w'));
1073 var date = Date.parseDate(_this.monthField.getValue(), "Y-m-d");
1075 var firstOfMonth = date.getFirstDayOfMonth();
1076 var days = date.getDaysInMonth();
1078 var firstAdded = false;
1079 for (var i = 0; i < result.totalRecords ; i++) {
1080 //var d= st.add(Date.DAY, i);
1083 for(var w = 0 ; w < 7 ; w++){
1084 if(!firstAdded && firstOfMonth != w){
1091 var dd = (d > 0 && d < 10) ? "0"+d : d;
1092 row['weekday'+w] = String.format(
1093 '<span style="font-size: 16px;"><b>{0}</b></span>'+
1094 '<span class="course-edit-link" style="color:blue;" data-id="0" data-date="{1}"> Add New</span>',
1096 date.format('Y-m-')+dd
1099 if(typeof(_this.tdata[d]) != 'undefined'){
1100 Roo.each(_this.tdata[d], function(r){
1104 var desc = (r.productitem_id_descrip) ? r.productitem_id_descrip : '';
1105 if(r.parent_id*1>0){
1106 is_sub = (r.productitem_id_visible*1 < 1) ? 'de-act-sup-link' :'sub-link';
1109 if(r.productitem_id_visible*1 < 1 && r.parent_id*1 < 1){
1110 deactive = 'de-act-link';
1113 row['weekday'+w] += String.format(
1114 '<br /><span class="course-edit-link {3} {4}" qtip="{5}" data-id="{0}">{2} - {1}</span>',
1116 r.product_id_name, //1
1117 r.when_dt.format('h:ia'), //2
1127 // only do this if something added..
1129 result.records.push(_this.grid.dataSource.reader.newRow(row));
1133 // push it twice. (second one with an hour..
1137 this.fireEvent("load", this, o, o.request.arg);
1138 o.request.callback.call(o.request.scope, result, o.request.arg, true);
1140 sortInfo : {field: 'when_dt', direction : 'ASC' },
1145 url : baseURL + '/Roo/Shop_course.php'
1148 xtype: 'JsonReader',
1165 'name': 'parent_id',
1169 'name': 'product_id',
1173 'name': 'productitem_id',
1191 click : function (_self, e)
1193 var sd = Date.parseDate(_this.monthField.getValue(), "Y-m-d");
1194 sd.setMonth(sd.getMonth()-1);
1195 _this.monthField.setValue(sd.format('Y-m-d'));
1196 _this.grid.ds.load({});
1206 xtype: 'MonthField',
1209 render : function (_self)
1211 _this.monthField = _self;
1212 // _this.monthField.set today
1214 select : function (combo, date)
1216 _this.grid.ds.load({});
1219 value : (function() { return new Date(); })()
1228 text : "Blue: in-active, green: in-active sup-event, red: de-active, purple: de-active sup-event"
1238 click : function (_self, e)
1240 var sd = Date.parseDate(_this.monthField.getValue(), "Y-m-d");
1241 sd.setMonth(sd.getMonth()+1);
1242 _this.monthField.setValue(sd.format('Y-m-d'));
1243 _this.grid.ds.load({});