3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.DataView"></div>/**
16 * @extends Ext.BoxComponent
17 * A mechanism for displaying data using custom layout templates and formatting. DataView uses an {@link Ext.XTemplate}
18 * as its internal templating mechanism, and is bound to an {@link Ext.data.Store}
19 * so that as the data in the store changes the view is automatically updated to reflect the changes. The view also
20 * provides built-in behavior for many common events that can occur for its contained items including click, doubleclick,
21 * mouseover, mouseout, etc. as well as a built-in selection model. <b>In order to use these features, an {@link #itemSelector}
22 * config must be provided for the DataView to determine what nodes it will be working with.</b>
24 * <p>The example below binds a DataView to a {@link Ext.data.Store} and renders it into an {@link Ext.Panel}.</p>
26 var store = new Ext.data.JsonStore({
27 url: 'get-images.php',
31 {name:'size', type: 'float'},
32 {name:'lastmod', type:'date', dateFormat:'timestamp'}
37 var tpl = new Ext.XTemplate(
38 '<tpl for=".">',
39 '<div class="thumb-wrap" id="{name}">',
40 '<div class="thumb"><img src="{url}" title="{name}"></div>',
41 '<span class="x-editable">{shortName}</span></div>',
43 '<div class="x-clear"></div>'
46 var panel = new Ext.Panel({
53 title:'Simple DataView',
55 items: new Ext.DataView({
60 overClass:'x-view-over',
61 itemSelector:'div.thumb-wrap',
62 emptyText: 'No images to display'
65 panel.render(document.body);
68 * Create a new DataView
69 * @param {Object} config The config object
72 Ext.DataView = Ext.extend(Ext.BoxComponent, {
73 <div id="cfg-Ext.DataView-tpl"></div>/**
74 * @cfg {String/Array} tpl
75 * The HTML fragment or an array of fragments that will make up the template used by this DataView. This should
76 * be specified in the same format expected by the constructor of {@link Ext.XTemplate}.
78 <div id="cfg-Ext.DataView-store"></div>/**
79 * @cfg {Ext.data.Store} store
80 * The {@link Ext.data.Store} to bind this DataView to.
82 <div id="cfg-Ext.DataView-itemSelector"></div>/**
83 * @cfg {String} itemSelector
84 * <b>This is a required setting</b>. A simple CSS selector (e.g. <tt>div.some-class</tt> or
85 * <tt>span:first-child</tt>) that will be used to determine what nodes this DataView will be
88 <div id="cfg-Ext.DataView-multiSelect"></div>/**
89 * @cfg {Boolean} multiSelect
90 * True to allow selection of more than one item at a time, false to allow selection of only a single item
91 * at a time or no selection at all, depending on the value of {@link #singleSelect} (defaults to false).
93 <div id="cfg-Ext.DataView-singleSelect"></div>/**
94 * @cfg {Boolean} singleSelect
95 * True to allow selection of exactly one item at a time, false to allow no selection at all (defaults to false).
96 * Note that if {@link #multiSelect} = true, this value will be ignored.
98 <div id="cfg-Ext.DataView-simpleSelect"></div>/**
99 * @cfg {Boolean} simpleSelect
100 * True to enable multiselection by clicking on multiple items without requiring the user to hold Shift or Ctrl,
101 * false to force the user to hold Ctrl or Shift to select more than on item (defaults to false).
103 <div id="cfg-Ext.DataView-overClass"></div>/**
104 * @cfg {String} overClass
105 * A CSS class to apply to each item in the view on mouseover (defaults to undefined).
107 <div id="cfg-Ext.DataView-loadingText"></div>/**
108 * @cfg {String} loadingText
109 * A string to display during data load operations (defaults to undefined). If specified, this text will be
110 * displayed in a loading div and the view's contents will be cleared while loading, otherwise the view's
111 * contents will continue to display normally until the new data is loaded and the contents are replaced.
113 <div id="cfg-Ext.DataView-selectedClass"></div>/**
114 * @cfg {String} selectedClass
115 * A CSS class to apply to each selected item in the view (defaults to 'x-view-selected').
117 selectedClass : "x-view-selected",
118 <div id="cfg-Ext.DataView-emptyText"></div>/**
119 * @cfg {String} emptyText
120 * The text to display in the view when there is no data to display (defaults to '').
124 <div id="cfg-Ext.DataView-deferEmptyText"></div>/**
125 * @cfg {Boolean} deferEmptyText True to defer emptyText being applied until the store's first load
127 deferEmptyText: true,
128 <div id="cfg-Ext.DataView-trackOver"></div>/**
129 * @cfg {Boolean} trackOver True to enable mouseenter and mouseleave events
137 initComponent : function(){
138 Ext.DataView.superclass.initComponent.call(this);
139 if(Ext.isString(this.tpl) || Ext.isArray(this.tpl)){
140 this.tpl = new Ext.XTemplate(this.tpl);
144 <div id="event-Ext.DataView-beforeclick"></div>/**
146 * Fires before a click is processed. Returns false to cancel the default action.
147 * @param {Ext.DataView} this
148 * @param {Number} index The index of the target node
149 * @param {HTMLElement} node The target node
150 * @param {Ext.EventObject} e The raw event object
153 <div id="event-Ext.DataView-click"></div>/**
155 * Fires when a template node is clicked.
156 * @param {Ext.DataView} this
157 * @param {Number} index The index of the target node
158 * @param {HTMLElement} node The target node
159 * @param {Ext.EventObject} e The raw event object
162 <div id="event-Ext.DataView-mouseenter"></div>/**
164 * Fires when the mouse enters a template node. trackOver:true or an overCls must be set to enable this event.
165 * @param {Ext.DataView} this
166 * @param {Number} index The index of the target node
167 * @param {HTMLElement} node The target node
168 * @param {Ext.EventObject} e The raw event object
171 <div id="event-Ext.DataView-mouseleave"></div>/**
173 * Fires when the mouse leaves a template node. trackOver:true or an overCls must be set to enable this event.
174 * @param {Ext.DataView} this
175 * @param {Number} index The index of the target node
176 * @param {HTMLElement} node The target node
177 * @param {Ext.EventObject} e The raw event object
180 <div id="event-Ext.DataView-containerclick"></div>/**
181 * @event containerclick
182 * Fires when a click occurs and it is not on a template node.
183 * @param {Ext.DataView} this
184 * @param {Ext.EventObject} e The raw event object
187 <div id="event-Ext.DataView-dblclick"></div>/**
189 * Fires when a template node is double clicked.
190 * @param {Ext.DataView} this
191 * @param {Number} index The index of the target node
192 * @param {HTMLElement} node The target node
193 * @param {Ext.EventObject} e The raw event object
196 <div id="event-Ext.DataView-contextmenu"></div>/**
198 * Fires when a template node is right clicked.
199 * @param {Ext.DataView} this
200 * @param {Number} index The index of the target node
201 * @param {HTMLElement} node The target node
202 * @param {Ext.EventObject} e The raw event object
205 <div id="event-Ext.DataView-containercontextmenu"></div>/**
206 * @event containercontextmenu
207 * Fires when a right click occurs that is not on a template node.
208 * @param {Ext.DataView} this
209 * @param {Ext.EventObject} e The raw event object
211 "containercontextmenu",
212 <div id="event-Ext.DataView-selectionchange"></div>/**
213 * @event selectionchange
214 * Fires when the selected nodes change.
215 * @param {Ext.DataView} this
216 * @param {Array} selections Array of the selected nodes
220 <div id="event-Ext.DataView-beforeselect"></div>/**
221 * @event beforeselect
222 * Fires before a selection is made. If any handlers return false, the selection is cancelled.
223 * @param {Ext.DataView} this
224 * @param {HTMLElement} node The node to be selected
225 * @param {Array} selections Array of currently selected nodes
230 this.store = Ext.StoreMgr.lookup(this.store);
231 this.all = new Ext.CompositeElementLite();
232 this.selected = new Ext.CompositeElementLite();
236 afterRender : function(){
237 Ext.DataView.superclass.afterRender.call(this);
239 this.mon(this.getTemplateTarget(), {
240 "click": this.onClick,
241 "dblclick": this.onDblClick,
242 "contextmenu": this.onContextMenu,
246 if(this.overClass || this.trackOver){
247 this.mon(this.getTemplateTarget(), {
248 "mouseover": this.onMouseOver,
249 "mouseout": this.onMouseOut,
255 this.bindStore(this.store, true);
259 <div id="method-Ext.DataView-refresh"></div>/**
260 * Refreshes the view by reloading the data from the store and re-rendering the template.
262 refresh : function(){
263 this.clearSelections(false, true);
264 var el = this.getTemplateTarget();
266 var records = this.store.getRange();
267 if(records.length < 1){
268 if(!this.deferEmptyText || this.hasSkippedEmptyText){
269 el.update(this.emptyText);
273 this.tpl.overwrite(el, this.collectData(records, 0));
274 this.all.fill(Ext.query(this.itemSelector, el.dom));
275 this.updateIndexes(0);
277 this.hasSkippedEmptyText = true;
280 getTemplateTarget: function(){
284 <div id="method-Ext.DataView-prepareData"></div>/**
285 * Function which can be overridden to provide custom formatting for each Record that is used by this
286 * DataView's {@link #tpl template} to render each node.
287 * @param {Array/Object} data The raw data object that was used to create the Record.
288 * @param {Number} recordIndex the index number of the Record being prepared for rendering.
289 * @param {Record} record The Record being prepared for rendering.
290 * @return {Array/Object} The formatted data in a format expected by the internal {@link #tpl template}'s overwrite() method.
291 * (either an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'}))
293 prepareData : function(data){
297 <div id="method-Ext.DataView-collectData"></div>/**
298 * <p>Function which can be overridden which returns the data object passed to this
299 * DataView's {@link #tpl template} to render the whole DataView.</p>
300 * <p>This is usually an Array of data objects, each element of which is processed by an
301 * {@link Ext.XTemplate XTemplate} which uses <tt>'<tpl for=".">'</tt> to iterate over its supplied
302 * data object as an Array. However, <i>named</i> properties may be placed into the data object to
303 * provide non-repeating data such as headings, totals etc.</p>
304 * @param {Array} records An Array of {@link Ext.data.Record}s to be rendered into the DataView.
305 * @param {Number} startIndex the index number of the Record being prepared for rendering.
306 * @return {Array} An Array of data objects to be processed by a repeating XTemplate. May also
307 * contain <i>named</i> properties.
309 collectData : function(records, startIndex){
311 for(var i = 0, len = records.length; i < len; i++){
312 r[r.length] = this.prepareData(records[i].data, startIndex+i, records[i]);
318 bufferRender : function(records){
319 var div = document.createElement('div');
320 this.tpl.overwrite(div, this.collectData(records));
321 return Ext.query(this.itemSelector, div);
325 onUpdate : function(ds, record){
326 var index = this.store.indexOf(record);
328 var sel = this.isSelected(index);
329 var original = this.all.elements[index];
330 var node = this.bufferRender([record], index)[0];
332 this.all.replaceElement(index, node, true);
334 this.selected.replaceElement(original, node);
335 this.all.item(index).addClass(this.selectedClass);
337 this.updateIndexes(index, index);
342 onAdd : function(ds, records, index){
343 if(this.all.getCount() === 0){
347 var nodes = this.bufferRender(records, index), n, a = this.all.elements;
348 if(index < this.all.getCount()){
349 n = this.all.item(index).insertSibling(nodes, 'before', true);
350 a.splice.apply(a, [index, 0].concat(nodes));
352 n = this.all.last().insertSibling(nodes, 'after', true);
353 a.push.apply(a, nodes);
355 this.updateIndexes(index);
359 onRemove : function(ds, record, index){
360 this.deselect(index);
361 this.all.removeElement(index, true);
362 this.updateIndexes(index);
363 if (this.store.getCount() === 0){
368 <div id="method-Ext.DataView-refreshNode"></div>/**
369 * Refreshes an individual node's data from the store.
370 * @param {Number} index The item's data index in the store
372 refreshNode : function(index){
373 this.onUpdate(this.store, this.store.getAt(index));
377 updateIndexes : function(startIndex, endIndex){
378 var ns = this.all.elements;
379 startIndex = startIndex || 0;
380 endIndex = endIndex || ((endIndex === 0) ? 0 : (ns.length - 1));
381 for(var i = startIndex; i <= endIndex; i++){
386 <div id="method-Ext.DataView-getStore"></div>/**
387 * Returns the store associated with this DataView.
388 * @return {Ext.data.Store} The store
390 getStore : function(){
394 <div id="method-Ext.DataView-bindStore"></div>/**
395 * Changes the data store bound to this view and refreshes it.
396 * @param {Store} store The store to bind to this view
398 bindStore : function(store, initial){
399 if(!initial && this.store){
400 if(store !== this.store && this.store.autoDestroy){
401 this.store.destroy();
403 this.store.un("beforeload", this.onBeforeLoad, this);
404 this.store.un("datachanged", this.refresh, this);
405 this.store.un("add", this.onAdd, this);
406 this.store.un("remove", this.onRemove, this);
407 this.store.un("update", this.onUpdate, this);
408 this.store.un("clear", this.refresh, this);
415 store = Ext.StoreMgr.lookup(store);
418 beforeload: this.onBeforeLoad,
419 datachanged: this.refresh,
421 remove: this.onRemove,
422 update: this.onUpdate,
432 <div id="method-Ext.DataView-findItemFromChild"></div>/**
433 * Returns the template node the passed child belongs to, or null if it doesn't belong to one.
434 * @param {HTMLElement} node
435 * @return {HTMLElement} The template node
437 findItemFromChild : function(node){
438 return Ext.fly(node).findParent(this.itemSelector, this.getTemplateTarget());
442 onClick : function(e){
443 var item = e.getTarget(this.itemSelector, this.getTemplateTarget());
445 var index = this.indexOf(item);
446 if(this.onItemClick(item, index, e) !== false){
447 this.fireEvent("click", this, index, item, e);
450 if(this.fireEvent("containerclick", this, e) !== false){
451 this.onContainerClick(e);
456 onContainerClick : function(e){
457 this.clearSelections();
461 onContextMenu : function(e){
462 var item = e.getTarget(this.itemSelector, this.getTemplateTarget());
464 this.fireEvent("contextmenu", this, this.indexOf(item), item, e);
466 this.fireEvent("containercontextmenu", this, e);
471 onDblClick : function(e){
472 var item = e.getTarget(this.itemSelector, this.getTemplateTarget());
474 this.fireEvent("dblclick", this, this.indexOf(item), item, e);
479 onMouseOver : function(e){
480 var item = e.getTarget(this.itemSelector, this.getTemplateTarget());
481 if(item && item !== this.lastItem){
482 this.lastItem = item;
483 Ext.fly(item).addClass(this.overClass);
484 this.fireEvent("mouseenter", this, this.indexOf(item), item, e);
489 onMouseOut : function(e){
491 if(!e.within(this.lastItem, true, true)){
492 Ext.fly(this.lastItem).removeClass(this.overClass);
493 this.fireEvent("mouseleave", this, this.indexOf(this.lastItem), this.lastItem, e);
494 delete this.lastItem;
500 onItemClick : function(item, index, e){
501 if(this.fireEvent("beforeclick", this, index, item, e) === false){
504 if(this.multiSelect){
505 this.doMultiSelection(item, index, e);
507 }else if(this.singleSelect){
508 this.doSingleSelection(item, index, e);
515 doSingleSelection : function(item, index, e){
516 if(e.ctrlKey && this.isSelected(index)){
517 this.deselect(index);
519 this.select(index, false);
524 doMultiSelection : function(item, index, e){
525 if(e.shiftKey && this.last !== false){
526 var last = this.last;
527 this.selectRange(last, index, e.ctrlKey);
528 this.last = last; // reset the last
530 if((e.ctrlKey||this.simpleSelect) && this.isSelected(index)){
531 this.deselect(index);
533 this.select(index, e.ctrlKey || e.shiftKey || this.simpleSelect);
538 <div id="method-Ext.DataView-getSelectionCount"></div>/**
539 * Gets the number of selected nodes.
540 * @return {Number} The node count
542 getSelectionCount : function(){
543 return this.selected.getCount();
546 <div id="method-Ext.DataView-getSelectedNodes"></div>/**
547 * Gets the currently selected nodes.
548 * @return {Array} An array of HTMLElements
550 getSelectedNodes : function(){
551 return this.selected.elements;
554 <div id="method-Ext.DataView-getSelectedIndexes"></div>/**
555 * Gets the indexes of the selected nodes.
556 * @return {Array} An array of numeric indexes
558 getSelectedIndexes : function(){
559 var indexes = [], s = this.selected.elements;
560 for(var i = 0, len = s.length; i < len; i++){
561 indexes.push(s[i].viewIndex);
566 <div id="method-Ext.DataView-getSelectedRecords"></div>/**
567 * Gets an array of the selected records
568 * @return {Array} An array of {@link Ext.data.Record} objects
570 getSelectedRecords : function(){
571 var r = [], s = this.selected.elements;
572 for(var i = 0, len = s.length; i < len; i++){
573 r[r.length] = this.store.getAt(s[i].viewIndex);
578 <div id="method-Ext.DataView-getRecords"></div>/**
579 * Gets an array of the records from an array of nodes
580 * @param {Array} nodes The nodes to evaluate
581 * @return {Array} records The {@link Ext.data.Record} objects
583 getRecords : function(nodes){
584 var r = [], s = nodes;
585 for(var i = 0, len = s.length; i < len; i++){
586 r[r.length] = this.store.getAt(s[i].viewIndex);
591 <div id="method-Ext.DataView-getRecord"></div>/**
592 * Gets a record from a node
593 * @param {HTMLElement} node The node to evaluate
594 * @return {Record} record The {@link Ext.data.Record} object
596 getRecord : function(node){
597 return this.store.getAt(node.viewIndex);
600 <div id="method-Ext.DataView-clearSelections"></div>/**
601 * Clears all selections.
602 * @param {Boolean} suppressEvent (optional) True to skip firing of the selectionchange event
604 clearSelections : function(suppressEvent, skipUpdate){
605 if((this.multiSelect || this.singleSelect) && this.selected.getCount() > 0){
607 this.selected.removeClass(this.selectedClass);
609 this.selected.clear();
612 this.fireEvent("selectionchange", this, this.selected.elements);
617 <div id="method-Ext.DataView-isSelected"></div>/**
618 * Returns true if the passed node is selected, else false.
619 * @param {HTMLElement/Number} node The node or node index to check
620 * @return {Boolean} True if selected, else false
622 isSelected : function(node){
623 return this.selected.contains(this.getNode(node));
626 <div id="method-Ext.DataView-deselect"></div>/**
628 * @param {HTMLElement/Number} node The node to deselect
630 deselect : function(node){
631 if(this.isSelected(node)){
632 node = this.getNode(node);
633 this.selected.removeElement(node);
634 if(this.last == node.viewIndex){
637 Ext.fly(node).removeClass(this.selectedClass);
638 this.fireEvent("selectionchange", this, this.selected.elements);
642 <div id="method-Ext.DataView-select"></div>/**
643 * Selects a set of nodes.
644 * @param {Array/HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node,
645 * id of a template node or an array of any of those to select
646 * @param {Boolean} keepExisting (optional) true to keep existing selections
647 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange vent
649 select : function(nodeInfo, keepExisting, suppressEvent){
650 if(Ext.isArray(nodeInfo)){
652 this.clearSelections(true);
654 for(var i = 0, len = nodeInfo.length; i < len; i++){
655 this.select(nodeInfo[i], true, true);
658 this.fireEvent("selectionchange", this, this.selected.elements);
661 var node = this.getNode(nodeInfo);
663 this.clearSelections(true);
665 if(node && !this.isSelected(node)){
666 if(this.fireEvent("beforeselect", this, node, this.selected.elements) !== false){
667 Ext.fly(node).addClass(this.selectedClass);
668 this.selected.add(node);
669 this.last = node.viewIndex;
671 this.fireEvent("selectionchange", this, this.selected.elements);
678 <div id="method-Ext.DataView-selectRange"></div>/**
679 * Selects a range of nodes. All nodes between start and end are selected.
680 * @param {Number} start The index of the first node in the range
681 * @param {Number} end The index of the last node in the range
682 * @param {Boolean} keepExisting (optional) True to retain existing selections
684 selectRange : function(start, end, keepExisting){
686 this.clearSelections(true);
688 this.select(this.getNodes(start, end), true);
691 <div id="method-Ext.DataView-getNode"></div>/**
692 * Gets a template node.
693 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
694 * @return {HTMLElement} The node or null if it wasn't found
696 getNode : function(nodeInfo){
697 if(Ext.isString(nodeInfo)){
698 return document.getElementById(nodeInfo);
699 }else if(Ext.isNumber(nodeInfo)){
700 return this.all.elements[nodeInfo];
705 <div id="method-Ext.DataView-getNodes"></div>/**
706 * Gets a range nodes.
707 * @param {Number} start (optional) The index of the first node in the range
708 * @param {Number} end (optional) The index of the last node in the range
709 * @return {Array} An array of nodes
711 getNodes : function(start, end){
712 var ns = this.all.elements;
714 end = !Ext.isDefined(end) ? Math.max(ns.length - 1, 0) : end;
717 for(i = start; i <= end && ns[i]; i++){
721 for(i = start; i >= end && ns[i]; i--){
728 <div id="method-Ext.DataView-indexOf"></div>/**
729 * Finds the index of the passed node.
730 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
731 * @return {Number} The index of the node or -1
733 indexOf : function(node){
734 node = this.getNode(node);
735 if(Ext.isNumber(node.viewIndex)){
736 return node.viewIndex;
738 return this.all.indexOf(node);
742 onBeforeLoad : function(){
743 if(this.loadingText){
744 this.clearSelections(false, true);
745 this.getTemplateTarget().update('<div class="loading-indicator">'+this.loadingText+'</div>');
750 onDestroy : function(){
751 Ext.DataView.superclass.onDestroy.call(this);
752 this.bindStore(null);
756 <div id="method-Ext.DataView-setStore"></div>/**
757 * Changes the data store bound to this view and refreshes it. (deprecated in favor of bindStore)
758 * @param {Store} store The store to bind to this view
760 Ext.DataView.prototype.setStore = Ext.DataView.prototype.bindStore;
762 Ext.reg('dataview', Ext.DataView);</pre>