AbstractView.html 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-view-AbstractView'>/**
  19. </span> * @class Ext.view.AbstractView
  20. * This is an abstract superclass and should not be used directly. Please see {@link Ext.view.View}.
  21. * @private
  22. */
  23. Ext.define('Ext.view.AbstractView', {
  24. extend: 'Ext.Component',
  25. requires: [
  26. 'Ext.LoadMask',
  27. 'Ext.data.StoreManager',
  28. 'Ext.CompositeElementLite',
  29. 'Ext.DomQuery',
  30. 'Ext.selection.DataViewModel'
  31. ],
  32. mixins: {
  33. bindable: 'Ext.util.Bindable'
  34. },
  35. inheritableStatics: {
  36. getRecord: function(node) {
  37. return this.getBoundView(node).getRecord(node);
  38. },
  39. getBoundView: function(node) {
  40. return Ext.getCmp(node.boundView);
  41. }
  42. },
  43. <span id='Ext-view-AbstractView-cfg-tpl'> /**
  44. </span> * @cfg {String/String[]/Ext.XTemplate} tpl (required)
  45. * The HTML fragment or an array of fragments that will make up the template used by this DataView. This should
  46. * be specified in the same format expected by the constructor of {@link Ext.XTemplate}.
  47. */
  48. <span id='Ext-view-AbstractView-cfg-store'> /**
  49. </span> * @cfg {Ext.data.Store} store (required)
  50. * The {@link Ext.data.Store} to bind this DataView to.
  51. */
  52. <span id='Ext-view-AbstractView-cfg-deferInitialRefresh'> /**
  53. </span> * @cfg {Boolean} deferInitialRefresh
  54. * &lt;p&gt;Defaults to &lt;code&gt;true&lt;/code&gt; to defer the initial refresh of the view.&lt;/p&gt;
  55. * &lt;p&gt;This allows the View to execute its render and initial layout more quickly because the process will not be encumbered
  56. * by the expensive update of the view structure.&lt;/p&gt;
  57. * &lt;p&gt;&lt;b&gt;Important: &lt;/b&gt;Be aware that this will mean that the View's item elements will not be available immediately upon render, so
  58. * &lt;i&gt;selection&lt;/i&gt; may not take place at render time. To access a View's item elements as soon as possible, use the {@link #viewready} event.
  59. * Or set &lt;code&gt;deferInitialrefresh&lt;/code&gt; to false, but this will be at the cost of slower rendering.&lt;/p&gt;
  60. */
  61. deferInitialRefresh: true,
  62. <span id='Ext-view-AbstractView-cfg-itemSelector'> /**
  63. </span> * @cfg {String} itemSelector (required)
  64. * &lt;b&gt;This is a required setting&lt;/b&gt;. A simple CSS selector (e.g. &lt;tt&gt;div.some-class&lt;/tt&gt; or
  65. * &lt;tt&gt;span:first-child&lt;/tt&gt;) that will be used to determine what nodes this DataView will be
  66. * working with. The itemSelector is used to map DOM nodes to records. As such, there should
  67. * only be one root level element that matches the selector for each record.
  68. */
  69. <span id='Ext-view-AbstractView-cfg-itemCls'> /**
  70. </span> * @cfg {String} itemCls
  71. * Specifies the class to be assigned to each element in the view when used in conjunction with the
  72. * {@link #itemTpl} configuration.
  73. */
  74. itemCls: Ext.baseCSSPrefix + 'dataview-item',
  75. <span id='Ext-view-AbstractView-cfg-itemTpl'> /**
  76. </span> * @cfg {String/String[]/Ext.XTemplate} itemTpl
  77. * The inner portion of the item template to be rendered. Follows an XTemplate
  78. * structure and will be placed inside of a tpl.
  79. */
  80. <span id='Ext-view-AbstractView-cfg-overItemCls'> /**
  81. </span> * @cfg {String} overItemCls
  82. * A CSS class to apply to each item in the view on mouseover.
  83. * Setting this will automatically set {@link #trackOver} to `true`.
  84. */
  85. //&lt;locale&gt;
  86. <span id='Ext-view-AbstractView-cfg-loadingText'> /**
  87. </span> * @cfg {String} loadingText
  88. * A string to display during data load operations. If specified, this text will be
  89. * displayed in a loading div and the view's contents will be cleared while loading, otherwise the view's
  90. * contents will continue to display normally until the new data is loaded and the contents are replaced.
  91. */
  92. loadingText: 'Loading...',
  93. //&lt;/locale&gt;
  94. <span id='Ext-view-AbstractView-cfg-loadMask'> /**
  95. </span> * @cfg {Boolean/Object} loadMask
  96. * False to disable a load mask from displaying while the view is loading. This can also be a
  97. * {@link Ext.LoadMask} configuration object.
  98. */
  99. loadMask: true,
  100. <span id='Ext-view-AbstractView-cfg-loadingCls'> /**
  101. </span> * @cfg {String} loadingCls
  102. * The CSS class to apply to the loading message element. Defaults to Ext.LoadMask.prototype.msgCls &quot;x-mask-loading&quot;.
  103. */
  104. <span id='Ext-view-AbstractView-cfg-loadingUseMsg'> /**
  105. </span> * @cfg {Boolean} loadingUseMsg
  106. * Whether or not to use the loading message.
  107. * @private
  108. */
  109. loadingUseMsg: true,
  110. <span id='Ext-view-AbstractView-cfg-loadingHeight'> /**
  111. </span> * @cfg {Number} loadingHeight
  112. * If specified, gives an explicit height for the data view when it is showing the {@link #loadingText},
  113. * if that is specified. This is useful to prevent the view's height from collapsing to zero when the
  114. * loading mask is applied and there are no other contents in the data view.
  115. */
  116. <span id='Ext-view-AbstractView-cfg-selectedItemCls'> /**
  117. </span> * @cfg {String} selectedItemCls
  118. * A CSS class to apply to each selected item in the view.
  119. */
  120. selectedItemCls: Ext.baseCSSPrefix + 'item-selected',
  121. //&lt;locale&gt;
  122. <span id='Ext-view-AbstractView-cfg-emptyText'> /**
  123. </span> * @cfg {String} emptyText
  124. * The text to display in the view when there is no data to display.
  125. * Note that when using local data the emptyText will not be displayed unless you set
  126. * the {@link #deferEmptyText} option to false.
  127. */
  128. emptyText: &quot;&quot;,
  129. //&lt;/locale&gt;
  130. <span id='Ext-view-AbstractView-cfg-deferEmptyText'> /**
  131. </span> * @cfg {Boolean} deferEmptyText
  132. * True to defer emptyText being applied until the store's first load.
  133. */
  134. deferEmptyText: true,
  135. <span id='Ext-view-AbstractView-cfg-trackOver'> /**
  136. </span> * @cfg {Boolean} trackOver
  137. * When `true` the {@link #overItemCls} will be applied to rows when hovered over.
  138. * This in return will also cause {@link Ext.view.View#highlightitem highlightitem} and
  139. * {@link Ext.view.View#unhighlightitem unhighlightitem} events to be fired.
  140. *
  141. * Enabled automatically when the {@link #overItemCls} config is set.
  142. */
  143. trackOver: false,
  144. <span id='Ext-view-AbstractView-cfg-blockRefresh'> /**
  145. </span> * @cfg {Boolean} blockRefresh
  146. * Set this to true to ignore refresh events on the bound store. This is useful if
  147. * you wish to provide custom transition animations via a plugin
  148. */
  149. blockRefresh: false,
  150. <span id='Ext-view-AbstractView-cfg-disableSelection'> /**
  151. </span> * @cfg {Boolean} disableSelection
  152. * True to disable selection within the DataView. This configuration will lock the selection model
  153. * that the DataView uses.
  154. */
  155. <span id='Ext-view-AbstractView-cfg-preserveScrollOnRefresh'> /**
  156. </span> * @cfg {Boolean} preserveScrollOnRefresh=false
  157. * True to preserve scroll position across refresh operations.
  158. */
  159. preserveScrollOnRefresh: false,
  160. //private
  161. last: false,
  162. triggerEvent: 'itemclick',
  163. triggerCtEvent: 'containerclick',
  164. addCmpEvents: function() {
  165. },
  166. // private
  167. initComponent : function(){
  168. var me = this,
  169. isDef = Ext.isDefined,
  170. itemTpl = me.itemTpl,
  171. memberFn = {};
  172. if (itemTpl) {
  173. if (Ext.isArray(itemTpl)) {
  174. // string array
  175. itemTpl = itemTpl.join('');
  176. } else if (Ext.isObject(itemTpl)) {
  177. // tpl instance
  178. memberFn = Ext.apply(memberFn, itemTpl.initialConfig);
  179. itemTpl = itemTpl.html;
  180. }
  181. if (!me.itemSelector) {
  182. me.itemSelector = '.' + me.itemCls;
  183. }
  184. itemTpl = Ext.String.format('&lt;tpl for=&quot;.&quot;&gt;&lt;div class=&quot;{0}&quot;&gt;{1}&lt;/div&gt;&lt;/tpl&gt;', me.itemCls, itemTpl);
  185. me.tpl = new Ext.XTemplate(itemTpl, memberFn);
  186. }
  187. //&lt;debug&gt;
  188. if (!isDef(me.tpl) || !isDef(me.itemSelector)) {
  189. Ext.Error.raise({
  190. sourceClass: 'Ext.view.View',
  191. tpl: me.tpl,
  192. itemSelector: me.itemSelector,
  193. msg: &quot;DataView requires both tpl and itemSelector configurations to be defined.&quot;
  194. });
  195. }
  196. //&lt;/debug&gt;
  197. me.callParent();
  198. if(Ext.isString(me.tpl) || Ext.isArray(me.tpl)){
  199. me.tpl = new Ext.XTemplate(me.tpl);
  200. }
  201. //&lt;debug&gt;
  202. // backwards compat alias for overClass/selectedClass
  203. // TODO: Consider support for overCls generation Ext.Component config
  204. if (isDef(me.overCls) || isDef(me.overClass)) {
  205. if (Ext.isDefined(Ext.global.console)) {
  206. Ext.global.console.warn('Ext.view.View: Using the deprecated overCls or overClass configuration. Use overItemCls instead.');
  207. }
  208. me.overItemCls = me.overCls || me.overClass;
  209. delete me.overCls;
  210. delete me.overClass;
  211. }
  212. if (me.overItemCls) {
  213. me.trackOver = true;
  214. }
  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.');
  218. }
  219. me.selectedItemCls = me.selectedCls || me.selectedClass;
  220. delete me.selectedCls;
  221. delete me.selectedClass;
  222. }
  223. //&lt;/debug&gt;
  224. me.addEvents(
  225. <span id='Ext-view-AbstractView-event-beforerefresh'> /**
  226. </span> * @event beforerefresh
  227. * Fires before the view is refreshed
  228. * @param {Ext.view.View} this The DataView object
  229. */
  230. 'beforerefresh',
  231. <span id='Ext-view-AbstractView-event-refresh'> /**
  232. </span> * @event refresh
  233. * Fires when the view is refreshed
  234. * @param {Ext.view.View} this The DataView object
  235. */
  236. 'refresh',
  237. <span id='Ext-view-AbstractView-event-viewready'> /**
  238. </span> * @event viewready
  239. * Fires when the View's item elements representing Store items has been rendered. If the {@link #deferInitialRefresh} flag
  240. * was set (and it is &lt;code&gt;true&lt;/code&gt; by default), this will be &lt;b&gt;after&lt;/b&gt; initial render, and no items will be available
  241. * for selection until this event fires.
  242. * @param {Ext.view.View} this
  243. */
  244. 'viewready',
  245. <span id='Ext-view-AbstractView-event-itemupdate'> /**
  246. </span> * @event itemupdate
  247. * Fires when the node associated with an individual record is updated
  248. * @param {Ext.data.Model} record The model instance
  249. * @param {Number} index The index of the record/node
  250. * @param {HTMLElement} node The node that has just been updated
  251. */
  252. 'itemupdate',
  253. <span id='Ext-view-AbstractView-event-itemadd'> /**
  254. </span> * @event itemadd
  255. * Fires when the nodes associated with an recordset have been added to the underlying store
  256. * @param {Ext.data.Model[]} records The model instance
  257. * @param {Number} index The index at which the set of record/nodes starts
  258. * @param {HTMLElement[]} node The node that has just been updated
  259. */
  260. 'itemadd',
  261. <span id='Ext-view-AbstractView-event-itemremove'> /**
  262. </span> * @event itemremove
  263. * Fires when the node associated with an individual record is removed
  264. * @param {Ext.data.Model} record The model instance
  265. * @param {Number} index The index of the record/node
  266. */
  267. 'itemremove'
  268. );
  269. me.addCmpEvents();
  270. // Look up the configured Store. If none configured, use the fieldless, empty Store defined in Ext.data.Store.
  271. me.store = Ext.data.StoreManager.lookup(me.store || 'ext-empty-store');
  272. me.bindStore(me.store, true);
  273. me.all = new Ext.CompositeElementLite();
  274. // We track the scroll position
  275. me.scrollState = {
  276. top: 0,
  277. left: 0
  278. };
  279. me.on({
  280. scroll: me.onViewScroll,
  281. element: 'el',
  282. scope: me
  283. });
  284. },
  285. onRender: function() {
  286. var me = this,
  287. mask = me.loadMask,
  288. cfg = {
  289. msg: me.loadingText,
  290. msgCls: me.loadingCls,
  291. useMsg: me.loadingUseMsg,
  292. // The store gets bound in initComponent, so while
  293. // rendering let's push on the store
  294. store: me.getMaskStore()
  295. };
  296. me.callParent(arguments);
  297. if (mask) {
  298. // either a config object
  299. if (Ext.isObject(mask)) {
  300. cfg = Ext.apply(cfg, mask);
  301. }
  302. // Attach the LoadMask to a *Component* so that it can be sensitive to resizing during long loads.
  303. // If this DataView is floating, then mask this DataView.
  304. // Otherwise, mask its owning Container (or this, if there *is* no owning Container).
  305. // LoadMask captures the element upon render.
  306. me.loadMask = new Ext.LoadMask(me, cfg);
  307. me.loadMask.on({
  308. scope: me,
  309. beforeshow: me.onMaskBeforeShow,
  310. hide: me.onMaskHide
  311. });
  312. }
  313. },
  314. finishRender: function(){
  315. var me = this;
  316. me.callParent(arguments);
  317. // Kick off the refresh before layouts are resumed after the render
  318. // completes, but after afterrender is fired on the view
  319. if (!me.up('[collapsed],[hidden]')) {
  320. me.doFirstRefresh(me.store);
  321. }
  322. },
  323. onBoxReady: function() {
  324. var me = this;
  325. me.callParent(arguments);
  326. // If the refresh was not kicked off on render due to a collapsed or hidden ancestor,
  327. // kick it off as soon as we get layed out
  328. if (!me.firstRefreshDone) {
  329. me.doFirstRefresh(me.store);
  330. }
  331. },
  332. getMaskStore: function(){
  333. return this.store;
  334. },
  335. onMaskBeforeShow: function(){
  336. var me = this,
  337. loadingHeight = me.loadingHeight;
  338. me.getSelectionModel().deselectAll();
  339. me.all.clear();
  340. if (loadingHeight &amp;&amp; loadingHeight &gt; me.getHeight()) {
  341. me.hasLoadingHeight = true;
  342. me.oldMinHeight = me.minHeight;
  343. me.minHeight = loadingHeight;
  344. me.updateLayout();
  345. }
  346. },
  347. onMaskHide: function(){
  348. var me = this;
  349. if (!me.destroying &amp;&amp; me.hasLoadingHeight) {
  350. me.minHeight = me.oldMinHeight;
  351. me.updateLayout();
  352. delete me.hasLoadingHeight;
  353. }
  354. },
  355. beforeRender: function() {
  356. this.callParent(arguments);
  357. this.getSelectionModel().beforeViewRender(this);
  358. },
  359. afterRender: function() {
  360. this.callParent(arguments);
  361. // Init the SelectionModel after any on('render') listeners have been added.
  362. // Drag plugins create a DragDrop instance in a render listener, and that needs
  363. // to see an itemmousedown event first.
  364. this.getSelectionModel().bindComponent(this);
  365. },
  366. <span id='Ext-view-AbstractView-method-getSelectionModel'> /**
  367. </span> * Gets the selection model for this view.
  368. * @return {Ext.selection.Model} The selection model
  369. */
  370. getSelectionModel: function(){
  371. var me = this,
  372. mode = 'SINGLE';
  373. if (!me.selModel) {
  374. me.selModel = {};
  375. }
  376. if (me.simpleSelect) {
  377. mode = 'SIMPLE';
  378. } else if (me.multiSelect) {
  379. mode = 'MULTI';
  380. }
  381. Ext.applyIf(me.selModel, {
  382. allowDeselect: me.allowDeselect,
  383. mode: mode
  384. });
  385. if (!me.selModel.events) {
  386. me.selModel = new Ext.selection.DataViewModel(me.selModel);
  387. }
  388. if (!me.selModel.hasRelaySetup) {
  389. me.relayEvents(me.selModel, [
  390. 'selectionchange', 'beforeselect', 'beforedeselect', 'select', 'deselect', 'focuschange'
  391. ]);
  392. me.selModel.hasRelaySetup = true;
  393. }
  394. // lock the selection model if user
  395. // has disabled selection
  396. if (me.disableSelection) {
  397. me.selModel.locked = true;
  398. }
  399. return me.selModel;
  400. },
  401. <span id='Ext-view-AbstractView-method-refresh'> /**
  402. </span> * Refreshes the view by reloading the data from the store and re-rendering the template.
  403. */
  404. refresh: function() {
  405. var me = this,
  406. targetEl,
  407. targetParent,
  408. oldDisplay,
  409. nextSibling,
  410. dom,
  411. records;
  412. if (!me.rendered || me.isDestroyed) {
  413. return;
  414. }
  415. if (!me.hasListeners.beforerefresh || me.fireEvent('beforerefresh', me) !== false) {
  416. targetEl = me.getTargetEl();
  417. records = me.store.getRange();
  418. dom = targetEl.dom;
  419. // Updating is much quicker if done when the targetEl is detached from the document, and not displayed.
  420. // But this resets the scroll position, so when preserving scroll position, this cannot be done.
  421. if (!me.preserveScrollOnRefresh) {
  422. targetParent = dom.parentNode;
  423. oldDisplay = dom.style.display;
  424. dom.style.display = 'none';
  425. nextSibling = dom.nextSibling;
  426. targetParent.removeChild(dom);
  427. }
  428. if (me.refreshCounter) {
  429. me.clearViewEl();
  430. } else {
  431. me.fixedNodes = targetEl.dom.childNodes.length;
  432. me.refreshCounter = 1;
  433. }
  434. // Always attempt to create the required markup after the fixedNodes.
  435. // Usually, for an empty record set, this would be blank, but when the Template
  436. // Creates markup outside of the record loop, this must still be honoured even if there are no
  437. // records.
  438. me.tpl.append(targetEl, me.collectData(records, 0));
  439. // The emptyText is now appended to the View's element
  440. // after any fixedNodes.
  441. if (records.length &lt; 1) {
  442. if (!me.deferEmptyText || me.hasSkippedEmptyText) {
  443. Ext.core.DomHelper.insertHtml('beforeEnd', targetEl.dom, me.emptyText);
  444. }
  445. me.all.clear();
  446. } else {
  447. me.all.fill(Ext.query(me.getItemSelector(), targetEl.dom));
  448. me.updateIndexes(0);
  449. }
  450. me.selModel.refresh();
  451. me.hasSkippedEmptyText = true;
  452. if (!me.preserveScrollOnRefresh) {
  453. targetParent.insertBefore(dom, nextSibling);
  454. dom.style.display = oldDisplay;
  455. }
  456. // Ensure layout system knows about new content size
  457. this.refreshSize();
  458. me.fireEvent('refresh', me);
  459. // Upon first refresh, fire the viewready event.
  460. // Reconfiguring the grid &quot;renews&quot; this event.
  461. if (!me.viewReady) {
  462. // Fire an event when deferred content becomes available.
  463. // This supports grid Panel's deferRowRender capability
  464. me.viewReady = true;
  465. me.fireEvent('viewready', me);
  466. }
  467. }
  468. },
  469. <span id='Ext-view-AbstractView-method-refreshSize'> /**
  470. </span> * @private
  471. * Called by the framework when the view is refreshed, or when rows are added or deleted.
  472. *
  473. * These operations may cause the view's dimensions to change, and if the owning container
  474. * is shrinkwrapping this view, then the layout must be updated to accommodate these new dimensions.
  475. */
  476. refreshSize: function() {
  477. var sizeModel = this.getSizeModel();
  478. if (sizeModel.height.shrinkWrap || sizeModel.width.shrinkWrap) {
  479. this.updateLayout();
  480. }
  481. },
  482. clearViewEl: function(){
  483. // The purpose of this is to allow boilerplate HTML nodes to remain in place inside a View
  484. // while the transient, templated data can be discarded and recreated.
  485. // The first time through this code, we take a count of the number of existing child nodes.
  486. // Subsequent refreshes then do not clear the entire element, but remove all nodes
  487. // *after* the fixedNodes count.
  488. // In particular, this is used in infinite grid scrolling: A very tall &quot;stretcher&quot; element is
  489. // inserted into the View's element to create a scrollbar of the correct proportion.
  490. var me = this,
  491. el = me.getTargetEl();
  492. if (me.fixedNodes) {
  493. while (el.dom.childNodes[me.fixedNodes]) {
  494. el.dom.removeChild(el.dom.childNodes[me.fixedNodes]);
  495. }
  496. } else {
  497. el.update('');
  498. }
  499. me.refreshCounter++;
  500. },
  501. // Private template method to be overridden in subclasses.
  502. onViewScroll: Ext.emptyFn,
  503. <span id='Ext-view-AbstractView-method-saveScrollState'> /**
  504. </span> * Saves the scrollState in a private variable. Must be used in conjunction with restoreScrollState.
  505. * @private
  506. */
  507. saveScrollState: function() {
  508. if (this.rendered) {
  509. var dom = this.el.dom,
  510. state = this.scrollState;
  511. state.left = dom.scrollLeft;
  512. state.top = dom.scrollTop;
  513. }
  514. },
  515. <span id='Ext-view-AbstractView-method-restoreScrollState'> /**
  516. </span> * Restores the scrollState.
  517. * Must be used in conjunction with saveScrollState
  518. * @private
  519. */
  520. restoreScrollState: function() {
  521. if (this.rendered) {
  522. var dom = this.el.dom,
  523. state = this.scrollState;
  524. dom.scrollLeft = state.left;
  525. dom.scrollTop = state.top;
  526. }
  527. },
  528. <span id='Ext-view-AbstractView-method-prepareData'> /**
  529. </span> * Function which can be overridden to provide custom formatting for each Record that is used by this
  530. * DataView's {@link #tpl template} to render each node.
  531. * @param {Object/Object[]} data The raw data object that was used to create the Record.
  532. * @param {Number} recordIndex the index number of the Record being prepared for rendering.
  533. * @param {Ext.data.Model} record The Record being prepared for rendering.
  534. * @return {Array/Object} The formatted data in a format expected by the internal {@link #tpl template}'s overwrite() method.
  535. * (either an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'}))
  536. */
  537. prepareData: function(data, index, record) {
  538. var associatedData, attr;
  539. if (record) {
  540. associatedData = record.getAssociatedData();
  541. for (attr in associatedData) {
  542. if (associatedData.hasOwnProperty(attr)) {
  543. data[attr] = associatedData[attr];
  544. }
  545. }
  546. }
  547. return data;
  548. },
  549. <span id='Ext-view-AbstractView-method-collectData'> /**
  550. </span> * &lt;p&gt;Function which can be overridden which returns the data object passed to this
  551. * DataView's {@link #tpl template} to render the whole DataView.&lt;/p&gt;
  552. * &lt;p&gt;This is usually an Array of data objects, each element of which is processed by an
  553. * {@link Ext.XTemplate XTemplate} which uses &lt;tt&gt;'&amp;lt;tpl for=&quot;.&quot;&amp;gt;'&lt;/tt&gt; to iterate over its supplied
  554. * data object as an Array. However, &lt;i&gt;named&lt;/i&gt; properties may be placed into the data object to
  555. * provide non-repeating data such as headings, totals etc.&lt;/p&gt;
  556. * @param {Ext.data.Model[]} records An Array of {@link Ext.data.Model}s to be rendered into the DataView.
  557. * @param {Number} startIndex the index number of the Record being prepared for rendering.
  558. * @return {Object[]} An Array of data objects to be processed by a repeating XTemplate. May also
  559. * contain &lt;i&gt;named&lt;/i&gt; properties.
  560. */
  561. collectData : function(records, startIndex){
  562. var data = [],
  563. i = 0,
  564. len = records.length,
  565. record;
  566. for (; i &lt; len; i++) {
  567. record = records[i];
  568. data[i] = this.prepareData(record.data, startIndex + i, record);
  569. }
  570. return data;
  571. },
  572. // private
  573. bufferRender : function(records, index){
  574. var me = this,
  575. div = me.renderBuffer || (me.renderBuffer = document.createElement('div'));
  576. me.tpl.overwrite(div, me.collectData(records, index));
  577. return Ext.query(me.getItemSelector(), div);
  578. },
  579. // private
  580. onUpdate : function(ds, record){
  581. var me = this,
  582. index,
  583. node;
  584. if (me.viewReady) {
  585. index = me.store.indexOf(record);
  586. if (index &gt; -1) {
  587. node = me.bufferRender([record], index)[0];
  588. // ensure the node actually exists in the DOM
  589. if (me.getNode(record)) {
  590. me.all.replaceElement(index, node, true);
  591. me.updateIndexes(index, index);
  592. // Maintain selection after update
  593. // TODO: Move to approriate event handler.
  594. me.selModel.refresh();
  595. if (me.hasListeners.itemupdate) {
  596. me.fireEvent('itemupdate', record, index, node);
  597. }
  598. return node;
  599. }
  600. }
  601. }
  602. },
  603. // private
  604. onAdd : function(ds, records, index) {
  605. var me = this,
  606. nodes;
  607. if (me.rendered) {
  608. // If we are adding into an empty view, we must refresh in order that the *full tpl* is applied
  609. // which might create boilerplate content *around* the record nodes.
  610. if (me.all.getCount() === 0) {
  611. me.refresh();
  612. return;
  613. }
  614. nodes = me.bufferRender(records, index);
  615. me.doAdd(nodes, records, index);
  616. me.selModel.refresh();
  617. me.updateIndexes(index);
  618. // Ensure layout system knows about new content size
  619. me.refreshSize();
  620. if (me.hasListeners.itemadd) {
  621. me.fireEvent('itemadd', records, index, nodes);
  622. }
  623. }
  624. },
  625. doAdd: function(nodes, records, index) {
  626. var all = this.all,
  627. count = all.getCount();
  628. if (count === 0) {
  629. this.clearViewEl();
  630. this.getTargetEl().appendChild(nodes);
  631. } else if (index &lt; count) {
  632. if (index === 0) {
  633. all.item(index).insertSibling(nodes, 'before', true);
  634. } else {
  635. all.item(index - 1).insertSibling(nodes, 'after', true);
  636. }
  637. } else {
  638. all.last().insertSibling(nodes, 'after', true);
  639. }
  640. Ext.Array.insert(all.elements, index, nodes);
  641. },
  642. // private
  643. onRemove : function(ds, record, index) {
  644. var me = this;
  645. if (me.all.getCount()) {
  646. if (me.store.getCount() === 0) {
  647. // Refresh so emptyText can be applied if necessary
  648. me.refresh();
  649. } else {
  650. // Just remove the element which corresponds to the removed record
  651. // The tpl's full HTML will still be in place.
  652. me.doRemove(record, index);
  653. if (me.selModel.refreshOnRemove) {
  654. me.selModel.refresh();
  655. }
  656. me.updateIndexes(index);
  657. }
  658. // Ensure layout system knows about new content height
  659. this.refreshSize();
  660. if (me.hasListeners.itemremove) {
  661. me.fireEvent('itemremove', record, index);
  662. }
  663. }
  664. },
  665. doRemove: function(record, index) {
  666. this.all.removeElement(index, true);
  667. },
  668. <span id='Ext-view-AbstractView-method-refreshNode'> /**
  669. </span> * Refreshes an individual node's data from the store.
  670. * @param {Number} index The item's data index in the store
  671. */
  672. refreshNode : function(index){
  673. this.onUpdate(this.store, this.store.getAt(index));
  674. },
  675. // private
  676. updateIndexes : function(startIndex, endIndex) {
  677. var ns = this.all.elements,
  678. records = this.store.getRange(),
  679. i;
  680. startIndex = startIndex || 0;
  681. endIndex = endIndex || ((endIndex === 0) ? 0 : (ns.length - 1));
  682. for (i = startIndex; i &lt;= endIndex; i++) {
  683. ns[i].viewIndex = i;
  684. ns[i].viewRecordId = records[i].internalId;
  685. if (!ns[i].boundView) {
  686. ns[i].boundView = this.id;
  687. }
  688. }
  689. },
  690. <span id='Ext-view-AbstractView-method-getStore'> /**
  691. </span> * Returns the store associated with this DataView.
  692. * @return {Ext.data.Store} The store
  693. */
  694. getStore : function() {
  695. return this.store;
  696. },
  697. <span id='Ext-view-AbstractView-method-bindStore'> /**
  698. </span> * Changes the data store bound to this view and refreshes it.
  699. * @param {Ext.data.Store} store The store to bind to this view
  700. */
  701. bindStore : function(store, initial) {
  702. var me = this;
  703. me.mixins.bindable.bindStore.apply(me, arguments);
  704. // Bind the store to our selection model unless it's the initial bind.
  705. // Initial bind takes place afterRender
  706. if (!initial) {
  707. me.getSelectionModel().bindStore(me.store);
  708. }
  709. // If we have already achieved our first layout, refresh immediately.
  710. // If we have bound to the Store before the first layout, then onBoxReady will
  711. // call doFirstRefresh
  712. if (me.componentLayoutCounter) {
  713. me.doFirstRefresh(store);
  714. }
  715. },
  716. <span id='Ext-view-AbstractView-method-doFirstRefresh'> /**
  717. </span> * @private
  718. * Perform the first refresh of the View from a newly bound store.
  719. *
  720. * This is called when this View has been sized for the first time.
  721. */
  722. doFirstRefresh: function(store) {
  723. var me = this;
  724. // Flag to prevent a second &quot;first&quot; refresh from onBoxReady
  725. me.firstRefreshDone = true;
  726. // 4.1.0: If we have a store, and the Store is *NOT* already loading (a refresh is on the way), then
  727. // on first layout, refresh regardless of record count.
  728. // Template may contain boilerplate HTML outside of record iteration loop.
  729. // Also, emptyText is appended by the refresh method.
  730. // We call refresh on a defer if this is the initial call, and we are configured to defer the initial refresh.
  731. if (store &amp;&amp; !store.loading) {
  732. if (me.deferInitialRefresh) {
  733. me.applyFirstRefresh();
  734. } else {
  735. me.refresh();
  736. }
  737. }
  738. },
  739. applyFirstRefresh: function(){
  740. var me = this;
  741. if (me.isDestroyed) {
  742. return;
  743. }
  744. // In the case of an animated collapse/expand, the layout will
  745. // be marked as though it's complete, yet the element itself may
  746. // still be animating, which means we could trigger a layout while
  747. // everything is not in the correct place. As such, wait until the
  748. // animation has finished before kicking off the refresh. The problem
  749. // occurs because both the refresh and the animation are running on
  750. // a timer which makes it impossible to control the order of when
  751. // the refresh is fired.
  752. if (me.up('[isCollapsingOrExpanding]')) {
  753. Ext.Function.defer(me.applyFirstRefresh, 100, me);
  754. } else {
  755. Ext.Function.defer(function () {
  756. if (!me.isDestroyed) {
  757. me.refresh();
  758. }
  759. }, 1);
  760. }
  761. },
  762. onUnbindStore: function(store) {
  763. this.setMaskBind(null);
  764. },
  765. onBindStore: function(store) {
  766. this.setMaskBind(store);
  767. },
  768. setMaskBind: function(store) {
  769. var mask = this.loadMask;
  770. if (mask &amp;&amp; mask.bindStore) {
  771. mask.bindStore(store);
  772. }
  773. },
  774. getStoreListeners: function() {
  775. var me = this;
  776. return {
  777. refresh: me.onDataRefresh,
  778. add: me.onAdd,
  779. remove: me.onRemove,
  780. update: me.onUpdate,
  781. clear: me.refresh
  782. };
  783. },
  784. <span id='Ext-view-AbstractView-method-onDataRefresh'> /**
  785. </span> * @private
  786. * Calls this.refresh if this.blockRefresh is not true
  787. */
  788. onDataRefresh: function() {
  789. var me = this,
  790. // If we have an ancestor in a non-boxready state (collapsed or in-transition, or hidden), and we are still waiting
  791. // for the first refresh, then block the refresh because that first visible, expanded layout will trigger the refresh
  792. blockedByAncestor = !me.firstRefreshDone &amp;&amp; (!me.rendered || me.up('[collapsed],[isCollapsingOrExpanding],[hidden]'));
  793. // If are blocking *an initial refresh* because of an ancestor in a non-boxready state,
  794. // then cancel any defer on the initial refresh that is going to happen on boxReady - that will be a data-driven refresh, NOT
  795. // a render-time, delayable refresh. This is particularly important if the boxready occurs because of the &quot;preflight&quot; layout
  796. // of an animated expand. If refresh is delayed it occur during the expand animation and cause errors.
  797. if (blockedByAncestor) {
  798. me.deferInitialRefresh = false;
  799. } else if (me.blockRefresh !== true) {
  800. me.firstRefreshDone = true;
  801. me.refresh.apply(me, arguments);
  802. }
  803. },
  804. <span id='Ext-view-AbstractView-method-findItemByChild'> /**
  805. </span> * Returns the template node the passed child belongs to, or null if it doesn't belong to one.
  806. * @param {HTMLElement} node
  807. * @return {HTMLElement} The template node
  808. */
  809. findItemByChild: function(node){
  810. return Ext.fly(node).findParent(this.getItemSelector(), this.getTargetEl());
  811. },
  812. <span id='Ext-view-AbstractView-method-findTargetByEvent'> /**
  813. </span> * Returns the template node by the Ext.EventObject or null if it is not found.
  814. * @param {Ext.EventObject} e
  815. */
  816. findTargetByEvent: function(e) {
  817. return e.getTarget(this.getItemSelector(), this.getTargetEl());
  818. },
  819. <span id='Ext-view-AbstractView-method-getSelectedNodes'> /**
  820. </span> * Gets the currently selected nodes.
  821. * @return {HTMLElement[]} An array of HTMLElements
  822. */
  823. getSelectedNodes: function(){
  824. var nodes = [],
  825. records = this.selModel.getSelection(),
  826. ln = records.length,
  827. i = 0;
  828. for (; i &lt; ln; i++) {
  829. nodes.push(this.getNode(records[i]));
  830. }
  831. return nodes;
  832. },
  833. <span id='Ext-view-AbstractView-method-getRecords'> /**
  834. </span> * Gets an array of the records from an array of nodes
  835. * @param {HTMLElement[]} nodes The nodes to evaluate
  836. * @return {Ext.data.Model[]} records The {@link Ext.data.Model} objects
  837. */
  838. getRecords: function(nodes) {
  839. var records = [],
  840. i = 0,
  841. len = nodes.length,
  842. data = this.store.data;
  843. for (; i &lt; len; i++) {
  844. records[records.length] = data.getByKey(nodes[i].viewRecordId);
  845. }
  846. return records;
  847. },
  848. <span id='Ext-view-AbstractView-method-getRecord'> /**
  849. </span> * Gets a record from a node
  850. * @param {Ext.Element/HTMLElement} node The node to evaluate
  851. *
  852. * @return {Ext.data.Model} record The {@link Ext.data.Model} object
  853. */
  854. getRecord: function(node){
  855. return this.store.data.getByKey(Ext.getDom(node).viewRecordId);
  856. },
  857. <span id='Ext-view-AbstractView-method-isSelected'> /**
  858. </span> * Returns true if the passed node is selected, else false.
  859. * @param {HTMLElement/Number/Ext.data.Model} node The node, node index or record to check
  860. * @return {Boolean} True if selected, else false
  861. */
  862. isSelected : function(node) {
  863. // TODO: El/Idx/Record
  864. var r = this.getRecord(node);
  865. return this.selModel.isSelected(r);
  866. },
  867. <span id='Ext-view-AbstractView-method-select'> /**
  868. </span> * Selects a record instance by record instance or index.
  869. * @param {Ext.data.Model[]/Number} records An array of records or an index
  870. * @param {Boolean} keepExisting
  871. * @param {Boolean} suppressEvent Set to false to not fire a select event
  872. * @deprecated 4.0 Use {@link Ext.selection.Model#select} instead.
  873. */
  874. select: function(records, keepExisting, suppressEvent) {
  875. this.selModel.select(records, keepExisting, suppressEvent);
  876. },
  877. <span id='Ext-view-AbstractView-method-deselect'> /**
  878. </span> * Deselects a record instance by record instance or index.
  879. * @param {Ext.data.Model[]/Number} records An array of records or an index
  880. * @param {Boolean} suppressEvent Set to false to not fire a deselect event
  881. */
  882. deselect: function(records, suppressEvent) {
  883. this.selModel.deselect(records, suppressEvent);
  884. },
  885. <span id='Ext-view-AbstractView-method-getNode'> /**
  886. </span> * Gets a template node.
  887. * @param {HTMLElement/String/Number/Ext.data.Model} nodeInfo An HTMLElement template node, index of a template node,
  888. * the id of a template node or the record associated with the node.
  889. * @return {HTMLElement} The node or null if it wasn't found
  890. */
  891. getNode : function(nodeInfo) {
  892. if ((!nodeInfo &amp;&amp; nodeInfo !== 0) || !this.rendered) {
  893. return null;
  894. }
  895. if (Ext.isString(nodeInfo)) {
  896. return document.getElementById(nodeInfo);
  897. }
  898. if (Ext.isNumber(nodeInfo)) {
  899. return this.all.elements[nodeInfo];
  900. }
  901. if (nodeInfo.isModel) {
  902. return this.getNodeByRecord(nodeInfo);
  903. }
  904. return nodeInfo; // already an HTMLElement
  905. },
  906. <span id='Ext-view-AbstractView-method-getNodeByRecord'> /**
  907. </span> * @private
  908. */
  909. getNodeByRecord: function(record) {
  910. var ns = this.all.elements,
  911. ln = ns.length,
  912. i = 0;
  913. for (; i &lt; ln; i++) {
  914. if (ns[i].viewRecordId === record.internalId) {
  915. return ns[i];
  916. }
  917. }
  918. return null;
  919. },
  920. <span id='Ext-view-AbstractView-method-getNodes'> /**
  921. </span> * Gets a range nodes.
  922. * @param {Number} start (optional) The index of the first node in the range
  923. * @param {Number} end (optional) The index of the last node in the range
  924. * @return {HTMLElement[]} An array of nodes
  925. */
  926. getNodes: function(start, end) {
  927. var ns = this.all.elements;
  928. if (end === undefined) {
  929. end = ns.length;
  930. } else {
  931. end++;
  932. }
  933. return this.all.elements.slice(start||0, end);
  934. },
  935. <span id='Ext-view-AbstractView-method-indexOf'> /**
  936. </span> * Finds the index of the passed node.
  937. * @param {HTMLElement/String/Number/Ext.data.Model} nodeInfo An HTMLElement template node, index of a template node, the id of a template node
  938. * or a record associated with a node.
  939. * @return {Number} The index of the node or -1
  940. */
  941. indexOf: function(node) {
  942. node = this.getNode(node);
  943. if (!node &amp;&amp; node !== 0) {
  944. return -1;
  945. }
  946. if (Ext.isNumber(node.viewIndex)) {
  947. return node.viewIndex;
  948. }
  949. return this.all.indexOf(node);
  950. },
  951. onDestroy : function() {
  952. var me = this;
  953. me.all.clear();
  954. me.callParent();
  955. me.bindStore(null);
  956. me.selModel.destroy();
  957. },
  958. // invoked by the selection model to maintain visual UI cues
  959. onItemSelect: function(record) {
  960. var node = this.getNode(record);
  961. if (node) {
  962. Ext.fly(node).addCls(this.selectedItemCls);
  963. }
  964. },
  965. // invoked by the selection model to maintain visual UI cues
  966. onItemDeselect: function(record) {
  967. var node = this.getNode(record);
  968. if (node) {
  969. Ext.fly(node).removeCls(this.selectedItemCls);
  970. }
  971. },
  972. getItemSelector: function() {
  973. return this.itemSelector;
  974. }
  975. }, function() {
  976. // all of this information is available directly
  977. // from the SelectionModel itself, the only added methods
  978. // to DataView regarding selection will perform some transformation/lookup
  979. // between HTMLElement/Nodes to records and vice versa.
  980. Ext.deprecate('extjs', '4.0', function() {
  981. Ext.view.AbstractView.override({
  982. <span id='Ext-view-AbstractView-cfg-multiSelect'> /**
  983. </span> * @cfg {Boolean} [multiSelect=false]
  984. * True to allow selection of more than one item at a time, false to allow selection of only a single item
  985. * at a time or no selection at all, depending on the value of {@link #singleSelect}.
  986. * @deprecated 4.1.1 Use {@link Ext.selection.Model#mode} 'MULTI' instead.
  987. */
  988. <span id='Ext-view-AbstractView-cfg-singleSelect'> /**
  989. </span> * @cfg {Boolean} [singleSelect]
  990. * Allows selection of exactly one item at a time. As this is the default selection mode anyway, this config
  991. * is completely ignored.
  992. * @removed 4.1.1 Use {@link Ext.selection.Model#mode} 'SINGLE' instead.
  993. */
  994. <span id='Ext-view-AbstractView-cfg-simpleSelect'> /**
  995. </span> * @cfg {Boolean} [simpleSelect=false]
  996. * True to enable multiselection by clicking on multiple items without requiring the user to hold Shift or Ctrl,
  997. * false to force the user to hold Ctrl or Shift to select more than on item.
  998. * @deprecated 4.1.1 Use {@link Ext.selection.Model#mode} 'SIMPLE' instead.
  999. */
  1000. <span id='Ext-view-AbstractView-method-getSelectionCount'> /**
  1001. </span> * Gets the number of selected nodes.
  1002. * @return {Number} The node count
  1003. * @deprecated 4.0 Use {@link Ext.selection.Model#getCount} instead.
  1004. */
  1005. getSelectionCount : function(){
  1006. if (Ext.global.console) {
  1007. Ext.global.console.warn(&quot;DataView: getSelectionCount will be removed, please interact with the Ext.selection.DataViewModel&quot;);
  1008. }
  1009. return this.selModel.getSelection().length;
  1010. },
  1011. <span id='Ext-view-AbstractView-method-getSelectedRecords'> /**
  1012. </span> * Gets an array of the selected records
  1013. * @return {Ext.data.Model[]} An array of {@link Ext.data.Model} objects
  1014. * @deprecated 4.0 Use {@link Ext.selection.Model#getSelection} instead.
  1015. */
  1016. getSelectedRecords : function(){
  1017. if (Ext.global.console) {
  1018. Ext.global.console.warn(&quot;DataView: getSelectedRecords will be removed, please interact with the Ext.selection.DataViewModel&quot;);
  1019. }
  1020. return this.selModel.getSelection();
  1021. },
  1022. select: function(records, keepExisting, supressEvents) {
  1023. if (Ext.global.console) {
  1024. Ext.global.console.warn(&quot;DataView: select will be removed, please access select through a DataView's SelectionModel, ie: view.getSelectionModel().select()&quot;);
  1025. }
  1026. var sm = this.getSelectionModel();
  1027. return sm.select.apply(sm, arguments);
  1028. },
  1029. <span id='Ext-view-AbstractView-method-clearSelections'> /**
  1030. </span> * Deselects all selected records.
  1031. * @deprecated 4.0 Use {@link Ext.selection.Model#deselectAll} instead.
  1032. */
  1033. clearSelections: function() {
  1034. if (Ext.global.console) {
  1035. Ext.global.console.warn(&quot;DataView: clearSelections will be removed, please access deselectAll through DataView's SelectionModel, ie: view.getSelectionModel().deselectAll()&quot;);
  1036. }
  1037. var sm = this.getSelectionModel();
  1038. return sm.deselectAll();
  1039. }
  1040. });
  1041. });
  1042. });
  1043. </pre>
  1044. </body>
  1045. </html>