4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-view-AbstractView'>/**
19 </span> * @class Ext.view.AbstractView
20 * @extends Ext.Component
21 * This is an abstract superclass and should not be used directly. Please see {@link Ext.view.View}.
23 Ext.define('Ext.view.AbstractView', {
24 extend: 'Ext.Component',
25 alternateClassName: 'Ext.view.AbstractView',
28 'Ext.data.StoreManager',
29 'Ext.CompositeElementLite',
31 'Ext.selection.DataViewModel'
35 getRecord: function(node) {
36 return this.getBoundView(node).getRecord(node);
39 getBoundView: function(node) {
40 return Ext.getCmp(node.boundView);
44 <span id='Ext-view-AbstractView-cfg-tpl'> /**
45 </span> * @cfg {String/Array/Ext.XTemplate} tpl
47 * The HTML fragment or an array of fragments that will make up the template used by this DataView. This should
48 * be specified in the same format expected by the constructor of {@link Ext.XTemplate}.
50 <span id='Ext-view-AbstractView-cfg-store'> /**
51 </span> * @cfg {Ext.data.Store} store
53 * The {@link Ext.data.Store} to bind this DataView to.
56 <span id='Ext-view-AbstractView-cfg-itemSelector'> /**
57 </span> * @cfg {String} itemSelector
59 * <b>This is a required setting</b>. A simple CSS selector (e.g. <tt>div.some-class</tt> or
60 * <tt>span:first-child</tt>) that will be used to determine what nodes this DataView will be
61 * working with. The itemSelector is used to map DOM nodes to records. As such, there should
62 * only be one root level element that matches the selector for each record.
65 <span id='Ext-view-AbstractView-cfg-itemCls'> /**
66 </span> * @cfg {String} itemCls
67 * Specifies the class to be assigned to each element in the view when used in conjunction with the
68 * {@link #itemTpl} configuration.
70 itemCls: Ext.baseCSSPrefix + 'dataview-item',
72 <span id='Ext-view-AbstractView-cfg-itemTpl'> /**
73 </span> * @cfg {String/Array/Ext.XTemplate} itemTpl
74 * The inner portion of the item template to be rendered. Follows an XTemplate
75 * structure and will be placed inside of a tpl.
78 <span id='Ext-view-AbstractView-cfg-overItemCls'> /**
79 </span> * @cfg {String} overItemCls
80 * A CSS class to apply to each item in the view on mouseover (defaults to undefined).
81 * Ensure {@link #trackOver} is set to `true` to make use of this.
84 <span id='Ext-view-AbstractView-cfg-loadingText'> /**
85 </span> * @cfg {String} loadingText
86 * A string to display during data load operations (defaults to undefined). If specified, this text will be
87 * displayed in a loading div and the view's contents will be cleared while loading, otherwise the view's
88 * contents will continue to display normally until the new data is loaded and the contents are replaced.
90 loadingText: 'Loading...',
92 <span id='Ext-view-AbstractView-cfg-loadingCls'> /**
93 </span> * @cfg {String} loadingCls
94 * The CSS class to apply to the loading message element (defaults to Ext.LoadMask.prototype.msgCls "x-mask-loading")
97 <span id='Ext-view-AbstractView-cfg-loadingUseMsg'> /**
98 </span> * @cfg {Boolean} loadingUseMsg
99 * Whether or not to use the loading message.
105 <span id='Ext-view-AbstractView-cfg-loadingHeight'> /**
106 </span> * @cfg {Number} loadingHeight
107 * If specified, gives an explicit height for the data view when it is showing the {@link #loadingText},
108 * if that is specified. This is useful to prevent the view's height from collapsing to zero when the
109 * loading mask is applied and there are no other contents in the data view. Defaults to undefined.
112 <span id='Ext-view-AbstractView-cfg-selectedItemCls'> /**
113 </span> * @cfg {String} selectedItemCls
114 * A CSS class to apply to each selected item in the view (defaults to 'x-view-selected').
116 selectedItemCls: Ext.baseCSSPrefix + 'item-selected',
118 <span id='Ext-view-AbstractView-cfg-emptyText'> /**
119 </span> * @cfg {String} emptyText
120 * The text to display in the view when there is no data to display (defaults to '').
121 * Note that when using local data the emptyText will not be displayed unless you set
122 * the {@link #deferEmptyText} option to false.
124 emptyText: "",
126 <span id='Ext-view-AbstractView-cfg-deferEmptyText'> /**
127 </span> * @cfg {Boolean} deferEmptyText True to defer emptyText being applied until the store's first load
129 deferEmptyText: true,
131 <span id='Ext-view-AbstractView-cfg-trackOver'> /**
132 </span> * @cfg {Boolean} trackOver True to enable mouseenter and mouseleave events
136 <span id='Ext-view-AbstractView-cfg-blockRefresh'> /**
137 </span> * @cfg {Boolean} blockRefresh Set this to true to ignore datachanged events on the bound store. This is useful if
138 * you wish to provide custom transition animations via a plugin (defaults to false)
142 <span id='Ext-view-AbstractView-cfg-disableSelection'> /**
143 </span> * @cfg {Boolean} disableSelection <p><tt>true</tt> to disable selection within the DataView. Defaults to <tt>false</tt>.
144 * This configuration will lock the selection model that the DataView uses.</p>
151 triggerEvent: 'itemclick',
152 triggerCtEvent: 'containerclick',
154 addCmpEvents: function() {
159 initComponent : function(){
161 isDef = Ext.isDefined,
162 itemTpl = me.itemTpl,
166 if (Ext.isArray(itemTpl)) {
168 itemTpl = itemTpl.join('');
169 } else if (Ext.isObject(itemTpl)) {
171 memberFn = Ext.apply(memberFn, itemTpl.initialConfig);
172 itemTpl = itemTpl.html;
175 if (!me.itemSelector) {
176 me.itemSelector = '.' + me.itemCls;
179 itemTpl = Ext.String.format('<tpl for="."><div class="{0}">{1}</div></tpl>', me.itemCls, itemTpl);
180 me.tpl = Ext.create('Ext.XTemplate', itemTpl, memberFn);
184 if (!isDef(me.tpl) || !isDef(me.itemSelector)) {
186 sourceClass: 'Ext.view.View',
188 itemSelector: me.itemSelector,
189 msg: "DataView requires both tpl and itemSelector configurations to be defined."
195 if(Ext.isString(me.tpl) || Ext.isArray(me.tpl)){
196 me.tpl = Ext.create('Ext.XTemplate', me.tpl);
200 // backwards compat alias for overClass/selectedClass
201 // TODO: Consider support for overCls generation Ext.Component config
202 if (isDef(me.overCls) || isDef(me.overClass)) {
203 if (Ext.isDefined(Ext.global.console)) {
204 Ext.global.console.warn('Ext.view.View: Using the deprecated overCls or overClass configuration. Use overItemCls instead.');
206 me.overItemCls = me.overCls || me.overClass;
211 if (me.overItemCls) {
215 if (isDef(me.selectedCls) || isDef(me.selectedClass)) {
216 if (Ext.isDefined(Ext.global.console)) {
217 Ext.global.console.warn('Ext.view.View: Using the deprecated selectedCls or selectedClass configuration. Use selectedItemCls instead.');
219 me.selectedItemCls = me.selectedCls || me.selectedClass;
220 delete me.selectedCls;
221 delete me.selectedClass;
226 <span id='Ext-view-AbstractView-event-beforerefresh'> /**
227 </span> * @event beforerefresh
228 * Fires before the view is refreshed
229 * @param {Ext.view.View} this The DataView object
232 <span id='Ext-view-AbstractView-event-refresh'> /**
233 </span> * @event refresh
234 * Fires when the view is refreshed
235 * @param {Ext.view.View} this The DataView object
238 <span id='Ext-view-AbstractView-event-itemupdate'> /**
239 </span> * @event itemupdate
240 * Fires when the node associated with an individual record is updated
241 * @param {Ext.data.Model} record The model instance
242 * @param {Number} index The index of the record/node
243 * @param {HTMLElement} node The node that has just been updated
246 <span id='Ext-view-AbstractView-event-itemadd'> /**
247 </span> * @event itemadd
248 * Fires when the nodes associated with an recordset have been added to the underlying store
249 * @param {Array[Ext.data.Model]} records The model instance
250 * @param {Number} index The index at which the set of record/nodes starts
251 * @param {Array[HTMLElement]} node The node that has just been updated
254 <span id='Ext-view-AbstractView-event-itemremove'> /**
255 </span> * @event itemremove
256 * Fires when the node associated with an individual record is removed
257 * @param {Ext.data.Model} record The model instance
258 * @param {Number} index The index of the record/node
266 me.store = Ext.data.StoreManager.lookup(me.store);
268 me.all = new Ext.CompositeElementLite();
269 me.getSelectionModel().bindComponent(me);
272 onRender: function() {
274 loadingText = me.loadingText,
275 loadingHeight = me.loadingHeight,
278 me.callParent(arguments);
281 // Attach the LoadMask to a *Component* so that it can be sensitive to resizing during long loads.
282 // If this DataView is floating, then mask this DataView.
283 // Otherwise, mask its owning Container (or this, if there *is* no owning Container).
284 // LoadMask captures the element upon render.
285 me.loadMask = Ext.create('Ext.LoadMask', me.floating ? me : me.ownerCt || me, {
287 msgCls: me.loadingCls,
288 useMsg: me.loadingUseMsg,
290 beforeshow: function() {
291 me.getTargetEl().update('');
292 me.getSelectionModel().deselectAll();
295 me.setCalculatedSize(undef, loadingHeight);
300 me.setHeight(me.height);
308 getSelectionModel: function(){
316 if (me.simpleSelect) {
318 } else if (me.multiSelect) {
322 Ext.applyIf(me.selModel, {
323 allowDeselect: me.allowDeselect,
327 if (!me.selModel.events) {
328 me.selModel = Ext.create('Ext.selection.DataViewModel', me.selModel);
331 if (!me.selModel.hasRelaySetup) {
332 me.relayEvents(me.selModel, ['selectionchange', 'beforeselect', 'select', 'deselect']);
333 me.selModel.hasRelaySetup = true;
336 // lock the selection model if user
337 // has disabled selection
338 if (me.disableSelection) {
339 me.selModel.locked = true;
345 <span id='Ext-view-AbstractView-method-refresh'> /**
346 </span> * Refreshes the view by reloading the data from the store and re-rendering the template.
348 refresh: function() {
357 me.fireEvent('beforerefresh', me);
358 el = me.getTargetEl();
359 records = me.store.getRange();
362 if (records.length < 1) {
363 if (!me.deferEmptyText || me.hasSkippedEmptyText) {
364 el.update(me.emptyText);
368 me.tpl.overwrite(el, me.collectData(records, 0));
369 me.all.fill(Ext.query(me.getItemSelector(), el.dom));
373 me.selModel.refresh();
374 me.hasSkippedEmptyText = true;
375 me.fireEvent('refresh', me);
378 <span id='Ext-view-AbstractView-method-prepareData'> /**
379 </span> * Function which can be overridden to provide custom formatting for each Record that is used by this
380 * DataView's {@link #tpl template} to render each node.
381 * @param {Array/Object} data The raw data object that was used to create the Record.
382 * @param {Number} recordIndex the index number of the Record being prepared for rendering.
383 * @param {Record} record The Record being prepared for rendering.
384 * @return {Array/Object} The formatted data in a format expected by the internal {@link #tpl template}'s overwrite() method.
385 * (either an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'}))
387 prepareData: function(data, index, record) {
389 Ext.apply(data, record.getAssociatedData());
394 <span id='Ext-view-AbstractView-method-collectData'> /**
395 </span> * <p>Function which can be overridden which returns the data object passed to this
396 * DataView's {@link #tpl template} to render the whole DataView.</p>
397 * <p>This is usually an Array of data objects, each element of which is processed by an
398 * {@link Ext.XTemplate XTemplate} which uses <tt>'&lt;tpl for="."&gt;'</tt> to iterate over its supplied
399 * data object as an Array. However, <i>named</i> properties may be placed into the data object to
400 * provide non-repeating data such as headings, totals etc.</p>
401 * @param {Array} records An Array of {@link Ext.data.Model}s to be rendered into the DataView.
402 * @param {Number} startIndex the index number of the Record being prepared for rendering.
403 * @return {Array} An Array of data objects to be processed by a repeating XTemplate. May also
404 * contain <i>named</i> properties.
406 collectData : function(records, startIndex){
409 len = records.length;
411 for(; i < len; i++){
412 r[r.length] = this.prepareData(records[i].data, startIndex + i, records[i]);
419 bufferRender : function(records, index){
420 var div = document.createElement('div');
421 this.tpl.overwrite(div, this.collectData(records, index));
422 return Ext.query(this.getItemSelector(), div);
426 onUpdate : function(ds, record){
428 index = me.store.indexOf(record),
433 original = me.all.elements[index];
434 node = me.bufferRender([record], index)[0];
436 me.all.replaceElement(index, node, true);
437 me.updateIndexes(index, index);
439 // Maintain selection after update
440 // TODO: Move to approriate event handler.
441 me.selModel.refresh();
442 me.fireEvent('itemupdate', record, index, node);
448 onAdd : function(ds, records, index) {
452 if (me.all.getCount() === 0) {
457 nodes = me.bufferRender(records, index);
458 me.doAdd(nodes, records, index);
460 me.selModel.refresh();
461 me.updateIndexes(index);
462 me.fireEvent('itemadd', records, index, nodes);
465 doAdd: function(nodes, records, index) {
466 var n, a = this.all.elements;
467 if (index < this.all.getCount()) {
468 n = this.all.item(index).insertSibling(nodes, 'before', true);
469 a.splice.apply(a, [index, 0].concat(nodes));
472 n = this.all.last().insertSibling(nodes, 'after', true);
473 a.push.apply(a, nodes);
478 onRemove : function(ds, record, index) {
481 me.doRemove(record, index);
482 me.updateIndexes(index);
483 if (me.store.getCount() === 0){
486 me.fireEvent('itemremove', record, index);
489 doRemove: function(record, index) {
490 this.all.removeElement(index, true);
493 <span id='Ext-view-AbstractView-method-refreshNode'> /**
494 </span> * Refreshes an individual node's data from the store.
495 * @param {Number} index The item's data index in the store
497 refreshNode : function(index){
498 this.onUpdate(this.store, this.store.getAt(index));
502 updateIndexes : function(startIndex, endIndex) {
503 var ns = this.all.elements,
504 records = this.store.getRange();
505 startIndex = startIndex || 0;
506 endIndex = endIndex || ((endIndex === 0) ? 0 : (ns.length - 1));
507 for(var i = startIndex; i <= endIndex; i++){
509 ns[i].viewRecordId = records[i].internalId;
510 if (!ns[i].boundView) {
511 ns[i].boundView = this.id;
516 <span id='Ext-view-AbstractView-method-getStore'> /**
517 </span> * Returns the store associated with this DataView.
518 * @return {Ext.data.Store} The store
520 getStore : function(){
524 <span id='Ext-view-AbstractView-method-bindStore'> /**
525 </span> * Changes the data store bound to this view and refreshes it.
526 * @param {Store} store The store to bind to this view
528 bindStore : function(store, initial) {
531 if (!initial && me.store) {
532 if (store !== me.store && me.store.autoDestroy) {
538 datachanged: me.onDataChanged,
547 me.loadMask.bindStore(null);
553 store = Ext.data.StoreManager.lookup(store);
556 datachanged: me.onDataChanged,
563 me.loadMask.bindStore(store);
568 // Bind the store to our selection model
569 me.getSelectionModel().bind(store);
576 <span id='Ext-view-AbstractView-method-onDataChanged'> /**
578 * Calls this.refresh if this.blockRefresh is not true
580 onDataChanged: function() {
581 if (this.blockRefresh !== true) {
582 this.refresh.apply(this, arguments);
586 <span id='Ext-view-AbstractView-method-findItemByChild'> /**
587 </span> * Returns the template node the passed child belongs to, or null if it doesn't belong to one.
588 * @param {HTMLElement} node
589 * @return {HTMLElement} The template node
591 findItemByChild: function(node){
592 return Ext.fly(node).findParent(this.getItemSelector(), this.getTargetEl());
595 <span id='Ext-view-AbstractView-method-findTargetByEvent'> /**
596 </span> * Returns the template node by the Ext.EventObject or null if it is not found.
597 * @param {Ext.EventObject} e
599 findTargetByEvent: function(e) {
600 return e.getTarget(this.getItemSelector(), this.getTargetEl());
604 <span id='Ext-view-AbstractView-method-getSelectedNodes'> /**
605 </span> * Gets the currently selected nodes.
606 * @return {Array} An array of HTMLElements
608 getSelectedNodes: function(){
610 records = this.selModel.getSelection(),
614 for (; i < ln; i++) {
615 nodes.push(this.getNode(records[i]));
621 <span id='Ext-view-AbstractView-method-getRecords'> /**
622 </span> * Gets an array of the records from an array of nodes
623 * @param {Array} nodes The nodes to evaluate
624 * @return {Array} records The {@link Ext.data.Model} objects
626 getRecords: function(nodes) {
630 data = this.store.data;
632 for (; i < len; i++) {
633 records[records.length] = data.getByKey(nodes[i].viewRecordId);
639 <span id='Ext-view-AbstractView-method-getRecord'> /**
640 </span> * Gets a record from a node
641 * @param {Element/HTMLElement} node The node to evaluate
643 * @return {Record} record The {@link Ext.data.Model} object
645 getRecord: function(node){
646 return this.store.data.getByKey(Ext.getDom(node).viewRecordId);
650 <span id='Ext-view-AbstractView-method-isSelected'> /**
651 </span> * Returns true if the passed node is selected, else false.
652 * @param {HTMLElement/Number/Ext.data.Model} node The node, node index or record to check
653 * @return {Boolean} True if selected, else false
655 isSelected : function(node) {
656 // TODO: El/Idx/Record
657 var r = this.getRecord(node);
658 return this.selModel.isSelected(r);
661 <span id='Ext-view-AbstractView-method-select'> /**
662 </span> * Selects a record instance by record instance or index.
663 * @param {Ext.data.Model/Index} records An array of records or an index
664 * @param {Boolean} keepExisting
665 * @param {Boolean} suppressEvent Set to false to not fire a select event
667 select: function(records, keepExisting, suppressEvent) {
668 this.selModel.select(records, keepExisting, suppressEvent);
671 <span id='Ext-view-AbstractView-method-deselect'> /**
672 </span> * Deselects a record instance by record instance or index.
673 * @param {Ext.data.Model/Index} records An array of records or an index
674 * @param {Boolean} suppressEvent Set to false to not fire a deselect event
676 deselect: function(records, suppressEvent) {
677 this.selModel.deselect(records, suppressEvent);
680 <span id='Ext-view-AbstractView-method-getNode'> /**
681 </span> * Gets a template node.
682 * @param {HTMLElement/String/Number/Ext.data.Model} nodeInfo An HTMLElement template node, index of a template node,
683 * the id of a template node or the record associated with the node.
684 * @return {HTMLElement} The node or null if it wasn't found
686 getNode : function(nodeInfo) {
687 if (Ext.isString(nodeInfo)) {
688 return document.getElementById(nodeInfo);
689 } else if (Ext.isNumber(nodeInfo)) {
690 return this.all.elements[nodeInfo];
691 } else if (nodeInfo instanceof Ext.data.Model) {
692 return this.getNodeByRecord(nodeInfo);
697 <span id='Ext-view-AbstractView-method-getNodeByRecord'> /**
700 getNodeByRecord: function(record) {
701 var ns = this.all.elements,
705 for (; i < ln; i++) {
706 if (ns[i].viewRecordId === record.internalId) {
714 <span id='Ext-view-AbstractView-method-getNodes'> /**
715 </span> * Gets a range nodes.
716 * @param {Number} start (optional) The index of the first node in the range
717 * @param {Number} end (optional) The index of the last node in the range
718 * @return {Array} An array of nodes
720 getNodes: function(start, end) {
721 var ns = this.all.elements,
726 end = !Ext.isDefined(end) ? Math.max(ns.length - 1, 0) : end;
727 if (start <= end) {
728 for (i = start; i <= end && ns[i]; i++) {
732 for (i = start; i >= end && ns[i]; i--) {
739 <span id='Ext-view-AbstractView-method-indexOf'> /**
740 </span> * Finds the index of the passed node.
741 * @param {HTMLElement/String/Number/Record} nodeInfo An HTMLElement template node, index of a template node, the id of a template node
742 * or a record associated with a node.
743 * @return {Number} The index of the node or -1
745 indexOf: function(node) {
746 node = this.getNode(node);
747 if (Ext.isNumber(node.viewIndex)) {
748 return node.viewIndex;
750 return this.all.indexOf(node);
753 onDestroy : function() {
759 me.selModel.destroy();
762 // invoked by the selection model to maintain visual UI cues
763 onItemSelect: function(record) {
764 var node = this.getNode(record);
765 Ext.fly(node).addCls(this.selectedItemCls);
768 // invoked by the selection model to maintain visual UI cues
769 onItemDeselect: function(record) {
770 var node = this.getNode(record);
771 Ext.fly(node).removeCls(this.selectedItemCls);
774 getItemSelector: function() {
775 return this.itemSelector;
778 // all of this information is available directly
779 // from the SelectionModel itself, the only added methods
780 // to DataView regarding selection will perform some transformation/lookup
781 // between HTMLElement/Nodes to records and vice versa.
782 Ext.deprecate('extjs', '4.0', function() {
783 Ext.view.AbstractView.override({
784 <span id='Ext-view-AbstractView-cfg-multiSelect'> /**
785 </span> * @cfg {Boolean} multiSelect
786 * True to allow selection of more than one item at a time, false to allow selection of only a single item
787 * at a time or no selection at all, depending on the value of {@link #singleSelect} (defaults to false).
789 <span id='Ext-view-AbstractView-cfg-singleSelect'> /**
790 </span> * @cfg {Boolean} singleSelect
791 * True to allow selection of exactly one item at a time, false to allow no selection at all (defaults to false).
792 * Note that if {@link #multiSelect} = true, this value will be ignored.
794 <span id='Ext-view-AbstractView-cfg-simpleSelect'> /**
795 </span> * @cfg {Boolean} simpleSelect
796 * True to enable multiselection by clicking on multiple items without requiring the user to hold Shift or Ctrl,
797 * false to force the user to hold Ctrl or Shift to select more than on item (defaults to false).
800 <span id='Ext-view-AbstractView-method-getSelectionCount'> /**
801 </span> * Gets the number of selected nodes.
802 * @return {Number} The node count
804 getSelectionCount : function(){
805 if (Ext.global.console) {
806 Ext.global.console.warn("DataView: getSelectionCount will be removed, please interact with the Ext.selection.DataViewModel");
808 return this.selModel.getSelection().length;
811 <span id='Ext-view-AbstractView-method-getSelectedRecords'> /**
812 </span> * Gets an array of the selected records
813 * @return {Array} An array of {@link Ext.data.Model} objects
815 getSelectedRecords : function(){
816 if (Ext.global.console) {
817 Ext.global.console.warn("DataView: getSelectedRecords will be removed, please interact with the Ext.selection.DataViewModel");
819 return this.selModel.getSelection();
822 select: function(records, keepExisting, supressEvents) {
823 if (Ext.global.console) {
824 Ext.global.console.warn("DataView: select will be removed, please access select through a DataView's SelectionModel, ie: view.getSelectionModel().select()");
826 var sm = this.getSelectionModel();
827 return sm.select.apply(sm, arguments);
830 clearSelections: function() {
831 if (Ext.global.console) {
832 Ext.global.console.warn("DataView: clearSelections will be removed, please access deselectAll through DataView's SelectionModel, ie: view.getSelectionModel().deselectAll()");
834 var sm = this.getSelectionModel();
835 return sm.deselectAll();