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.

424 lines
15 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.Reorderer
  9. * @extends Object
  10. * Generic base class for handling reordering of items. This base class must be extended to provide the
  11. * actual reordering functionality - the base class just sets up events and abstract logic functions.
  12. * It will fire events and set defaults, deferring the actual reordering to a doReorder implementation.
  13. * See Ext.ux.TabReorderer for an example.
  14. */
  15. Ext.ux.Reorderer = Ext.extend(Object, {
  16. /**
  17. * @property defaults
  18. * @type Object
  19. * Object containing default values for plugin configuration details. These can be overridden when
  20. * constructing the plugin
  21. */
  22. defaults: {
  23. /**
  24. * @cfg animate
  25. * @type Boolean
  26. * If set to true, the rearranging of the toolbar items is animated
  27. */
  28. animate: true,
  29. /**
  30. * @cfg animationDuration
  31. * @type Number
  32. * The duration of the animation used to move other toolbar items out of the way
  33. */
  34. animationDuration: 0.2,
  35. /**
  36. * @cfg defaultReorderable
  37. * @type Boolean
  38. * True to make every toolbar draggable unless reorderable is specifically set to false.
  39. * This defaults to false
  40. */
  41. defaultReorderable: false
  42. },
  43. /**
  44. * Creates the plugin instance, applies defaults
  45. * @constructor
  46. * @param {Object} config Optional config object
  47. */
  48. constructor: function(config) {
  49. Ext.apply(this, config || {}, this.defaults);
  50. },
  51. /**
  52. * Initializes the plugin, stores a reference to the target
  53. * @param {Mixed} target The target component which contains the reorderable items
  54. */
  55. init: function(target) {
  56. /**
  57. * @property target
  58. * @type Ext.Component
  59. * Reference to the target component which contains the reorderable items
  60. */
  61. this.target = target;
  62. this.initEvents();
  63. var items = this.getItems(),
  64. length = items.length,
  65. i;
  66. for (i = 0; i < length; i++) {
  67. this.createIfReorderable(items[i]);
  68. }
  69. },
  70. /**
  71. * Reorders the items in the target component according to the given mapping object. Example:
  72. * this.reorder({
  73. * 1: 5,
  74. * 3: 2
  75. * });
  76. * Would move the item at index 1 to index 5, and the item at index 3 to index 2
  77. * @param {Object} mappings Object containing current item index as key and new index as property
  78. */
  79. reorder: function(mappings) {
  80. var target = this.target;
  81. if (target.fireEvent('before-reorder', mappings, target, this) !== false) {
  82. this.doReorder(mappings);
  83. target.fireEvent('reorder', mappings, target, this);
  84. }
  85. },
  86. /**
  87. * Abstract function to perform the actual reordering. This MUST be overridden in a subclass
  88. * @param {Object} mappings Mappings of the old item indexes to new item indexes
  89. */
  90. doReorder: function(paramName) {
  91. throw new Error("doReorder must be implemented in the Ext.ux.Reorderer subclass");
  92. },
  93. /**
  94. * Should create and return an Ext.dd.DD for the given item. This MUST be overridden in a subclass
  95. * @param {Mixed} item The item to create a DD for. This could be a TabPanel tab, a Toolbar button, etc
  96. * @return {Ext.dd.DD} The DD for the given item
  97. */
  98. createItemDD: function(item) {
  99. throw new Error("createItemDD must be implemented in the Ext.ux.Reorderer subclass");
  100. },
  101. /**
  102. * Sets up the given Toolbar item as a draggable
  103. * @param {Mixed} button The item to make draggable (usually an Ext.Button instance)
  104. */
  105. createItemDD: function(button) {
  106. var el = button.getEl(),
  107. id = el.id,
  108. tbar = this.target,
  109. me = this;
  110. button.dd = new Ext.dd.DD(el, undefined, {
  111. isTarget: false
  112. });
  113. button.dd.constrainTo(tbar.getEl());
  114. button.dd.setYConstraint(0, 0, 0);
  115. Ext.apply(button.dd, {
  116. b4StartDrag: function() {
  117. this.startPosition = el.getXY();
  118. //bump up the z index of the button being dragged but keep a reference to the original
  119. this.startZIndex = el.getStyle('zIndex');
  120. el.setStyle('zIndex', 10000);
  121. button.suspendEvents();
  122. },
  123. onDrag: function(e) {
  124. //calculate the button's index within the toolbar and its current midpoint
  125. var buttonX = el.getXY()[0],
  126. deltaX = buttonX - this.startPosition[0],
  127. items = tbar.items.items,
  128. oldIndex = items.indexOf(button),
  129. newIndex;
  130. //find which item in the toolbar the midpoint is currently over
  131. for (var index = 0; index < items.length; index++) {
  132. var item = items[index];
  133. if (item.reorderable && item.id != button.id) {
  134. //find the midpoint of the button
  135. var box = item.getEl().getBox(),
  136. midpoint = (me.buttonXCache[item.id] || box.x) + (box.width / 2),
  137. movedLeft = oldIndex > index && deltaX < 0 && buttonX < midpoint,
  138. movedRight = oldIndex < index && deltaX > 0 && (buttonX + el.getWidth()) > midpoint;
  139. if (movedLeft || movedRight) {
  140. me[movedLeft ? 'onMovedLeft' : 'onMovedRight'](button, index, oldIndex);
  141. break;
  142. }
  143. }
  144. }
  145. },
  146. /**
  147. * After the drag has been completed, make sure the button being dragged makes it back to
  148. * the correct location and resets its z index
  149. */
  150. endDrag: function() {
  151. //we need to update the cache here for cases where the button was dragged but its
  152. //position in the toolbar did not change
  153. me.updateButtonXCache();
  154. el.moveTo(me.buttonXCache[button.id], undefined, {
  155. duration: me.animationDuration,
  156. scope : this,
  157. callback: function() {
  158. button.resumeEvents();
  159. tbar.fireEvent('reordered', button, tbar);
  160. }
  161. });
  162. el.setStyle('zIndex', this.startZIndex);
  163. }
  164. });
  165. },
  166. /**
  167. * @private
  168. * Creates a DD instance for a given item if it is reorderable
  169. * @param {Mixed} item The item
  170. */
  171. createIfReorderable: function(item) {
  172. if (this.defaultReorderable && item.reorderable == undefined) {
  173. item.reorderable = true;
  174. }
  175. if (item.reorderable && !item.dd) {
  176. if (item.rendered) {
  177. this.createItemDD(item);
  178. } else {
  179. item.on('render', this.createItemDD.createDelegate(this, [item]), this, {single: true});
  180. }
  181. }
  182. },
  183. /**
  184. * Returns an array of items which will be made draggable. This defaults to the contents of this.target.items,
  185. * but can be overridden - e.g. for TabPanels
  186. * @return {Array} The array of items which will be made draggable
  187. */
  188. getItems: function() {
  189. return this.target.items.items;
  190. },
  191. /**
  192. * Adds before-reorder and reorder events to the target component
  193. */
  194. initEvents: function() {
  195. this.target.addEvents(
  196. /**
  197. * @event before-reorder
  198. * Fires before a reorder occurs. Return false to cancel
  199. * @param {Object} mappings Mappings of the old item indexes to new item indexes
  200. * @param {Mixed} component The target component
  201. * @param {Ext.ux.TabReorderer} this The plugin instance
  202. */
  203. 'before-reorder',
  204. /**
  205. * @event reorder
  206. * Fires after a reorder has occured.
  207. * @param {Object} mappings Mappings of the old item indexes to the new item indexes
  208. * @param {Mixed} component The target component
  209. * @param {Ext.ux.TabReorderer} this The plugin instance
  210. */
  211. 'reorder'
  212. );
  213. }
  214. });
  215. /**
  216. * @class Ext.ux.HBoxReorderer
  217. * @extends Ext.ux.Reorderer
  218. * Description
  219. */
  220. Ext.ux.HBoxReorderer = Ext.extend(Ext.ux.Reorderer, {
  221. /**
  222. * Initializes the plugin, decorates the container with additional functionality
  223. */
  224. init: function(container) {
  225. /**
  226. * This is used to store the correct x value of each button in the array. We need to use this
  227. * instead of the button's reported x co-ordinate because the buttons are animated when they move -
  228. * if another onDrag is fired while the button is still moving, the comparison x value will be incorrect
  229. */
  230. this.buttonXCache = {};
  231. container.on({
  232. scope: this,
  233. add : function(container, item) {
  234. this.createIfReorderable(item);
  235. }
  236. });
  237. //super sets a reference to the toolbar in this.target
  238. Ext.ux.HBoxReorderer.superclass.init.apply(this, arguments);
  239. },
  240. /**
  241. * Sets up the given Toolbar item as a draggable
  242. * @param {Mixed} button The item to make draggable (usually an Ext.Button instance)
  243. */
  244. createItemDD: function(button) {
  245. if (button.dd != undefined) {
  246. return;
  247. }
  248. var el = button.getEl(),
  249. id = el.id,
  250. me = this,
  251. tbar = me.target;
  252. button.dd = new Ext.dd.DD(el, undefined, {
  253. isTarget: false
  254. });
  255. el.applyStyles({
  256. position: 'absolute'
  257. });
  258. //if a button has a menu, it is disabled while dragging with this function
  259. var menuDisabler = function() {
  260. return false;
  261. };
  262. Ext.apply(button.dd, {
  263. b4StartDrag: function() {
  264. this.startPosition = el.getXY();
  265. //bump up the z index of the button being dragged but keep a reference to the original
  266. this.startZIndex = el.getStyle('zIndex');
  267. el.setStyle('zIndex', 10000);
  268. button.suspendEvents();
  269. if (button.menu) {
  270. button.menu.on('beforeshow', menuDisabler, me);
  271. }
  272. },
  273. startDrag: function() {
  274. this.constrainTo(tbar.getEl());
  275. this.setYConstraint(0, 0, 0);
  276. },
  277. onDrag: function(e) {
  278. //calculate the button's index within the toolbar and its current midpoint
  279. var buttonX = el.getXY()[0],
  280. deltaX = buttonX - this.startPosition[0],
  281. items = tbar.items.items,
  282. length = items.length,
  283. oldIndex = items.indexOf(button),
  284. newIndex, index, item;
  285. //find which item in the toolbar the midpoint is currently over
  286. for (index = 0; index < length; index++) {
  287. item = items[index];
  288. if (item.reorderable && item.id != button.id) {
  289. //find the midpoint of the button
  290. var box = item.getEl().getBox(),
  291. midpoint = (me.buttonXCache[item.id] || box.x) + (box.width / 2),
  292. movedLeft = oldIndex > index && deltaX < 0 && buttonX < midpoint,
  293. movedRight = oldIndex < index && deltaX > 0 && (buttonX + el.getWidth()) > midpoint;
  294. if (movedLeft || movedRight) {
  295. me[movedLeft ? 'onMovedLeft' : 'onMovedRight'](button, index, oldIndex);
  296. break;
  297. }
  298. }
  299. }
  300. },
  301. /**
  302. * After the drag has been completed, make sure the button being dragged makes it back to
  303. * the correct location and resets its z index
  304. */
  305. endDrag: function() {
  306. //we need to update the cache here for cases where the button was dragged but its
  307. //position in the toolbar did not change
  308. me.updateButtonXCache();
  309. el.moveTo(me.buttonXCache[button.id], el.getY(), {
  310. duration: me.animationDuration,
  311. scope : this,
  312. callback: function() {
  313. button.resumeEvents();
  314. if (button.menu) {
  315. button.menu.un('beforeshow', menuDisabler, me);
  316. }
  317. tbar.fireEvent('reordered', button, tbar);
  318. }
  319. });
  320. el.setStyle('zIndex', this.startZIndex);
  321. }
  322. });
  323. },
  324. onMovedLeft: function(item, newIndex, oldIndex) {
  325. var tbar = this.target,
  326. items = tbar.items.items,
  327. length = items.length,
  328. index;
  329. if (newIndex != undefined && newIndex != oldIndex) {
  330. //move the button currently under drag to its new location
  331. tbar.remove(item, false);
  332. tbar.insert(newIndex, item);
  333. //set the correct x location of each item in the toolbar
  334. this.updateButtonXCache();
  335. for (index = 0; index < length; index++) {
  336. var obj = items[index],
  337. newX = this.buttonXCache[obj.id];
  338. if (item == obj) {
  339. item.dd.startPosition[0] = newX;
  340. } else {
  341. var el = obj.getEl();
  342. el.moveTo(newX, el.getY(), {
  343. duration: this.animationDuration
  344. });
  345. }
  346. }
  347. }
  348. },
  349. onMovedRight: function(item, newIndex, oldIndex) {
  350. this.onMovedLeft.apply(this, arguments);
  351. },
  352. /**
  353. * @private
  354. * Updates the internal cache of button X locations.
  355. */
  356. updateButtonXCache: function() {
  357. var tbar = this.target,
  358. items = tbar.items,
  359. totalX = tbar.getEl().getBox(true).x;
  360. items.each(function(item) {
  361. this.buttonXCache[item.id] = totalX;
  362. totalX += item.getEl().getWidth();
  363. }, this);
  364. }
  365. });