You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

331 lines
12 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. /**
  16. * @class Ext.ux.DataViewTransition
  17. * @extends Object
  18. * @author Ed Spencer (http://extjs.com)
  19. * Transition plugin for DataViews
  20. */
  21. Ext.ux.DataViewTransition = Ext.extend(Object, {
  22. /**
  23. * @property defaults
  24. * @type Object
  25. * Default configuration options for all DataViewTransition instances
  26. */
  27. defaults: {
  28. duration : 750,
  29. idProperty: 'id'
  30. },
  31. /**
  32. * Creates the plugin instance, applies defaults
  33. * @constructor
  34. * @param {Object} config Optional config object
  35. */
  36. constructor: function(config) {
  37. Ext.apply(this, config || {}, this.defaults);
  38. },
  39. /**
  40. * Initializes the transition plugin. Overrides the dataview's default refresh function
  41. * @param {Ext.DataView} dataview The dataview
  42. */
  43. init: function(dataview) {
  44. /**
  45. * @property dataview
  46. * @type Ext.DataView
  47. * Reference to the DataView this instance is bound to
  48. */
  49. this.dataview = dataview;
  50. var idProperty = this.idProperty;
  51. dataview.blockRefresh = true;
  52. dataview.updateIndexes = dataview.updateIndexes.createSequence(function() {
  53. this.getTemplateTarget().select(this.itemSelector).each(function(element, composite, index) {
  54. element.id = element.dom.id = String.format("{0}-{1}", dataview.id, store.getAt(index).get(idProperty));
  55. }, this);
  56. }, dataview);
  57. /**
  58. * @property dataviewID
  59. * @type String
  60. * The string ID of the DataView component. This is used internally when animating child objects
  61. */
  62. this.dataviewID = dataview.id;
  63. /**
  64. * @property cachedStoreData
  65. * @type Object
  66. * A cache of existing store data, keyed by id. This is used to determine
  67. * whether any items were added or removed from the store on data change
  68. */
  69. this.cachedStoreData = {};
  70. var store = dataview.store;
  71. //cache the data immediately, and again on any load operation
  72. this.cacheStoreData(store);
  73. store.on('load', this.cacheStoreData, this);
  74. store.on('datachanged', function(store) {
  75. var parentEl = dataview.getTemplateTarget(),
  76. calcItem = store.getAt(0),
  77. added = this.getAdded(store),
  78. removed = this.getRemoved(store),
  79. previous = this.getRemaining(store),
  80. existing = Ext.apply({}, previous, added);
  81. //hide old items
  82. Ext.each(removed, function(item) {
  83. Ext.fly(this.dataviewID + '-' + item.get(this.idProperty)).fadeOut({
  84. remove : false,
  85. duration: duration / 1000,
  86. useDisplay: true
  87. });
  88. }, this);
  89. //store is empty
  90. if (calcItem == undefined) {
  91. this.cacheStoreData(store);
  92. return;
  93. }
  94. var el = parentEl.child("#" + this.dataviewID + "-" + calcItem.get(this.idProperty));
  95. //calculate the number of rows and columns we have
  96. var itemCount = store.getCount(),
  97. itemWidth = el.getMargins('lr') + el.getWidth(),
  98. itemHeight = el.getMargins('bt') + el.getHeight(),
  99. dvWidth = parentEl.getWidth(),
  100. columns = Math.floor(dvWidth / itemWidth),
  101. rows = Math.ceil(itemCount / columns),
  102. currentRows = Math.ceil(this.getExistingCount() / columns);
  103. //make sure the correct styles are applied to the parent element
  104. parentEl.applyStyles({
  105. display : 'block',
  106. position: 'relative'
  107. // ,
  108. // height : Ext.max([rows, currentRows]) * itemHeight,
  109. // width : columns * itemWidth
  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] = parentEl.child('#' + this.dataviewID + '-' + id);
  119. oldPositions[id] = {
  120. top : el.getTop() - parentEl.getTop() - el.getMargins('t') - parentEl.getPadding('t'),
  121. left: el.getLeft() - parentEl.getLeft() - el.getMargins('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 + "px",
  133. top : oldPos.top + "px",
  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 >= 1) {
  163. for (var id in newPositions) {
  164. Ext.fly(dataviewID + '-' + id).applyStyles({
  165. top : newPositions[id].top + "px",
  166. left: newPositions[id].left + "px"
  167. });
  168. }
  169. Ext.TaskMgr.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 > newTop ? oldTop - diffTop : oldTop + diffTop,
  183. midLeft = oldLeft > newLeft ? oldLeft - diffLeft : oldLeft + diffLeft;
  184. Ext.fly(dataviewID + '-' + id).applyStyles({
  185. top : midTop + "px",
  186. left: midLeft + "px"
  187. });
  188. }
  189. }
  190. };
  191. var task = {
  192. run : doAnimate,
  193. interval: 20,
  194. scope : this
  195. };
  196. Ext.TaskMgr.start(task);
  197. //show new items
  198. Ext.iterate(added, function(id, item) {
  199. Ext.fly(this.dataviewID + '-' + item.get(this.idProperty)).applyStyles({
  200. top : newPositions[id].top + "px",
  201. left: newPositions[id].left + "px"
  202. }).fadeIn({
  203. remove : false,
  204. duration: duration / 1000
  205. });
  206. }, this);
  207. this.cacheStoreData(store);
  208. }, this);
  209. },
  210. /**
  211. * Caches the records from a store locally for comparison later
  212. * @param {Ext.data.Store} store The store to cache data from
  213. */
  214. cacheStoreData: function(store) {
  215. this.cachedStoreData = {};
  216. store.each(function(record) {
  217. this.cachedStoreData[record.get(this.idProperty)] = record;
  218. }, this);
  219. },
  220. /**
  221. * Returns all records that were already in the DataView
  222. * @return {Object} All existing records
  223. */
  224. getExisting: function() {
  225. return this.cachedStoreData;
  226. },
  227. /**
  228. * Returns the total number of items that are currently visible in the DataView
  229. * @return {Number} The number of existing items
  230. */
  231. getExistingCount: function() {
  232. var count = 0,
  233. items = this.getExisting();
  234. for (var k in items) count++;
  235. return count;
  236. },
  237. /**
  238. * Returns all records in the given store that were not already present
  239. * @param {Ext.data.Store} store The updated store instance
  240. * @return {Object} Object of records not already present in the dataview in format {id: record}
  241. */
  242. getAdded: function(store) {
  243. var added = {};
  244. store.each(function(record) {
  245. if (this.cachedStoreData[record.get(this.idProperty)] == undefined) {
  246. added[record.get(this.idProperty)] = record;
  247. }
  248. }, this);
  249. return added;
  250. },
  251. /**
  252. * Returns all records that are present in the DataView but not the new store
  253. * @param {Ext.data.Store} store The updated store instance
  254. * @return {Array} Array of records that used to be present
  255. */
  256. getRemoved: function(store) {
  257. var removed = [];
  258. for (var id in this.cachedStoreData) {
  259. if (store.findExact(this.idProperty, Number(id)) == -1) removed.push(this.cachedStoreData[id]);
  260. }
  261. return removed;
  262. },
  263. /**
  264. * Returns all records that are already present and are still present in the new store
  265. * @param {Ext.data.Store} store The updated store instance
  266. * @return {Object} Object of records that are still present from last time in format {id: record}
  267. */
  268. getRemaining: function(store) {
  269. var remaining = {};
  270. store.each(function(record) {
  271. if (this.cachedStoreData[record.get(this.idProperty)] != undefined) {
  272. remaining[record.get(this.idProperty)] = record;
  273. }
  274. }, this);
  275. return remaining;
  276. }
  277. });