DataViewModel.js
4.23 KB
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
152
153
154
155
156
157
158
159
160
161
162
/**
* @private
*/
Ext.define('Ext.selection.DataViewModel', {
extend: 'Ext.selection.Model',
requires: ['Ext.util.KeyNav'],
deselectOnContainerClick: true,
/**
* @cfg {Boolean} enableKeyNav
*
* Turns on/off keyboard navigation within the DataView.
*/
enableKeyNav: true,
constructor: function(cfg){
this.addEvents(
/**
* @event beforedeselect
* Fired before a record is deselected. If any listener returns false, the
* deselection is cancelled.
* @param {Ext.selection.DataViewModel} this
* @param {Ext.data.Model} record The deselected record
*/
'beforedeselect',
/**
* @event beforeselect
* Fired before a record is selected. If any listener returns false, the
* selection is cancelled.
* @param {Ext.selection.DataViewModel} this
* @param {Ext.data.Model} record The selected record
*/
'beforeselect',
/**
* @event deselect
* Fired after a record is deselected
* @param {Ext.selection.DataViewModel} this
* @param {Ext.data.Model} record The deselected record
*/
'deselect',
/**
* @event select
* Fired after a record is selected
* @param {Ext.selection.DataViewModel} this
* @param {Ext.data.Model} record The selected record
*/
'select'
);
this.callParent(arguments);
},
bindComponent: function(view) {
var me = this,
eventListeners = {
refresh: me.refresh,
scope: me
};
me.view = view;
me.bindStore(view.getStore());
eventListeners[view.triggerEvent] = me.onItemClick;
eventListeners[view.triggerCtEvent] = me.onContainerClick;
view.on(eventListeners);
if (me.enableKeyNav) {
me.initKeyNav(view);
}
},
onItemClick: function(view, record, item, index, e) {
this.selectWithEvent(record, e);
},
onContainerClick: function() {
if (this.deselectOnContainerClick) {
this.deselectAll();
}
},
initKeyNav: function(view) {
var me = this;
if (!view.rendered) {
view.on({
render: Ext.Function.bind(me.initKeyNav, me, [view]),
single: true
});
return;
}
view.el.set({
tabIndex: -1
});
me.keyNav = new Ext.util.KeyNav({
target: view.el,
ignoreInputFields: true,
down: Ext.pass(me.onNavKey, [1], me),
right: Ext.pass(me.onNavKey, [1], me),
left: Ext.pass(me.onNavKey, [-1], me),
up: Ext.pass(me.onNavKey, [-1], me),
scope: me
});
},
onNavKey: function(step) {
step = step || 1;
var me = this,
view = me.view,
selected = me.getSelection()[0],
numRecords = me.view.store.getCount(),
idx;
if (selected) {
idx = view.indexOf(view.getNode(selected)) + step;
} else {
idx = 0;
}
if (idx < 0) {
idx = numRecords - 1;
} else if (idx >= numRecords) {
idx = 0;
}
me.select(idx);
},
// Allow the DataView to update the ui
onSelectChange: function(record, isSelected, suppressEvent, commitFn) {
var me = this,
view = me.view,
eventName = isSelected ? 'select' : 'deselect';
if ((suppressEvent || me.fireEvent('before' + eventName, me, record)) !== false &&
commitFn() !== false) {
if (view) {
if (isSelected) {
view.onItemSelect(record);
} else {
view.onItemDeselect(record);
}
}
if (!suppressEvent) {
me.fireEvent(eventName, me, record);
}
}
},
destroy: function(){
Ext.destroy(this.keyNav);
this.callParent();
}
});