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');
519 cell.className += " fc-today";
520 cell.className += " fc-state-highlight";
521 cell.title = cal.todayText;
524 // disable highlight in other month..
525 cell.className += " fc-state-highlight";
530 //cell.className = " fc-state-disabled";
531 cell.title = cal.minText;
535 //cell.className = " fc-state-disabled";
536 cell.title = cal.maxText;
540 if(ddays.indexOf(d.getDay()) != -1){
541 // cell.title = ddaysText;
542 // cell.className = " fc-state-disabled";
545 if(ddMatch && format){
546 var fvalue = d.dateFormat(format);
547 if(ddMatch.test(fvalue)){
548 cell.title = ddText.replace("%0", fvalue);
549 cell.className = " fc-state-disabled";
553 if (!cell.initialClassName) {
554 cell.initialClassName = cell.dom.className;
557 cell.dom.className = cell.initialClassName + ' ' + cell.className;
562 for(; i < startingPos; i++) {
563 cells[i].dayName = (++prevStart);
565 d.setDate(d.getDate()+1);
567 //cells[i].className = "fc-past fc-other-month";
568 setCellClass(this, cells[i]);
573 for(; i < days; i++){
574 intDay = i - startingPos + 1;
575 cells[i].dayName = (intDay);
576 d.setDate(d.getDate()+1);
578 cells[i].className = ''; // "x-date-active";
579 setCellClass(this, cells[i]);
584 //textEls[i].innerHTML = (++extraDays);
586 d.setDate(d.getDate()+1);
587 cells[i].dayName = (++extraDays);
588 cells[i].className = "fc-future fc-other-month";
589 setCellClass(this, cells[i]);
592 //this.el.select('.fc-header-title h2',true).update(Date.monthNames[date.getMonth()] + " " + date.getFullYear());
594 var totalRows = Math.ceil((date.getDaysInMonth() + date.getFirstDateOfMonth().getDay()) / 7);
598 for (var r = 0;r < 6;r++) {
599 for (var c =0;c < 7;c++) {
600 this.ds.getAt(r).set('weekday' + c ,cells[i++].dayName );
606 //this.el.select('tr.fc-week.fc-prev-last',true).removeClass('fc-last');
607 //this.el.select('tr.fc-week.fc-next-last',true).addClass('fc-last').show();
609 ////if(totalRows != 6){
610 //this.el.select('tr.fc-week.fc-last',true).removeClass('fc-last').addClass('fc-next-last').hide();
611 // this.el.select('tr.fc-week.fc-prev-last',true).addClass('fc-last');
614 this.fireEvent('monthchange', this, date);
619 * Returns the grid's SelectionModel.
620 * @return {SelectionModel}
622 getSelectionModel : function(){
624 this.selModel = new Roo.grid.CellSelectionModel();
626 return this.selModel;
630 this.eventStore.load()
636 findCell : function(dt) {
637 dt = dt.clearTime().getTime();
639 this.cells.each(function(c){
640 //Roo.log("check " +c.dateValue + '?=' + dt);
641 if(c.dateValue == dt){
651 findCells : function(ev) {
652 var s = ev.start_dt.clone().clearTime().getTime();
654 var e= ev.end_dt.clone().clearTime().getTime();
657 this.cells.each(function(c){
658 ////Roo.log("check " +c.dateValue + '<' + e + ' > ' + s);
672 findBestRow: function(cells)
676 for (var i =0 ; i < cells.length;i++) {
677 ret = Math.max(cells[i].rows || 0,ret);
684 addItem : function(ev)
686 // look for vertical location slot in
687 var cells = this.findCells(ev);
689 ev.row = this.findBestRow(cells);
691 // work out the location.
695 for(var i =0; i < cells.length; i++) {
703 if (crow.start.getY() == cells[i].getY()) {
721 for (var i = 0; i < cells.length;i++) {
722 cells[i].rows = Math.max(cells[i].rows || 0 , ev.row + 1 );
726 this.calevents.push(ev);
729 clearEvents: function() {
735 Roo.each(this.cells.elements, function(c){
739 Roo.each(this.calevents, function(e) {
740 Roo.each(e.els, function(el) {
741 el.un('mouseenter' ,this.onEventEnter, this);
742 el.un('mouseleave' ,this.onEventLeave, this);
749 renderEvents: function()
751 // first make sure there is enough space..
753 this.cells.each(function(c) {
754 Roo.log(Math.max(34, c.rows * 20));
755 Roo.log(c.select('.fc-day-content div',true).first());
756 c.select('.fc-day-content div',true).first().setHeight(Math.max(34, c.rows * 20));
759 for (var e = 0; e < this.calevents.length; e++) {
760 var ev = this.calevents[e];
761 var cells = ev.cells;
764 for(var i =0; i < rows.length; i++) {
767 // how many rows should it span..
770 cls : 'roo-dynamic fc-event fc-event-hori fc-event-draggable ui-draggable',
771 style : 'position: absolute', // left: 387px; width: 121px; top: 359px;
776 cls: 'fc-event-inner',
780 cls: 'fc-event-time',
781 html : cells.length > 1 ? '' : ev.start_dt.format('h:ia')
785 cls: 'fc-event-title',
786 html : String.format('{0}', ev.title)
793 cls: 'ui-resizable-handle ui-resizable-e',
794 html : '  '
800 cfg.cls += ' fc-event-start';
802 if ((i+1) == rows.length) {
803 cfg.cls += ' fc-event-end';
806 var ctr = this.view.el.select('.fc-event-container',true).first();
807 var cg = ctr.createChild(cfg);
809 cg.on('mouseenter' ,this.onEventEnter, this, ev);
810 cg.on('mouseleave' ,this.onEventLeave, this, ev);
811 cg.on('click', this.onEventClick, this, ev);
815 var sbox = rows[i].start.select('.fc-day-content',true).first().getBox();
816 var ebox = rows[i].end.select('.fc-day-content',true).first().getBox();
818 cg.setXY([sbox.x +2, sbox.y +(ev.row * 20)]);
819 cg.setWidth(ebox.right - sbox.x -2);
827 onEventEnter: function (e, el,event,d) {
828 this.fireEvent('evententer', this, el, event);
831 onEventLeave: function (e, el,event,d) {
832 this.fireEvent('eventleave', this, el, event);
835 onEventClick: function (e, el,event,d) {
836 this.fireEvent('eventclick', this, el, event);
839 onMonthChange: function () {
843 onLoad: function () {
846 //Roo.log('calendar onload');
850 if(this.eventStore.getCount() > 0){
851 this.eventStore.data.each(function(d){
855 var add = Roo.apply({}, d.data);
856 if (typeof(add.end_dt) == 'undefined') {
857 Roo.log("Missing End time in calendar data: ");
861 if (typeof(add.start_dt) == 'undefined') {
862 Roo.log("Missing Start time in calendar data: ");
866 add.start_dt = typeof(add.start_dt) == 'string' ? Date.parseDate(add.start_dt,'Y-m-d H:i:s') : add.start_dt,
867 add.end_dt = typeof(add.end_dt) == 'string' ? Date.parseDate(add.end_dt,'Y-m-d H:i:s') : add.end_dt,
868 add.id = add.id || d.id;
869 add.title = add.title || '??';
872 // other than the 'required' coluns, we should just pass the data from the store.
877 start: new Date(d.data.start_dt),
878 end : new Date(d.data.end_dt),
879 time : d.data.start_time,
880 title : d.data.title,
881 description : d.data.description,
901 if (!this.view.el.hasClass('course-timesheet')) {
902 this.view.el.addClass('course-timesheet');
909 Roo.log(_this.grid.view.el.getWidth());
912 this.tsStyle = Roo.util.CSS.createStyleSheet({
913 '.course-timesheet .x-grid-row' : {
919 '.course-edit-link' : {
921 'text-overflow' : 'ellipsis',
922 'overflow' : 'hidden',
923 'white-space' : 'nowrap',
929 '.de-act-sup-link' : {
931 'text-decoration' : 'line-through'
935 'text-decoration' : 'line-through'
937 '.course-timesheet .course-highlight' : {
938 'border-top-style': 'dashed !important',
939 'border-bottom-bottom': 'dashed !important'
941 '.course-timesheet .course-item' : {
942 'font-family' : 'tahoma, arial, helvetica',
943 'font-size' : '11px',
944 'overflow' : 'hidden',
945 'padding-left' : '10px',
946 'padding-right' : '10px',
947 'padding-top' : '10px'
955 monitorWindowResize : false,
956 cellrenderer : function(v,x,r)
961 xtype: 'CellSelectionModel',
968 beforeload : function (_self, options)
970 options.params = options.params || {};
971 options.params._month = _this.monthField.getValue();
972 options.params.limit = 9999;
973 options.params['sort'] = 'when_dt';
974 options.params['dir'] = 'ASC';
975 this.proxy.loadResponse = this.loadResponse;
979 load : function (_self, records, options)
981 _this.grid.view.el.select('.course-edit-link', true).on('click', function() {
982 // if you click on the translation.. you can edit it...
983 var el = Roo.get(this);
984 var id = el.dom.getAttribute('data-id');
985 var d = el.dom.getAttribute('data-date');
986 var t = el.dom.getAttribute('data-time');
987 //var id = this.child('span').dom.textContent;
990 Pman.Dialog.CourseCalendar.show({
994 productitem_active : id ? 1 : 0
996 _this.grid.ds.load({});
1001 _this.panel.fireEvent('resize', [ '', '' ]);
1004 loadResponse : function(o, success, response){
1005 // this is overridden on before load..
1007 Roo.log("our code?");
1010 delete this.activeRequest;
1012 this.fireEvent("loadexception", this, o, response);
1013 o.request.callback.call(o.request.scope, null, o.request.arg, false);
1018 result = o.reader.read(response);
1020 Roo.log("load exception?");
1021 this.fireEvent("loadexception", this, o, response, e);
1022 o.request.callback.call(o.request.scope, null, o.request.arg, false);
1025 Roo.log("ready...");
1026 // loop through result.records;
1027 // and set this.tdate[date] = [] << array of records..
1029 Roo.each(result.records, function(r){
1031 if(typeof(_this.tdata[r.data.when_dt.format('j')]) == 'undefined'){
1032 _this.tdata[r.data.when_dt.format('j')] = [];
1034 _this.tdata[r.data.when_dt.format('j')].push(r.data);
1037 //Roo.log(_this.tdata);
1039 result.records = [];
1040 result.totalRecords = 6;
1042 // let's generate some duumy records for the rows.
1043 //var st = _this.dateField.getValue();
1045 // work out monday..
1046 //st = st.add(Date.DAY, -1 * st.format('w'));
1048 var date = Date.parseDate(_this.monthField.getValue(), "Y-m-d");
1050 var firstOfMonth = date.getFirstDayOfMonth();
1051 var days = date.getDaysInMonth();
1053 var firstAdded = false;
1054 for (var i = 0; i < result.totalRecords ; i++) {
1055 //var d= st.add(Date.DAY, i);
1058 for(var w = 0 ; w < 7 ; w++){
1059 if(!firstAdded && firstOfMonth != w){
1066 var dd = (d > 0 && d < 10) ? "0"+d : d;
1067 row['weekday'+w] = String.format(
1068 '<span style="font-size: 16px;"><b>{0}</b></span>'+
1069 '<span class="course-edit-link" style="color:blue;" data-id="0" data-date="{1}"> Add New</span>',
1071 date.format('Y-m-')+dd
1074 if(typeof(_this.tdata[d]) != 'undefined'){
1075 Roo.each(_this.tdata[d], function(r){
1079 var desc = (r.productitem_id_descrip) ? r.productitem_id_descrip : '';
1080 if(r.parent_id*1>0){
1081 is_sub = (r.productitem_id_visible*1 < 1) ? 'de-act-sup-link' :'sub-link';
1084 if(r.productitem_id_visible*1 < 1 && r.parent_id*1 < 1){
1085 deactive = 'de-act-link';
1088 row['weekday'+w] += String.format(
1089 '<br /><span class="course-edit-link {3} {4}" qtip="{5}" data-id="{0}">{2} - {1}</span>',
1091 r.product_id_name, //1
1092 r.when_dt.format('h:ia'), //2
1102 // only do this if something added..
1104 result.records.push(_this.grid.dataSource.reader.newRow(row));
1108 // push it twice. (second one with an hour..
1112 this.fireEvent("load", this, o, o.request.arg);
1113 o.request.callback.call(o.request.scope, result, o.request.arg, true);
1115 sortInfo : {field: 'when_dt', direction : 'ASC' },
1120 url : baseURL + '/Roo/Shop_course.php'
1123 xtype: 'JsonReader',
1140 'name': 'parent_id',
1144 'name': 'product_id',
1148 'name': 'productitem_id',
1166 click : function (_self, e)
1168 var sd = Date.parseDate(_this.monthField.getValue(), "Y-m-d");
1169 sd.setMonth(sd.getMonth()-1);
1170 _this.monthField.setValue(sd.format('Y-m-d'));
1171 _this.grid.ds.load({});
1181 xtype: 'MonthField',
1184 render : function (_self)
1186 _this.monthField = _self;
1187 // _this.monthField.set today
1189 select : function (combo, date)
1191 _this.grid.ds.load({});
1194 value : (function() { return new Date(); })()
1203 text : "Blue: in-active, green: in-active sup-event, red: de-active, purple: de-active sup-event"
1213 click : function (_self, e)
1215 var sd = Date.parseDate(_this.monthField.getValue(), "Y-m-d");
1216 sd.setMonth(sd.getMonth()+1);
1217 _this.monthField.setValue(sd.format('Y-m-d'));
1218 _this.grid.ds.load({});