3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.DataView"></div>/**
10 * @extends Ext.BoxComponent
11 * A mechanism for displaying data using custom layout templates and formatting. DataView uses an {@link Ext.XTemplate}
12 * as its internal templating mechanism, and is bound to an {@link Ext.data.Store}
13 * so that as the data in the store changes the view is automatically updated to reflect the changes. The view also
14 * provides built-in behavior for many common events that can occur for its contained items including click, doubleclick,
15 * mouseover, mouseout, etc. as well as a built-in selection model. <b>In order to use these features, an {@link #itemSelector}
16 * config must be provided for the DataView to determine what nodes it will be working with.</b>
18 * <p>The example below binds a DataView to a {@link Ext.data.Store} and renders it into an {@link Ext.Panel}.</p>
20 var store = new Ext.data.JsonStore({
21 url: 'get-images.php',
25 {name:'size', type: 'float'},
26 {name:'lastmod', type:'date', dateFormat:'timestamp'}
31 var tpl = new Ext.XTemplate(
32 '<tpl for=".">',
33 '<div class="thumb-wrap" id="{name}">',
34 '<div class="thumb"><img src="{url}" title="{name}"></div>',
35 '<span class="x-editable">{shortName}</span></div>',
37 '<div class="x-clear"></div>'
40 var panel = new Ext.Panel({
47 title:'Simple DataView',
49 items: new Ext.DataView({
54 overClass:'x-view-over',
55 itemSelector:'div.thumb-wrap',
56 emptyText: 'No images to display'
59 panel.render(document.body);
62 * Create a new DataView
63 * @param {Object} config The config object
66 Ext.DataView = Ext.extend(Ext.BoxComponent, {
67 <div id="cfg-Ext.DataView-tpl"></div>/**
68 * @cfg {String/Array} tpl
69 * The HTML fragment or an array of fragments that will make up the template used by this DataView. This should
70 * be specified in the same format expected by the constructor of {@link Ext.XTemplate}.
72 <div id="cfg-Ext.DataView-store"></div>/**
73 * @cfg {Ext.data.Store} store
74 * The {@link Ext.data.Store} to bind this DataView to.
76 <div id="cfg-Ext.DataView-itemSelector"></div>/**
77 * @cfg {String} itemSelector
78 * <b>This is a required setting</b>. A simple CSS selector (e.g. <tt>div.some-class</tt> or
79 * <tt>span:first-child</tt>) that will be used to determine what nodes this DataView will be
82 <div id="cfg-Ext.DataView-multiSelect"></div>/**
83 * @cfg {Boolean} multiSelect
84 * True to allow selection of more than one item at a time, false to allow selection of only a single item
85 * at a time or no selection at all, depending on the value of {@link #singleSelect} (defaults to false).
87 <div id="cfg-Ext.DataView-singleSelect"></div>/**
88 * @cfg {Boolean} singleSelect
89 * True to allow selection of exactly one item at a time, false to allow no selection at all (defaults to false).
90 * Note that if {@link #multiSelect} = true, this value will be ignored.
92 <div id="cfg-Ext.DataView-simpleSelect"></div>/**
93 * @cfg {Boolean} simpleSelect
94 * True to enable multiselection by clicking on multiple items without requiring the user to hold Shift or Ctrl,
95 * false to force the user to hold Ctrl or Shift to select more than on item (defaults to false).
97 <div id="cfg-Ext.DataView-overClass"></div>/**
98 * @cfg {String} overClass
99 * A CSS class to apply to each item in the view on mouseover (defaults to undefined).
101 <div id="cfg-Ext.DataView-loadingText"></div>/**
102 * @cfg {String} loadingText
103 * A string to display during data load operations (defaults to undefined). If specified, this text will be
104 * displayed in a loading div and the view's contents will be cleared while loading, otherwise the view's
105 * contents will continue to display normally until the new data is loaded and the contents are replaced.
107 <div id="cfg-Ext.DataView-selectedClass"></div>/**
108 * @cfg {String} selectedClass
109 * A CSS class to apply to each selected item in the view (defaults to 'x-view-selected').
111 selectedClass : "x-view-selected",
112 <div id="cfg-Ext.DataView-emptyText"></div>/**
113 * @cfg {String} emptyText
114 * The text to display in the view when there is no data to display (defaults to '').
118 <div id="cfg-Ext.DataView-deferEmptyText"></div>/**
119 * @cfg {Boolean} deferEmptyText True to defer emptyText being applied until the store's first load
121 deferEmptyText: true,
122 <div id="cfg-Ext.DataView-trackOver"></div>/**
123 * @cfg {Boolean} trackOver True to enable mouseenter and mouseleave events
131 initComponent : function(){
132 Ext.DataView.superclass.initComponent.call(this);
133 if(Ext.isString(this.tpl) || Ext.isArray(this.tpl)){
134 this.tpl = new Ext.XTemplate(this.tpl);
138 <div id="event-Ext.DataView-beforeclick"></div>/**
140 * Fires before a click is processed. Returns false to cancel the default action.
141 * @param {Ext.DataView} this
142 * @param {Number} index The index of the target node
143 * @param {HTMLElement} node The target node
144 * @param {Ext.EventObject} e The raw event object
147 <div id="event-Ext.DataView-click"></div>/**
149 * Fires when a template node is clicked.
150 * @param {Ext.DataView} this
151 * @param {Number} index The index of the target node
152 * @param {HTMLElement} node The target node
153 * @param {Ext.EventObject} e The raw event object
156 <div id="event-Ext.DataView-mouseenter"></div>/**
158 * Fires when the mouse enters a template node. trackOver:true or an overCls must be set to enable this event.
159 * @param {Ext.DataView} this
160 * @param {Number} index The index of the target node
161 * @param {HTMLElement} node The target node
162 * @param {Ext.EventObject} e The raw event object
165 <div id="event-Ext.DataView-mouseleave"></div>/**
167 * Fires when the mouse leaves a template node. trackOver:true or an overCls must be set to enable this event.
168 * @param {Ext.DataView} this
169 * @param {Number} index The index of the target node
170 * @param {HTMLElement} node The target node
171 * @param {Ext.EventObject} e The raw event object
174 <div id="event-Ext.DataView-containerclick"></div>/**
175 * @event containerclick
176 * Fires when a click occurs and it is not on a template node.
177 * @param {Ext.DataView} this
178 * @param {Ext.EventObject} e The raw event object
181 <div id="event-Ext.DataView-dblclick"></div>/**
183 * Fires when a template node is double clicked.
184 * @param {Ext.DataView} this
185 * @param {Number} index The index of the target node
186 * @param {HTMLElement} node The target node
187 * @param {Ext.EventObject} e The raw event object
190 <div id="event-Ext.DataView-contextmenu"></div>/**
192 * Fires when a template node is right clicked.
193 * @param {Ext.DataView} this
194 * @param {Number} index The index of the target node
195 * @param {HTMLElement} node The target node
196 * @param {Ext.EventObject} e The raw event object
199 <div id="event-Ext.DataView-containercontextmenu"></div>/**
200 * @event containercontextmenu
201 * Fires when a right click occurs that is not on a template node.
202 * @param {Ext.DataView} this
203 * @param {Ext.EventObject} e The raw event object
205 "containercontextmenu",
206 <div id="event-Ext.DataView-selectionchange"></div>/**
207 * @event selectionchange
208 * Fires when the selected nodes change.
209 * @param {Ext.DataView} this
210 * @param {Array} selections Array of the selected nodes
214 <div id="event-Ext.DataView-beforeselect"></div>/**
215 * @event beforeselect
216 * Fires before a selection is made. If any handlers return false, the selection is cancelled.
217 * @param {Ext.DataView} this
218 * @param {HTMLElement} node The node to be selected
219 * @param {Array} selections Array of currently selected nodes
224 this.store = Ext.StoreMgr.lookup(this.store);
225 this.all = new Ext.CompositeElementLite();
226 this.selected = new Ext.CompositeElementLite();
230 afterRender : function(){
231 Ext.DataView.superclass.afterRender.call(this);
233 this.mon(this.getTemplateTarget(), {
234 "click": this.onClick,
235 "dblclick": this.onDblClick,
236 "contextmenu": this.onContextMenu,
240 if(this.overClass || this.trackOver){
241 this.mon(this.getTemplateTarget(), {
242 "mouseover": this.onMouseOver,
243 "mouseout": this.onMouseOut,
249 this.bindStore(this.store, true);
253 <div id="method-Ext.DataView-refresh"></div>/**
254 * Refreshes the view by reloading the data from the store and re-rendering the template.
256 refresh : function(){
257 this.clearSelections(false, true);
258 var el = this.getTemplateTarget();
260 var records = this.store.getRange();
261 if(records.length < 1){
262 if(!this.deferEmptyText || this.hasSkippedEmptyText){
263 el.update(this.emptyText);
267 this.tpl.overwrite(el, this.collectData(records, 0));
268 this.all.fill(Ext.query(this.itemSelector, el.dom));
269 this.updateIndexes(0);
271 this.hasSkippedEmptyText = true;
274 getTemplateTarget: function(){
278 <div id="method-Ext.DataView-prepareData"></div>/**
279 * Function which can be overridden to provide custom formatting for each Record that is used by this
280 * DataView's {@link #tpl template} to render each node.
281 * @param {Array/Object} data The raw data object that was used to create the Record.
282 * @param {Number} recordIndex the index number of the Record being prepared for rendering.
283 * @param {Record} record The Record being prepared for rendering.
284 * @return {Array/Object} The formatted data in a format expected by the internal {@link #tpl template}'s overwrite() method.
285 * (either an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'}))
287 prepareData : function(data){
291 <div id="method-Ext.DataView-collectData"></div>/**
292 * <p>Function which can be overridden which returns the data object passed to this
293 * DataView's {@link #tpl template} to render the whole DataView.</p>
294 * <p>This is usually an Array of data objects, each element of which is processed by an
295 * {@link Ext.XTemplate XTemplate} which uses <tt>'<tpl for=".">'</tt> to iterate over its supplied
296 * data object as an Array. However, <i>named</i> properties may be placed into the data object to
297 * provide non-repeating data such as headings, totals etc.</p>
298 * @param {Array} records An Array of {@link Ext.data.Record}s to be rendered into the DataView.
299 * @param {Number} startIndex the index number of the Record being prepared for rendering.
300 * @return {Array} An Array of data objects to be processed by a repeating XTemplate. May also
301 * contain <i>named</i> properties.
303 collectData : function(records, startIndex){
305 for(var i = 0, len = records.length; i < len; i++){
306 r[r.length] = this.prepareData(records[i].data, startIndex+i, records[i]);
312 bufferRender : function(records){
313 var div = document.createElement('div');
314 this.tpl.overwrite(div, this.collectData(records));
315 return Ext.query(this.itemSelector, div);
319 onUpdate : function(ds, record){
320 var index = this.store.indexOf(record);
321 var sel = this.isSelected(index);
322 var original = this.all.elements[index];
323 var node = this.bufferRender([record], index)[0];
325 this.all.replaceElement(index, node, true);
327 this.selected.replaceElement(original, node);
328 this.all.item(index).addClass(this.selectedClass);
330 this.updateIndexes(index, index);
334 onAdd : function(ds, records, index){
335 if(this.all.getCount() === 0){
339 var nodes = this.bufferRender(records, index), n, a = this.all.elements;
340 if(index < this.all.getCount()){
341 n = this.all.item(index).insertSibling(nodes, 'before', true);
342 a.splice.apply(a, [index, 0].concat(nodes));
344 n = this.all.last().insertSibling(nodes, 'after', true);
345 a.push.apply(a, nodes);
347 this.updateIndexes(index);
351 onRemove : function(ds, record, index){
352 this.deselect(index);
353 this.all.removeElement(index, true);
354 this.updateIndexes(index);
355 if (this.store.getCount() === 0){
360 <div id="method-Ext.DataView-refreshNode"></div>/**
361 * Refreshes an individual node's data from the store.
362 * @param {Number} index The item's data index in the store
364 refreshNode : function(index){
365 this.onUpdate(this.store, this.store.getAt(index));
369 updateIndexes : function(startIndex, endIndex){
370 var ns = this.all.elements;
371 startIndex = startIndex || 0;
372 endIndex = endIndex || ((endIndex === 0) ? 0 : (ns.length - 1));
373 for(var i = startIndex; i <= endIndex; i++){
378 <div id="method-Ext.DataView-getStore"></div>/**
379 * Returns the store associated with this DataView.
380 * @return {Ext.data.Store} The store
382 getStore : function(){
386 <div id="method-Ext.DataView-bindStore"></div>/**
387 * Changes the data store bound to this view and refreshes it.
388 * @param {Store} store The store to bind to this view
390 bindStore : function(store, initial){
391 if(!initial && this.store){
392 this.store.un("beforeload", this.onBeforeLoad, this);
393 this.store.un("datachanged", this.refresh, this);
394 this.store.un("add", this.onAdd, this);
395 this.store.un("remove", this.onRemove, this);
396 this.store.un("update", this.onUpdate, this);
397 this.store.un("clear", this.refresh, this);
398 if(store !== this.store && this.store.autoDestroy){
399 this.store.destroy();
403 store = Ext.StoreMgr.lookup(store);
406 beforeload: this.onBeforeLoad,
407 datachanged: this.refresh,
409 remove: this.onRemove,
410 update: this.onUpdate,
420 <div id="method-Ext.DataView-findItemFromChild"></div>/**
421 * Returns the template node the passed child belongs to, or null if it doesn't belong to one.
422 * @param {HTMLElement} node
423 * @return {HTMLElement} The template node
425 findItemFromChild : function(node){
426 return Ext.fly(node).findParent(this.itemSelector, this.getTemplateTarget());
430 onClick : function(e){
431 var item = e.getTarget(this.itemSelector, this.getTemplateTarget());
433 var index = this.indexOf(item);
434 if(this.onItemClick(item, index, e) !== false){
435 this.fireEvent("click", this, index, item, e);
438 if(this.fireEvent("containerclick", this, e) !== false){
439 this.onContainerClick(e);
444 onContainerClick : function(e){
445 this.clearSelections();
449 onContextMenu : function(e){
450 var item = e.getTarget(this.itemSelector, this.getTemplateTarget());
452 this.fireEvent("contextmenu", this, this.indexOf(item), item, e);
454 this.fireEvent("containercontextmenu", this, e);
459 onDblClick : function(e){
460 var item = e.getTarget(this.itemSelector, this.getTemplateTarget());
462 this.fireEvent("dblclick", this, this.indexOf(item), item, e);
467 onMouseOver : function(e){
468 var item = e.getTarget(this.itemSelector, this.getTemplateTarget());
469 if(item && item !== this.lastItem){
470 this.lastItem = item;
471 Ext.fly(item).addClass(this.overClass);
472 this.fireEvent("mouseenter", this, this.indexOf(item), item, e);
477 onMouseOut : function(e){
479 if(!e.within(this.lastItem, true, true)){
480 Ext.fly(this.lastItem).removeClass(this.overClass);
481 this.fireEvent("mouseleave", this, this.indexOf(this.lastItem), this.lastItem, e);
482 delete this.lastItem;
488 onItemClick : function(item, index, e){
489 if(this.fireEvent("beforeclick", this, index, item, e) === false){
492 if(this.multiSelect){
493 this.doMultiSelection(item, index, e);
495 }else if(this.singleSelect){
496 this.doSingleSelection(item, index, e);
503 doSingleSelection : function(item, index, e){
504 if(e.ctrlKey && this.isSelected(index)){
505 this.deselect(index);
507 this.select(index, false);
512 doMultiSelection : function(item, index, e){
513 if(e.shiftKey && this.last !== false){
514 var last = this.last;
515 this.selectRange(last, index, e.ctrlKey);
516 this.last = last; // reset the last
518 if((e.ctrlKey||this.simpleSelect) && this.isSelected(index)){
519 this.deselect(index);
521 this.select(index, e.ctrlKey || e.shiftKey || this.simpleSelect);
526 <div id="method-Ext.DataView-getSelectionCount"></div>/**
527 * Gets the number of selected nodes.
528 * @return {Number} The node count
530 getSelectionCount : function(){
531 return this.selected.getCount();
534 <div id="method-Ext.DataView-getSelectedNodes"></div>/**
535 * Gets the currently selected nodes.
536 * @return {Array} An array of HTMLElements
538 getSelectedNodes : function(){
539 return this.selected.elements;
542 <div id="method-Ext.DataView-getSelectedIndexes"></div>/**
543 * Gets the indexes of the selected nodes.
544 * @return {Array} An array of numeric indexes
546 getSelectedIndexes : function(){
547 var indexes = [], s = this.selected.elements;
548 for(var i = 0, len = s.length; i < len; i++){
549 indexes.push(s[i].viewIndex);
554 <div id="method-Ext.DataView-getSelectedRecords"></div>/**
555 * Gets an array of the selected records
556 * @return {Array} An array of {@link Ext.data.Record} objects
558 getSelectedRecords : function(){
559 var r = [], s = this.selected.elements;
560 for(var i = 0, len = s.length; i < len; i++){
561 r[r.length] = this.store.getAt(s[i].viewIndex);
566 <div id="method-Ext.DataView-getRecords"></div>/**
567 * Gets an array of the records from an array of nodes
568 * @param {Array} nodes The nodes to evaluate
569 * @return {Array} records The {@link Ext.data.Record} objects
571 getRecords : function(nodes){
572 var r = [], s = nodes;
573 for(var i = 0, len = s.length; i < len; i++){
574 r[r.length] = this.store.getAt(s[i].viewIndex);
579 <div id="method-Ext.DataView-getRecord"></div>/**
580 * Gets a record from a node
581 * @param {HTMLElement} node The node to evaluate
582 * @return {Record} record The {@link Ext.data.Record} object
584 getRecord : function(node){
585 return this.store.getAt(node.viewIndex);
588 <div id="method-Ext.DataView-clearSelections"></div>/**
589 * Clears all selections.
590 * @param {Boolean} suppressEvent (optional) True to skip firing of the selectionchange event
592 clearSelections : function(suppressEvent, skipUpdate){
593 if((this.multiSelect || this.singleSelect) && this.selected.getCount() > 0){
595 this.selected.removeClass(this.selectedClass);
597 this.selected.clear();
600 this.fireEvent("selectionchange", this, this.selected.elements);
605 <div id="method-Ext.DataView-isSelected"></div>/**
606 * Returns true if the passed node is selected, else false.
607 * @param {HTMLElement/Number} node The node or node index to check
608 * @return {Boolean} True if selected, else false
610 isSelected : function(node){
611 return this.selected.contains(this.getNode(node));
614 <div id="method-Ext.DataView-deselect"></div>/**
616 * @param {HTMLElement/Number} node The node to deselect
618 deselect : function(node){
619 if(this.isSelected(node)){
620 node = this.getNode(node);
621 this.selected.removeElement(node);
622 if(this.last == node.viewIndex){
625 Ext.fly(node).removeClass(this.selectedClass);
626 this.fireEvent("selectionchange", this, this.selected.elements);
630 <div id="method-Ext.DataView-select"></div>/**
631 * Selects a set of nodes.
632 * @param {Array/HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node,
633 * id of a template node or an array of any of those to select
634 * @param {Boolean} keepExisting (optional) true to keep existing selections
635 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange vent
637 select : function(nodeInfo, keepExisting, suppressEvent){
638 if(Ext.isArray(nodeInfo)){
640 this.clearSelections(true);
642 for(var i = 0, len = nodeInfo.length; i < len; i++){
643 this.select(nodeInfo[i], true, true);
646 this.fireEvent("selectionchange", this, this.selected.elements);
649 var node = this.getNode(nodeInfo);
651 this.clearSelections(true);
653 if(node && !this.isSelected(node)){
654 if(this.fireEvent("beforeselect", this, node, this.selected.elements) !== false){
655 Ext.fly(node).addClass(this.selectedClass);
656 this.selected.add(node);
657 this.last = node.viewIndex;
659 this.fireEvent("selectionchange", this, this.selected.elements);
666 <div id="method-Ext.DataView-selectRange"></div>/**
667 * Selects a range of nodes. All nodes between start and end are selected.
668 * @param {Number} start The index of the first node in the range
669 * @param {Number} end The index of the last node in the range
670 * @param {Boolean} keepExisting (optional) True to retain existing selections
672 selectRange : function(start, end, keepExisting){
674 this.clearSelections(true);
676 this.select(this.getNodes(start, end), true);
679 <div id="method-Ext.DataView-getNode"></div>/**
680 * Gets a template node.
681 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
682 * @return {HTMLElement} The node or null if it wasn't found
684 getNode : function(nodeInfo){
685 if(Ext.isString(nodeInfo)){
686 return document.getElementById(nodeInfo);
687 }else if(Ext.isNumber(nodeInfo)){
688 return this.all.elements[nodeInfo];
693 <div id="method-Ext.DataView-getNodes"></div>/**
694 * Gets a range nodes.
695 * @param {Number} start (optional) The index of the first node in the range
696 * @param {Number} end (optional) The index of the last node in the range
697 * @return {Array} An array of nodes
699 getNodes : function(start, end){
700 var ns = this.all.elements;
702 end = !Ext.isDefined(end) ? Math.max(ns.length - 1, 0) : end;
705 for(i = start; i <= end && ns[i]; i++){
709 for(i = start; i >= end && ns[i]; i--){
716 <div id="method-Ext.DataView-indexOf"></div>/**
717 * Finds the index of the passed node.
718 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
719 * @return {Number} The index of the node or -1
721 indexOf : function(node){
722 node = this.getNode(node);
723 if(Ext.isNumber(node.viewIndex)){
724 return node.viewIndex;
726 return this.all.indexOf(node);
730 onBeforeLoad : function(){
731 if(this.loadingText){
732 this.clearSelections(false, true);
733 this.getTemplateTarget().update('<div class="loading-indicator">'+this.loadingText+'</div>');
738 onDestroy : function(){
739 Ext.DataView.superclass.onDestroy.call(this);
740 this.bindStore(null);
744 <div id="method-Ext.DataView-setStore"></div>/**
745 * Changes the data store bound to this view and refreshes it. (deprecated in favor of bindStore)
746 * @param {Store} store The store to bind to this view
748 Ext.DataView.prototype.setStore = Ext.DataView.prototype.bindStore;
750 Ext.reg('dataview', Ext.DataView);</pre>
\r