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.

202 lines
7.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>The source code</title>
  5. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  6. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  7. </head>
  8. <body onload="prettyPrint();">
  9. <pre class="prettyprint lang-js">/*!
  10. * Ext JS Library 3.3.1
  11. * Copyright(c) 2006-2010 Sencha Inc.
  12. * licensing@sencha.com
  13. * http://www.sencha.com/license
  14. */
  15. <div id="cls-Ext.ux.ToolbarReorderer"></div>/**
  16. * @class Ext.ux.ToolbarReorderer
  17. * @extends Ext.ux.Reorderer
  18. * Plugin which can be attached to any Ext.Toolbar instance. Provides ability to reorder toolbar items
  19. * with drag and drop. Example:
  20. * <pre>
  21. * new Ext.Toolbar({
  22. * plugins: [
  23. * new Ext.ux.ToolbarReorderer({
  24. * defaultReorderable: true
  25. * })
  26. * ],
  27. * items: [
  28. * {text: 'Button 1', reorderable: false},
  29. * {text: 'Button 2'},
  30. * {text: 'Button 3'}
  31. * ]
  32. * });
  33. * </pre>
  34. * In the example above, buttons 2 and 3 will be reorderable via drag and drop. An event named 'reordered'
  35. * is added to the Toolbar, and is fired whenever a reorder has been completed.
  36. */
  37. Ext.ux.ToolbarReorderer = Ext.extend(Ext.ux.Reorderer, {
  38. <div id="method-Ext.ux.ToolbarReorderer-init"></div>/**
  39. * Initializes the plugin, decorates the toolbar with additional functionality
  40. */
  41. init: function(toolbar) {
  42. <div id="prop-Ext.ux.ToolbarReorderer-buttonXCache"></div>/**
  43. * This is used to store the correct x value of each button in the array. We need to use this
  44. * instead of the button's reported x co-ordinate because the buttons are animated when they move -
  45. * if another onDrag is fired while the button is still moving, the comparison x value will be incorrect
  46. */
  47. this.buttonXCache = {};
  48. toolbar.on({
  49. scope: this,
  50. add : function(toolbar, item) {
  51. this.createIfReorderable(item);
  52. }
  53. });
  54. //super sets a reference to the toolbar in this.target
  55. Ext.ux.ToolbarReorderer.superclass.init.apply(this, arguments);
  56. },
  57. <div id="method-Ext.ux.ToolbarReorderer-createItemDD"></div>/**
  58. * Sets up the given Toolbar item as a draggable
  59. * @param {Mixed} button The item to make draggable (usually an Ext.Button instance)
  60. */
  61. createItemDD: function(button) {
  62. if (button.dd != undefined) {
  63. return;
  64. }
  65. var el = button.getEl(),
  66. id = el.id,
  67. tbar = this.target,
  68. me = this;
  69. button.dd = new Ext.dd.DD(el, undefined, {
  70. isTarget: false
  71. });
  72. //if a button has a menu, it is disabled while dragging with this function
  73. var menuDisabler = function() {
  74. return false;
  75. };
  76. Ext.apply(button.dd, {
  77. b4StartDrag: function() {
  78. this.startPosition = el.getXY();
  79. //bump up the z index of the button being dragged but keep a reference to the original
  80. this.startZIndex = el.getStyle('zIndex');
  81. el.setStyle('zIndex', 10000);
  82. button.suspendEvents();
  83. if (button.menu) {
  84. button.menu.on('beforeshow', menuDisabler, me);
  85. }
  86. },
  87. startDrag: function() {
  88. this.constrainTo(tbar.getEl());
  89. this.setYConstraint(0, 0, 0);
  90. },
  91. onDrag: function(e) {
  92. //calculate the button's index within the toolbar and its current midpoint
  93. var buttonX = el.getXY()[0],
  94. deltaX = buttonX - this.startPosition[0],
  95. items = tbar.items.items,
  96. oldIndex = items.indexOf(button),
  97. newIndex;
  98. //find which item in the toolbar the midpoint is currently over
  99. for (var index = 0; index < items.length; index++) {
  100. var item = items[index];
  101. if (item.reorderable && item.id != button.id) {
  102. //find the midpoint of the button
  103. var box = item.getEl().getBox(),
  104. midpoint = (me.buttonXCache[item.id] || box.x) + (box.width / 2),
  105. movedLeft = oldIndex > index && deltaX < 0 && buttonX < midpoint,
  106. movedRight = oldIndex < index && deltaX > 0 && (buttonX + el.getWidth()) > midpoint;
  107. if (movedLeft || movedRight) {
  108. me[movedLeft ? 'onMovedLeft' : 'onMovedRight'](button, index, oldIndex);
  109. break;
  110. }
  111. }
  112. }
  113. },
  114. <div id="method-Ext.ux.ToolbarReorderer-endDrag"></div>/**
  115. * After the drag has been completed, make sure the button being dragged makes it back to
  116. * the correct location and resets its z index
  117. */
  118. endDrag: function() {
  119. //we need to update the cache here for cases where the button was dragged but its
  120. //position in the toolbar did not change
  121. me.updateButtonXCache();
  122. el.moveTo(me.buttonXCache[button.id], el.getY(), {
  123. duration: me.animationDuration,
  124. scope : this,
  125. callback: function() {
  126. button.resumeEvents();
  127. if (button.menu) {
  128. button.menu.un('beforeshow', menuDisabler, me);
  129. }
  130. tbar.fireEvent('reordered', button, tbar);
  131. }
  132. });
  133. el.setStyle('zIndex', this.startZIndex);
  134. }
  135. });
  136. },
  137. onMovedLeft: function(item, newIndex, oldIndex) {
  138. var tbar = this.target,
  139. items = tbar.items.items;
  140. if (newIndex != undefined && newIndex != oldIndex) {
  141. //move the button currently under drag to its new location
  142. tbar.remove(item, false);
  143. tbar.insert(newIndex, item);
  144. //set the correct x location of each item in the toolbar
  145. this.updateButtonXCache();
  146. for (var index = 0; index < items.length; index++) {
  147. var obj = items[index],
  148. newX = this.buttonXCache[obj.id];
  149. if (item == obj) {
  150. item.dd.startPosition[0] = newX;
  151. } else {
  152. var el = obj.getEl();
  153. el.moveTo(newX, el.getY(), {duration: this.animationDuration});
  154. }
  155. }
  156. }
  157. },
  158. onMovedRight: function(item, newIndex, oldIndex) {
  159. this.onMovedLeft.apply(this, arguments);
  160. },
  161. /**
  162. * @private
  163. * Updates the internal cache of button X locations.
  164. */
  165. updateButtonXCache: function() {
  166. var tbar = this.target,
  167. items = tbar.items,
  168. totalX = tbar.getEl().getBox(true).x;
  169. items.each(function(item) {
  170. this.buttonXCache[item.id] = totalX;
  171. totalX += item.getEl().getWidth();
  172. }, this);
  173. }
  174. });</pre>
  175. </body>
  176. </html>