DataViewTransition.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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-ux-DataViewTransition'>/**
  19. </span> * @class Ext.ux.DataViewTransition
  20. * @extends Object
  21. * @author Ed Spencer (http://sencha.com)
  22. * Transition plugin for DataViews
  23. */
  24. Ext.ux.DataViewTransition = Ext.extend(Object, {
  25. <span id='Ext-ux-DataViewTransition-property-defaults'> /**
  26. </span> * @property defaults
  27. * @type Object
  28. * Default configuration options for all DataViewTransition instances
  29. */
  30. defaults: {
  31. duration : 750,
  32. idProperty: 'id'
  33. },
  34. <span id='Ext-ux-DataViewTransition-method-constructor'> /**
  35. </span> * Creates the plugin instance, applies defaults
  36. * @constructor
  37. * @param {Object} config Optional config object
  38. */
  39. constructor: function(config) {
  40. Ext.apply(this, config || {}, this.defaults);
  41. },
  42. <span id='Ext-ux-DataViewTransition-method-init'> /**
  43. </span> * Initializes the transition plugin. Overrides the dataview's default refresh function
  44. * @param {Ext.view.View} dataview The dataview
  45. */
  46. init: function(dataview) {
  47. <span id='Ext-ux-DataViewTransition-property-dataview'> /**
  48. </span> * @property dataview
  49. * @type Ext.view.View
  50. * Reference to the DataView this instance is bound to
  51. */
  52. this.dataview = dataview;
  53. var idProperty = this.idProperty;
  54. dataview.blockRefresh = true;
  55. dataview.updateIndexes = Ext.Function.createSequence(dataview.updateIndexes, function() {
  56. this.getTargetEl().select(this.itemSelector).each(function(element, composite, index) {
  57. element.id = element.dom.id = Ext.util.Format.format(&quot;{0}-{1}&quot;, dataview.id, dataview.store.getAt(index).get(idProperty));
  58. }, this);
  59. }, dataview);
  60. <span id='Ext-ux-DataViewTransition-property-dataviewID'> /**
  61. </span> * @property dataviewID
  62. * @type String
  63. * The string ID of the DataView component. This is used internally when animating child objects
  64. */
  65. this.dataviewID = dataview.id;
  66. <span id='Ext-ux-DataViewTransition-property-cachedStoreData'> /**
  67. </span> * @property cachedStoreData
  68. * @type Object
  69. * A cache of existing store data, keyed by id. This is used to determine
  70. * whether any items were added or removed from the store on data change
  71. */
  72. this.cachedStoreData = {};
  73. //var store = dataview.store;
  74. //catch the store data with the snapshot immediately
  75. this.cacheStoreData(dataview.store.snapshot);
  76. dataview.store.on('datachanged', function(store) {
  77. var parentEl = dataview.getTargetEl(),
  78. calcItem = store.getAt(0),
  79. added = this.getAdded(store),
  80. removed = this.getRemoved(store),
  81. previous = this.getRemaining(store),
  82. existing = Ext.apply({}, previous, added);
  83. //hide old items
  84. Ext.each(removed, function(item) {
  85. Ext.fly(this.dataviewID + '-' + item.get(this.idProperty)).animate({
  86. remove : false,
  87. duration: duration,
  88. opacity : 0,
  89. useDisplay: true
  90. });
  91. }, this);
  92. //store is empty
  93. if (calcItem == undefined) {
  94. this.cacheStoreData(store);
  95. return;
  96. }
  97. var el = Ext.get(this.dataviewID + &quot;-&quot; + calcItem.get(this.idProperty));
  98. //calculate the number of rows and columns we have
  99. var itemCount = store.getCount(),
  100. itemWidth = el.getMargin('lr') + el.getWidth(),
  101. itemHeight = el.getMargin('bt') + el.getHeight(),
  102. dvWidth = parentEl.getWidth(),
  103. columns = Math.floor(dvWidth / itemWidth),
  104. rows = Math.ceil(itemCount / columns),
  105. currentRows = Math.ceil(this.getExistingCount() / columns);
  106. //make sure the correct styles are applied to the parent element
  107. parentEl.applyStyles({
  108. display : 'block',
  109. position: 'relative'
  110. });
  111. //stores the current top and left values for each element (discovered below)
  112. var oldPositions = {},
  113. newPositions = {},
  114. elCache = {};
  115. //find current positions of each element and save a reference in the elCache
  116. Ext.iterate(previous, function(id, item) {
  117. var id = item.get(this.idProperty),
  118. el = elCache[id] = Ext.get(this.dataviewID + '-' + id);
  119. oldPositions[id] = {
  120. top : el.getTop() - parentEl.getTop() - el.getMargin('t') - parentEl.getPadding('t'),
  121. left: el.getLeft() - parentEl.getLeft() - el.getMargin('l') - parentEl.getPadding('l')
  122. };
  123. }, this);
  124. //set absolute positioning on all DataView items. We need to set position, left and
  125. //top at the same time to avoid any flickering
  126. Ext.iterate(previous, function(id, item) {
  127. var oldPos = oldPositions[id],
  128. el = elCache[id];
  129. if (el.getStyle('position') != 'absolute') {
  130. elCache[id].applyStyles({
  131. position: 'absolute',
  132. left : oldPos.left + &quot;px&quot;,
  133. top : oldPos.top + &quot;px&quot;,
  134. //we set the width here to make ListViews work correctly. This is not needed for DataViews
  135. width : el.getWidth(!Ext.isIE || Ext.isStrict),
  136. height : el.getHeight(!Ext.isIE || Ext.isStrict)
  137. });
  138. }
  139. });
  140. //get new positions
  141. var index = 0;
  142. Ext.iterate(store.data.items, function(item) {
  143. var id = item.get(idProperty),
  144. el = elCache[id];
  145. var column = index % columns,
  146. row = Math.floor(index / columns),
  147. top = row * itemHeight,
  148. left = column * itemWidth;
  149. newPositions[id] = {
  150. top : top,
  151. left: left
  152. };
  153. index ++;
  154. }, this);
  155. //do the movements
  156. var startTime = new Date(),
  157. duration = this.duration,
  158. dataviewID = this.dataviewID;
  159. var doAnimate = function() {
  160. var elapsed = new Date() - startTime,
  161. fraction = elapsed / duration;
  162. if (fraction &gt;= 1) {
  163. for (var id in newPositions) {
  164. Ext.fly(dataviewID + '-' + id).applyStyles({
  165. top : newPositions[id].top + &quot;px&quot;,
  166. left: newPositions[id].left + &quot;px&quot;
  167. });
  168. }
  169. Ext.TaskManager.stop(task);
  170. } else {
  171. //move each item
  172. for (var id in newPositions) {
  173. if (!previous[id]) continue;
  174. var oldPos = oldPositions[id],
  175. newPos = newPositions[id],
  176. oldTop = oldPos.top,
  177. newTop = newPos.top,
  178. oldLeft = oldPos.left,
  179. newLeft = newPos.left,
  180. diffTop = fraction * Math.abs(oldTop - newTop),
  181. diffLeft= fraction * Math.abs(oldLeft - newLeft),
  182. midTop = oldTop &gt; newTop ? oldTop - diffTop : oldTop + diffTop,
  183. midLeft = oldLeft &gt; newLeft ? oldLeft - diffLeft : oldLeft + diffLeft;
  184. Ext.fly(dataviewID + '-' + id).applyStyles({
  185. top : midTop + &quot;px&quot;,
  186. left: midLeft + &quot;px&quot;
  187. });
  188. }
  189. }
  190. };
  191. var task = {
  192. run : doAnimate,
  193. interval: 20,
  194. scope : this
  195. };
  196. Ext.TaskManager.start(task);
  197. //&lt;debug&gt;
  198. var count = 0;
  199. for (var k in added) {
  200. count++;
  201. }
  202. if (Ext.global.console &amp;&amp; Ext.global.console.log) {
  203. Ext.global.console.log('added:', count);
  204. }
  205. //&lt;/debug&gt;
  206. //show new items
  207. Ext.iterate(added, function(id, item) {
  208. Ext.fly(this.dataviewID + '-' + item.get(this.idProperty)).applyStyles({
  209. top : newPositions[item.get(this.idProperty)].top + &quot;px&quot;,
  210. left : newPositions[item.get(this.idProperty)].left + &quot;px&quot;
  211. });
  212. Ext.fly(this.dataviewID + '-' + item.get(this.idProperty)).animate({
  213. remove : false,
  214. duration: duration,
  215. opacity : 1
  216. });
  217. }, this);
  218. this.cacheStoreData(store);
  219. }, this);
  220. },
  221. <span id='Ext-ux-DataViewTransition-method-cacheStoreData'> /**
  222. </span> * Caches the records from a store locally for comparison later
  223. * @param {Ext.data.Store} store The store to cache data from
  224. */
  225. cacheStoreData: function(store) {
  226. this.cachedStoreData = {};
  227. store.each(function(record) {
  228. this.cachedStoreData[record.get(this.idProperty)] = record;
  229. }, this);
  230. },
  231. <span id='Ext-ux-DataViewTransition-method-getExisting'> /**
  232. </span> * Returns all records that were already in the DataView
  233. * @return {Object} All existing records
  234. */
  235. getExisting: function() {
  236. return this.cachedStoreData;
  237. },
  238. <span id='Ext-ux-DataViewTransition-method-getExistingCount'> /**
  239. </span> * Returns the total number of items that are currently visible in the DataView
  240. * @return {Number} The number of existing items
  241. */
  242. getExistingCount: function() {
  243. var count = 0,
  244. items = this.getExisting();
  245. for (var k in items) count++;
  246. return count;
  247. },
  248. <span id='Ext-ux-DataViewTransition-method-getAdded'> /**
  249. </span> * Returns all records in the given store that were not already present
  250. * @param {Ext.data.Store} store The updated store instance
  251. * @return {Object} Object of records not already present in the dataview in format {id: record}
  252. */
  253. getAdded: function(store) {
  254. var added = {};
  255. store.each(function(record) {
  256. if (this.cachedStoreData[record.get(this.idProperty)] == undefined) {
  257. added[record.get(this.idProperty)] = record;
  258. }
  259. }, this);
  260. return added;
  261. },
  262. <span id='Ext-ux-DataViewTransition-method-getRemoved'> /**
  263. </span> * Returns all records that are present in the DataView but not the new store
  264. * @param {Ext.data.Store} store The updated store instance
  265. * @return {Array} Array of records that used to be present
  266. */
  267. getRemoved: function(store) {
  268. var removed = [];
  269. for (var id in this.cachedStoreData) {
  270. if (store.findExact(this.idProperty, Number(id)) == -1) {
  271. removed.push(this.cachedStoreData[id]);
  272. }
  273. }
  274. return removed;
  275. },
  276. <span id='Ext-ux-DataViewTransition-method-getRemaining'> /**
  277. </span> * Returns all records that are already present and are still present in the new store
  278. * @param {Ext.data.Store} store The updated store instance
  279. * @return {Object} Object of records that are still present from last time in format {id: record}
  280. */
  281. getRemaining: function(store) {
  282. var remaining = {};
  283. store.each(function(record) {
  284. if (this.cachedStoreData[record.get(this.idProperty)] != undefined) {
  285. remaining[record.get(this.idProperty)] = record;
  286. }
  287. }, this);
  288. return remaining;
  289. }
  290. });
  291. </pre>
  292. </body>
  293. </html>