3 * @extends Ext.view.AbstractView
5 * A mechanism for displaying data using custom layout templates and formatting. DataView uses an {@link Ext.XTemplate}
6 * as its internal templating mechanism, and is bound to an {@link Ext.data.Store}
7 * so that as the data in the store changes the view is automatically updated to reflect the changes. The view also
8 * provides built-in behavior for many common events that can occur for its contained items including click, doubleclick,
9 * mouseover, mouseout, etc. as well as a built-in selection model. <b>In order to use these features, an {@link #itemSelector}
10 * config must be provided for the DataView to determine what nodes it will be working with.</b>
12 * <p>The example below binds a DataView to a {@link Ext.data.Store} and renders it into an {@link Ext.panel.Panel}.</p>
13 * {@img Ext.DataView/Ext.DataView.png Ext.DataView component}
15 Ext.regModel('Image', {
17 {name:'src', type:'string'},
18 {name:'caption', type:'string'}
22 Ext.create('Ext.data.Store', {
26 {src:'http://www.sencha.com/img/20110215-feat-drawing.png', caption:'Drawing & Charts'},
27 {src:'http://www.sencha.com/img/20110215-feat-data.png', caption:'Advanced Data'},
28 {src:'http://www.sencha.com/img/20110215-feat-html5.png', caption:'Overhauled Theme'},
29 {src:'http://www.sencha.com/img/20110215-feat-perf.png', caption:'Performance Tuned'}
33 var imageTpl = new Ext.XTemplate(
35 '<div style="thumb-wrap">',
36 '<img src="{src}" />',
37 '<br/><span>{caption}</span>',
42 Ext.create('Ext.DataView', {
43 store: Ext.data.StoreManager.lookup('imagesStore'),
45 itemSelector: 'div.thumb-wrap',
46 emptyText: 'No images available',
47 renderTo: Ext.getBody()
52 Ext.define('Ext.view.View', {
53 extend: 'Ext.view.AbstractView',
54 alternateClassName: 'Ext.view.View',
55 alias: 'widget.dataview',
59 mousedown: 'MouseDown',
63 contextmenu: 'ContextMenu',
64 mouseover: 'MouseOver',
66 mouseenter: 'MouseEnter',
67 mouseleave: 'MouseLeave',
72 addCmpEvents: function() {
75 * @event beforeitemmousedown
76 * Fires before the mousedown event on an item is processed. Returns false to cancel the default action.
77 * @param {Ext.view.View} this
78 * @param {Ext.data.Model} record The record that belongs to the item
79 * @param {HTMLElement} item The item's element
80 * @param {Number} index The item's index
81 * @param {Ext.EventObject} e The raw event object
83 'beforeitemmousedown',
85 * @event beforeitemmouseup
86 * Fires before the mouseup event on an item is processed. Returns false to cancel the default action.
87 * @param {Ext.view.View} this
88 * @param {Ext.data.Model} record The record that belongs to the item
89 * @param {HTMLElement} item The item's element
90 * @param {Number} index The item's index
91 * @param {Ext.EventObject} e The raw event object
95 * @event beforeitemmouseenter
96 * Fires before the mouseenter event on an item is processed. Returns false to cancel the default action.
97 * @param {Ext.view.View} this
98 * @param {Ext.data.Model} record The record that belongs to the item
99 * @param {HTMLElement} item The item's element
100 * @param {Number} index The item's index
101 * @param {Ext.EventObject} e The raw event object
103 'beforeitemmouseenter',
105 * @event beforeitemmouseleave
106 * Fires before the mouseleave event on an item is processed. Returns false to cancel the default action.
107 * @param {Ext.view.View} this
108 * @param {Ext.data.Model} record The record that belongs to the item
109 * @param {HTMLElement} item The item's element
110 * @param {Number} index The item's index
111 * @param {Ext.EventObject} e The raw event object
113 'beforeitemmouseleave',
115 * @event beforeitemclick
116 * Fires before the click event on an item is processed. Returns false to cancel the default action.
117 * @param {Ext.view.View} this
118 * @param {Ext.data.Model} record The record that belongs to the item
119 * @param {HTMLElement} item The item's element
120 * @param {Number} index The item's index
121 * @param {Ext.EventObject} e The raw event object
125 * @event beforeitemdblclick
126 * Fires before the dblclick event on an item is processed. Returns false to cancel the default action.
127 * @param {Ext.view.View} this
128 * @param {Ext.data.Model} record The record that belongs to the item
129 * @param {HTMLElement} item The item's element
130 * @param {Number} index The item's index
131 * @param {Ext.EventObject} e The raw event object
133 'beforeitemdblclick',
135 * @event beforeitemcontextmenu
136 * Fires before the contextmenu event on an item is processed. Returns false to cancel the default action.
137 * @param {Ext.view.View} this
138 * @param {Ext.data.Model} record The record that belongs to the item
139 * @param {HTMLElement} item The item's element
140 * @param {Number} index The item's index
141 * @param {Ext.EventObject} e The raw event object
143 'beforeitemcontextmenu',
145 * @event beforeitemkeydown
146 * Fires before the keydown event on an item is processed. Returns false to cancel the default action.
147 * @param {Ext.view.View} this
148 * @param {Ext.data.Model} record The record that belongs to the item
149 * @param {HTMLElement} item The item's element
150 * @param {Number} index The item's index
151 * @param {Ext.EventObject} e The raw event object. Use {@link Ext.EventObject#getKey getKey()} to retrieve the key that was pressed.
155 * @event itemmousedown
156 * Fires when there is a mouse down on an item
157 * @param {Ext.view.View} this
158 * @param {Ext.data.Model} record The record that belongs to the item
159 * @param {HTMLElement} item The item's element
160 * @param {Number} index The item's index
161 * @param {Ext.EventObject} e The raw event object
166 * Fires when there is a mouse up on an item
167 * @param {Ext.view.View} this
168 * @param {Ext.data.Model} record The record that belongs to the item
169 * @param {HTMLElement} item The item's element
170 * @param {Number} index The item's index
171 * @param {Ext.EventObject} e The raw event object
175 * @event itemmouseenter
176 * Fires when the mouse enters an item.
177 * @param {Ext.view.View} this
178 * @param {Ext.data.Model} record The record that belongs to the item
179 * @param {HTMLElement} item The item's element
180 * @param {Number} index The item's index
181 * @param {Ext.EventObject} e The raw event object
185 * @event itemmouseleave
186 * Fires when the mouse leaves an item.
187 * @param {Ext.view.View} this
188 * @param {Ext.data.Model} record The record that belongs to the item
189 * @param {HTMLElement} item The item's element
190 * @param {Number} index The item's index
191 * @param {Ext.EventObject} e The raw event object
196 * Fires when an item is clicked.
197 * @param {Ext.view.View} this
198 * @param {Ext.data.Model} record The record that belongs to the item
199 * @param {HTMLElement} item The item's element
200 * @param {Number} index The item's index
201 * @param {Ext.EventObject} e The raw event object
205 * @event itemdblclick
206 * Fires when an item is double clicked.
207 * @param {Ext.view.View} this
208 * @param {Ext.data.Model} record The record that belongs to the item
209 * @param {HTMLElement} item The item's element
210 * @param {Number} index The item's index
211 * @param {Ext.EventObject} e The raw event object
215 * @event itemcontextmenu
216 * Fires when an item is right clicked.
217 * @param {Ext.view.View} this
218 * @param {Ext.data.Model} record The record that belongs to the item
219 * @param {HTMLElement} item The item's element
220 * @param {Number} index The item's index
221 * @param {Ext.EventObject} e The raw event object
226 * Fires when a key is pressed while an item is currently selected.
227 * @param {Ext.view.View} this
228 * @param {Ext.data.Model} record The record that belongs to the item
229 * @param {HTMLElement} item The item's element
230 * @param {Number} index The item's index
231 * @param {Ext.EventObject} e The raw event object. Use {@link Ext.EventObject#getKey getKey()} to retrieve the key that was pressed.
235 * @event beforecontainermousedown
236 * Fires before the mousedown event on the container is processed. Returns false to cancel the default action.
237 * @param {Ext.view.View} this
238 * @param {Ext.EventObject} e The raw event object
240 'beforecontainermousedown',
242 * @event beforecontainermouseup
243 * Fires before the mouseup event on the container is processed. Returns false to cancel the default action.
244 * @param {Ext.view.View} this
245 * @param {Ext.EventObject} e The raw event object
247 'beforecontainermouseup',
249 * @event beforecontainermouseover
250 * Fires before the mouseover event on the container is processed. Returns false to cancel the default action.
251 * @param {Ext.view.View} this
252 * @param {Ext.EventObject} e The raw event object
254 'beforecontainermouseover',
256 * @event beforecontainermouseout
257 * Fires before the mouseout event on the container is processed. Returns false to cancel the default action.
258 * @param {Ext.view.View} this
259 * @param {Ext.EventObject} e The raw event object
261 'beforecontainermouseout',
263 * @event beforecontainerclick
264 * Fires before the click event on the container is processed. Returns false to cancel the default action.
265 * @param {Ext.view.View} this
266 * @param {Ext.EventObject} e The raw event object
268 'beforecontainerclick',
270 * @event beforecontainerdblclick
271 * Fires before the dblclick event on the container is processed. Returns false to cancel the default action.
272 * @param {Ext.view.View} this
273 * @param {Ext.EventObject} e The raw event object
275 'beforecontainerdblclick',
277 * @event beforecontainercontextmenu
278 * Fires before the contextmenu event on the container is processed. Returns false to cancel the default action.
279 * @param {Ext.view.View} this
280 * @param {Ext.EventObject} e The raw event object
282 'beforecontainercontextmenu',
284 * @event beforecontainerkeydown
285 * Fires before the keydown event on the container is processed. Returns false to cancel the default action.
286 * @param {Ext.view.View} this
287 * @param {Ext.EventObject} e The raw event object. Use {@link Ext.EventObject#getKey getKey()} to retrieve the key that was pressed.
289 'beforecontainerkeydown',
291 * @event containermouseup
292 * Fires when there is a mouse up on the container
293 * @param {Ext.view.View} this
294 * @param {Ext.EventObject} e The raw event object
298 * @event containermouseover
299 * Fires when you move the mouse over the container.
300 * @param {Ext.view.View} this
301 * @param {Ext.EventObject} e The raw event object
303 'containermouseover',
305 * @event containermouseout
306 * Fires when you move the mouse out of the container.
307 * @param {Ext.view.View} this
308 * @param {Ext.EventObject} e The raw event object
312 * @event containerclick
313 * Fires when the container is clicked.
314 * @param {Ext.view.View} this
315 * @param {Ext.EventObject} e The raw event object
319 * @event containerdblclick
320 * Fires when the container is double clicked.
321 * @param {Ext.view.View} this
322 * @param {Ext.EventObject} e The raw event object
326 * @event containercontextmenu
327 * Fires when the container is right clicked.
328 * @param {Ext.view.View} this
329 * @param {Ext.EventObject} e The raw event object
331 'containercontextmenu',
333 * @event containerkeydown
334 * Fires when a key is pressed while the container is focused, and no item is currently selected.
335 * @param {Ext.view.View} this
336 * @param {Ext.EventObject} e The raw event object. Use {@link Ext.EventObject#getKey getKey()} to retrieve the key that was pressed.
341 * @event selectionchange
342 * Fires when the selected nodes change. Relayed event from the underlying selection model.
343 * @param {Ext.view.View} this
344 * @param {Array} selections Array of the selected nodes
348 * @event beforeselect
349 * Fires before a selection is made. If any handlers return false, the selection is cancelled.
350 * @param {Ext.view.View} this
351 * @param {HTMLElement} node The node to be selected
352 * @param {Array} selections Array of currently selected nodes
358 afterRender: function(){
366 click: me.handleEvent,
367 mousedown: me.handleEvent,
368 mouseup: me.handleEvent,
369 dblclick: me.handleEvent,
370 contextmenu: me.handleEvent,
371 mouseover: me.handleEvent,
372 mouseout: me.handleEvent,
373 keydown: me.handleEvent
376 me.mon(me.getTargetEl(), listeners);
379 me.bindStore(me.store, true);
383 handleEvent: function(e) {
384 if (this.processUIEvent(e) !== false) {
385 this.processSpecialEvent(e);
389 // Private template method
390 processItemEvent: Ext.emptyFn,
391 processContainerEvent: Ext.emptyFn,
392 processSpecialEvent: Ext.emptyFn,
394 processUIEvent: function(e, type) {
395 type = type || e.type;
397 item = e.getTarget(me.getItemSelector(), me.getTargetEl()),
398 map = this.statics().EventMap,
402 // There is this weird bug when you hover over the border of a cell it is saying
403 // the target is the table.
404 // BrowserBug: IE6 & 7. If me.mouseOverItem has been removed and is no longer
405 // in the DOM then accessing .offsetParent will throw an "Unspecified error." exception.
406 // typeof'ng and checking to make sure the offsetParent is an object will NOT throw
407 // this hard exception.
408 if (type == 'mouseover' && me.mouseOverItem && typeof me.mouseOverItem.offsetParent === "object" && Ext.fly(me.mouseOverItem).getRegion().contains(e.getPoint())) {
409 item = me.mouseOverItem;
412 // Try to get the selected item to handle the keydown event, otherwise we'll just fire a container keydown event
413 if (type == 'keydown') {
414 record = me.getSelectionModel().getLastSelected();
416 item = me.getNode(record);
422 index = me.indexOf(item);
424 record = me.getRecord(item);
427 if (me.processItemEvent(type, record, item, index, e) === false) {
431 type = me.isNewItemEvent(type, item, e);
432 if (type === false) {
437 (me['onBeforeItem' + map[type]](record, item, index, e) === false) ||
438 (me.fireEvent('beforeitem' + type, me, record, item, index, e) === false) ||
439 (me['onItem' + map[type]](record, item, index, e) === false)
444 me.fireEvent('item' + type, me, record, item, index, e);
448 (me.processContainerEvent(type, e) === false) ||
449 (me['onBeforeContainer' + map[type]](e) === false) ||
450 (me.fireEvent('beforecontainer' + type, me, e) === false) ||
451 (me['onContainer' + map[type]](e) === false)
456 me.fireEvent('container' + type, me, e);
462 isNewItemEvent: function(type, item, e) {
464 overItem = me.mouseOverItem,
470 if (item === overItem) {
473 me.mouseOverItem = item;
479 * Need an extra check here to see if it's the parent element. See the
480 * comment re: the browser bug at the start of processUIEvent
482 if (overItem && typeof overItem.offsetParent === "object") {
483 contains = Ext.fly(me.mouseOverItem).getRegion().contains(e.getPoint());
484 isItem = Ext.fly(e.getTarget()).hasCls(me.itemSelector);
485 if (contains && isItem) {
489 me.mouseOverItem = null;
497 onItemMouseEnter: function(record, item, index, e) {
498 if (this.trackOver) {
499 this.highlightItem(item);
504 onItemMouseLeave : function(record, item, index, e) {
505 if (this.trackOver) {
506 this.clearHighlight();
510 // @private, template methods
511 onItemMouseDown: Ext.emptyFn,
512 onItemMouseUp: Ext.emptyFn,
513 onItemClick: Ext.emptyFn,
514 onItemDblClick: Ext.emptyFn,
515 onItemContextMenu: Ext.emptyFn,
516 onItemKeyDown: Ext.emptyFn,
517 onBeforeItemMouseDown: Ext.emptyFn,
518 onBeforeItemMouseUp: Ext.emptyFn,
519 onBeforeItemMouseEnter: Ext.emptyFn,
520 onBeforeItemMouseLeave: Ext.emptyFn,
521 onBeforeItemClick: Ext.emptyFn,
522 onBeforeItemDblClick: Ext.emptyFn,
523 onBeforeItemContextMenu: Ext.emptyFn,
524 onBeforeItemKeyDown: Ext.emptyFn,
526 // @private, template methods
527 onContainerMouseDown: Ext.emptyFn,
528 onContainerMouseUp: Ext.emptyFn,
529 onContainerMouseOver: Ext.emptyFn,
530 onContainerMouseOut: Ext.emptyFn,
531 onContainerClick: Ext.emptyFn,
532 onContainerDblClick: Ext.emptyFn,
533 onContainerContextMenu: Ext.emptyFn,
534 onContainerKeyDown: Ext.emptyFn,
535 onBeforeContainerMouseDown: Ext.emptyFn,
536 onBeforeContainerMouseUp: Ext.emptyFn,
537 onBeforeContainerMouseOver: Ext.emptyFn,
538 onBeforeContainerMouseOut: Ext.emptyFn,
539 onBeforeContainerClick: Ext.emptyFn,
540 onBeforeContainerDblClick: Ext.emptyFn,
541 onBeforeContainerContextMenu: Ext.emptyFn,
542 onBeforeContainerKeyDown: Ext.emptyFn,
545 * Highlight a given item in the DataView. This is called by the mouseover handler if {@link #overItemCls}
546 * and {@link #trackOver} are configured, but can also be called manually by other code, for instance to
547 * handle stepping through the list via keyboard navigation.
548 * @param {HTMLElement} item The item to highlight
550 highlightItem: function(item) {
553 me.highlightedItem = item;
554 Ext.fly(item).addCls(me.overItemCls);
558 * Un-highlight the currently highlighted item, if any.
560 clearHighlight: function() {
562 highlighted = me.highlightedItem;
565 Ext.fly(highlighted).removeCls(me.overItemCls);
566 delete me.highlightedItem;
570 refresh: function() {
571 this.clearHighlight();
572 this.callParent(arguments);