/ext-4.1.0_b3/src/dd/DDProxy.js

https://bitbucket.org/srogerf/javascript · JavaScript · 202 lines · 91 code · 34 blank · 77 comment · 7 complexity · a192a7013f3fdb04c72fa3d841fadb7e MD5 · raw file

  1. /*
  2. * This is a derivative of the similarly named class in the YUI Library.
  3. * The original license:
  4. * Copyright (c) 2006, Yahoo! Inc. All rights reserved.
  5. * Code licensed under the BSD License:
  6. * http://developer.yahoo.net/yui/license.txt
  7. */
  8. /**
  9. * A DragDrop implementation that inserts an empty, bordered div into
  10. * the document that follows the cursor during drag operations. At the time of
  11. * the click, the frame div is resized to the dimensions of the linked html
  12. * element, and moved to the exact location of the linked element.
  13. *
  14. * References to the "frame" element refer to the single proxy element that
  15. * was created to be dragged in place of all DDProxy elements on the
  16. * page.
  17. */
  18. Ext.define('Ext.dd.DDProxy', {
  19. extend: 'Ext.dd.DD',
  20. statics: {
  21. /**
  22. * The default drag frame div id
  23. * @static
  24. */
  25. dragElId: "ygddfdiv"
  26. },
  27. /**
  28. * Creates new DDProxy.
  29. * @param {String} id the id of the linked html element
  30. * @param {String} sGroup the group of related DragDrop objects
  31. * @param {Object} config an object containing configurable attributes.
  32. * Valid properties for DDProxy in addition to those in DragDrop:
  33. *
  34. * - resizeFrame
  35. * - centerFrame
  36. * - dragElId
  37. */
  38. constructor: function(id, sGroup, config) {
  39. if (id) {
  40. this.init(id, sGroup, config);
  41. this.initFrame();
  42. }
  43. },
  44. /**
  45. * @property {Boolean} resizeFrame
  46. * By default we resize the drag frame to be the same size as the element
  47. * we want to drag (this is to get the frame effect). We can turn it off
  48. * if we want a different behavior.
  49. */
  50. resizeFrame: true,
  51. /**
  52. * @property {Boolean} centerFrame
  53. * By default the frame is positioned exactly where the drag element is, so
  54. * we use the cursor offset provided by Ext.dd.DD. Another option that works only if
  55. * you do not have constraints on the obj is to have the drag frame centered
  56. * around the cursor. Set centerFrame to true for this effect.
  57. */
  58. centerFrame: false,
  59. /**
  60. * Creates the proxy element if it does not yet exist
  61. */
  62. createFrame: function() {
  63. var self = this;
  64. var body = document.body;
  65. if (!body || !body.firstChild) {
  66. setTimeout( function() { self.createFrame(); }, 50 );
  67. return;
  68. }
  69. var div = this.getDragEl();
  70. if (!div) {
  71. div = document.createElement("div");
  72. div.id = this.dragElId;
  73. var s = div.style;
  74. s.position = "absolute";
  75. s.visibility = "hidden";
  76. s.cursor = "move";
  77. s.border = "2px solid #aaa";
  78. s.zIndex = 999;
  79. // appendChild can blow up IE if invoked prior to the window load event
  80. // while rendering a table. It is possible there are other scenarios
  81. // that would cause this to happen as well.
  82. body.insertBefore(div, body.firstChild);
  83. }
  84. },
  85. /**
  86. * Initialization for the drag frame element. Must be called in the
  87. * constructor of all subclasses
  88. */
  89. initFrame: function() {
  90. this.createFrame();
  91. },
  92. applyConfig: function() {
  93. this.callParent();
  94. this.resizeFrame = (this.config.resizeFrame !== false);
  95. this.centerFrame = (this.config.centerFrame);
  96. this.setDragElId(this.config.dragElId || Ext.dd.DDProxy.dragElId);
  97. },
  98. /**
  99. * Resizes the drag frame to the dimensions of the clicked object, positions
  100. * it over the object, and finally displays it
  101. * @param {Number} iPageX X click position
  102. * @param {Number} iPageY Y click position
  103. * @private
  104. */
  105. showFrame: function(iPageX, iPageY) {
  106. var el = this.getEl();
  107. var dragEl = this.getDragEl();
  108. var s = dragEl.style;
  109. this._resizeProxy();
  110. if (this.centerFrame) {
  111. this.setDelta( Math.round(parseInt(s.width, 10)/2),
  112. Math.round(parseInt(s.height, 10)/2) );
  113. }
  114. this.setDragElPos(iPageX, iPageY);
  115. Ext.fly(dragEl).show();
  116. },
  117. /**
  118. * The proxy is automatically resized to the dimensions of the linked
  119. * element when a drag is initiated, unless resizeFrame is set to false
  120. * @private
  121. */
  122. _resizeProxy: function() {
  123. if (this.resizeFrame) {
  124. var el = this.getEl();
  125. Ext.fly(this.getDragEl()).setSize(el.offsetWidth, el.offsetHeight);
  126. }
  127. },
  128. // overrides Ext.dd.DragDrop
  129. b4MouseDown: function(e) {
  130. var x = e.getPageX();
  131. var y = e.getPageY();
  132. this.autoOffset(x, y);
  133. this.setDragElPos(x, y);
  134. },
  135. // overrides Ext.dd.DragDrop
  136. b4StartDrag: function(x, y) {
  137. // show the drag frame
  138. this.showFrame(x, y);
  139. },
  140. // overrides Ext.dd.DragDrop
  141. b4EndDrag: function(e) {
  142. Ext.fly(this.getDragEl()).hide();
  143. },
  144. // overrides Ext.dd.DragDrop
  145. // By default we try to move the element to the last location of the frame.
  146. // This is so that the default behavior mirrors that of Ext.dd.DD.
  147. endDrag: function(e) {
  148. var lel = this.getEl();
  149. var del = this.getDragEl();
  150. // Show the drag frame briefly so we can get its position
  151. del.style.visibility = "";
  152. this.beforeMove();
  153. // Hide the linked element before the move to get around a Safari
  154. // rendering bug.
  155. lel.style.visibility = "hidden";
  156. Ext.dd.DDM.moveToEl(lel, del);
  157. del.style.visibility = "hidden";
  158. lel.style.visibility = "";
  159. this.afterDrag();
  160. },
  161. beforeMove : function(){
  162. },
  163. afterDrag : function(){
  164. },
  165. toString: function() {
  166. return ("DDProxy " + this.id);
  167. }
  168. });