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..
416 if (!Roo.grid.Calendar.style) {
417 Roo.grid.Calendar.style = Roo.util.CSS.createStyleSheet({
420 '.x-grid-cal .x-grid-col' : {
421 height: 'auto !important',
422 'vertical-align': 'top'
424 '.x-grid-cal .fc-event-hori' : {
435 Roo.extend(Roo.grid.Calendar, Roo.grid.Grid, {
437 * @cfg {Store} eventStore The store that loads events.
445 monitorWindowResize : false,
448 resizeColumns : function() {
449 var col = (this.view.el.getWidth() / 7) - 3;
450 // loop through cols, and setWidth
451 for(var i =0 ; i < 7 ; i++){
452 this.cm.setColumnWidth(i, col);
455 setDate :function(date) {
459 this.resizeColumns();
460 var vd = this.activeDate;
461 this.activeDate = date;
462 // if(vd && this.el){
463 // var t = date.getTime();
464 // if(vd.getMonth() == date.getMonth() && vd.getFullYear() == date.getFullYear()){
465 // Roo.log('using add remove');
467 // this.fireEvent('monthchange', this, date);
469 // this.cells.removeClass("fc-state-highlight");
470 // this.cells.each(function(c){
471 // if(c.dateValue == t){
472 // c.addClass("fc-state-highlight");
473 // setTimeout(function(){
474 // try{c.dom.firstChild.focus();}catch(e){}
484 var days = date.getDaysInMonth();
486 var firstOfMonth = date.getFirstDateOfMonth();
487 var startingPos = firstOfMonth.getDay()-this.startDay;
489 if(startingPos < this.startDay){
493 var pm = date.add(Date.MONTH, -1);
494 var prevStart = pm.getDaysInMonth()-startingPos;
498 this.cells = this.view.el.select('.x-grid-row .x-grid-col',true);
500 this.textNodes = this.view.el.query('.x-grid-row .x-grid-col .x-grid-cell-text');
501 //this.cells.addClassOnOver('fc-state-hover');
503 var cells = this.cells.elements;
504 var textEls = this.textNodes;
506 //Roo.each(cells, function(cell){
507 // cell.removeClass([ 'fc-past', 'fc-other-month', 'fc-future', 'fc-state-highlight', 'fc-state-disabled']);
512 // convert everything to numbers so it's fast
514 var d = (new Date(pm.getFullYear(), pm.getMonth(), prevStart)).clearTime();
517 //Roo.log(prevStart);
519 var today = new Date().clearTime().getTime();
520 var sel = date.clearTime().getTime();
521 var min = this.minDate ? this.minDate.clearTime() : Number.NEGATIVE_INFINITY;
522 var max = this.maxDate ? this.maxDate.clearTime() : Number.POSITIVE_INFINITY;
523 var ddMatch = this.disabledDatesRE;
524 var ddText = this.disabledDatesText;
525 var ddays = this.disabledDays ? this.disabledDays.join("") : false;
526 var ddaysText = this.disabledDaysText;
527 var format = this.format;
529 var setCellClass = function(cal, cell){
531 //Roo.log('set Cell Class');
540 cell.className += " fc-today";
541 cell.className += " fc-state-highlight";
542 cell.title = cal.todayText;
545 // disable highlight in other month..
546 cell.className += " fc-state-highlight";
551 //cell.className = " fc-state-disabled";
552 cell.title = cal.minText;
556 //cell.className = " fc-state-disabled";
557 cell.title = cal.maxText;
561 if(ddays.indexOf(d.getDay()) != -1){
562 // cell.title = ddaysText;
563 // cell.className = " fc-state-disabled";
566 if(ddMatch && format){
567 var fvalue = d.dateFormat(format);
568 if(ddMatch.test(fvalue)){
569 cell.title = ddText.replace("%0", fvalue);
570 cell.className = " fc-state-disabled";
574 if (!cell.initialClassName) {
575 cell.initialClassName = cell.dom.className;
578 cell.dom.className = cell.initialClassName + ' ' + cell.className;
583 for(; i < startingPos; i++) {
584 cells[i].dayName = (++prevStart);
586 d.setDate(d.getDate()+1);
588 //cells[i].className = "fc-past fc-other-month";
589 setCellClass(this, cells[i]);
594 for(; i < days; i++){
595 intDay = i - startingPos + 1;
596 cells[i].dayName = (intDay);
597 d.setDate(d.getDate()+1);
599 cells[i].className = ''; // "x-date-active";
600 setCellClass(this, cells[i]);
605 //textEls[i].innerHTML = (++extraDays);
607 d.setDate(d.getDate()+1);
608 cells[i].dayName = (++extraDays);
609 cells[i].className = "fc-future fc-other-month";
610 setCellClass(this, cells[i]);
613 //this.el.select('.fc-header-title h2',true).update(Date.monthNames[date.getMonth()] + " " + date.getFullYear());
615 var totalRows = Math.ceil((date.getDaysInMonth() + date.getFirstDateOfMonth().getDay()) / 7);
617 // this will cause all the cells to mis
620 for (var r = 0;r < 6;r++) {
621 for (var c =0;c < 7;c++) {
622 this.ds.getAt(r).set('weekday' + c ,cells[i++].dayName );
626 this.cells = this.view.el.select('.x-grid-row .x-grid-col',true);
627 for(i=0;i<cells.length;i++) {
629 this.cells.elements[i].dayName = cells[i].dayName ;
630 this.cells.elements[i].className = cells[i].className;
631 this.cells.elements[i].initialClassName = cells[i].initialClassName ;
632 this.cells.elements[i].title = cells[i].title ;
633 this.cells.elements[i].dateValue = cells[i].dateValue ;
639 //this.el.select('tr.fc-week.fc-prev-last',true).removeClass('fc-last');
640 //this.el.select('tr.fc-week.fc-next-last',true).addClass('fc-last').show();
642 ////if(totalRows != 6){
643 //this.el.select('tr.fc-week.fc-last',true).removeClass('fc-last').addClass('fc-next-last').hide();
644 // this.el.select('tr.fc-week.fc-prev-last',true).addClass('fc-last');
647 this.fireEvent('monthchange', this, date);
652 * Returns the grid's SelectionModel.
653 * @return {SelectionModel}
655 getSelectionModel : function(){
657 this.selModel = new Roo.grid.CellSelectionModel();
659 return this.selModel;
663 this.eventStore.load()
669 findCell : function(dt) {
670 dt = dt.clearTime().getTime();
672 this.cells.each(function(c){
673 //Roo.log("check " +c.dateValue + '?=' + dt);
674 if(c.dateValue == dt){
684 findCells : function(ev) {
685 var s = ev.start_dt.clone().clearTime().getTime();
687 var e= ev.end_dt.clone().clearTime().getTime();
690 this.cells.each(function(c){
691 ////Roo.log("check " +c.dateValue + '<' + e + ' > ' + s);
705 findBestRow: function(cells)
709 for (var i =0 ; i < cells.length;i++) {
710 ret = Math.max(cells[i].rows || 0,ret);
717 addItem : function(ev)
719 // look for vertical location slot in
720 var cells = this.findCells(ev);
722 ev.row = this.findBestRow(cells);
724 // work out the location.
728 for(var i =0; i < cells.length; i++) {
736 if (crow.start.getY() == cells[i].getY()) {
754 for (var i = 0; i < cells.length;i++) {
755 cells[i].rows = Math.max(cells[i].rows || 0 , ev.row + 1 );
759 this.calevents.push(ev);
762 clearEvents: function() {
768 Roo.each(this.cells.elements, function(c){
772 Roo.each(this.calevents, function(e) {
773 Roo.each(e.els, function(el) {
774 el.un('mouseenter' ,this.onEventEnter, this);
775 el.un('mouseleave' ,this.onEventLeave, this);
785 renderEvents: function()
787 // first make sure there is enough space..
789 if (!this.eventTmpl) {
790 this.eventTmpl = new Roo.Template(
791 '<div class="roo-dynamic fc-event fc-event-hori fc-event-draggable ui-draggable {fccls} {cls}" style="position: absolute" unselectable="on">' +
792 '<div class="fc-event-inner">' +
793 '<span class="fc-event-time">{time}</span>', +
794 '<span class="fc-event-title" qtip="{qtip}">{title}</span>', +
796 '<div class="ui-resizable-heandle ui-resizable-e"> </div>' +
804 this.cells.each(function(c) {
805 Roo.log(c.select('.fc-day-content div',true).first());
806 c.select('.fc-day-content div',true).first().setHeight(Math.max(34, (c.rows || 1) * 20));
809 var ctr = this.view.el.select('.fc-event-container',true).first();
812 for (var e = 0; e < this.calevents.length; e++) {
813 var ev = this.calevents[e];
815 var cells = ev.cells;
819 for(var i =0; i < rows.length; i++) {
823 cls += ' fc-event-start';
825 if ((i+1) == rows.length) {
826 cls += ' fc-event-end';
830 // how many rows should it span..
831 var cg = this.eventTmpl.append(ctr,Roo.apply({
834 }, rec.data) , true);
837 cg.on('mouseenter' ,this.onEventEnter, this, ev);
838 cg.on('mouseleave' ,this.onEventLeave, this, ev);
839 cg.on('click', this.onEventClick, this, ev);
843 var sbox = rows[i].start.select('.fc-day-content',true).first().getBox();
844 var ebox = rows[i].end.select('.fc-day-content',true).first().getBox();
847 cg.setXY([sbox.x +2, sbox.y +(ev.row * 20)]);
848 cg.setWidth(ebox.right - sbox.x -2);
857 onEventEnter: function (e, el,event,d) {
858 this.fireEvent('evententer', this, el, event);
861 onEventLeave: function (e, el,event,d) {
862 this.fireEvent('eventleave', this, el, event);
865 onEventClick: function (e, el,event,d) {
866 this.fireEvent('eventclick', this, el, event);
869 onMonthChange: function () {
873 onLoad: function () {
876 //Roo.log('calendar onload');
880 if(this.eventStore.getCount() > 0){
884 this.eventStore.data.each(function(d){
889 if (typeof(add.end_dt) == 'undefined') {
890 Roo.log("Missing End time in calendar data: ");
894 if (typeof(add.start_dt) == 'undefined') {
895 Roo.log("Missing Start time in calendar data: ");
899 d.data.start_dt = typeof(add.start_dt) == 'string' ? Date.parseDate(add.start_dt,'Y-m-d H:i:s') : add.start_dt,
900 add.end_dt = typeof(add.end_dt) == 'string' ? Date.parseDate(add.end_dt,'Y-m-d H:i:s') : add.end_dt,
901 add.id = add.id || d.id;
902 add.title = add.title || '??';
907 // other than the 'required' coluns, we should just pass the data from the store.
912 start: new Date(d.data.start_dt),
913 end : new Date(d.data.end_dt),
914 time : d.data.start_time,
915 title : d.data.title,
916 description : d.data.description,
936 if (!this.view.el.hasClass('course-timesheet')) {
937 this.view.el.addClass('course-timesheet');
944 Roo.log(_this.grid.view.el.getWidth());
947 this.tsStyle = Roo.util.CSS.createStyleSheet({
948 '.course-timesheet .x-grid-row' : {
954 '.course-edit-link' : {
956 'text-overflow' : 'ellipsis',
957 'overflow' : 'hidden',
958 'white-space' : 'nowrap',
964 '.de-act-sup-link' : {
966 'text-decoration' : 'line-through'
970 'text-decoration' : 'line-through'
972 '.course-timesheet .course-highlight' : {
973 'border-top-style': 'dashed !important',
974 'border-bottom-bottom': 'dashed !important'
976 '.course-timesheet .course-item' : {
977 'font-family' : 'tahoma, arial, helvetica',
978 'font-size' : '11px',
979 'overflow' : 'hidden',
980 'padding-left' : '10px',
981 'padding-right' : '10px',
982 'padding-top' : '10px'
990 monitorWindowResize : false,
991 cellrenderer : function(v,x,r)
996 xtype: 'CellSelectionModel',
1003 beforeload : function (_self, options)
1005 options.params = options.params || {};
1006 options.params._month = _this.monthField.getValue();
1007 options.params.limit = 9999;
1008 options.params['sort'] = 'when_dt';
1009 options.params['dir'] = 'ASC';
1010 this.proxy.loadResponse = this.loadResponse;
1012 //this.addColumns();
1014 load : function (_self, records, options)
1016 _this.grid.view.el.select('.course-edit-link', true).on('click', function() {
1017 // if you click on the translation.. you can edit it...
1018 var el = Roo.get(this);
1019 var id = el.dom.getAttribute('data-id');
1020 var d = el.dom.getAttribute('data-date');
1021 var t = el.dom.getAttribute('data-time');
1022 //var id = this.child('span').dom.textContent;
1025 Pman.Dialog.CourseCalendar.show({
1029 productitem_active : id ? 1 : 0
1031 _this.grid.ds.load({});
1036 _this.panel.fireEvent('resize', [ '', '' ]);
1039 loadResponse : function(o, success, response){
1040 // this is overridden on before load..
1042 Roo.log("our code?");
1045 delete this.activeRequest;
1047 this.fireEvent("loadexception", this, o, response);
1048 o.request.callback.call(o.request.scope, null, o.request.arg, false);
1053 result = o.reader.read(response);
1055 Roo.log("load exception?");
1056 this.fireEvent("loadexception", this, o, response, e);
1057 o.request.callback.call(o.request.scope, null, o.request.arg, false);
1060 Roo.log("ready...");
1061 // loop through result.records;
1062 // and set this.tdate[date] = [] << array of records..
1064 Roo.each(result.records, function(r){
1066 if(typeof(_this.tdata[r.data.when_dt.format('j')]) == 'undefined'){
1067 _this.tdata[r.data.when_dt.format('j')] = [];
1069 _this.tdata[r.data.when_dt.format('j')].push(r.data);
1072 //Roo.log(_this.tdata);
1074 result.records = [];
1075 result.totalRecords = 6;
1077 // let's generate some duumy records for the rows.
1078 //var st = _this.dateField.getValue();
1080 // work out monday..
1081 //st = st.add(Date.DAY, -1 * st.format('w'));
1083 var date = Date.parseDate(_this.monthField.getValue(), "Y-m-d");
1085 var firstOfMonth = date.getFirstDayOfMonth();
1086 var days = date.getDaysInMonth();
1088 var firstAdded = false;
1089 for (var i = 0; i < result.totalRecords ; i++) {
1090 //var d= st.add(Date.DAY, i);
1093 for(var w = 0 ; w < 7 ; w++){
1094 if(!firstAdded && firstOfMonth != w){
1101 var dd = (d > 0 && d < 10) ? "0"+d : d;
1102 row['weekday'+w] = String.format(
1103 '<span style="font-size: 16px;"><b>{0}</b></span>'+
1104 '<span class="course-edit-link" style="color:blue;" data-id="0" data-date="{1}"> Add New</span>',
1106 date.format('Y-m-')+dd
1109 if(typeof(_this.tdata[d]) != 'undefined'){
1110 Roo.each(_this.tdata[d], function(r){
1114 var desc = (r.productitem_id_descrip) ? r.productitem_id_descrip : '';
1115 if(r.parent_id*1>0){
1116 is_sub = (r.productitem_id_visible*1 < 1) ? 'de-act-sup-link' :'sub-link';
1119 if(r.productitem_id_visible*1 < 1 && r.parent_id*1 < 1){
1120 deactive = 'de-act-link';
1123 row['weekday'+w] += String.format(
1124 '<br /><span class="course-edit-link {3} {4}" qtip="{5}" data-id="{0}">{2} - {1}</span>',
1126 r.product_id_name, //1
1127 r.when_dt.format('h:ia'), //2
1137 // only do this if something added..
1139 result.records.push(_this.grid.dataSource.reader.newRow(row));
1143 // push it twice. (second one with an hour..
1147 this.fireEvent("load", this, o, o.request.arg);
1148 o.request.callback.call(o.request.scope, result, o.request.arg, true);
1150 sortInfo : {field: 'when_dt', direction : 'ASC' },
1155 url : baseURL + '/Roo/Shop_course.php'
1158 xtype: 'JsonReader',
1175 'name': 'parent_id',
1179 'name': 'product_id',
1183 'name': 'productitem_id',
1201 click : function (_self, e)
1203 var sd = Date.parseDate(_this.monthField.getValue(), "Y-m-d");
1204 sd.setMonth(sd.getMonth()-1);
1205 _this.monthField.setValue(sd.format('Y-m-d'));
1206 _this.grid.ds.load({});
1216 xtype: 'MonthField',
1219 render : function (_self)
1221 _this.monthField = _self;
1222 // _this.monthField.set today
1224 select : function (combo, date)
1226 _this.grid.ds.load({});
1229 value : (function() { return new Date(); })()
1238 text : "Blue: in-active, green: in-active sup-event, red: de-active, purple: de-active sup-event"
1248 click : function (_self, e)
1250 var sd = Date.parseDate(_this.monthField.getValue(), "Y-m-d");
1251 sd.setMonth(sd.getMonth()+1);
1252 _this.monthField.setValue(sd.format('Y-m-d'));
1253 _this.grid.ds.load({});