Web Interface For Lite Argus Control modules
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.

317 lines
12 KiB

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