`
myyugioh
  • 浏览: 20925 次
社区版块
存档分类
最新评论

【Ext2.0】只有月份的日期控件

    博客分类:
  • EXT
阅读更多

下载见附件

 

使用注意事项:

1.  MonthField里的select事件在2.0里无法正常监听,2.2版本有效。

2.  3.1版本中无法使用该控件 

 

/**
 * A picker that allows you to select a month and year.
 *
 * This component has been originally started by Joseph Kralicky and
 * further enhanced by cariad. As of version 0.2 I added support for
 * a minimum and maximum date (allowed date range) and noPastMonths.
 *
 * @class     Ext.ux.MonthPicker
 * @extends   Ext.Component
 *
 * @author    Philipp Rosenhagen
 * @date      2008-08-28
 * @version   0.2
 * @link      http://extjs.com/forum/showthread.php?t=20181
 *
 * @license Ext.ux.grid.Search is licensed under the terms of
 * the Open Source LGPL 3.0 license.  Commercial use is permitted to the extent
 * that the code/component(s) do NOT become part of another Open Source or Commercially
 * licensed development library or toolkit without explicit permission.
 *
 * License details: http://www.gnu.org/licenses/lgpl.html
 *
 * This extension has been tested and developed with IE6, FF2, FF3 using Ext 2.0.2.
 * The license is LGPL 3.0 - at least for the Ext 2.0.x branch. Feel free to try it
 * with the latest Ext but don't ask me under which license you can use it...
 */

Ext.namespace('Ext.ux');

/**
 * @class     Ext.ux.MonthPicker
 * @extends   Ext.Component
 * @param     {Object} config configuration object
 * @constructor
 */
Ext.ux.MonthPicker = function(config) {
    Ext.apply(this, config);
    Ext.ux.MonthPicker.superclass.constructor.call(this);
};

Ext.ux.MonthPicker = Ext.extend(Ext.Component, {
    format : "M, Y",
    okText : Ext.MessageBox.buttonText.ok,
    cancelText : Ext.MessageBox.buttonText.cancel,
    constrainToViewport : true,
    monthNames : Date.monthNames,
    startDay : 0,
    minDate: null,
    maxDate: null,
    value : 0,
    noPastYears: false, // only use the current year and future years
    noPastMonths: false, // only use the current month and future months
    useDayDate : 1, // set to a number between 1-31 to use this day when creating the resulting date object (or null to use todays date or keep existing)
    initComponent: function(){
        Ext.ux.MonthPicker.superclass.initComponent.call(this);
        this.value = this.value ? this.value.clearTime() : new Date().clearTime();
        this.activeDate = this.value;
        this.addEvents(
            'select'
        );

        if(this.handler){
            this.on("select", this.handler,  this.scope || this);
        }
    },

    focus : function(){
        if(this.el){
            this.update(this.activeDate);
        }
    },

    onRender : function(container, position){
        var m = [ '<div style="width: 200px; height:190px;"></div>' ]
        m[m.length] = '<div class="x-date-mp"></div>';
        var el = document.createElement("div");
        el.className = "x-date-picker";
        el.innerHTML = m.join("");
        container.dom.insertBefore(el, position);
        this.el = Ext.get(el);
        this.monthPicker = this.el.down('div.x-date-mp');
        this.monthPicker.enableDisplayMode('block');
        this.el.unselectable();
        this.showMonthPicker();
        if(Ext.isIE){
            this.el.repaint();
        }
        this.update(this.value);
    },

    createMonthPicker : function(){
        var minMonth = -1;
        if (this.noPastMonths) {
            minMonth = new Date().getMonth();
        }
        if(!this.monthPicker.dom.firstChild){
            var buf = ['<table border="0" cellspacing="0">'];
            for(var i = 0; i < 6; i++){
                buf.push(
                    '<tr><td class="x-date-mp-month'+(i < minMonth ? ' x-date-disabled' : '')+'"><a href="#">', this.monthNames[i].substr(0, 3), '</a></td>',
                    '<td class="x-date-mp-month x-date-mp-sep"><a href="#">', this.monthNames[i+6].substr(0, 3), '</a></td>',
                    i == 0 ?
                    '<td class="x-date-mp-ybtn" align="center"><a class="x-date-mp-prev"></a></td><td class="x-date-mp-ybtn" align="center"><a class="x-date-mp-next"></a></td></tr>' :
                    '<td class="x-date-mp-year"><a href="#"></a></td><td class="x-date-mp-year"><a href="#"></a></td></tr>'
                );
            }
            buf.push(
                '<tr class="x-date-mp-btns"><td colspan="4"><button type="button" class="x-date-mp-ok">',
                    this.okText,
                    '</button><button type="button" class="x-date-mp-cancel">',
                    this.cancelText,
                    '</button></td></tr>',
                '</table>'
            );
            this.monthPicker.update(buf.join(''));
            this.monthPicker.on('click', this.onMonthClick, this);
            this.monthPicker.on('dblclick', this.onMonthDblClick, this);

            this.mpMonths = this.monthPicker.select('td.x-date-mp-month');
            this.mpYears = this.monthPicker.select('td.x-date-mp-year');

            this.mpMonths.each(function(m, a, i){
                i += 1;
                if((i%2) == 0){
                    m.dom.xmonth = 5 + Math.round(i * .5);
                }else{
                    m.dom.xmonth = Math.round((i-1) * .5);
                }
            });
        }
    },

    showMonthPicker : function(){
        this.createMonthPicker();
        var size = this.el.getSize();
        this.monthPicker.setSize(size);
        this.monthPicker.child('table').setSize(size);

        this.mpSelMonth = (this.activeDate || this.value).getMonth();
        this.updateMPMonth(this.mpSelMonth);
        this.mpSelYear = (this.activeDate || this.value).getFullYear();
        this.updateMPYear(this.mpSelYear);

        this.monthPicker.show();
        //this.monthPicker.slideIn('t', {duration:.2});
    },

    updateMonthPicker: function() {
        if ((this.activeDate && !isNaN(this.activeDate.getElapsed())) || (this.value && !isNaN(this.value.getElapsed))) {
            this.mpSelMonth = (this.activeDate || this.value || new Date()).getMonth();
            this.updateMPMonth(this.mpSelMonth);
            this.mpSelYear = (this.activeDate || this.value || new Date()).getFullYear();
            this.updateMPYear(this.mpSelYear);
        }
    },

    updateMPYear : function(y){
        if ( this.noPastYears ) {
            var minYear = new Date().getFullYear();
            if ( y < (minYear+4) ) {
                y = minYear+4;
            }
        }

        this.mpyear = y;
        var ys = this.mpYears.elements;
        for(var i = 1; i <= 10; i++){
            var td = ys[i-1], y2;
            if((i%2) == 0){
                y2 = y + Math.round(i * .5);
                td.firstChild.innerHTML = y2;
                td.xyear = y2;
            }else{
                y2 = y - (5-Math.round(i * .5));
                td.firstChild.innerHTML = y2;
                td.xyear = y2;
            }

            /*
             * Add disabled class if out of allowed range.
             */
            var yearDate = new Date(Date.parse(y2+'/1/1'));
            if (this.minDate && this.maxDate) {
                if (!yearDate.between(new Date(Date.parse(this.minDate.getFullYear()+'/1/1')) || yearDate, new Date(Date.parse(this.maxDate.getFullYear()+'/1/1')) || yearDate)) {
                    Ext.get(td).addClass('x-date-disabled');
                } else {
                    Ext.get(td).removeClass('x-date-disabled');
                }
            }

            this.mpYears.item(i-1)[y2 == this.mpSelYear ? 'addClass' : 'removeClass']('x-date-mp-sel');
        }

        /*
         * We have to make sure, that the user can only select the months which lay within the range!
         */
        if (this.minDate && this.maxDate) {
            this.mpMonths.each( function(m, a, i){
                i += 1;
                if (this.mpSelYear == this.maxDate.getFullYear()) {
                    if (m.dom.xmonth > this.maxDate.getMonth()) {
                        m.addClass('x-date-disabled')
                    } else {
                        m.removeClass('x-date-disabled');
                    }
                } else if (this.mpSelYear == this.minDate.getFullYear()) {
                    if (m.dom.xmonth < this.minDate.getMonth()) {
                        m.addClass('x-date-disabled')
                    } else {
                        m.removeClass('x-date-disabled');
                    }
                } else {
                    m.removeClass('x-date-disabled');
                }
            }, this);
        }
    },

    updateMPMonth : function(sm){
        this.mpMonths.each(function(m, a, i){
            m[m.dom.xmonth == sm ? 'addClass' : 'removeClass']('x-date-mp-sel');
        });
    },

    selectMPMonth: function(m){

    },

    getAdjustedDate : function (year,month){
        return new Date(
            year,
            month,
            this.useDayDate ? // use a specific day date?
            (Math.min(this.useDayDate, (new Date(year, month, 1)).getDaysInMonth())) // yes, cap it to month max
            :
            (this.activeDate || this.value).getDate() // keep existing
        );
    },

    onMonthClick : function(e, t){
        e.stopEvent();
        if (!Ext.fly(t.parentNode).hasClass("x-date-disabled")) {
            var el = new Ext.Element(t), pn;

            // now following the other handling (from original implementation)
            if(el.is('button.x-date-mp-cancel')){
                this.hideMonthPicker();
            }
            else if(el.is('button.x-date-mp-ok')){
                this.update(this.getAdjustedDate(this.mpSelYear, this.mpSelMonth));
                this.fireEvent("select", this, this.value, this.oldValue);
            }
            else if(pn = el.up('td.x-date-mp-month', 2)){
                this.mpMonths.removeClass('x-date-mp-sel');
                pn.addClass('x-date-mp-sel');
                this.mpSelMonth = pn.dom.xmonth;
            }
            else if(pn = el.up('td.x-date-mp-year', 2)){
                this.mpYears.removeClass('x-date-mp-sel');
                pn.addClass('x-date-mp-sel');
                this.mpSelYear = pn.dom.xyear;

                /*
                 * We have to make sure, that no invalid (out of allowed range) month is selected
                 * when switching to a different year. It might be possible that currently the
                 * selected month is valid (meaning lays within the allowed range) but switching
                 * to a different year now invalidates the current month selection. So we deal with
                 * this by switching to the first possible (allowed) month when switching the years.
                 */
                if (this.minDate && this.maxDate) {
                    this.mpMonths.removeClass('x-date-mp-sel'); // bulk-remove from all
                    this.mpMonths.each( function(m, a, i) {
                        if (this.mpSelYear == this.maxDate.getFullYear()) {
                            if (m.dom.xmonth > this.maxDate.getMonth()) {

                            } else {
                                m.addClass('x-date-mp-sel');
                                this.mpSelMonth = m.dom.xmonth;
                                return false;
                            }
                        } else if (this.mpSelYear == this.minDate.getFullYear()) {
                            if (m.dom.xmonth < this.minDate.getMonth()) {

                            } else {
                                m.addClass('x-date-mp-sel');
                                this.mpSelMonth = m.dom.xmonth;
                                return false;
                            }
                        } else {
                            m.addClass('x-date-mp-sel');
                            this.mpSelMonth = m.dom.xmonth;
                            return false;
                        }
                    }, this);
                }
            }
            else if(el.is('a.x-date-mp-prev')){
                this.updateMPYear(this.mpyear-10);
            }
            else if(el.is('a.x-date-mp-next')){
                this.updateMPYear(this.mpyear+10);
            }

            /*
             * We have to make sure, that the user can only select the months which lay within the range!
             */
            if (this.minDate && this.maxDate) {
                this.mpMonths.each( function(m, a, i){
                    i += 1;
                    if (this.mpSelYear == this.maxDate.getFullYear()) {
                        if (m.dom.xmonth > this.maxDate.getMonth()) {
                            m.addClass('x-date-disabled')
                        } else {
                            m.removeClass('x-date-disabled');
                        }
                    } else if (this.mpSelYear == this.minDate.getFullYear()) {
                        if (m.dom.xmonth < this.minDate.getMonth()) {
                            m.addClass('x-date-disabled')
                        } else {
                            m.removeClass('x-date-disabled');
                        }
                    } else {
                        m.removeClass('x-date-disabled');
                    }
                }, this);
            }
        }
    },

    onMonthDblClick : function(e, t){
        e.stopEvent();
        if (!Ext.fly(t.parentNode).hasClass("x-date-disabled")) {
            var el = new Ext.Element(t), pn;
            if(pn = el.up('td.x-date-mp-month', 2)){
                this.update(this.getAdjustedDate(this.mpSelYear, pn.dom.xmonth));
                this.fireEvent("select", this, this.value, this.oldValue);
            }
            else if(pn = el.up('td.x-date-mp-year', 2)){
                this.update(this.getAdjustedDate(pn.dom.xyear, this.mpSelMonth));
                this.fireEvent("select", this, this.value, this.oldValue);
            }
        }
    },

    hideMonthPicker : function(disableAnim){
        Ext.menu.MenuMgr.hideAll();
    },


    showPrevMonth : function(e){
       this.update(this.activeDate.add("mo", -1));
    },


    showNextMonth : function(e){
        this.update(this.activeDate.add("mo", 1));
    },


    showPrevYear : function(){
        this.update(this.activeDate.add("y", -1));
    },


    showNextYear : function(){
        this.update(this.activeDate.add("y", 1));
    },

    update : function( date ) {
        this.activeDate = date;
        this.oldValue = this.value || date; // remember the old value
        this.value = date;

        if(!this.internalRender){
            var main = this.el.dom.firstChild;
            var w = main.offsetWidth;
            this.el.setWidth(w + this.el.getBorderWidth("lr"));
            Ext.fly(main).setWidth(w);
            this.internalRender = true;

            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]);
            }
        }
    },

    setValue : function( date ) {
        if (date == 'Invalid Date') {
            this.activeDate = null;
            this.value = null;
        } else {
            this.activeDate = date;
            this.value = date;
        }
    }

});

Ext.reg('ux.monthpicker', Ext.ux.MonthPicker);

Ext.ux.MonthItem = function(config){
    Ext.ux.MonthItem.superclass.constructor.call(this, new Ext.ux.MonthPicker(config), config);

    this.picker = this.component;
    this.addEvents('select');

    this.picker.on("render", function(picker){
        picker.getEl().swallowEvent("click");
        picker.container.addClass("x-menu-date-item");
    });

    this.picker.on("select", this.onSelect, this, this.picker.value, this.picker.oldValue);
};

Ext.extend(Ext.ux.MonthItem, Ext.menu.Adapter, {
    onSelect : function(picker, date, value, oldValue){
        this.fireEvent("select", this, date, picker, value, oldValue);
        Ext.ux.MonthItem.superclass.handleClick.call(this);
    }
});

Ext.reg('ux.monthitem', Ext.ux.MonthItem);

Ext.ux.MonthMenu = function(config){
    Ext.ux.MonthMenu.superclass.constructor.call(this, config);
    this.plain = true;
    var mi = new Ext.ux.MonthItem(config);
    this.add(mi);

    this.picker = mi.picker;

    this.relayEvents(mi, ["select"]);
};

Ext.extend(Ext.ux.MonthMenu, Ext.menu.Menu, {
    cls:'x-date-menu',

    /**
     * (Pre-)Set the date.
     */
    setDate: function(d) {
        this.picker.setValue(d);
    },

    setMinDate: function(d) {
        this.picker.minDate = d;
    },

    setMaxDate: function(d) {
        this.picker.maxDate = d;
    }

});

Ext.reg('ux.monthmenu', Ext.ux.MonthMenu);

Ext.ux.MonthField = function(config){
    Ext.ux.MonthField.superclass.constructor.call(this, config);
}

Ext.extend(Ext.ux.MonthField, Ext.form.DateField, {
    format : Ext.ux.MonthPicker.prototype.format,
    noPastYears: Ext.ux.MonthPicker.prototype.noPastYears,
    noPastMonths: Ext.ux.MonthPicker.prototype.noPastMonths,
    useDayDate: Ext.ux.MonthPicker.prototype.useDayDate,
    onTriggerClick : function(){
        if(this.disabled){
            return;
        }
        if(this.menu == null){
            this.menu = new Ext.ux.MonthMenu();
        }
        Ext.apply(this.menu.picker, {
            format : this.format,
            noPastYears : this.noPastYears,
            noPastMonths : this.noPastMonths,
            useDayDate : this.useDayDate
        });
        this.menu.on(Ext.apply({}, this.menuListeners, {
            scope:this
        }));
        this.menu.picker.setValue(this.getValue() || new Date());
        this.menu.show(this.el, "tl-bl?");
    }
});
分享到:
评论

相关推荐

    最新的ext2.0下载

    1. **组件库增强**:EXT2.0扩展了原有的组件库,增加了如树形表格、tabpanel、日期选择器等更多实用的UI组件,使得开发者可以构建出更为复杂的Web应用界面。 2. **布局管理**:在布局方面,EXT2.0引入了更灵活的...

    ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记

    ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记ext2.0笔记...

    ext-2.0 ext-2.0 ext-2.0 ext-2.0 ext-2.0

    ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0ext-2.0

    ext2.0(jsp标签)

    EXT 2.0是Sencha公司推出的一个基于JavaScript的开源用户界面库,专门用于构建富互联网应用程序(RIA)。它提供了一套完整的组件化、可定制的UI控件,包括树形视图、菜单、弹出式菜单等,使得开发者能够轻松创建具有...

    ext2.0官方文档

    **EXT2.0官方文档详解** EXT2,全称Extended File System 2,是Linux操作系统中广泛使用的一种文件系统。这个文件系统在1993年由Rebecca Ann Heineman和Stephen Tweedie开发,作为EXT1的升级版,旨在解决EXT1在大...

    Ext2.0中文文档

    Ext 2.0是Sencha公司开发的一个JavaScript库,主要用于构建富互联网应用程序(RIA)。这个中文文档包含了关于Ext 2.0的详细说明,对于开发者来说是一个非常宝贵的资源,可以帮助他们理解和使用这个强大的前端框架。 ...

    Ext2.0 中文文档

    本文档主要针对EXT2.0版本进行介绍。 EXT2.0在EXT1.x的基础上进行了大量的改进和优化,增加了更多的UI组件,提升了性能,并且强化了对Ajax和数据绑定的支持。Ajax技术允许Web应用在不刷新整个页面的情况下与服务器...

    ext2.0官方文档(chm),Ext 2.0 简明教程,Ext2经典应用

    3. **EXT2.0中文教程.exe**: 这可能是一个以可执行文件(.exe)形式的中文教学程序,对于中文用户来说非常友好。它可能包含了视频教程、交互式示例或者图文并茂的讲解,帮助学习者更直观地理解Ext 2.0的用法和功能。...

    EXT2.0在本地浏览API的插件

    EXT2.0是一款基于JavaScript的富客户端开发框架,它提供了丰富的组件库和强大的数据管理功能,使得在浏览器端构建复杂的用户界面变得简单。本插件是EXT2.0针对本地浏览API的扩展,旨在增强EXT应用程序对本地资源的...

    Ext2.0组件,全组件和帮助手册

    Ext2.0是Ext JS库的一个早期版本,它是一个用于构建富互联网应用程序(RIA)的JavaScript框架。这个框架提供了一套完整的UI组件和强大的数据管理功能,使得开发者能够创建交互性强、功能丰富的Web应用。全组件和帮助...

    EXT2.0 GRID 示例

    EXT2.0 GRID 示例是一个基于EXT JavaScript库的高级数据展示和操作组件的实例。EXT是一个强大的前端开发框架,尤其在创建交互式Web应用程序方面表现出色。EXT2.0是EXT库的一个早期版本,它提供了丰富的用户界面组件...

    EXT2.0最新压缩包

    EXT2.0是一种广泛应用于Linux操作系统中的文件系统,它的全称是Extended File System Version 2.0。在Linux世界中,文件系统是管理和组织磁盘数据的核心组件,它决定了文件如何存储、检索以及如何实现高效的读写操作...

    ext 2.0

    4. **表单组件**:Ext 2.0提供了各种表单控件,如文本框、下拉框、复选框、单选按钮等,以及强大的表单布局和验证功能。表单组件与数据模型紧密集成,方便数据的双向绑定。 5. **布局管理**:Ext 2.0的布局系统允许...

    ext2.0API

    EXT2.0 API详解 EXT2(Second Extended File System)是Linux操作系统中广泛使用的文件系统之一,它在1992年由Rene Rebeau设计并实现,为Linux社区提供了可靠且高效的文件存储解决方案。随着技术的发展,EXT2也经历...

    ext2.0项目源代码供大家学习ext使用

    EXT 2.0是EXT库的一个早期版本,它包含了一系列用于构建用户界面的组件和工具,如表格、面板、菜单、窗口等。EXT 2.0项目源代码的分享为开发者提供了一个深入了解EXT框架内部工作原理和最佳实践的机会。 在EXT 2.0...

    ext 2.0 源码 下载

    6. **表单处理**:EXT 2.0提供了丰富的表单控件和表单处理功能,支持验证、远程提交等。 7. **树形结构**:EXT 2.0的TreePanel组件可以展示和操作层次数据,常用于文件系统或组织结构的展示。 8. **图表组件**:...

    EXT2.0中文API

    1. **组件系统**:EXT2.0拥有强大的组件体系,包括窗口(Window)、面板(Panel)、表格(Grid)、表单(Form)、树形控件(Tree)等。每个组件都有其特定的属性、配置选项、事件和方法,这些在API文档中都有详细的...

    EXT2.0 API文档

    EXT2.0 API文档是针对EXT JavaScript库的一个关键参考资料,主要涵盖了EXT库的版本2.0中的各种组件、方法和功能。EXT是一个强大的前端开发框架,尤其在构建数据驱动的Web应用程序方面表现突出。EXT2.0 API文档是...

    EXT2.0学习资料

    EXT2.0是Ext JS框架的一个早期版本,它在Web应用程序开发中被广泛使用,尤其在构建富客户端界面时。这个框架提供了丰富的组件库,帮助开发者创建交互性强、功能丰富的用户界面。EXT2.0的学习资料包括JAR包和相关的...

Global site tag (gtag.js) - Google Analytics