1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-view.AbstractView'>/**
2 </span> * @class Ext.view.AbstractView
3 * @extends Ext.Component
4 * This is an abstract superclass and should not be used directly. Please see {@link Ext.view.View}.
6 Ext.define('Ext.view.AbstractView', {
7 extend: 'Ext.Component',
8 alternateClassName: 'Ext.view.AbstractView',
11 'Ext.data.StoreManager',
12 'Ext.CompositeElementLite',
14 'Ext.selection.DataViewModel'
18 getRecord: function(node) {
19 return this.getBoundView(node).getRecord(node);
22 getBoundView: function(node) {
23 return Ext.getCmp(node.boundView);
27 <span id='Ext-view.AbstractView-cfg-tpl'> /**
28 </span> * @cfg {String/Array/Ext.XTemplate} tpl
30 * The HTML fragment or an array of fragments that will make up the template used by this DataView. This should
31 * be specified in the same format expected by the constructor of {@link Ext.XTemplate}.
33 <span id='Ext-view.AbstractView-cfg-store'> /**
34 </span> * @cfg {Ext.data.Store} store
36 * The {@link Ext.data.Store} to bind this DataView to.
39 <span id='Ext-view.AbstractView-cfg-itemSelector'> /**
40 </span> * @cfg {String} itemSelector
42 * <b>This is a required setting</b>. A simple CSS selector (e.g. <tt>div.some-class</tt> or
43 * <tt>span:first-child</tt>) that will be used to determine what nodes this DataView will be
44 * working with. The itemSelector is used to map DOM nodes to records. As such, there should
45 * only be one root level element that matches the selector for each record.
48 <span id='Ext-view.AbstractView-cfg-itemCls'> /**
49 </span> * @cfg {String} itemCls
50 * Specifies the class to be assigned to each element in the view when used in conjunction with the
51 * {@link #itemTpl} configuration.
53 itemCls: Ext.baseCSSPrefix + 'dataview-item',
55 <span id='Ext-view.AbstractView-cfg-itemTpl'> /**
56 </span> * @cfg {String/Array/Ext.XTemplate} itemTpl
57 * The inner portion of the item template to be rendered. Follows an XTemplate
58 * structure and will be placed inside of a tpl.
61 <span id='Ext-view.AbstractView-cfg-overItemCls'> /**
62 </span> * @cfg {String} overItemCls
63 * A CSS class to apply to each item in the view on mouseover (defaults to undefined).
64 * Ensure {@link #trackOver} is set to `true` to make use of this.
67 <span id='Ext-view.AbstractView-cfg-loadingText'> /**
68 </span> * @cfg {String} loadingText
69 * A string to display during data load operations (defaults to undefined). If specified, this text will be
70 * displayed in a loading div and the view's contents will be cleared while loading, otherwise the view's
71 * contents will continue to display normally until the new data is loaded and the contents are replaced.
73 loadingText: 'Loading...',
75 <span id='Ext-view.AbstractView-cfg-loadingCls'> /**
76 </span> * @cfg {String} loadingCls
77 * The CSS class to apply to the loading message element (defaults to Ext.LoadMask.prototype.msgCls "x-mask-loading")
80 <span id='Ext-view.AbstractView-cfg-loadingUseMsg'> /**
81 </span> * @cfg {Boolean} loadingUseMsg
82 * Whether or not to use the loading message.
88 <span id='Ext-view.AbstractView-cfg-loadingHeight'> /**
89 </span> * @cfg {Number} loadingHeight
90 * If specified, gives an explicit height for the data view when it is showing the {@link #loadingText},
91 * if that is specified. This is useful to prevent the view's height from collapsing to zero when the
92 * loading mask is applied and there are no other contents in the data view. Defaults to undefined.
95 <span id='Ext-view.AbstractView-cfg-selectedItemCls'> /**
96 </span> * @cfg {String} selectedItemCls
97 * A CSS class to apply to each selected item in the view (defaults to 'x-view-selected').
99 selectedItemCls: Ext.baseCSSPrefix + 'item-selected',
101 <span id='Ext-view.AbstractView-cfg-emptyText'> /**
102 </span> * @cfg {String} emptyText
103 * The text to display in the view when there is no data to display (defaults to '').
104 * Note that when using local data the emptyText will not be displayed unless you set
105 * the {@link #deferEmptyText} option to false.
107 emptyText: "",
109 <span id='Ext-view.AbstractView-cfg-deferEmptyText'> /**
110 </span> * @cfg {Boolean} deferEmptyText True to defer emptyText being applied until the store's first load
112 deferEmptyText: true,
114 <span id='Ext-view.AbstractView-cfg-trackOver'> /**
115 </span> * @cfg {Boolean} trackOver True to enable mouseenter and mouseleave events
119 <span id='Ext-view.AbstractView-cfg-blockRefresh'> /**
120 </span> * @cfg {Boolean} blockRefresh Set this to true to ignore datachanged events on the bound store. This is useful if
121 * you wish to provide custom transition animations via a plugin (defaults to false)
125 <span id='Ext-view.AbstractView-cfg-disableSelection'> /**
126 </span> * @cfg {Boolean} disableSelection <p><tt>true</tt> to disable selection within the DataView. Defaults to <tt>false</tt>.
127 * This configuration will lock the selection model that the DataView uses.</p>
134 triggerEvent: 'itemclick',
135 triggerCtEvent: 'containerclick',
137 addCmpEvents: function() {
142 initComponent : function(){
144 isDef = Ext.isDefined,
145 itemTpl = me.itemTpl,
149 if (Ext.isArray(itemTpl)) {
151 itemTpl = itemTpl.join('');
152 } else if (Ext.isObject(itemTpl)) {
154 memberFn = Ext.apply(memberFn, itemTpl.initialConfig);
155 itemTpl = itemTpl.html;
158 if (!me.itemSelector) {
159 me.itemSelector = '.' + me.itemCls;
162 itemTpl = Ext.String.format('<tpl for="."><div class="{0}">{1}</div></tpl>', me.itemCls, itemTpl);
163 me.tpl = Ext.create('Ext.XTemplate', itemTpl, memberFn);
167 if (!isDef(me.tpl) || !isDef(me.itemSelector)) {
169 sourceClass: 'Ext.view.View',
171 itemSelector: me.itemSelector,
172 msg: "DataView requires both tpl and itemSelector configurations to be defined."
178 if(Ext.isString(me.tpl) || Ext.isArray(me.tpl)){
179 me.tpl = Ext.create('Ext.XTemplate', me.tpl);
183 // backwards compat alias for overClass/selectedClass
184 // TODO: Consider support for overCls generation Ext.Component config
185 if (isDef(me.overCls) || isDef(me.overClass)) {
186 if (Ext.isDefined(Ext.global.console)) {
187 Ext.global.console.warn('Ext.view.View: Using the deprecated overCls or overClass configuration. Use overItemCls instead.');
189 me.overItemCls = me.overCls || me.overClass;
194 if (me.overItemCls) {
198 if (isDef(me.selectedCls) || isDef(me.selectedClass)) {
199 if (Ext.isDefined(Ext.global.console)) {
200 Ext.global.console.warn('Ext.view.View: Using the deprecated selectedCls or selectedClass configuration. Use selectedItemCls instead.');
202 me.selectedItemCls = me.selectedCls || me.selectedClass;
203 delete me.selectedCls;
204 delete me.selectedClass;
209 <span id='Ext-view.AbstractView-event-beforerefresh'> /**
210 </span> * @event beforerefresh
211 * Fires before the view is refreshed
212 * @param {Ext.view.View} this The DataView object
215 <span id='Ext-view.AbstractView-event-refresh'> /**
216 </span> * @event refresh
217 * Fires when the view is refreshed
218 * @param {Ext.view.View} this The DataView object
221 <span id='Ext-view.AbstractView-event-itemupdate'> /**
222 </span> * @event itemupdate
223 * Fires when the node associated with an individual record is updated
224 * @param {Ext.data.Model} record The model instance
225 * @param {Number} index The index of the record/node
226 * @param {HTMLElement} node The node that has just been updated
229 <span id='Ext-view.AbstractView-event-itemadd'> /**
230 </span> * @event itemadd
231 * Fires when the nodes associated with an recordset have been added to the underlying store
232 * @param {Array[Ext.data.Model]} records The model instance
233 * @param {Number} index The index at which the set of record/nodes starts
234 * @param {Array[HTMLElement]} node The node that has just been updated
237 <span id='Ext-view.AbstractView-event-itemremove'> /**
238 </span> * @event itemremove
239 * Fires when the node associated with an individual record is removed
240 * @param {Ext.data.Model} record The model instance
241 * @param {Number} index The index of the record/node
249 me.store = Ext.data.StoreManager.lookup(me.store);
251 me.all = new Ext.CompositeElementLite();
252 me.getSelectionModel().bindComponent(me);
255 onRender: function() {
257 loadingText = me.loadingText,
258 loadingHeight = me.loadingHeight,
261 me.callParent(arguments);
264 // Attach the LoadMask to a *Component* so that it can be sensitive to resizing during long loads.
265 // If this DataView is floating, then mask this DataView.
266 // Otherwise, mask its owning Container (or this, if there *is* no owning Container).
267 // LoadMask captures the element upon render.
268 me.loadMask = Ext.create('Ext.LoadMask', me.floating ? me : me.ownerCt || me, {
270 msgCls: me.loadingCls,
271 useMsg: me.loadingUseMsg,
273 beforeshow: function() {
274 me.getTargetEl().update('');
275 me.getSelectionModel().deselectAll();
278 me.setCalculatedSize(undef, loadingHeight);
283 me.setHeight(me.height);
291 getSelectionModel: function(){
299 if (me.simpleSelect) {
301 } else if (me.multiSelect) {
305 Ext.applyIf(me.selModel, {
306 allowDeselect: me.allowDeselect,
310 if (!me.selModel.events) {
311 me.selModel = Ext.create('Ext.selection.DataViewModel', me.selModel);
314 if (!me.selModel.hasRelaySetup) {
315 me.relayEvents(me.selModel, ['selectionchange', 'beforeselect', 'select', 'deselect']);
316 me.selModel.hasRelaySetup = true;
319 // lock the selection model if user
320 // has disabled selection
321 if (me.disableSelection) {
322 me.selModel.locked = true;
328 <span id='Ext-view.AbstractView-method-refresh'> /**
329 </span> * Refreshes the view by reloading the data from the store and re-rendering the template.
331 refresh: function() {
340 me.fireEvent('beforerefresh', me);
341 el = me.getTargetEl();
342 records = me.store.getRange();
345 if (records.length < 1) {
346 if (!me.deferEmptyText || me.hasSkippedEmptyText) {
347 el.update(me.emptyText);
351 me.tpl.overwrite(el, me.collectData(records, 0));
352 me.all.fill(Ext.query(me.getItemSelector(), el.dom));
356 me.selModel.refresh();
357 me.hasSkippedEmptyText = true;
358 me.fireEvent('refresh', me);
361 <span id='Ext-view.AbstractView-method-prepareData'> /**
362 </span> * Function which can be overridden to provide custom formatting for each Record that is used by this
363 * DataView's {@link #tpl template} to render each node.
364 * @param {Array/Object} data The raw data object that was used to create the Record.
365 * @param {Number} recordIndex the index number of the Record being prepared for rendering.
366 * @param {Record} record The Record being prepared for rendering.
367 * @return {Array/Object} The formatted data in a format expected by the internal {@link #tpl template}'s overwrite() method.
368 * (either an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'}))
370 prepareData: function(data, index, record) {
372 Ext.apply(data, record.getAssociatedData());
377 <span id='Ext-view.AbstractView-method-collectData'> /**
378 </span> * <p>Function which can be overridden which returns the data object passed to this
379 * DataView's {@link #tpl template} to render the whole DataView.</p>
380 * <p>This is usually an Array of data objects, each element of which is processed by an
381 * {@link Ext.XTemplate XTemplate} which uses <tt>'&lt;tpl for="."&gt;'</tt> to iterate over its supplied
382 * data object as an Array. However, <i>named</i> properties may be placed into the data object to
383 * provide non-repeating data such as headings, totals etc.</p>
384 * @param {Array} records An Array of {@link Ext.data.Model}s to be rendered into the DataView.
385 * @param {Number} startIndex the index number of the Record being prepared for rendering.
386 * @return {Array} An Array of data objects to be processed by a repeating XTemplate. May also
387 * contain <i>named</i> properties.
389 collectData : function(records, startIndex){
392 len = records.length;
394 for(; i < len; i++){
395 r[r.length] = this.prepareData(records[i].data, startIndex + i, records[i]);
402 bufferRender : function(records, index){
403 var div = document.createElement('div');
404 this.tpl.overwrite(div, this.collectData(records, index));
405 return Ext.query(this.getItemSelector(), div);
409 onUpdate : function(ds, record){
411 index = me.store.indexOf(record),
416 original = me.all.elements[index];
417 node = me.bufferRender([record], index)[0];
419 me.all.replaceElement(index, node, true);
420 me.updateIndexes(index, index);
422 // Maintain selection after update
423 // TODO: Move to approriate event handler.
424 me.selModel.refresh();
425 me.fireEvent('itemupdate', record, index, node);
431 onAdd : function(ds, records, index) {
435 if (me.all.getCount() === 0) {
440 nodes = me.bufferRender(records, index);
441 me.doAdd(nodes, records, index);
443 me.selModel.refresh();
444 me.updateIndexes(index);
445 me.fireEvent('itemadd', records, index, nodes);
448 doAdd: function(nodes, records, index) {
449 var n, a = this.all.elements;
450 if (index < this.all.getCount()) {
451 n = this.all.item(index).insertSibling(nodes, 'before', true);
452 a.splice.apply(a, [index, 0].concat(nodes));
455 n = this.all.last().insertSibling(nodes, 'after', true);
456 a.push.apply(a, nodes);
461 onRemove : function(ds, record, index) {
464 me.doRemove(record, index);
465 me.updateIndexes(index);
466 if (me.store.getCount() === 0){
469 me.fireEvent('itemremove', record, index);
472 doRemove: function(record, index) {
473 this.all.removeElement(index, true);
476 <span id='Ext-view.AbstractView-method-refreshNode'> /**
477 </span> * Refreshes an individual node's data from the store.
478 * @param {Number} index The item's data index in the store
480 refreshNode : function(index){
481 this.onUpdate(this.store, this.store.getAt(index));
485 updateIndexes : function(startIndex, endIndex) {
486 var ns = this.all.elements,
487 records = this.store.getRange();
488 startIndex = startIndex || 0;
489 endIndex = endIndex || ((endIndex === 0) ? 0 : (ns.length - 1));
490 for(var i = startIndex; i <= endIndex; i++){
492 ns[i].viewRecordId = records[i].internalId;
493 if (!ns[i].boundView) {
494 ns[i].boundView = this.id;
499 <span id='Ext-view.AbstractView-method-getStore'> /**
500 </span> * Returns the store associated with this DataView.
501 * @return {Ext.data.Store} The store
503 getStore : function(){
507 <span id='Ext-view.AbstractView-method-bindStore'> /**
508 </span> * Changes the data store bound to this view and refreshes it.
509 * @param {Store} store The store to bind to this view
511 bindStore : function(store, initial) {
514 if (!initial && me.store) {
515 if (store !== me.store && me.store.autoDestroy) {
521 datachanged: me.onDataChanged,
530 me.loadMask.bindStore(null);
536 store = Ext.data.StoreManager.lookup(store);
539 datachanged: me.onDataChanged,
546 me.loadMask.bindStore(store);
551 // Bind the store to our selection model
552 me.getSelectionModel().bind(store);
559 <span id='Ext-view.AbstractView-method-onDataChanged'> /**
561 * Calls this.refresh if this.blockRefresh is not true
563 onDataChanged: function() {
564 if (this.blockRefresh !== true) {
565 this.refresh.apply(this, arguments);
569 <span id='Ext-view.AbstractView-method-findItemByChild'> /**
570 </span> * Returns the template node the passed child belongs to, or null if it doesn't belong to one.
571 * @param {HTMLElement} node
572 * @return {HTMLElement} The template node
574 findItemByChild: function(node){
575 return Ext.fly(node).findParent(this.getItemSelector(), this.getTargetEl());
578 <span id='Ext-view.AbstractView-method-findTargetByEvent'> /**
579 </span> * Returns the template node by the Ext.EventObject or null if it is not found.
580 * @param {Ext.EventObject} e
582 findTargetByEvent: function(e) {
583 return e.getTarget(this.getItemSelector(), this.getTargetEl());
587 <span id='Ext-view.AbstractView-method-getSelectedNodes'> /**
588 </span> * Gets the currently selected nodes.
589 * @return {Array} An array of HTMLElements
591 getSelectedNodes: function(){
593 records = this.selModel.getSelection(),
597 for (; i < ln; i++) {
598 nodes.push(this.getNode(records[i]));
604 <span id='Ext-view.AbstractView-method-getRecords'> /**
605 </span> * Gets an array of the records from an array of nodes
606 * @param {Array} nodes The nodes to evaluate
607 * @return {Array} records The {@link Ext.data.Model} objects
609 getRecords: function(nodes) {
613 data = this.store.data;
615 for (; i < len; i++) {
616 records[records.length] = data.getByKey(nodes[i].viewRecordId);
622 <span id='Ext-view.AbstractView-method-getRecord'> /**
623 </span> * Gets a record from a node
624 * @param {Element/HTMLElement} node The node to evaluate
626 * @return {Record} record The {@link Ext.data.Model} object
628 getRecord: function(node){
629 return this.store.data.getByKey(Ext.getDom(node).viewRecordId);
633 <span id='Ext-view.AbstractView-method-isSelected'> /**
634 </span> * Returns true if the passed node is selected, else false.
635 * @param {HTMLElement/Number/Ext.data.Model} node The node, node index or record to check
636 * @return {Boolean} True if selected, else false
638 isSelected : function(node) {
639 // TODO: El/Idx/Record
640 var r = this.getRecord(node);
641 return this.selModel.isSelected(r);
644 <span id='Ext-view.AbstractView-method-select'> /**
645 </span> * Selects a record instance by record instance or index.
646 * @param {Ext.data.Model/Index} records An array of records or an index
647 * @param {Boolean} keepExisting
648 * @param {Boolean} suppressEvent Set to false to not fire a select event
650 select: function(records, keepExisting, suppressEvent) {
651 this.selModel.select(records, keepExisting, suppressEvent);
654 <span id='Ext-view.AbstractView-method-deselect'> /**
655 </span> * Deselects a record instance by record instance or index.
656 * @param {Ext.data.Model/Index} records An array of records or an index
657 * @param {Boolean} suppressEvent Set to false to not fire a deselect event
659 deselect: function(records, suppressEvent) {
660 this.selModel.deselect(records, suppressEvent);
663 <span id='Ext-view.AbstractView-method-getNode'> /**
664 </span> * Gets a template node.
665 * @param {HTMLElement/String/Number/Ext.data.Model} nodeInfo An HTMLElement template node, index of a template node,
666 * the id of a template node or the record associated with the node.
667 * @return {HTMLElement} The node or null if it wasn't found
669 getNode : function(nodeInfo) {
670 if (Ext.isString(nodeInfo)) {
671 return document.getElementById(nodeInfo);
672 } else if (Ext.isNumber(nodeInfo)) {
673 return this.all.elements[nodeInfo];
674 } else if (nodeInfo instanceof Ext.data.Model) {
675 return this.getNodeByRecord(nodeInfo);
680 <span id='Ext-view.AbstractView-method-getNodeByRecord'> /**
683 getNodeByRecord: function(record) {
684 var ns = this.all.elements,
688 for (; i < ln; i++) {
689 if (ns[i].viewRecordId === record.internalId) {
697 <span id='Ext-view.AbstractView-method-getNodes'> /**
698 </span> * Gets a range nodes.
699 * @param {Number} start (optional) The index of the first node in the range
700 * @param {Number} end (optional) The index of the last node in the range
701 * @return {Array} An array of nodes
703 getNodes: function(start, end) {
704 var ns = this.all.elements,
709 end = !Ext.isDefined(end) ? Math.max(ns.length - 1, 0) : end;
710 if (start <= end) {
711 for (i = start; i <= end && ns[i]; i++) {
715 for (i = start; i >= end && ns[i]; i--) {
722 <span id='Ext-view.AbstractView-method-indexOf'> /**
723 </span> * Finds the index of the passed node.
724 * @param {HTMLElement/String/Number/Record} nodeInfo An HTMLElement template node, index of a template node, the id of a template node
725 * or a record associated with a 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);
736 onDestroy : function() {
742 me.selModel.destroy();
745 // invoked by the selection model to maintain visual UI cues
746 onItemSelect: function(record) {
747 var node = this.getNode(record);
748 Ext.fly(node).addCls(this.selectedItemCls);
751 // invoked by the selection model to maintain visual UI cues
752 onItemDeselect: function(record) {
753 var node = this.getNode(record);
754 Ext.fly(node).removeCls(this.selectedItemCls);
757 getItemSelector: function() {
758 return this.itemSelector;
761 // all of this information is available directly
762 // from the SelectionModel itself, the only added methods
763 // to DataView regarding selection will perform some transformation/lookup
764 // between HTMLElement/Nodes to records and vice versa.
765 Ext.deprecate('extjs', '4.0', function() {
766 Ext.view.AbstractView.override({
767 <span id='Ext-view.AbstractView-cfg-multiSelect'> /**
768 </span> * @cfg {Boolean} multiSelect
769 * True to allow selection of more than one item at a time, false to allow selection of only a single item
770 * at a time or no selection at all, depending on the value of {@link #singleSelect} (defaults to false).
772 <span id='Ext-view.AbstractView-cfg-singleSelect'> /**
773 </span> * @cfg {Boolean} singleSelect
774 * True to allow selection of exactly one item at a time, false to allow no selection at all (defaults to false).
775 * Note that if {@link #multiSelect} = true, this value will be ignored.
777 <span id='Ext-view.AbstractView-cfg-simpleSelect'> /**
778 </span> * @cfg {Boolean} simpleSelect
779 * True to enable multiselection by clicking on multiple items without requiring the user to hold Shift or Ctrl,
780 * false to force the user to hold Ctrl or Shift to select more than on item (defaults to false).
783 <span id='Ext-view.AbstractView-method-getSelectionCount'> /**
784 </span> * Gets the number of selected nodes.
785 * @return {Number} The node count
787 getSelectionCount : function(){
788 console.warn("DataView: getSelectionCount will be removed, please interact with the Ext.selection.DataViewModel");
789 return this.selModel.getSelection().length;
792 <span id='Ext-view.AbstractView-method-getSelectedRecords'> /**
793 </span> * Gets an array of the selected records
794 * @return {Array} An array of {@link Ext.data.Model} objects
796 getSelectedRecords : function(){
797 console.warn("DataView: getSelectedRecords will be removed, please interact with the Ext.selection.DataViewModel");
798 return this.selModel.getSelection();
801 select: function(records, keepExisting, supressEvents) {
802 console.warn("DataView: select will be removed, please access select through a DataView's SelectionModel, ie: view.getSelectionModel().select()");
803 var sm = this.getSelectionModel();
804 return sm.select.apply(sm, arguments);
807 clearSelections: function() {
808 console.warn("DataView: clearSelections will be removed, please access deselectAll through DataView's SelectionModel, ie: view.getSelectionModel().deselectAll()");
809 var sm = this.getSelectionModel();
810 return sm.deselectAll();
815 </pre></pre></body></html>