﻿// 重载 Ext.data.Store.prototype.applySort 函数以修复 DataStore 对汉字排序异常的问题    
// var _applySort = Ext.data.Store.prototype.applySort;              
//如有需要，保存原 applySort 函数的引用    
//重载 applySort
Ext.data.Store.prototype.applySort = function(){ 
        if(this.sortInfo && !this.remoteSort){  
                var s = this.sortInfo, f = s.field; 
                var st = this.fields.get(f).sortType; 
                
                var fn = function(r1, r2){   
                        var v1 = st(r1.data[f]), v2 = st(r2.data[f]);                         
                        // 添加:修复汉字排序异常的Bug    
                        if(typeof(v1) == "string"){ //若为字符串，   
                                v1=v1.toLowerCase();
                                v2=v2.toLowerCase(); 
                                return v1.localeCompare(v2);//则用 localeCompare 比较汉字字符串, Firefox 与 IE 均支持 
                            } 
                        // 添加结束  
                        return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0); 
                     };    

                    this.data.sort(s.direction, fn);
                    if(this.snapshot && this.snapshot != this.data){ 
                            this.snapshot.sort(s.direction, fn);   
                     }
                }  
 }; 


Ext.override(Ext.PagingToolbar ,{
	onRender : function(ct, position){
        Ext.PagingToolbar.superclass.onRender.call(this, ct, position);
        this.first = this.addButton({
            tooltip: this.firstText,
            iconCls: "x-tbar-page-first",
            disabled: true,
            handler: this.onClick.createDelegate(this, ["first"])
        });
        this.prev = this.addButton({
            tooltip: this.prevText,
            iconCls: "x-tbar-page-prev",
            disabled: true,
            handler: this.onClick.createDelegate(this, ["prev"])
        });
        this.addSeparator();
        this.add(this.beforePageText);
        this.field = Ext.get(this.addDom({
           tag: "input",
           type: "text",
           size: "3",
           value: "1",
           cls: "x-tbar-page-number"
        }).el);
        this.field.on("keydown", this.onPagingKeydown, this);
        this.field.on("focus", function(){this.dom.select();});
        this.afterTextEl = this.addText(String.format(this.afterPageText, 1));
        this.field.setHeight(18);
        this.addSeparator();
        this.next = this.addButton({
            tooltip: this.nextText,
            iconCls: "x-tbar-page-next",
            disabled: true,
            handler: this.onClick.createDelegate(this, ["next"])
        });
        this.last = this.addButton({
            tooltip: this.lastText,
            iconCls: "x-tbar-page-last",
            disabled: true,
            handler: this.onClick.createDelegate(this, ["last"])
        });
        this.addSeparator();
        this.loading = this.addButton({
            //tooltip: this.refreshText
            //iconCls: "x-tbar-loading",
            //handler: this.onClick.createDelegate(this, ["refresh"])
        });

        if(this.displayInfo){
            this.displayEl = Ext.fly(this.el.dom).createChild({cls:'x-paging-info'});
        }
        if(this.dsLoaded){
            this.onLoad.apply(this, this.dsLoaded);
        }
    }
	
});


Ext.override(Ext.Button ,{
	    onRender : function(ct, position){
        if(!this.template){
            if(!Ext.Button.buttonTemplate){
                // hideous table template
                Ext.Button.buttonTemplate = new Ext.Template(
                    '<table border="0" cellpadding="0" cellspacing="0" class="x-btn-wrap"><tbody><tr>',
                    '<td class="x-btn-left"><i>&#160;</i></td><td class="x-btn-center"><em unselectable="on"><button class="x-btn-text" type="{1}">{0}</button></em></td><td class="x-btn-right"><i>&#160;</i></td>',
                    "</tr></tbody></table>");
            }
            this.template = Ext.Button.buttonTemplate;
        }
        var btn, targs = [this.text || '&#160;', this.type];

        if(position){
            btn = this.template.insertBefore(position, targs, true);
        }else{
            btn = this.template.append(ct, targs, true);
        }
        var btnEl = btn.child(this.buttonSelector);
        btnEl.on('focus', this.onFocus, this);
        btnEl.on('blur', this.onBlur, this);

        this.initButtonEl(btn, btnEl);

        if(this.menu){
            this.el.child(this.menuClassTarget).addClass("x-btn-with-menu");
        }
        Ext.ButtonToggleMgr.register(this);
    }
});


