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) {
438 this.resizeColumns();
439 var vd = this.activeDate;
440 this.activeDate = date;
441 // if(vd && this.el){
442 // var t = date.getTime();
443 // if(vd.getMonth() == date.getMonth() && vd.getFullYear() == date.getFullYear()){
444 // Roo.log('using add remove');
446 // this.fireEvent('monthchange', this, date);
448 // this.cells.removeClass("fc-state-highlight");
449 // this.cells.each(function(c){
450 // if(c.dateValue == t){
451 // c.addClass("fc-state-highlight");
452 // setTimeout(function(){
453 // try{c.dom.firstChild.focus();}catch(e){}
463 var days = date.getDaysInMonth();
465 var firstOfMonth = date.getFirstDateOfMonth();
466 var startingPos = firstOfMonth.getDay()-this.startDay;
468 if(startingPos < this.startDay){
472 var pm = date.add(Date.MONTH, -1);
473 var prevStart = pm.getDaysInMonth()-startingPos;
477 this.cells = this.view.el.select('.x-grid-row .x-grid-col',true);
479 this.textNodes = this.view.el.query('.x-grid-row .x-grid-col .x-grid-cell-text');
480 //this.cells.addClassOnOver('fc-state-hover');
482 var cells = this.cells.elements;
483 var textEls = this.textNodes;
485 //Roo.each(cells, function(cell){
486 // cell.removeClass([ 'fc-past', 'fc-other-month', 'fc-future', 'fc-state-highlight', 'fc-state-disabled']);
491 // convert everything to numbers so it's fast
493 var d = (new Date(pm.getFullYear(), pm.getMonth(), prevStart)).clearTime();
496 //Roo.log(prevStart);
498 var today = new Date().clearTime().getTime();
499 var sel = date.clearTime().getTime();
500 var min = this.minDate ? this.minDate.clearTime() : Number.NEGATIVE_INFINITY;
501 var max = this.maxDate ? this.maxDate.clearTime() : Number.POSITIVE_INFINITY;
502 var ddMatch = this.disabledDatesRE;
503 var ddText = this.disabledDatesText;
504 var ddays = this.disabledDays ? this.disabledDays.join("") : false;
505 var ddaysText = this.disabledDaysText;
506 var format = this.format;
508 var setCellClass = function(cal, cell){
510 //Roo.log('set Cell Class');
518 // cell.className += " fc-today";
519 // cell.className += " fc-state-highlight";
520 cell.title = cal.todayText;
523 // disable highlight in other month..
524 //cell.className += " fc-state-highlight";
529 //cell.className = " fc-state-disabled";
530 cell.title = cal.minText;
534 //cell.className = " fc-state-disabled";
535 cell.title = cal.maxText;
539 if(ddays.indexOf(d.getDay()) != -1){
540 // cell.title = ddaysText;
541 // cell.className = " fc-state-disabled";
544 if(ddMatch && format){
545 var fvalue = d.dateFormat(format);
546 if(ddMatch.test(fvalue)){
547 cell.title = ddText.replace("%0", fvalue);
548 // cell.className = " fc-state-disabled";
552 if (!cell.initialClassName) {
553 //cell.initialClassName = cell.dom.className;
556 cell.dom.className = cell.initialClassName + ' ' + cell.className;
561 for(; i < startingPos; i++) {
562 cells[i].dayName = (++prevStart);
564 d.setDate(d.getDate()+1);
566 //cells[i].className = "fc-past fc-other-month";
567 setCellClass(this, cells[i]);
572 for(; i < days; i++){
573 intDay = i - startingPos + 1;
574 cells[i].dayName = (intDay);
575 d.setDate(d.getDate()+1);
577 cells[i].className = ''; // "x-date-active";
578 setCellClass(this, cells[i]);
583 //textEls[i].innerHTML = (++extraDays);
585 d.setDate(d.getDate()+1);
586 cells[i].dayName = (++extraDays);
587 cells[i].className = "fc-future fc-other-month";
588 setCellClass(this, cells[i]);
591 //this.el.select('.fc-header-title h2',true).update(Date.monthNames[date.getMonth()] + " " + date.getFullYear());
593 var totalRows = Math.ceil((date.getDaysInMonth() + date.getFirstDateOfMonth().getDay()) / 7);
597 for (var r = 0;r < 6;r++) {
598 for (var c =0;c < 7;c++) {
599 this.ds.getAt(r).set('weekday' + c ,cells[i++].dayName );
605 //this.el.select('tr.fc-week.fc-prev-last',true).removeClass('fc-last');
606 //this.el.select('tr.fc-week.fc-next-last',true).addClass('fc-last').show();
608 ////if(totalRows != 6){
609 //this.el.select('tr.fc-week.fc-last',true).removeClass('fc-last').addClass('fc-next-last').hide();
610 // this.el.select('tr.fc-week.fc-prev-last',true).addClass('fc-last');
613 this.fireEvent('monthchange', this, date);
618 * Returns the grid's SelectionModel.
619 * @return {SelectionModel}
621 getSelectionModel : function(){
623 this.selModel = new Roo.grid.CellSelectionModel();
625 return this.selModel;
629 this.eventStore.load()
635 findCell : function(dt) {
636 dt = dt.clearTime().getTime();
638 this.cells.each(function(c){
639 //Roo.log("check " +c.dateValue + '?=' + dt);
640 if(c.dateValue == dt){
650 findCells : function(ev) {
651 var s = ev.start_dt.clone().clearTime().getTime();
653 var e= ev.end_dt.clone().clearTime().getTime();
656 this.cells.each(function(c){
657 ////Roo.log("check " +c.dateValue + '<' + e + ' > ' + s);
671 findBestRow: function(cells)
675 for (var i =0 ; i < cells.length;i++) {
676 ret = Math.max(cells[i].rows || 0,ret);
683 addItem : function(ev)
685 // look for vertical location slot in
686 var cells = this.findCells(ev);
688 ev.row = this.findBestRow(cells);
690 // work out the location.
694 for(var i =0; i < cells.length; i++) {
702 if (crow.start.getY() == cells[i].getY()) {
720 for (var i = 0; i < cells.length;i++) {
721 cells[i].rows = Math.max(cells[i].rows || 0 , ev.row + 1 );
725 this.calevents.push(ev);
728 clearEvents: function() {
734 Roo.each(this.cells.elements, function(c){
738 Roo.each(this.calevents, function(e) {
739 Roo.each(e.els, function(el) {
740 el.un('mouseenter' ,this.onEventEnter, this);
741 el.un('mouseleave' ,this.onEventLeave, this);
748 renderEvents: function()
750 // first make sure there is enough space..
752 this.cells.each(function(c) {
753 c.select('.fc-day-content div',true).first().setHeight(Math.max(34, c.rows * 20));
756 for (var e = 0; e < this.calevents.length; e++) {
757 var ev = this.calevents[e];
758 var cells = ev.cells;
761 for(var i =0; i < rows.length; i++) {
764 // how many rows should it span..
767 cls : 'roo-dynamic fc-event fc-event-hori fc-event-draggable ui-draggable',
768 style : 'position: absolute', // left: 387px; width: 121px; top: 359px;
773 cls: 'fc-event-inner',
777 cls: 'fc-event-time',
778 html : cells.length > 1 ? '' : ev.start_dt.format('h:ia')
782 cls: 'fc-event-title',
783 html : String.format('{0}', ev.title)
790 cls: 'ui-resizable-handle ui-resizable-e',
791 html : '  '
797 cfg.cls += ' fc-event-start';
799 if ((i+1) == rows.length) {
800 cfg.cls += ' fc-event-end';
803 var ctr = this.view.el.select('.fc-event-container',true).first();
804 var cg = ctr.createChild(cfg);
806 cg.on('mouseenter' ,this.onEventEnter, this, ev);
807 cg.on('mouseleave' ,this.onEventLeave, this, ev);
808 cg.on('click', this.onEventClick, this, ev);
812 var sbox = rows[i].start.select('.fc-day-content',true).first().getBox();
813 var ebox = rows[i].end.select('.fc-day-content',true).first().getBox();
815 cg.setXY([sbox.x +2, sbox.y +(ev.row * 20)]);
816 cg.setWidth(ebox.right - sbox.x -2);
824 onEventEnter: function (e, el,event,d) {
825 this.fireEvent('evententer', this, el, event);
828 onEventLeave: function (e, el,event,d) {
829 this.fireEvent('eventleave', this, el, event);
832 onEventClick: function (e, el,event,d) {
833 this.fireEvent('eventclick', this, el, event);
836 onMonthChange: function () {
840 onLoad: function () {
843 //Roo.log('calendar onload');
847 if(this.eventStore.getCount() > 0){
848 this.eventStore.data.each(function(d){
852 var add = Roo.apply({}, d.data);
853 if (typeof(add.end_dt) == 'undefined') {
854 Roo.log("Missing End time in calendar data: ");
858 if (typeof(add.start_dt) == 'undefined') {
859 Roo.log("Missing Start time in calendar data: ");
863 add.start_dt = typeof(add.start_dt) == 'string' ? Date.parseDate(add.start_dt,'Y-m-d H:i:s') : add.start_dt,
864 add.end_dt = typeof(add.end_dt) == 'string' ? Date.parseDate(add.end_dt,'Y-m-d H:i:s') : add.end_dt,
865 add.id = add.id || d.id;
866 add.title = add.title || '??';
869 // other than the 'required' coluns, we should just pass the data from the store.
874 start: new Date(d.data.start_dt),
875 end : new Date(d.data.end_dt),
876 time : d.data.start_time,
877 title : d.data.title,
878 description : d.data.description,
898 if (!this.view.el.hasClass('course-timesheet')) {
899 this.view.el.addClass('course-timesheet');
906 Roo.log(_this.grid.view.el.getWidth());
909 this.tsStyle = Roo.util.CSS.createStyleSheet({
910 '.course-timesheet .x-grid-row' : {
916 '.course-edit-link' : {
918 'text-overflow' : 'ellipsis',
919 'overflow' : 'hidden',
920 'white-space' : 'nowrap',
926 '.de-act-sup-link' : {
928 'text-decoration' : 'line-through'
932 'text-decoration' : 'line-through'
934 '.course-timesheet .course-highlight' : {
935 'border-top-style': 'dashed !important',
936 'border-bottom-bottom': 'dashed !important'
938 '.course-timesheet .course-item' : {
939 'font-family' : 'tahoma, arial, helvetica',
940 'font-size' : '11px',
941 'overflow' : 'hidden',
942 'padding-left' : '10px',
943 'padding-right' : '10px',
944 'padding-top' : '10px'
952 monitorWindowResize : false,
953 cellrenderer : function(v,x,r)
958 xtype: 'CellSelectionModel',
965 beforeload : function (_self, options)
967 options.params = options.params || {};
968 options.params._month = _this.monthField.getValue();
969 options.params.limit = 9999;
970 options.params['sort'] = 'when_dt';
971 options.params['dir'] = 'ASC';
972 this.proxy.loadResponse = this.loadResponse;
976 load : function (_self, records, options)
978 _this.grid.view.el.select('.course-edit-link', true).on('click', function() {
979 // if you click on the translation.. you can edit it...
980 var el = Roo.get(this);
981 var id = el.dom.getAttribute('data-id');
982 var d = el.dom.getAttribute('data-date');
983 var t = el.dom.getAttribute('data-time');
984 //var id = this.child('span').dom.textContent;
987 Pman.Dialog.CourseCalendar.show({
991 productitem_active : id ? 1 : 0
993 _this.grid.ds.load({});
998 _this.panel.fireEvent('resize', [ '', '' ]);
1001 loadResponse : function(o, success, response){
1002 // this is overridden on before load..
1004 Roo.log("our code?");
1007 delete this.activeRequest;
1009 this.fireEvent("loadexception", this, o, response);
1010 o.request.callback.call(o.request.scope, null, o.request.arg, false);
1015 result = o.reader.read(response);
1017 Roo.log("load exception?");
1018 this.fireEvent("loadexception", this, o, response, e);
1019 o.request.callback.call(o.request.scope, null, o.request.arg, false);
1022 Roo.log("ready...");
1023 // loop through result.records;
1024 // and set this.tdate[date] = [] << array of records..
1026 Roo.each(result.records, function(r){
1028 if(typeof(_this.tdata[r.data.when_dt.format('j')]) == 'undefined'){
1029 _this.tdata[r.data.when_dt.format('j')] = [];
1031 _this.tdata[r.data.when_dt.format('j')].push(r.data);
1034 //Roo.log(_this.tdata);
1036 result.records = [];
1037 result.totalRecords = 6;
1039 // let's generate some duumy records for the rows.
1040 //var st = _this.dateField.getValue();
1042 // work out monday..
1043 //st = st.add(Date.DAY, -1 * st.format('w'));
1045 var date = Date.parseDate(_this.monthField.getValue(), "Y-m-d");
1047 var firstOfMonth = date.getFirstDayOfMonth();
1048 var days = date.getDaysInMonth();
1050 var firstAdded = false;
1051 for (var i = 0; i < result.totalRecords ; i++) {
1052 //var d= st.add(Date.DAY, i);
1055 for(var w = 0 ; w < 7 ; w++){
1056 if(!firstAdded && firstOfMonth != w){
1063 var dd = (d > 0 && d < 10) ? "0"+d : d;
1064 row['weekday'+w] = String.format(
1065 '<span style="font-size: 16px;"><b>{0}</b></span>'+
1066 '<span class="course-edit-link" style="color:blue;" data-id="0" data-date="{1}"> Add New</span>',
1068 date.format('Y-m-')+dd
1071 if(typeof(_this.tdata[d]) != 'undefined'){
1072 Roo.each(_this.tdata[d], function(r){
1076 var desc = (r.productitem_id_descrip) ? r.productitem_id_descrip : '';
1077 if(r.parent_id*1>0){
1078 is_sub = (r.productitem_id_visible*1 < 1) ? 'de-act-sup-link' :'sub-link';
1081 if(r.productitem_id_visible*1 < 1 && r.parent_id*1 < 1){
1082 deactive = 'de-act-link';
1085 row['weekday'+w] += String.format(
1086 '<br /><span class="course-edit-link {3} {4}" qtip="{5}" data-id="{0}">{2} - {1}</span>',
1088 r.product_id_name, //1
1089 r.when_dt.format('h:ia'), //2
1099 // only do this if something added..
1101 result.records.push(_this.grid.dataSource.reader.newRow(row));
1105 // push it twice. (second one with an hour..
1109 this.fireEvent("load", this, o, o.request.arg);
1110 o.request.callback.call(o.request.scope, result, o.request.arg, true);
1112 sortInfo : {field: 'when_dt', direction : 'ASC' },
1117 url : baseURL + '/Roo/Shop_course.php'
1120 xtype: 'JsonReader',
1137 'name': 'parent_id',
1141 'name': 'product_id',
1145 'name': 'productitem_id',
1163 click : function (_self, e)
1165 var sd = Date.parseDate(_this.monthField.getValue(), "Y-m-d");
1166 sd.setMonth(sd.getMonth()-1);
1167 _this.monthField.setValue(sd.format('Y-m-d'));
1168 _this.grid.ds.load({});
1178 xtype: 'MonthField',
1181 render : function (_self)
1183 _this.monthField = _self;
1184 // _this.monthField.set today
1186 select : function (combo, date)
1188 _this.grid.ds.load({});
1191 value : (function() { return new Date(); })()
1200 text : "Blue: in-active, green: in-active sup-event, red: de-active, purple: de-active sup-event"
1210 click : function (_self, e)
1212 var sd = Date.parseDate(_this.monthField.getValue(), "Y-m-d");
1213 sd.setMonth(sd.getMonth()+1);
1214 _this.monthField.setValue(sd.format('Y-m-d'));
1215 _this.grid.ds.load({});