zhuguifei
2026-03-10 58402bd5e762361363a0f7d7907153c77dbb819f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
    @name Edo.navigators.ToggleBar
    @class
    @typeName togglebar
    @description selectedIndex,overClickable(true时,当鼠标移动到某一按钮上,此按钮被激发fire click事件)
    @extend Edo.navigators.Navigator
*/
Edo.navigators.ToggleBar = function(config){
    
    /**
        @name Edo.navigators.ToggleBar#beforeselectionchange
        @event
        @description 选中状态改变前事件
        @property {Number} selectedIndex 新选中索引        
    */      
    
    /**
        @name Edo.navigators.ToggleBar#selectionchange
        @event
        @description 选中状态改变事件
        @property {Number} selectedIndex 当前选中索引
    */        
            
    Edo.navigators.ToggleBar.superclass.constructor.call(this);         
};
Edo.navigators.ToggleBar.extend(Edo.navigators.Navigator,{
//    set: function(o, value){    
//        if(!o) return;        
//        if(typeof o === 'string') {
//            var _ = o;
//            o = {};
//            o[_] = value;
//        }
//        var selectedIndex = o.selectedIndex;        
//        delete o.selectedIndex;
//        
//        Edo.navigators.ToggleBar.superclass.set.c(this, o);
//        
//        if(Edo.isValue(selectedIndex)){             
//            this._setSelectedIndex(selectedIndex);
//        }
//        
//    },        
    /**
        @name Edo.navigators.ToggleBar#selectedIndex
        @property
        @type Number
        @description 选中的index
    */ 
    selectedIndex: -1,
    /**
        @name Edo.navigators.ToggleBar#selectedItem
        @property
        @type Object
        @description 选中的对象
    */    
    selectedItem: null,  
    
    selectedCls: 'e-togglebar-selected',  
    
    //selectedSet: false,
    
//    doLayout: function(t, box){        
//        Edo.navigators.ToggleBar.superclass.doLayout.call(this, t, box);
//        if(!this.selectedSet && Edo.isValue(this.selectedIndex)){        
//            var i = this.selectedIndex;
//            this.selectedIndex = null;
//            this.setSelectedIndex(i);
//            this.selectedSet = true;
//        }
//    },
    addChildAt: function(index, c){        
        
//        if(c.cls ) c.cls += ' '+this.itemCls;
//        else c.cls = this.itemCls;
        
        var c = Edo.navigators.ToggleBar.superclass.addChildAt.apply(this, arguments);
        c.on('click', this._onChildrenClick, this);        
        return c;
    },
    removeChildAt: function(index){
        var c = Edo.navigators.ToggleBar.superclass.removeChildAt.apply(this, arguments);
        
        if(index == this.selectedIndex){
            this._setSelectedIndex(-1);
        }
               
        return c;
    },
        
    _setSelectedItem: function(value){
        var index = this.children.indexOf(value);
        if(index != -1 && value != this.selectedItem){
            this.selectedIndex = -1;
        }
        this._setSelectedIndex(index);
    },
    _setSelectedIndex: function(value){
        
        if(!this.rendered){
            this.selectedIndex = value;
            return;
        }
        
        if(value < 0 || value >= this.children.length || this.selectedIndex === value) return;        
        if(this.fireEvent('beforeselectionchange', {
            type: 'beforeselectionchange',
            source: this,
            selectedIndex: value
        }) !== false){
            this.selectedIndex = value;        
            this.children.each(function(o,i){
                if(i==value) {
                    o._setPressed(true);
                    o.addCls(this.selectedCls);
                }
                else {
                    o._setPressed(false);
                    o.removeCls(this.selectedCls);
                }
            },this);
            
            this.selectedItem = this.getDisplayChildren()[value];
            this.fireEvent('selectionchange', {
                type: 'selectionchange',
                source: this,
                index: this.selectedIndex,
                selectedIndex: this.selectedIndex
            });
            
            this.changeProperty('selectedIndex', value);
            this.el.style.visible = 'hidden';
            //this.el.style.display = 'none';
            this.relayout('selectedIndex');
        }
    },
    _onChildrenClick: function(e){    
        var index = this.children.indexOf(e.source);       
        if(index != this.selectedIndex){
            this._setSelectedIndex(index);
        }
    },
    render: function(){
        Edo.navigators.ToggleBar.superclass.render.apply(this, arguments);
        var index = this.selectedIndex;
        delete this.selectedIndex;
        this._setSelectedIndex(index);
    }
});
 
Edo.navigators.ToggleBar.regType('togglebar');