Roo/bootstrap/Component.js
[roojs1] / roojs-calendar-debug.js
1 /*
2  * - LGPL
3  *
4  * base class for bootstrap elements.
5  * 
6  */
7
8 Roo.bootstrap = Roo.bootstrap || {};
9 /**
10  * @class Roo.bootstrap.Component
11  * @extends Roo.Component
12  * Bootstrap Component base class
13  * @cfg {String} cls css class
14  * @cfg {String} style any extra css
15  * @cfg {Object} xattr extra attributes to add to 'element' (used by builder to store stuff.)
16  * @cfg {Boolean} can_build_overlaid  True if element can be rebuild from a HTML page
17  * @cfg {string} dataId cutomer id
18  * @cfg {string} name Specifies name attribute
19  * @cfg {string} tooltip  Text for the tooltip
20  * @cfg {string} container_method method to fetch parents container element (used by NavHeaderbar -  getHeaderChildContainer)
21  * @cfg {string|object} visibilityEl (el|parent) What element to use for visibility (@see getVisibilityEl())
22  
23  * @constructor
24  * Do not use directly - it does not do anything..
25  * @param {Object} config The config object
26  */
27
28
29
30 Roo.bootstrap.Component = function(config){
31     Roo.bootstrap.Component.superclass.constructor.call(this, config);
32        
33     this.addEvents({
34         /**
35          * @event childrenrendered
36          * Fires when the children have been rendered..
37          * @param {Roo.bootstrap.Component} this
38          */
39         "childrenrendered" : true
40         
41         
42         
43     });
44     
45     
46 };
47
48 Roo.extend(Roo.bootstrap.Component, Roo.BoxComponent,  {
49     
50     
51     allowDomMove : false, // to stop relocations in parent onRender...
52     
53     cls : false,
54     
55     style : false,
56     
57     autoCreate : false,
58     
59     tooltip : null,
60     /**
61      * Initialize Events for the element
62      */
63     initEvents : function() { },
64     
65     xattr : false,
66     
67     parentId : false,
68     
69     can_build_overlaid : true,
70     
71     container_method : false,
72     
73     dataId : false,
74     
75     name : false,
76     
77     parent: function() {
78         // returns the parent component..
79         return Roo.ComponentMgr.get(this.parentId)
80         
81         
82     },
83     
84     // private
85     onRender : function(ct, position)
86     {
87        // Roo.log("Call onRender: " + this.xtype);
88         
89         Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
90         
91         if(this.el){
92             if (this.el.attr('xtype')) {
93                 this.el.attr('xtypex', this.el.attr('xtype'));
94                 this.el.dom.removeAttribute('xtype');
95                 
96                 this.initEvents();
97             }
98             
99             return;
100         }
101         
102          
103         
104         var cfg = Roo.apply({},  this.getAutoCreate());
105         
106         cfg.id = this.id || Roo.id();
107         
108         // fill in the extra attributes 
109         if (this.xattr && typeof(this.xattr) =='object') {
110             for (var i in this.xattr) {
111                 cfg[i] = this.xattr[i];
112             }
113         }
114         
115         if(this.dataId){
116             cfg.dataId = this.dataId;
117         }
118         
119         if (this.cls) {
120             cfg.cls = (typeof(cfg.cls) == 'undefined') ? this.cls : cfg.cls + ' ' + this.cls;
121         }
122         
123         if (this.style) { // fixme needs to support more complex style data.
124             cfg.style = this.style;
125         }
126         
127         if(this.name){
128             cfg.name = this.name;
129         }
130         
131         this.el = ct.createChild(cfg, position);
132         
133         if (this.tooltip) {
134             this.tooltipEl().attr('tooltip', this.tooltip);
135         }
136         
137         if(this.tabIndex !== undefined){
138             this.el.dom.setAttribute('tabIndex', this.tabIndex);
139         }
140         
141         this.initEvents();
142         
143     },
144     /**
145      * Fetch the element to add children to
146      * @return {Roo.Element} defaults to this.el
147      */
148     getChildContainer : function()
149     {
150         return this.el;
151     },
152     /**
153      * Fetch the element to display the tooltip on.
154      * @return {Roo.Element} defaults to this.el
155      */
156     tooltipEl : function()
157     {
158         return this.el;
159     },
160     /**
161      * This is really a wrapper for addxtypeChild
162      * it handles stuff relating to flexy:foreach / flexy:if
163      * = some of our projects use a flat rendering of the output, and try and overlay it with dynamic data.
164      *  -- this is a bit of a nightmare... and is even more confusing to debug..
165      *
166      *  
167      *
168      */
169     addxtype  : function(tree,cntr)
170     {
171         var cn = this;
172         
173         cn = Roo.factory(tree);
174         //Roo.log(['addxtype', cn]);
175            
176         cn.parentType = this.xtype; //??
177         cn.parentId = this.id;
178         
179         cntr = (typeof(cntr) == 'undefined' ) ? 'getChildContainer' : cntr;
180         if (typeof(cn.container_method) == 'string') {
181             cntr = cn.container_method;
182         }
183         
184         
185         var has_flexy_each =  (typeof(tree['flexy:foreach']) != 'undefined');
186         
187         var has_flexy_if =  (typeof(tree['flexy:if']) != 'undefined');
188         
189         var build_from_html =  Roo.XComponent.build_from_html;
190           
191         var is_body  = (tree.xtype == 'Body') ;
192           
193         var page_has_body = (Roo.get(document.body).attr('xtype') == 'Roo.bootstrap.Body');
194           
195         var self_cntr_el = Roo.get(this[cntr](false));
196         
197         // do not try and build conditional elements 
198         if ((has_flexy_each || has_flexy_if || this.can_build_overlaid == false ) && build_from_html) {
199             return false;
200         }
201         
202         if (!has_flexy_each || !build_from_html || is_body || !page_has_body) {
203             if(!has_flexy_if || typeof(tree.name) == 'undefined' || !build_from_html || is_body || !page_has_body){
204                 return this.addxtypeChild(tree,cntr, is_body);
205             }
206             
207             var echild =self_cntr_el ? self_cntr_el.child('>*[name=' + tree.name + ']') : false;
208                 
209             if(echild){
210                 return this.addxtypeChild(Roo.apply({}, tree),cntr);
211             }
212             
213             Roo.log('skipping render');
214             return cn;
215             
216         }
217         
218         var ret = false;
219         if (!build_from_html) {
220             return false;
221         }
222         
223         // this i think handles overlaying multiple children of the same type
224         // with the sam eelement.. - which might be buggy..
225         while (true) {
226             var echild =self_cntr_el ? self_cntr_el.child('>*[xtype]') : false;
227             
228             if (!echild) {
229                 break;
230             }
231             
232             if (echild && echild.attr('xtype').split('.').pop() != cn.xtype) {
233                 break;
234             }
235             
236             ret = this.addxtypeChild(Roo.apply({}, tree),cntr);
237         }
238        
239         return ret;
240     },
241     /**
242      * add a child to this element
243      *   - turn the child.cfg into a child_instance
244      *   - call child_instance.render( this { getContainerMethod()} )
245      *   - loop through the children, and call addxtype.. (reall this) on newly created child.
246      *  
247      */
248     
249     addxtypeChild : function (tree, cntr, is_body)
250     {
251         Roo.debug && Roo.log('addxtypeChild:' + cntr);
252         var cn = this;
253         cntr = (typeof(cntr) == 'undefined' ) ? 'getChildContainer' : cntr;
254         
255         
256         var has_flexy = (typeof(tree['flexy:if']) != 'undefined') ||
257                     (typeof(tree['flexy:foreach']) != 'undefined');
258           
259     
260         
261         skip_children = false;
262         // render the element if it's not BODY.
263         if (!is_body) {
264             
265             // if parent was disabled, then do not try and create the children..
266             if(!this[cntr](true)){
267                 tree.items = [];
268                 return tree;
269             }
270            
271             cn = Roo.factory(tree);
272            
273             cn.parentType = this.xtype; //??
274             cn.parentId = this.id;
275             
276             var build_from_html =  Roo.XComponent.build_from_html;
277             
278             
279             // does the container contain child eleemnts with 'xtype' attributes.
280             // that match this xtype..
281             // note - when we render we create these as well..
282             // so we should check to see if body has xtype set.
283             if (build_from_html && Roo.get(document.body).attr('xtype') == 'Roo.bootstrap.Body') {
284                
285                 var self_cntr_el = Roo.get(this[cntr](false));
286                 var echild =self_cntr_el ? self_cntr_el.child('>*[xtype]') : false;
287                 if (echild) { 
288                     //Roo.log(Roo.XComponent.build_from_html);
289                     //Roo.log("got echild:");
290                     //Roo.log(echild);
291                 }
292                 // there is a scenario where some of the child elements are flexy:if (and all of the same type)
293                 // and are not displayed -this causes this to use up the wrong element when matching.
294                 // at present the only work around for this is to nest flexy:if elements in another element that is always rendered.
295                 
296                 
297                 if (echild && echild.attr('xtype').split('.').pop() == cn.xtype) {
298                   //  Roo.log("found child for " + this.xtype +": " + echild.attr('xtype') );
299                   
300                   
301                   
302                     cn.el = echild;
303                   //  Roo.log("GOT");
304                     //echild.dom.removeAttribute('xtype');
305                 } else {
306                     Roo.debug && Roo.log("MISSING " + cn.xtype + " on child of " + (this.el ? this.el.attr('xbuilderid') : 'no parent'));
307                     Roo.debug && Roo.log(self_cntr_el);
308                     Roo.debug && Roo.log(echild);
309                     Roo.debug && Roo.log(cn);
310                 }
311             }
312            
313             
314            
315             // if object has flexy:if - then it may or may not be rendered.
316             if (build_from_html && has_flexy && !cn.el &&  cn.can_build_overlaid) {
317                 // skip a flexy if element.
318                 Roo.debug && Roo.log('skipping render');
319                 Roo.debug && Roo.log(tree);
320                 if (!cn.el) {
321                     Roo.debug && Roo.log('skipping all children');
322                     skip_children = true;
323                 }
324                 
325              } else {
326                  
327                 // actually if flexy:foreach is found, we really want to create 
328                 // multiple copies here...
329                 //Roo.log('render');
330                 //Roo.log(this[cntr]());
331                 // some elements do not have render methods.. like the layouts...
332                 /*
333                 if(this[cntr](true) === false){
334                     cn.items = [];
335                     return cn;
336                 }
337                 */
338                 cn.render && cn.render(this[cntr](true));
339                 
340              }
341             // then add the element..
342         }
343         
344         
345         
346         
347         
348         cn.addxtypeChildren(tree.items, skip_children);
349         delete tree.items;
350         return cn;
351     },
352     
353     /**
354      *  add a number of children to this object,
355      *     which in turn calls render...
356      *  
357      */
358     
359     addxtypeChildren: function(child_array, skip_children)
360     {
361         var nitems = [];
362         if (!child_array || !child_array.length ) {
363             this.items = nitems;
364             return;
365         }
366         
367         for(var i =0;i < child_array.length;i++) {
368             if (skip_children) {
369                 break;
370             }
371             //  Roo.log(['add child', items[i]]);
372             nitems.push(this.addxtype(Roo.apply({}, child_array[i])));
373         }
374         this.items = nitems;
375         
376         this.fireEvent('childrenrendered', this);
377          
378     },
379     
380     
381     
382     /**
383      * Set the element that will be used to show or hide
384      */
385     setVisibilityEl : function(el)
386     {
387         this.visibilityEl = el;
388     },
389     
390      /**
391      * Get the element that will be used to show or hide
392      */
393     getVisibilityEl : function()
394     {
395         if (typeof(this.visibilityEl) == 'object') {
396             return this.visibilityEl;
397         }
398         
399         if (typeof(this.visibilityEl) == 'string') {
400             return this.visibilityEl == 'parent' ? this.parent().getEl() : this.getEl();
401         }
402         
403         return this.getEl();
404     },
405     
406     /**
407      * Show a component - removes 'hidden' class
408      */
409     show : function()
410     {
411         if(!this.getVisibilityEl()){
412             return;
413         }
414          
415         this.getVisibilityEl().removeClass(['hidden','d-none']);
416         
417         this.fireEvent('show', this);
418         
419         
420     },
421     /**
422      * Hide a component - adds 'hidden' class
423      */
424     hide: function()
425     {
426         if(!this.getVisibilityEl()){
427             return;
428         }
429         
430         this.getVisibilityEl().addClass(['hidden','d-none']);
431         
432         this.fireEvent('hide', this);
433         
434     }
435 });
436
437  /*
438  * - LGPL
439  *
440  * based on jquery fullcalendar
441  * 
442  */
443
444 Roo.bootstrap = Roo.bootstrap || {};
445 /**
446  * @class Roo.bootstrap.Calendar
447  * @extends Roo.bootstrap.Component
448  * Bootstrap Calendar class
449  * @cfg {Boolean} loadMask (true|false) default false
450  * @cfg {Object} header generate the user specific header of the calendar, default false
451
452  * @constructor
453  * Create a new Container
454  * @param {Object} config The config object
455  */
456
457
458
459 Roo.bootstrap.Calendar = function(config){
460     Roo.bootstrap.Calendar.superclass.constructor.call(this, config);
461      this.addEvents({
462         /**
463              * @event select
464              * Fires when a date is selected
465              * @param {DatePicker} this
466              * @param {Date} date The selected date
467              */
468         'select': true,
469         /**
470              * @event monthchange
471              * Fires when the displayed month changes 
472              * @param {DatePicker} this
473              * @param {Date} date The selected month
474              */
475         'monthchange': true,
476         /**
477              * @event evententer
478              * Fires when mouse over an event
479              * @param {Calendar} this
480              * @param {event} Event
481              */
482         'evententer': true,
483         /**
484              * @event eventleave
485              * Fires when the mouse leaves an
486              * @param {Calendar} this
487              * @param {event}
488              */
489         'eventleave': true,
490         /**
491              * @event eventclick
492              * Fires when the mouse click an
493              * @param {Calendar} this
494              * @param {event}
495              */
496         'eventclick': true
497         
498     });
499
500 };
501
502 Roo.extend(Roo.bootstrap.Calendar, Roo.bootstrap.Component,  {
503     
504      /**
505      * @cfg {Number} startDay
506      * Day index at which the week should begin, 0-based (defaults to 0, which is Sunday)
507      */
508     startDay : 0,
509     
510     loadMask : false,
511     
512     header : false,
513       
514     getAutoCreate : function(){
515         
516         
517         var fc_button = function(name, corner, style, content ) {
518             return Roo.apply({},{
519                 tag : 'span',
520                 cls : 'fc-button fc-button-'+name+' fc-state-default ' + 
521                          (corner.length ?
522                             'fc-corner-' + corner.split(' ').join(' fc-corner-') :
523                             ''
524                         ),
525                 html : '<SPAN class="fc-text-'+style+ '">'+content +'</SPAN>',
526                 unselectable: 'on'
527             });
528         };
529         
530         var header = {};
531         
532         if(!this.header){
533             header = {
534                 tag : 'table',
535                 cls : 'fc-header',
536                 style : 'width:100%',
537                 cn : [
538                     {
539                         tag: 'tr',
540                         cn : [
541                             {
542                                 tag : 'td',
543                                 cls : 'fc-header-left',
544                                 cn : [
545                                     fc_button('prev', 'left', 'arrow', '&#8249;' ),
546                                     fc_button('next', 'right', 'arrow', '&#8250;' ),
547                                     { tag: 'span', cls: 'fc-header-space' },
548                                     fc_button('today', 'left right', '', 'today' )  // neds state disabled..
549
550
551                                 ]
552                             },
553
554                             {
555                                 tag : 'td',
556                                 cls : 'fc-header-center',
557                                 cn : [
558                                     {
559                                         tag: 'span',
560                                         cls: 'fc-header-title',
561                                         cn : {
562                                             tag: 'H2',
563                                             html : 'month / year'
564                                         }
565                                     }
566
567                                 ]
568                             },
569                             {
570                                 tag : 'td',
571                                 cls : 'fc-header-right',
572                                 cn : [
573                               /*      fc_button('month', 'left', '', 'month' ),
574                                     fc_button('week', '', '', 'week' ),
575                                     fc_button('day', 'right', '', 'day' )
576                                 */    
577
578                                 ]
579                             }
580
581                         ]
582                     }
583                 ]
584             };
585         }
586         
587         header = this.header;
588         
589        
590         var cal_heads = function() {
591             var ret = [];
592             // fixme - handle this.
593             
594             for (var i =0; i < Date.dayNames.length; i++) {
595                 var d = Date.dayNames[i];
596                 ret.push({
597                     tag: 'th',
598                     cls : 'fc-day-header fc-' + d.substring(0,3).toLowerCase() + ' fc-widget-header',
599                     html : d.substring(0,3)
600                 });
601                 
602             }
603             ret[0].cls += ' fc-first';
604             ret[6].cls += ' fc-last';
605             return ret;
606         };
607         var cal_cell = function(n) {
608             return  {
609                 tag: 'td',
610                 cls : 'fc-day fc-'+n + ' fc-widget-content', ///fc-other-month fc-past
611                 cn : [
612                     {
613                         cn : [
614                             {
615                                 cls: 'fc-day-number',
616                                 html: 'D'
617                             },
618                             {
619                                 cls: 'fc-day-content',
620                              
621                                 cn : [
622                                      {
623                                         style: 'position: relative;' // height: 17px;
624                                     }
625                                 ]
626                             }
627                             
628                             
629                         ]
630                     }
631                 ]
632                 
633             }
634         };
635         var cal_rows = function() {
636             
637             var ret = [];
638             for (var r = 0; r < 6; r++) {
639                 var row= {
640                     tag : 'tr',
641                     cls : 'fc-week',
642                     cn : []
643                 };
644                 
645                 for (var i =0; i < Date.dayNames.length; i++) {
646                     var d = Date.dayNames[i];
647                     row.cn.push(cal_cell(d.substring(0,3).toLowerCase()));
648
649                 }
650                 row.cn[0].cls+=' fc-first';
651                 row.cn[0].cn[0].style = 'min-height:90px';
652                 row.cn[6].cls+=' fc-last';
653                 ret.push(row);
654                 
655             }
656             ret[0].cls += ' fc-first';
657             ret[4].cls += ' fc-prev-last';
658             ret[5].cls += ' fc-last';
659             return ret;
660             
661         };
662         
663         var cal_table = {
664             tag: 'table',
665             cls: 'fc-border-separate',
666             style : 'width:100%',
667             cellspacing  : 0,
668             cn : [
669                 { 
670                     tag: 'thead',
671                     cn : [
672                         { 
673                             tag: 'tr',
674                             cls : 'fc-first fc-last',
675                             cn : cal_heads()
676                         }
677                     ]
678                 },
679                 { 
680                     tag: 'tbody',
681                     cn : cal_rows()
682                 }
683                   
684             ]
685         };
686          
687          var cfg = {
688             cls : 'fc fc-ltr',
689             cn : [
690                 header,
691                 {
692                     cls : 'fc-content',
693                     style : "position: relative;",
694                     cn : [
695                         {
696                             cls : 'fc-view fc-view-month fc-grid',
697                             style : 'position: relative',
698                             unselectable : 'on',
699                             cn : [
700                                 {
701                                     cls : 'fc-event-container',
702                                     style : 'position:absolute;z-index:8;top:0;left:0;'
703                                 },
704                                 cal_table
705                             ]
706                         }
707                     ]
708     
709                 }
710            ] 
711             
712         };
713         
714          
715         
716         return cfg;
717     },
718     
719     
720     initEvents : function()
721     {
722         if(!this.store){
723             throw "can not find store for calendar";
724         }
725         
726         var mark = {
727             tag: "div",
728             cls:"x-dlg-mask",
729             style: "text-align:center",
730             cn: [
731                 {
732                     tag: "div",
733                     style: "background-color:white;width:50%;margin:250 auto",
734                     cn: [
735                         {
736                             tag: "img",
737                             src: Roo.rootURL + '/images/ux/lightbox/loading.gif' 
738                         },
739                         {
740                             tag: "span",
741                             html: "Loading"
742                         }
743                         
744                     ]
745                 }
746             ]
747         };
748         this.maskEl = Roo.DomHelper.append(this.el.select('.fc-content', true).first(), mark, true);
749         
750         var size = this.el.select('.fc-content', true).first().getSize();
751         this.maskEl.setSize(size.width, size.height);
752         this.maskEl.enableDisplayMode("block");
753         if(!this.loadMask){
754             this.maskEl.hide();
755         }
756         
757         this.store = Roo.factory(this.store, Roo.data);
758         this.store.on('load', this.onLoad, this);
759         this.store.on('beforeload', this.onBeforeLoad, this);
760         
761         this.resize();
762         
763         this.cells = this.el.select('.fc-day',true);
764         //Roo.log(this.cells);
765         this.textNodes = this.el.query('.fc-day-number');
766         this.cells.addClassOnOver('fc-state-hover');
767         
768         this.el.select('.fc-button-prev',true).on('click', this.showPrevMonth, this);
769         this.el.select('.fc-button-next',true).on('click', this.showNextMonth, this);
770         this.el.select('.fc-button-today',true).on('click', this.showToday, this);
771         this.el.select('.fc-button',true).addClassOnOver('fc-state-hover');
772         
773         this.on('monthchange', this.onMonthChange, this);
774         
775         this.update(new Date().clearTime());
776     },
777     
778     resize : function() {
779         var sz  = this.el.getSize();
780         
781         this.el.select('.fc-day-header',true).setWidth(sz.width / 7);
782         this.el.select('.fc-day-content div',true).setHeight(34);
783     },
784     
785     
786     // private
787     showPrevMonth : function(e){
788         this.update(this.activeDate.add("mo", -1));
789     },
790     showToday : function(e){
791         this.update(new Date().clearTime());
792     },
793     // private
794     showNextMonth : function(e){
795         this.update(this.activeDate.add("mo", 1));
796     },
797
798     // private
799     showPrevYear : function(){
800         this.update(this.activeDate.add("y", -1));
801     },
802
803     // private
804     showNextYear : function(){
805         this.update(this.activeDate.add("y", 1));
806     },
807
808     
809    // private
810     update : function(date)
811     {
812         var vd = this.activeDate;
813         this.activeDate = date;
814 //        if(vd && this.el){
815 //            var t = date.getTime();
816 //            if(vd.getMonth() == date.getMonth() && vd.getFullYear() == date.getFullYear()){
817 //                Roo.log('using add remove');
818 //                
819 //                this.fireEvent('monthchange', this, date);
820 //                
821 //                this.cells.removeClass("fc-state-highlight");
822 //                this.cells.each(function(c){
823 //                   if(c.dateValue == t){
824 //                       c.addClass("fc-state-highlight");
825 //                       setTimeout(function(){
826 //                            try{c.dom.firstChild.focus();}catch(e){}
827 //                       }, 50);
828 //                       return false;
829 //                   }
830 //                   return true;
831 //                });
832 //                return;
833 //            }
834 //        }
835         
836         var days = date.getDaysInMonth();
837         
838         var firstOfMonth = date.getFirstDateOfMonth();
839         var startingPos = firstOfMonth.getDay()-this.startDay;
840         
841         if(startingPos < this.startDay){
842             startingPos += 7;
843         }
844         
845         var pm = date.add(Date.MONTH, -1);
846         var prevStart = pm.getDaysInMonth()-startingPos;
847 //        
848         this.cells = this.el.select('.fc-day',true);
849         this.textNodes = this.el.query('.fc-day-number');
850         this.cells.addClassOnOver('fc-state-hover');
851         
852         var cells = this.cells.elements;
853         var textEls = this.textNodes;
854         
855         Roo.each(cells, function(cell){
856             cell.removeClass([ 'fc-past', 'fc-other-month', 'fc-future', 'fc-state-highlight', 'fc-state-disabled']);
857         });
858         
859         days += startingPos;
860
861         // convert everything to numbers so it's fast
862         var day = 86400000;
863         var d = (new Date(pm.getFullYear(), pm.getMonth(), prevStart)).clearTime();
864         //Roo.log(d);
865         //Roo.log(pm);
866         //Roo.log(prevStart);
867         
868         var today = new Date().clearTime().getTime();
869         var sel = date.clearTime().getTime();
870         var min = this.minDate ? this.minDate.clearTime() : Number.NEGATIVE_INFINITY;
871         var max = this.maxDate ? this.maxDate.clearTime() : Number.POSITIVE_INFINITY;
872         var ddMatch = this.disabledDatesRE;
873         var ddText = this.disabledDatesText;
874         var ddays = this.disabledDays ? this.disabledDays.join("") : false;
875         var ddaysText = this.disabledDaysText;
876         var format = this.format;
877         
878         var setCellClass = function(cal, cell){
879             cell.row = 0;
880             cell.events = [];
881             cell.more = [];
882             //Roo.log('set Cell Class');
883             cell.title = "";
884             var t = d.getTime();
885             
886             //Roo.log(d);
887             
888             cell.dateValue = t;
889             if(t == today){
890                 cell.className += " fc-today";
891                 cell.className += " fc-state-highlight";
892                 cell.title = cal.todayText;
893             }
894             if(t == sel){
895                 // disable highlight in other month..
896                 //cell.className += " fc-state-highlight";
897                 
898             }
899             // disabling
900             if(t < min) {
901                 cell.className = " fc-state-disabled";
902                 cell.title = cal.minText;
903                 return;
904             }
905             if(t > max) {
906                 cell.className = " fc-state-disabled";
907                 cell.title = cal.maxText;
908                 return;
909             }
910             if(ddays){
911                 if(ddays.indexOf(d.getDay()) != -1){
912                     cell.title = ddaysText;
913                     cell.className = " fc-state-disabled";
914                 }
915             }
916             if(ddMatch && format){
917                 var fvalue = d.dateFormat(format);
918                 if(ddMatch.test(fvalue)){
919                     cell.title = ddText.replace("%0", fvalue);
920                     cell.className = " fc-state-disabled";
921                 }
922             }
923             
924             if (!cell.initialClassName) {
925                 cell.initialClassName = cell.dom.className;
926             }
927             
928             cell.dom.className = cell.initialClassName  + ' ' +  cell.className;
929         };
930
931         var i = 0;
932         
933         for(; i < startingPos; i++) {
934             textEls[i].innerHTML = (++prevStart);
935             d.setDate(d.getDate()+1);
936             
937             cells[i].className = "fc-past fc-other-month";
938             setCellClass(this, cells[i]);
939         }
940         
941         var intDay = 0;
942         
943         for(; i < days; i++){
944             intDay = i - startingPos + 1;
945             textEls[i].innerHTML = (intDay);
946             d.setDate(d.getDate()+1);
947             
948             cells[i].className = ''; // "x-date-active";
949             setCellClass(this, cells[i]);
950         }
951         var extraDays = 0;
952         
953         for(; i < 42; i++) {
954             textEls[i].innerHTML = (++extraDays);
955             d.setDate(d.getDate()+1);
956             
957             cells[i].className = "fc-future fc-other-month";
958             setCellClass(this, cells[i]);
959         }
960         
961         this.el.select('.fc-header-title h2',true).update(Date.monthNames[date.getMonth()] + " " + date.getFullYear());
962         
963         var totalRows = Math.ceil((date.getDaysInMonth() + date.getFirstDateOfMonth().getDay()) / 7);
964         
965         this.el.select('tr.fc-week.fc-prev-last',true).removeClass('fc-last');
966         this.el.select('tr.fc-week.fc-next-last',true).addClass('fc-last').show();
967         
968         if(totalRows != 6){
969             this.el.select('tr.fc-week.fc-last',true).removeClass('fc-last').addClass('fc-next-last').hide();
970             this.el.select('tr.fc-week.fc-prev-last',true).addClass('fc-last');
971         }
972         
973         this.fireEvent('monthchange', this, date);
974         
975         
976         /*
977         if(!this.internalRender){
978             var main = this.el.dom.firstChild;
979             var w = main.offsetWidth;
980             this.el.setWidth(w + this.el.getBorderWidth("lr"));
981             Roo.fly(main).setWidth(w);
982             this.internalRender = true;
983             // opera does not respect the auto grow header center column
984             // then, after it gets a width opera refuses to recalculate
985             // without a second pass
986             if(Roo.isOpera && !this.secondPass){
987                 main.rows[0].cells[1].style.width = (w - (main.rows[0].cells[0].offsetWidth+main.rows[0].cells[2].offsetWidth)) + "px";
988                 this.secondPass = true;
989                 this.update.defer(10, this, [date]);
990             }
991         }
992         */
993         
994     },
995     
996     findCell : function(dt) {
997         dt = dt.clearTime().getTime();
998         var ret = false;
999         this.cells.each(function(c){
1000             //Roo.log("check " +c.dateValue + '?=' + dt);
1001             if(c.dateValue == dt){
1002                 ret = c;
1003                 return false;
1004             }
1005             return true;
1006         });
1007         
1008         return ret;
1009     },
1010     
1011     findCells : function(ev) {
1012         var s = ev.start.clone().clearTime().getTime();
1013        // Roo.log(s);
1014         var e= ev.end.clone().clearTime().getTime();
1015        // Roo.log(e);
1016         var ret = [];
1017         this.cells.each(function(c){
1018              ////Roo.log("check " +c.dateValue + '<' + e + ' > ' + s);
1019             
1020             if(c.dateValue > e){
1021                 return ;
1022             }
1023             if(c.dateValue < s){
1024                 return ;
1025             }
1026             ret.push(c);
1027         });
1028         
1029         return ret;    
1030     },
1031     
1032 //    findBestRow: function(cells)
1033 //    {
1034 //        var ret = 0;
1035 //        
1036 //        for (var i =0 ; i < cells.length;i++) {
1037 //            ret  = Math.max(cells[i].rows || 0,ret);
1038 //        }
1039 //        return ret;
1040 //        
1041 //    },
1042     
1043     
1044     addItem : function(ev)
1045     {
1046         // look for vertical location slot in
1047         var cells = this.findCells(ev);
1048         
1049 //        ev.row = this.findBestRow(cells);
1050         
1051         // work out the location.
1052         
1053         var crow = false;
1054         var rows = [];
1055         for(var i =0; i < cells.length; i++) {
1056             
1057             cells[i].row = cells[0].row;
1058             
1059             if(i == 0){
1060                 cells[i].row = cells[i].row + 1;
1061             }
1062             
1063             if (!crow) {
1064                 crow = {
1065                     start : cells[i],
1066                     end :  cells[i]
1067                 };
1068                 continue;
1069             }
1070             if (crow.start.getY() == cells[i].getY()) {
1071                 // on same row.
1072                 crow.end = cells[i];
1073                 continue;
1074             }
1075             // different row.
1076             rows.push(crow);
1077             crow = {
1078                 start: cells[i],
1079                 end : cells[i]
1080             };
1081             
1082         }
1083         
1084         rows.push(crow);
1085         ev.els = [];
1086         ev.rows = rows;
1087         ev.cells = cells;
1088         
1089         cells[0].events.push(ev);
1090         
1091         this.calevents.push(ev);
1092     },
1093     
1094     clearEvents: function() {
1095         
1096         if(!this.calevents){
1097             return;
1098         }
1099         
1100         Roo.each(this.cells.elements, function(c){
1101             c.row = 0;
1102             c.events = [];
1103             c.more = [];
1104         });
1105         
1106         Roo.each(this.calevents, function(e) {
1107             Roo.each(e.els, function(el) {
1108                 el.un('mouseenter' ,this.onEventEnter, this);
1109                 el.un('mouseleave' ,this.onEventLeave, this);
1110                 el.remove();
1111             },this);
1112         },this);
1113         
1114         Roo.each(Roo.select('.fc-more-event', true).elements, function(e){
1115             e.remove();
1116         });
1117         
1118     },
1119     
1120     renderEvents: function()
1121     {   
1122         var _this = this;
1123         
1124         this.cells.each(function(c) {
1125             
1126             if(c.row < 5){
1127                 return;
1128             }
1129             
1130             var ev = c.events;
1131             
1132             var r = 4;
1133             if(c.row != c.events.length){
1134                 r = 4 - (4 - (c.row - c.events.length));
1135             }
1136             
1137             c.events = ev.slice(0, r);
1138             c.more = ev.slice(r);
1139             
1140             if(c.more.length && c.more.length == 1){
1141                 c.events.push(c.more.pop());
1142             }
1143             
1144             c.row = (c.row - ev.length) + c.events.length + ((c.more.length) ? 1 : 0);
1145             
1146         });
1147             
1148         this.cells.each(function(c) {
1149             
1150             c.select('.fc-day-content div',true).first().setHeight(Math.max(34, c.row * 20));
1151             
1152             
1153             for (var e = 0; e < c.events.length; e++){
1154                 var ev = c.events[e];
1155                 var rows = ev.rows;
1156                 
1157                 for(var i = 0; i < rows.length; i++) {
1158                 
1159                     // how many rows should it span..
1160
1161                     var  cfg = {
1162                         cls : 'roo-dynamic fc-event fc-event-hori fc-event-draggable ui-draggable',
1163                         style : 'position: absolute', // left: 387px; width: 121px; top: 359px;
1164
1165                         unselectable : "on",
1166                         cn : [
1167                             {
1168                                 cls: 'fc-event-inner',
1169                                 cn : [
1170     //                                {
1171     //                                  tag:'span',
1172     //                                  cls: 'fc-event-time',
1173     //                                  html : cells.length > 1 ? '' : ev.time
1174     //                                },
1175                                     {
1176                                       tag:'span',
1177                                       cls: 'fc-event-title',
1178                                       html : String.format('{0}', ev.title)
1179                                     }
1180
1181
1182                                 ]
1183                             },
1184                             {
1185                                 cls: 'ui-resizable-handle ui-resizable-e',
1186                                 html : '&nbsp;&nbsp;&nbsp'
1187                             }
1188
1189                         ]
1190                     };
1191
1192                     if (i == 0) {
1193                         cfg.cls += ' fc-event-start';
1194                     }
1195                     if ((i+1) == rows.length) {
1196                         cfg.cls += ' fc-event-end';
1197                     }
1198
1199                     var ctr = _this.el.select('.fc-event-container',true).first();
1200                     var cg = ctr.createChild(cfg);
1201
1202                     var sbox = rows[i].start.select('.fc-day-content',true).first().getBox();
1203                     var ebox = rows[i].end.select('.fc-day-content',true).first().getBox();
1204
1205                     var r = (c.more.length) ? 1 : 0;
1206                     cg.setXY([sbox.x +2, sbox.y + ((c.row - c.events.length - r + e) * 20)]);    
1207                     cg.setWidth(ebox.right - sbox.x -2);
1208
1209                     cg.on('mouseenter' ,_this.onEventEnter, _this, ev);
1210                     cg.on('mouseleave' ,_this.onEventLeave, _this, ev);
1211                     cg.on('click', _this.onEventClick, _this, ev);
1212
1213                     ev.els.push(cg);
1214                     
1215                 }
1216                 
1217             }
1218             
1219             
1220             if(c.more.length){
1221                 var  cfg = {
1222                     cls : 'fc-more-event roo-dynamic fc-event fc-event-hori fc-event-draggable ui-draggable fc-event-start fc-event-end',
1223                     style : 'position: absolute',
1224                     unselectable : "on",
1225                     cn : [
1226                         {
1227                             cls: 'fc-event-inner',
1228                             cn : [
1229                                 {
1230                                   tag:'span',
1231                                   cls: 'fc-event-title',
1232                                   html : 'More'
1233                                 }
1234
1235
1236                             ]
1237                         },
1238                         {
1239                             cls: 'ui-resizable-handle ui-resizable-e',
1240                             html : '&nbsp;&nbsp;&nbsp'
1241                         }
1242
1243                     ]
1244                 };
1245
1246                 var ctr = _this.el.select('.fc-event-container',true).first();
1247                 var cg = ctr.createChild(cfg);
1248
1249                 var sbox = c.select('.fc-day-content',true).first().getBox();
1250                 var ebox = c.select('.fc-day-content',true).first().getBox();
1251                 //Roo.log(cg);
1252                 cg.setXY([sbox.x +2, sbox.y +((c.row - 1) * 20)]);    
1253                 cg.setWidth(ebox.right - sbox.x -2);
1254
1255                 cg.on('click', _this.onMoreEventClick, _this, c.more);
1256                 
1257             }
1258             
1259         });
1260         
1261         
1262         
1263     },
1264     
1265     onEventEnter: function (e, el,event,d) {
1266         this.fireEvent('evententer', this, el, event);
1267     },
1268     
1269     onEventLeave: function (e, el,event,d) {
1270         this.fireEvent('eventleave', this, el, event);
1271     },
1272     
1273     onEventClick: function (e, el,event,d) {
1274         this.fireEvent('eventclick', this, el, event);
1275     },
1276     
1277     onMonthChange: function () {
1278         this.store.load();
1279     },
1280     
1281     onMoreEventClick: function(e, el, more)
1282     {
1283         var _this = this;
1284         
1285         this.calpopover.placement = 'right';
1286         this.calpopover.setTitle('More');
1287         
1288         this.calpopover.setContent('');
1289         
1290         var ctr = this.calpopover.el.select('.popover-content', true).first();
1291         
1292         Roo.each(more, function(m){
1293             var cfg = {
1294                 cls : 'fc-event-hori fc-event-draggable',
1295                 html : m.title
1296             };
1297             var cg = ctr.createChild(cfg);
1298             
1299             cg.on('click', _this.onEventClick, _this, m);
1300         });
1301         
1302         this.calpopover.show(el);
1303         
1304         
1305     },
1306     
1307     onLoad: function () 
1308     {   
1309         this.calevents = [];
1310         var cal = this;
1311         
1312         if(this.store.getCount() > 0){
1313             this.store.data.each(function(d){
1314                cal.addItem({
1315                     id : d.data.id,
1316                     start: (typeof(d.data.start_dt) === 'string') ? new Date.parseDate(d.data.start_dt, 'Y-m-d H:i:s') : d.data.start_dt,
1317                     end : (typeof(d.data.end_dt) === 'string') ? new Date.parseDate(d.data.end_dt, 'Y-m-d H:i:s') : d.data.end_dt,
1318                     time : d.data.start_time,
1319                     title : d.data.title,
1320                     description : d.data.description,
1321                     venue : d.data.venue
1322                 });
1323             });
1324         }
1325         
1326         this.renderEvents();
1327         
1328         if(this.calevents.length && this.loadMask){
1329             this.maskEl.hide();
1330         }
1331     },
1332     
1333     onBeforeLoad: function()
1334     {
1335         this.clearEvents();
1336         if(this.loadMask){
1337             this.maskEl.show();
1338         }
1339     }
1340 });
1341
1342  
1343  /*
1344  * - LGPL
1345  *
1346  * based on jquery fullcalendar
1347  * 
1348  */
1349
1350
1351 /**
1352  * @class Roo.Calendar
1353  * @extends Roo.Component
1354  * Bootstrap Calendar class
1355     
1356  * @constructor
1357  * Create a new Container
1358  * @param {Object} config The config object
1359  */
1360
1361 Roo.CalendarPanel = function(config){
1362     
1363     Roo.log("cal panel ctr");
1364   
1365     this.wrapper = Roo.DomHelper.append(document.body, // wrapper for IE7 strict & safari scroll issue
1366         {tag: "div", cls: "x-layout-grid-wrapper x-layout-inactive-content"}, true);
1367         
1368     //this.wrapper.dom.appendChild(grid.getGridEl().dom);
1369     
1370     Roo.CalendarPanel.superclass.constructor.call(this, this.wrapper, config);
1371     
1372     Roo.log(this.el);
1373     
1374     if(this.toolbar){
1375         this.toolbar.el.insertBefore(this.wrapper.dom.firstChild);
1376     }
1377     // xtype created footer. - not sure if will work as we normally have to render first..
1378     if (this.footer && !this.footer.el && this.footer.xtype) {
1379         
1380         //this.footer.container = this.grid.getView().getFooterPanel(true);
1381         //this.footer.dataSource = this.grid.dataSource;
1382         //this.footer = Roo.factory(this.footer, Roo);
1383         
1384     }
1385     this.view = new Roo.calendar.View(Roo.apply({
1386         skipNavHeader : true,
1387         skipMonthHeader : false
1388         
1389     },config));
1390     
1391      
1392     this.on('activate', function()
1393     {
1394         Roo.log('activate');
1395          
1396         //console.log('render tree');
1397         this.render();
1398     },this);
1399     
1400     this.addEvents({
1401         /**
1402              * @event select
1403              * Fires when a date is selected
1404              * @param {DatePicker} this
1405              * @param {Date} date The selected date
1406              */
1407         'select': true,
1408         /**
1409              * @event monthchange
1410              * Fires when the displayed month changes 
1411              * @param {DatePicker} this
1412              * @param {Date} date The selected month
1413              */
1414         'monthchange': true,
1415         /**
1416              * @event evententer
1417              * Fires when mouse over an event
1418              * @param {Calendar} this
1419              * @param {event} Event
1420              */
1421         'evententer': true,
1422         /**
1423              * @event eventleave
1424              * Fires when the mouse leaves an
1425              * @param {Calendar} this
1426              * @param {event}
1427              */
1428         'eventleave': true,
1429         /**
1430              * @event eventclick
1431              * Fires when the mouse click an
1432              * @param {Calendar} this
1433              * @param {event}
1434              */
1435         'eventclick': true,
1436         /**
1437              * @event rendered
1438              * Fires when the grid is rendered
1439              * @param {Calendar} this
1440             
1441              */
1442         'rendered': true
1443         
1444         
1445     });
1446     this.relayEvents(this.view, ["select","monthchange","evententer","eventleave","rendered"]);
1447
1448     //this.grid = grid;
1449     //this.grid.getGridEl().replaceClass("x-layout-inactive-content", "x-layout-component-panel");
1450 };
1451
1452
1453 Roo.extend(Roo.CalendarPanel, Roo.ContentPanel, {
1454     
1455       
1456     render : function()
1457     {
1458         var ct = this.el.appendChild(document.createElement("div"));
1459         this.onRender(this.el, false)
1460     },
1461     
1462     onRender : function(ct, position)
1463     {
1464         if (this.rendered) {
1465             return;
1466         }
1467         this.rendered = true;
1468         
1469         Roo.log("render calendar");
1470         
1471         
1472         //Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
1473         
1474         
1475         var cfg = Roo.apply({},  this.view.getAutoCreate());
1476         cfg.id = Roo.id();
1477         
1478         // fill in the extra attributes 
1479         if (this.xattr && typeof(this.xattr) =='object') {
1480             for (var i in this.xattr) {
1481                 cfg[i] = this.xattr[i];
1482             }
1483         }
1484         
1485         if(this.dataId){
1486             cfg.dataId = this.dataId;
1487         }
1488         
1489         if (this.cls) {
1490             cfg.cls = (typeof(cfg.cls) == 'undefined') ? this.cls : cfg.cls + ' ' + this.cls;
1491         }
1492         
1493         if (this.style) { // fixme needs to support more complex style data.
1494             cfg.style = this.style;
1495         }
1496         
1497         if(this.name){
1498             cfg.name = this.name;
1499         }
1500         
1501         this.view.el =  ct.createChild(cfg, position);
1502         
1503         //if(this.tabIndex !== undefined){
1504         //    this.el.dom.setAttribute('tabIndex', this.tabIndex);
1505         //}
1506         
1507         
1508         this.view.initEvents();
1509         this.fireEvent('rendered');
1510     }
1511     
1512     
1513     
1514     
1515 });
1516
1517