Ext.menu.TwoDateMenu = Ext.extend(Ext.menu.Menu, { /** * @cfg {Boolean} enableScrolling * @hide */ enableScrolling : false, /** * @cfg {Boolean} hideOnClick * False to continue showing the menu after a date is selected, defaults to true. */ hideOnClick : false, /** * @cfg {String} pickerId * An id to assign to the underlying date picker. Defaults to null. */ pickerId : null, cls : 'x-date-menu', /** * @event itemclick * @hide */ initComponent : function(){ this.on('beforeshow', this.onBeforeShow, this); if(this.strict = (Ext.isIE7 && Ext.isStrict)){ this.on('show', this.onShow, this, {single: true, delay: 20}); } var pm = this; var selectTwo = function(btn){ pm.picker.fireEvent('select', btn); }; this.buttoner = new Ext.Container({ itemId:'towcont', style: 'width:80px; height:100%; float:left; margin-right:1px; background-color:#fff;', items: [{xtype:'button', text:'本周', handler:selectTwo, itemId:'week', width:78, style:'margin-top:16px;'}, {xtype:'button', text:'本月', handler:selectTwo, itemId:'month', width:78, style:'margin-top:2px;'}, {xtype:'button', text:'三月', handler:selectTwo, itemId:'quarter', width:78, style:'margin-top:2px;'}, {xtype:'button', text:'本年', handler:selectTwo, itemId:'year', width:78, style:'margin-top:2px;'}, {xtype:'button', text:'上周', handler:selectTwo, itemId:'preweek', width:78, style:'margin-top:2px;'}, {xtype:'button', text:'上月', handler:selectTwo, itemId:'premonth', width:78, style:'margin-top:2px;'} ] }); this.picker = new Ext.DatePicker(Ext.applyIf({ internalRender: this.strict || !Ext.isIE, ctCls: 'x-menu-date-item', style: 'width:280px; float:left; margin-right:1px;', id: this.pickerId }, this.initialConfig)); this.two_picker = new Ext.DatePicker(Ext.applyIf({ internalRender: this.strict || !Ext.isIE, ctCls: 'x-menu-date-item', style: 'width:280px; float:left;', id: this.pickerId+'2' }, this.initialConfig)); Ext.apply(this, { plain: true, showSeparator: false, items: {xtype:'container', style:'width:642px; height:248px; background-color:#cacaca;', items:[this.buttoner, this.picker, this.two_picker]} }); this.picker.purgeListeners(); this.two_picker.purgeListeners(); Ext.menu.TwoDateMenu.superclass.initComponent.call(this); //select date setvalue, hide menu this.relayEvents(this.picker, ['select']); this.relayEvents(this.two_picker, ['select']); this.on('show', this.picker.focus, this.picker); this.on('select', this.menuHide, this); if(this.handler){ this.on('select', this.handler, this.scope || this); } }, menuHide : function() { if(this.hideOnClick){ this.hide(true); } }, onBeforeShow : function(){ if(this.picker){ this.picker.hideMonthPicker(true); } } }); Ext.reg('twodatemenu', Ext.menu.TwoDateMenu); // -------------------调整 Ext.DatePicker 控件,支持日期范围控件------------------- // 设置第2个日期值时,不清空第1个值标记 Ext.DatePicker.prototype.setTwoValue = function(value){ this.two_value = value.clearTime(true); this.update(this.two_value, null, true); }; Ext.DatePicker.prototype.setValue = function(value){ this.two_value = null;//清除第1个值 this.value = value.clearTime(true); this.update(this.value); }; Ext.DatePicker.prototype.update = function(date, forceRefresh){ if(this.rendered){ var vd = this.activeDate, vis = this.isVisible(); this.activeDate = date; if(!forceRefresh && vd && this.el){ var t = date.getTime(); if(vd.getMonth() == date.getMonth() && vd.getFullYear() == date.getFullYear()){ if (!this.two_value) {//add by tony.tan this.cells.removeClass('x-date-selected'); }//--------------------------- this.cells.each(function(c){ if(c.dom.firstChild.dateValue == t){ c.addClass('x-date-selected'); if(vis && !this.cancelFocus){ Ext.fly(c.dom.firstChild).focus(50); } return false; } }, this); return; } } var days = date.getDaysInMonth(), firstOfMonth = date.getFirstDateOfMonth(), startingPos = firstOfMonth.getDay()-this.startDay; if(startingPos < 0){ startingPos += 7; } days += startingPos; var pm = date.add('mo', -1), prevStart = pm.getDaysInMonth()-startingPos, cells = this.cells.elements, textEls = this.textNodes, // convert everything to numbers so it's fast d = (new Date(pm.getFullYear(), pm.getMonth(), prevStart, this.initHour)), today = new Date().clearTime().getTime(), sel = date.clearTime(true).getTime(), min = this.minDate ? this.minDate.clearTime(true) : Number.NEGATIVE_INFINITY, max = this.maxDate ? this.maxDate.clearTime(true) : Number.POSITIVE_INFINITY, ddMatch = this.disabledDatesRE, ddText = this.disabledDatesText, ddays = this.disabledDays ? this.disabledDays.join('') : false, ddaysText = this.disabledDaysText, format = this.format; if(this.showToday){ var td = new Date().clearTime(), disable = (td < min || td > max || (ddMatch && format && ddMatch.test(td.dateFormat(format))) || (ddays && ddays.indexOf(td.getDay()) != -1)); if(!this.disabled){ this.todayBtn.setDisabled(disable); this.todayKeyListener[disable ? 'disable' : 'enable'](); } } var setCellClass = function(cal, cell){ cell.title = ''; var t = d.clearTime(true).getTime(); cell.firstChild.dateValue = t; if(t == today){ cell.className += ' x-date-today'; cell.title = cal.todayText; } if(t == sel){ cell.className += ' x-date-selected'; if(vis){ Ext.fly(cell.firstChild).focus(50); } } // disabling if(t < min) { cell.className = ' x-date-disabled'; cell.title = cal.minText; return; } if(t > max) { cell.className = ' x-date-disabled'; cell.title = cal.maxText; return; } if(ddays){ if(ddays.indexOf(d.getDay()) != -1){ cell.title = ddaysText; cell.className = ' x-date-disabled'; } } if(ddMatch && format){ var fvalue = d.dateFormat(format); if(ddMatch.test(fvalue)){ cell.title = ddText.replace('%0', fvalue); cell.className = ' x-date-disabled'; } } }; var i = 0; for(; i < startingPos; i++) { textEls[i].innerHTML = (++prevStart); d.setDate(d.getDate()+1); cells[i].className = 'x-date-prevday'; setCellClass(this, cells[i]); } for(; i < days; i++){ var intDay = i - startingPos + 1; textEls[i].innerHTML = (intDay); d.setDate(d.getDate()+1); cells[i].className = 'x-date-active'; setCellClass(this, cells[i]); } var extraDays = 0; for(; i < 42; i++) { textEls[i].innerHTML = (++extraDays); d.setDate(d.getDate()+1); cells[i].className = 'x-date-nextday'; setCellClass(this, cells[i]); } this.mbtn.setText(this.monthNames[date.getMonth()] + ' ' + date.getFullYear()); if(!this.internalRender){ var main = this.el.dom.firstChild, w = main.offsetWidth; this.el.setWidth(w + this.el.getBorderWidth('lr')); Ext.fly(main).setWidth(w); this.internalRender = true; // opera does not respect the auto grow header center column // then, after it gets a width opera refuses to recalculate // without a second pass if(Ext.isOpera && !this.secondPass){ main.rows[0].cells[1].style.width = (w - (main.rows[0].cells[0].offsetWidth+main.rows[0].cells[2].offsetWidth)) + 'px'; this.secondPass = true; this.update.defer(10, this, [date]); } } } };