ComponentDragger.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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-util-ComponentDragger'>/**
  19. </span> * A subclass of Ext.dd.DragTracker which handles dragging any Component.
  20. *
  21. * This is configured with a Component to be made draggable, and a config object for the {@link Ext.dd.DragTracker}
  22. * class.
  23. *
  24. * A {@link #delegate} may be provided which may be either the element to use as the mousedown target or a {@link
  25. * Ext.DomQuery} selector to activate multiple mousedown targets.
  26. *
  27. * When the Component begins to be dragged, its `beginDrag` method will be called if implemented.
  28. *
  29. * When the drag ends, its `endDrag` method will be called if implemented.
  30. */
  31. Ext.define('Ext.util.ComponentDragger', {
  32. extend: 'Ext.dd.DragTracker',
  33. <span id='Ext-util-ComponentDragger-cfg-constrain'> /**
  34. </span> * @cfg {Boolean} constrain
  35. * Specify as `true` to constrain the Component to within the bounds of the {@link #constrainTo} region.
  36. */
  37. <span id='Ext-util-ComponentDragger-cfg-delegate'> /**
  38. </span> * @cfg {String/Ext.Element} delegate
  39. * A {@link Ext.DomQuery DomQuery} selector which identifies child elements within the Component's encapsulating
  40. * Element which are the drag handles. This limits dragging to only begin when the matching elements are
  41. * mousedowned.
  42. *
  43. * This may also be a specific child element within the Component's encapsulating element to use as the drag handle.
  44. */
  45. <span id='Ext-util-ComponentDragger-cfg-constrainDelegate'> /**
  46. </span> * @cfg {Boolean} constrainDelegate
  47. * Specify as `true` to constrain the drag handles within the {@link #constrainTo} region.
  48. */
  49. autoStart: 500,
  50. <span id='Ext-util-ComponentDragger-method-constructor'> /**
  51. </span> * Creates new ComponentDragger.
  52. * @param {Object} comp The Component to provide dragging for.
  53. * @param {Object} [config] Config object
  54. */
  55. constructor: function(comp, config) {
  56. this.comp = comp;
  57. this.initialConstrainTo = config.constrainTo;
  58. this.callParent([ config ]);
  59. },
  60. onStart: function(e) {
  61. var me = this,
  62. comp = me.comp;
  63. // Cache the start [X, Y] array
  64. this.startPosition = comp.el.getXY();
  65. // If client Component has a ghost method to show a lightweight version of itself
  66. // then use that as a drag proxy unless configured to liveDrag.
  67. if (comp.ghost &amp;&amp; !comp.liveDrag) {
  68. me.proxy = comp.ghost();
  69. me.dragTarget = me.proxy.header.el;
  70. }
  71. // Set the constrainTo Region before we start dragging.
  72. if (me.constrain || me.constrainDelegate) {
  73. me.constrainTo = me.calculateConstrainRegion();
  74. }
  75. if (comp.beginDrag) {
  76. comp.beginDrag();
  77. }
  78. },
  79. calculateConstrainRegion: function() {
  80. var me = this,
  81. comp = me.comp,
  82. c = me.initialConstrainTo,
  83. delegateRegion,
  84. elRegion,
  85. dragEl = me.proxy ? me.proxy.el : comp.el,
  86. shadowSize = (!me.constrainDelegate &amp;&amp; dragEl.shadow &amp;&amp; !dragEl.shadowDisabled) ? dragEl.shadow.getShadowSize() : 0;
  87. // The configured constrainTo might be a Region or an element
  88. if (!(c instanceof Ext.util.Region)) {
  89. c = Ext.fly(c).getViewRegion();
  90. }
  91. // Reduce the constrain region to allow for shadow
  92. if (shadowSize) {
  93. c.adjust(shadowSize[0], -shadowSize[1], -shadowSize[2], shadowSize[3]);
  94. }
  95. // If they only want to constrain the *delegate* to within the constrain region,
  96. // adjust the region to be larger based on the insets of the delegate from the outer
  97. // edges of the Component.
  98. if (!me.constrainDelegate) {
  99. delegateRegion = Ext.fly(me.dragTarget).getRegion();
  100. elRegion = dragEl.getRegion();
  101. c.adjust(
  102. delegateRegion.top - elRegion.top,
  103. delegateRegion.right - elRegion.right,
  104. delegateRegion.bottom - elRegion.bottom,
  105. delegateRegion.left - elRegion.left
  106. );
  107. }
  108. return c;
  109. },
  110. // Move either the ghost Component or the target Component to its new position on drag
  111. onDrag: function(e) {
  112. var me = this,
  113. comp = (me.proxy &amp;&amp; !me.comp.liveDrag) ? me.proxy : me.comp,
  114. offset = me.getOffset(me.constrain || me.constrainDelegate ? 'dragTarget' : null);
  115. comp.setPagePosition(me.startPosition[0] + offset[0], me.startPosition[1] + offset[1]);
  116. },
  117. onEnd: function(e) {
  118. var comp = this.comp;
  119. if (this.proxy &amp;&amp; !comp.liveDrag) {
  120. comp.unghost();
  121. }
  122. if (comp.endDrag) {
  123. comp.endDrag();
  124. }
  125. }
  126. });</pre>
  127. </body>
  128. </html>