3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.DataView"></div>/**
11 * @extends Ext.BoxComponent
12 * A mechanism for displaying data using custom layout templates and formatting. DataView uses an {@link Ext.XTemplate}
13 * as its internal templating mechanism, and is bound to an {@link Ext.data.Store}
14 * so that as the data in the store changes the view is automatically updated to reflect the changes. The view also
15 * provides built-in behavior for many common events that can occur for its contained items including click, doubleclick,
16 * mouseover, mouseout, etc. as well as a built-in selection model. <b>In order to use these features, an {@link #itemSelector}
17 * config must be provided for the DataView to determine what nodes it will be working with.</b>
19 * <p>The example below binds a DataView to a {@link Ext.data.Store} and renders it into an {@link Ext.Panel}.</p>
21 var store = new Ext.data.JsonStore({
22 url: 'get-images.php',
26 {name:'size', type: 'float'},
27 {name:'lastmod', type:'date', dateFormat:'timestamp'}
32 var tpl = new Ext.XTemplate(
33 '<tpl for=".">',
34 '<div class="thumb-wrap" id="{name}">',
35 '<div class="thumb"><img src="{url}" title="{name}"></div>',
36 '<span class="x-editable">{shortName}</span></div>',
38 '<div class="x-clear"></div>'
41 var panel = new Ext.Panel({
48 title:'Simple DataView',
50 items: new Ext.DataView({
55 overClass:'x-view-over',
56 itemSelector:'div.thumb-wrap',
57 emptyText: 'No images to display'
60 panel.render(document.body);
63 * Create a new DataView
64 * @param {Object} config The config object
67 Ext.DataView = Ext.extend(Ext.BoxComponent, {
68 <div id="cfg-Ext.DataView-tpl"></div>/**
69 * @cfg {String/Array} tpl
70 * The HTML fragment or an array of fragments that will make up the template used by this DataView. This should
71 * be specified in the same format expected by the constructor of {@link Ext.XTemplate}.
73 <div id="cfg-Ext.DataView-store"></div>/**
74 * @cfg {Ext.data.Store} store
75 * The {@link Ext.data.Store} to bind this DataView to.
77 <div id="cfg-Ext.DataView-itemSelector"></div>/**
78 * @cfg {String} itemSelector
79 * <b>This is a required setting</b>. A simple CSS selector (e.g. <tt>div.some-class</tt> or
80 * <tt>span:first-child</tt>) that will be used to determine what nodes this DataView will be
83 <div id="cfg-Ext.DataView-multiSelect"></div>/**
84 * @cfg {Boolean} multiSelect
85 * True to allow selection of more than one item at a time, false to allow selection of only a single item
86 * at a time or no selection at all, depending on the value of {@link #singleSelect} (defaults to false).
88 <div id="cfg-Ext.DataView-singleSelect"></div>/**
89 * @cfg {Boolean} singleSelect
90 * True to allow selection of exactly one item at a time, false to allow no selection at all (defaults to false).
91 * Note that if {@link #multiSelect} = true, this value will be ignored.
93 <div id="cfg-Ext.DataView-simpleSelect"></div>/**
94 * @cfg {Boolean} simpleSelect
95 * True to enable multiselection by clicking on multiple items without requiring the user to hold Shift or Ctrl,
96 * false to force the user to hold Ctrl or Shift to select more than on item (defaults to false).
98 <div id="cfg-Ext.DataView-overClass"></div>/**
99 * @cfg {String} overClass
100 * A CSS class to apply to each item in the view on mouseover (defaults to undefined).
102 <div id="cfg-Ext.DataView-loadingText"></div>/**
103 * @cfg {String} loadingText
104 * A string to display during data load operations (defaults to undefined). If specified, this text will be
105 * displayed in a loading div and the view's contents will be cleared while loading, otherwise the view's
106 * contents will continue to display normally until the new data is loaded and the contents are replaced.
108 <div id="cfg-Ext.DataView-selectedClass"></div>/**
109 * @cfg {String} selectedClass
110 * A CSS class to apply to each selected item in the view (defaults to 'x-view-selected').
112 selectedClass : "x-view-selected",
113 <div id="cfg-Ext.DataView-emptyText"></div>/**
114 * @cfg {String} emptyText
115 * The text to display in the view when there is no data to display (defaults to '').
119 <div id="cfg-Ext.DataView-deferEmptyText"></div>/**
120 * @cfg {Boolean} deferEmptyText True to defer emptyText being applied until the store's first load
122 deferEmptyText: true,
123 <div id="cfg-Ext.DataView-trackOver"></div>/**
124 * @cfg {Boolean} trackOver True to enable mouseenter and mouseleave events
132 initComponent : function(){
133 Ext.DataView.superclass.initComponent.call(this);
134 if(Ext.isString(this.tpl) || Ext.isArray(this.tpl)){
135 this.tpl = new Ext.XTemplate(this.tpl);
139 <div id="event-Ext.DataView-beforeclick"></div>/**
141 * Fires before a click is processed. Returns false to cancel the default action.
142 * @param {Ext.DataView} this
143 * @param {Number} index The index of the target node
144 * @param {HTMLElement} node The target node
145 * @param {Ext.EventObject} e The raw event object
148 <div id="event-Ext.DataView-click"></div>/**
150 * Fires when a template node is clicked.
151 * @param {Ext.DataView} this
152 * @param {Number} index The index of the target node
153 * @param {HTMLElement} node The target node
154 * @param {Ext.EventObject} e The raw event object
157 <div id="event-Ext.DataView-mouseenter"></div>/**
159 * Fires when the mouse enters a template node. trackOver:true or an overClass must be set to enable this event.
160 * @param {Ext.DataView} this
161 * @param {Number} index The index of the target node
162 * @param {HTMLElement} node The target node
163 * @param {Ext.EventObject} e The raw event object
166 <div id="event-Ext.DataView-mouseleave"></div>/**
168 * Fires when the mouse leaves a template node. trackOver:true or an overClass must be set to enable this event.
169 * @param {Ext.DataView} this
170 * @param {Number} index The index of the target node
171 * @param {HTMLElement} node The target node
172 * @param {Ext.EventObject} e The raw event object
175 <div id="event-Ext.DataView-containerclick"></div>/**
176 * @event containerclick
177 * Fires when a click occurs and it is not on a template node.
178 * @param {Ext.DataView} this
179 * @param {Ext.EventObject} e The raw event object
182 <div id="event-Ext.DataView-dblclick"></div>/**
184 * Fires when a template node is double clicked.
185 * @param {Ext.DataView} this
186 * @param {Number} index The index of the target node
187 * @param {HTMLElement} node The target node
188 * @param {Ext.EventObject} e The raw event object
191 <div id="event-Ext.DataView-contextmenu"></div>/**
193 * Fires when a template node is right clicked.
194 * @param {Ext.DataView} this
195 * @param {Number} index The index of the target node
196 * @param {HTMLElement} node The target node
197 * @param {Ext.EventObject} e The raw event object
200 <div id="event-Ext.DataView-containercontextmenu"></div>/**
201 * @event containercontextmenu
202 * Fires when a right click occurs that is not on a template node.
203 * @param {Ext.DataView} this
204 * @param {Ext.EventObject} e The raw event object
206 "containercontextmenu",
207 <div id="event-Ext.DataView-selectionchange"></div>/**
208 * @event selectionchange
209 * Fires when the selected nodes change.
210 * @param {Ext.DataView} this
211 * @param {Array} selections Array of the selected nodes
215 <div id="event-Ext.DataView-beforeselect"></div>/**
216 * @event beforeselect
217 * Fires before a selection is made. If any handlers return false, the selection is cancelled.
218 * @param {Ext.DataView} this
219 * @param {HTMLElement} node The node to be selected
220 * @param {Array} selections Array of currently selected nodes
225 this.store = Ext.StoreMgr.lookup(this.store);
226 this.all = new Ext.CompositeElementLite();
227 this.selected = new Ext.CompositeElementLite();
231 afterRender : function(){
232 Ext.DataView.superclass.afterRender.call(this);
234 this.mon(this.getTemplateTarget(), {
235 "click": this.onClick,
236 "dblclick": this.onDblClick,
237 "contextmenu": this.onContextMenu,
241 if(this.overClass || this.trackOver){
242 this.mon(this.getTemplateTarget(), {
243 "mouseover": this.onMouseOver,
244 "mouseout": this.onMouseOut,
250 this.bindStore(this.store, true);
254 <div id="method-Ext.DataView-refresh"></div>/**
255 * Refreshes the view by reloading the data from the store and re-rendering the template.
257 refresh : function(){
258 this.clearSelections(false, true);
259 var el = this.getTemplateTarget();
261 var records = this.store.getRange();
262 if(records.length < 1){
263 if(!this.deferEmptyText || this.hasSkippedEmptyText){
264 el.update(this.emptyText);
268 this.tpl.overwrite(el, this.collectData(records, 0));
269 this.all.fill(Ext.query(this.itemSelector, el.dom));
270 this.updateIndexes(0);
272 this.hasSkippedEmptyText = true;
275 getTemplateTarget: function(){
279 <div id="method-Ext.DataView-prepareData"></div>/**
280 * Function which can be overridden to provide custom formatting for each Record that is used by this
281 * DataView's {@link #tpl template} to render each node.
282 * @param {Array/Object} data The raw data object that was used to create the Record.
283 * @param {Number} recordIndex the index number of the Record being prepared for rendering.
284 * @param {Record} record The Record being prepared for rendering.
285 * @return {Array/Object} The formatted data in a format expected by the internal {@link #tpl template}'s overwrite() method.
286 * (either an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'}))
288 prepareData : function(data){
292 <div id="method-Ext.DataView-collectData"></div>/**
293 * <p>Function which can be overridden which returns the data object passed to this
294 * DataView's {@link #tpl template} to render the whole DataView.</p>
295 * <p>This is usually an Array of data objects, each element of which is processed by an
296 * {@link Ext.XTemplate XTemplate} which uses <tt>'<tpl for=".">'</tt> to iterate over its supplied
297 * data object as an Array. However, <i>named</i> properties may be placed into the data object to
298 * provide non-repeating data such as headings, totals etc.</p>
299 * @param {Array} records An Array of {@link Ext.data.Record}s to be rendered into the DataView.
300 * @param {Number} startIndex the index number of the Record being prepared for rendering.
301 * @return {Array} An Array of data objects to be processed by a repeating XTemplate. May also
302 * contain <i>named</i> properties.
304 collectData : function(records, startIndex){
306 for(var i = 0, len = records.length; i < len; i++){
307 r[r.length] = this.prepareData(records[i].data, startIndex+i, records[i]);
313 bufferRender : function(records){
314 var div = document.createElement('div');
315 this.tpl.overwrite(div, this.collectData(records));
316 return Ext.query(this.itemSelector, div);
320 onUpdate : function(ds, record){
321 var index = this.store.indexOf(record);
323 var sel = this.isSelected(index);
324 var original = this.all.elements[index];
325 var node = this.bufferRender([record], index)[0];
327 this.all.replaceElement(index, node, true);
329 this.selected.replaceElement(original, node);
330 this.all.item(index).addClass(this.selectedClass);
332 this.updateIndexes(index, index);
337 onAdd : function(ds, records, index){
338 if(this.all.getCount() === 0){
342 var nodes = this.bufferRender(records, index), n, a = this.all.elements;
343 if(index < this.all.getCount()){
344 n = this.all.item(index).insertSibling(nodes, 'before', true);
345 a.splice.apply(a, [index, 0].concat(nodes));
347 n = this.all.last().insertSibling(nodes, 'after', true);
348 a.push.apply(a, nodes);
350 this.updateIndexes(index);
354 onRemove : function(ds, record, index){
355 this.deselect(index);
356 this.all.removeElement(index, true);
357 this.updateIndexes(index);
358 if (this.store.getCount() === 0){
363 <div id="method-Ext.DataView-refreshNode"></div>/**
364 * Refreshes an individual node's data from the store.
365 * @param {Number} index The item's data index in the store
367 refreshNode : function(index){
368 this.onUpdate(this.store, this.store.getAt(index));
372 updateIndexes : function(startIndex, endIndex){
373 var ns = this.all.elements;
374 startIndex = startIndex || 0;
375 endIndex = endIndex || ((endIndex === 0) ? 0 : (ns.length - 1));
376 for(var i = startIndex; i <= endIndex; i++){
381 <div id="method-Ext.DataView-getStore"></div>/**
382 * Returns the store associated with this DataView.
383 * @return {Ext.data.Store} The store
385 getStore : function(){
389 <div id="method-Ext.DataView-bindStore"></div>/**
390 * Changes the data store bound to this view and refreshes it.
391 * @param {Store} store The store to bind to this view
393 bindStore : function(store, initial){
394 if(!initial && this.store){
395 if(store !== this.store && this.store.autoDestroy){
396 this.store.destroy();
398 this.store.un("beforeload", this.onBeforeLoad, this);
399 this.store.un("datachanged", this.refresh, this);
400 this.store.un("add", this.onAdd, this);
401 this.store.un("remove", this.onRemove, this);
402 this.store.un("update", this.onUpdate, this);
403 this.store.un("clear", this.refresh, this);
410 store = Ext.StoreMgr.lookup(store);
413 beforeload: this.onBeforeLoad,
414 datachanged: this.refresh,
416 remove: this.onRemove,
417 update: this.onUpdate,
427 <div id="method-Ext.DataView-findItemFromChild"></div>/**
428 * Returns the template node the passed child belongs to, or null if it doesn't belong to one.
429 * @param {HTMLElement} node
430 * @return {HTMLElement} The template node
432 findItemFromChild : function(node){
433 return Ext.fly(node).findParent(this.itemSelector, this.getTemplateTarget());
437 onClick : function(e){
438 var item = e.getTarget(this.itemSelector, this.getTemplateTarget());
440 var index = this.indexOf(item);
441 if(this.onItemClick(item, index, e) !== false){
442 this.fireEvent("click", this, index, item, e);
445 if(this.fireEvent("containerclick", this, e) !== false){
446 this.onContainerClick(e);
451 onContainerClick : function(e){
452 this.clearSelections();
456 onContextMenu : function(e){
457 var item = e.getTarget(this.itemSelector, this.getTemplateTarget());
459 this.fireEvent("contextmenu", this, this.indexOf(item), item, e);
461 this.fireEvent("containercontextmenu", this, e);
466 onDblClick : function(e){
467 var item = e.getTarget(this.itemSelector, this.getTemplateTarget());
469 this.fireEvent("dblclick", this, this.indexOf(item), item, e);
474 onMouseOver : function(e){
475 var item = e.getTarget(this.itemSelector, this.getTemplateTarget());
476 if(item && item !== this.lastItem){
477 this.lastItem = item;
478 Ext.fly(item).addClass(this.overClass);
479 this.fireEvent("mouseenter", this, this.indexOf(item), item, e);
484 onMouseOut : function(e){
486 if(!e.within(this.lastItem, true, true)){
487 Ext.fly(this.lastItem).removeClass(this.overClass);
488 this.fireEvent("mouseleave", this, this.indexOf(this.lastItem), this.lastItem, e);
489 delete this.lastItem;
495 onItemClick : function(item, index, e){
496 if(this.fireEvent("beforeclick", this, index, item, e) === false){
499 if(this.multiSelect){
500 this.doMultiSelection(item, index, e);
502 }else if(this.singleSelect){
503 this.doSingleSelection(item, index, e);
510 doSingleSelection : function(item, index, e){
511 if(e.ctrlKey && this.isSelected(index)){
512 this.deselect(index);
514 this.select(index, false);
519 doMultiSelection : function(item, index, e){
520 if(e.shiftKey && this.last !== false){
521 var last = this.last;
522 this.selectRange(last, index, e.ctrlKey);
523 this.last = last; // reset the last
525 if((e.ctrlKey||this.simpleSelect) && this.isSelected(index)){
526 this.deselect(index);
528 this.select(index, e.ctrlKey || e.shiftKey || this.simpleSelect);
533 <div id="method-Ext.DataView-getSelectionCount"></div>/**
534 * Gets the number of selected nodes.
535 * @return {Number} The node count
537 getSelectionCount : function(){
538 return this.selected.getCount();
541 <div id="method-Ext.DataView-getSelectedNodes"></div>/**
542 * Gets the currently selected nodes.
543 * @return {Array} An array of HTMLElements
545 getSelectedNodes : function(){
546 return this.selected.elements;
549 <div id="method-Ext.DataView-getSelectedIndexes"></div>/**
550 * Gets the indexes of the selected nodes.
551 * @return {Array} An array of numeric indexes
553 getSelectedIndexes : function(){
554 var indexes = [], s = this.selected.elements;
555 for(var i = 0, len = s.length; i < len; i++){
556 indexes.push(s[i].viewIndex);
561 <div id="method-Ext.DataView-getSelectedRecords"></div>/**
562 * Gets an array of the selected records
563 * @return {Array} An array of {@link Ext.data.Record} objects
565 getSelectedRecords : function(){
566 var r = [], s = this.selected.elements;
567 for(var i = 0, len = s.length; i < len; i++){
568 r[r.length] = this.store.getAt(s[i].viewIndex);
573 <div id="method-Ext.DataView-getRecords"></div>/**
574 * Gets an array of the records from an array of nodes
575 * @param {Array} nodes The nodes to evaluate
576 * @return {Array} records The {@link Ext.data.Record} objects
578 getRecords : function(nodes){
579 var r = [], s = nodes;
580 for(var i = 0, len = s.length; i < len; i++){
581 r[r.length] = this.store.getAt(s[i].viewIndex);
586 <div id="method-Ext.DataView-getRecord"></div>/**
587 * Gets a record from a node
588 * @param {HTMLElement} node The node to evaluate
589 * @return {Record} record The {@link Ext.data.Record} object
591 getRecord : function(node){
592 return this.store.getAt(node.viewIndex);
595 <div id="method-Ext.DataView-clearSelections"></div>/**
596 * Clears all selections.
597 * @param {Boolean} suppressEvent (optional) True to skip firing of the selectionchange event
599 clearSelections : function(suppressEvent, skipUpdate){
600 if((this.multiSelect || this.singleSelect) && this.selected.getCount() > 0){
602 this.selected.removeClass(this.selectedClass);
604 this.selected.clear();
607 this.fireEvent("selectionchange", this, this.selected.elements);
612 <div id="method-Ext.DataView-isSelected"></div>/**
613 * Returns true if the passed node is selected, else false.
614 * @param {HTMLElement/Number} node The node or node index to check
615 * @return {Boolean} True if selected, else false
617 isSelected : function(node){
618 return this.selected.contains(this.getNode(node));
621 <div id="method-Ext.DataView-deselect"></div>/**
623 * @param {HTMLElement/Number} node The node to deselect
625 deselect : function(node){
626 if(this.isSelected(node)){
627 node = this.getNode(node);
628 this.selected.removeElement(node);
629 if(this.last == node.viewIndex){
632 Ext.fly(node).removeClass(this.selectedClass);
633 this.fireEvent("selectionchange", this, this.selected.elements);
637 <div id="method-Ext.DataView-select"></div>/**
638 * Selects a set of nodes.
639 * @param {Array/HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node,
640 * id of a template node or an array of any of those to select
641 * @param {Boolean} keepExisting (optional) true to keep existing selections
642 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange vent
644 select : function(nodeInfo, keepExisting, suppressEvent){
645 if(Ext.isArray(nodeInfo)){
647 this.clearSelections(true);
649 for(var i = 0, len = nodeInfo.length; i < len; i++){
650 this.select(nodeInfo[i], true, true);
653 this.fireEvent("selectionchange", this, this.selected.elements);
656 var node = this.getNode(nodeInfo);
658 this.clearSelections(true);
660 if(node && !this.isSelected(node)){
661 if(this.fireEvent("beforeselect", this, node, this.selected.elements) !== false){
662 Ext.fly(node).addClass(this.selectedClass);
663 this.selected.add(node);
664 this.last = node.viewIndex;
666 this.fireEvent("selectionchange", this, this.selected.elements);
673 <div id="method-Ext.DataView-selectRange"></div>/**
674 * Selects a range of nodes. All nodes between start and end are selected.
675 * @param {Number} start The index of the first node in the range
676 * @param {Number} end The index of the last node in the range
677 * @param {Boolean} keepExisting (optional) True to retain existing selections
679 selectRange : function(start, end, keepExisting){
681 this.clearSelections(true);
683 this.select(this.getNodes(start, end), true);
686 <div id="method-Ext.DataView-getNode"></div>/**
687 * Gets a template node.
688 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
689 * @return {HTMLElement} The node or null if it wasn't found
691 getNode : function(nodeInfo){
692 if(Ext.isString(nodeInfo)){
693 return document.getElementById(nodeInfo);
694 }else if(Ext.isNumber(nodeInfo)){
695 return this.all.elements[nodeInfo];
700 <div id="method-Ext.DataView-getNodes"></div>/**
701 * Gets a range nodes.
702 * @param {Number} start (optional) The index of the first node in the range
703 * @param {Number} end (optional) The index of the last node in the range
704 * @return {Array} An array of nodes
706 getNodes : function(start, end){
707 var ns = this.all.elements;
709 end = !Ext.isDefined(end) ? Math.max(ns.length - 1, 0) : end;
712 for(i = start; i <= end && ns[i]; i++){
716 for(i = start; i >= end && ns[i]; i--){
723 <div id="method-Ext.DataView-indexOf"></div>/**
724 * Finds the index of the passed node.
725 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
726 * @return {Number} The index of the node or -1
728 indexOf : function(node){
729 node = this.getNode(node);
730 if(Ext.isNumber(node.viewIndex)){
731 return node.viewIndex;
733 return this.all.indexOf(node);
737 onBeforeLoad : function(){
738 if(this.loadingText){
739 this.clearSelections(false, true);
740 this.getTemplateTarget().update('<div class="loading-indicator">'+this.loadingText+'</div>');
745 onDestroy : function(){
747 this.selected.clear();
748 Ext.DataView.superclass.onDestroy.call(this);
749 this.bindStore(null);
753 <div id="method-Ext.DataView-setStore"></div>/**
754 * Changes the data store bound to this view and refreshes it. (deprecated in favor of bindStore)
755 * @param {Store} store The store to bind to this view
757 Ext.DataView.prototype.setStore = Ext.DataView.prototype.bindStore;
759 Ext.reg('dataview', Ext.DataView);