Animated.html 13 KB

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