dnd_with_dom.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. Ext.onReady(function() {
  2. // Create an object that we'll use to implement and override drag behaviors a little later
  3. var overrides = {
  4. // Called the instance the element is dragged.
  5. b4StartDrag : function() {
  6. // Cache the drag element
  7. if (!this.el) {
  8. this.el = Ext.get(this.getEl());
  9. }
  10. //Cache the original XY Coordinates of the element, we'll use this later.
  11. this.originalXY = this.el.getXY();
  12. },
  13. // Called when element is dropped not anything other than a dropzone with the same ddgroup
  14. onInvalidDrop : function() {
  15. // Set a flag to invoke the animated repair
  16. this.invalidDrop = true;
  17. },
  18. // Called when the drag operation completes
  19. endDrag : function() {
  20. // Invoke the animation if the invalidDrop flag is set to true
  21. if (this.invalidDrop === true) {
  22. // Remove the drop invitation
  23. this.el.removeCls('dropOK');
  24. // Create the animation configuration object
  25. var animCfgObj = {
  26. easing : 'elasticOut',
  27. duration : 1,
  28. scope : this,
  29. callback : function() {
  30. // Remove the position attribute
  31. this.el.dom.style.position = '';
  32. }
  33. };
  34. // Apply the repair animation
  35. this.el.moveTo(this.originalXY[0], this.originalXY[1], animCfgObj);
  36. delete this.invalidDrop;
  37. }
  38. },
  39. // Called upon successful drop of an element on a DDTarget with the same
  40. onDragDrop : function(evtObj, targetElId) {
  41. // Wrap the drop target element with Ext.Element
  42. var dropEl = Ext.get(targetElId);
  43. // Perform the node move only if the drag element's
  44. // parent is not the same as the drop target
  45. if (this.el.dom.parentNode.id != targetElId) {
  46. // Move the element
  47. dropEl.appendChild(this.el);
  48. // Remove the drag invitation
  49. this.onDragOut(evtObj, targetElId);
  50. // Clear the styles
  51. this.el.dom.style.position ='';
  52. this.el.dom.style.top = '';
  53. this.el.dom.style.left = '';
  54. }
  55. else {
  56. // This was an invalid drop, initiate a repair
  57. this.onInvalidDrop();
  58. }
  59. },
  60. // Only called when the drag element is dragged over the a drop target with the same ddgroup
  61. onDragEnter : function(evtObj, targetElId) {
  62. // Colorize the drag target if the drag node's parent is not the same as the drop target
  63. if (targetElId != this.el.dom.parentNode.id) {
  64. this.el.addCls('dropOK');
  65. }
  66. else {
  67. // Remove the invitation
  68. this.onDragOut();
  69. }
  70. },
  71. // Only called when element is dragged out of a dropzone with the same ddgroup
  72. onDragOut : function(evtObj, targetElId) {
  73. this.el.removeCls('dropOK');
  74. }
  75. };
  76. // Configure the cars to be draggable
  77. var carElements = Ext.get('cars').select('div');
  78. Ext.each(carElements.elements, function(el) {
  79. var dd = Ext.create('Ext.dd.DD', el, 'carsDDGroup', {
  80. isTarget : false
  81. });
  82. //Apply the overrides object to the newly created instance of DD
  83. Ext.apply(dd, overrides);
  84. });
  85. var truckElements = Ext.get('trucks').select('div');
  86. Ext.each(truckElements.elements, function(el) {
  87. var dd = Ext.create('Ext.dd.DD', el, 'trucksDDGroup', {
  88. isTarget : false
  89. });
  90. Ext.apply(dd, overrides);
  91. });
  92. // Instantiate instances of Ext.dd.DDTarget for the cars and trucks container
  93. var carsDDTarget = Ext.create('Ext.dd.DDTarget', 'cars','carsDDGroup');
  94. var trucksDDTarget = Ext.create('Ext.dd.DDTarget', 'trucks', 'trucksDDGroup');
  95. // Instantiate instnaces of DDTarget for the rented and repair drop target elements
  96. var rentedDDTarget = Ext.create('Ext.dd.DDTarget', 'rented', 'carsDDGroup');
  97. var repairDDTarget = Ext.create('Ext.dd.DDTarget', 'repair', 'carsDDGroup');
  98. // Ensure that the rented and repair DDTargets will participate in the trucksDDGroup
  99. rentedDDTarget.addToGroup('trucksDDGroup');
  100. repairDDTarget.addToGroup('trucksDDGroup');
  101. });