DropZone.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. Ext.define('erp.view.common.DeskTop.DropZone', {
  2. extend: 'Ext.dd.DropTarget',
  3. constructor: function(portal, cfg) {
  4. this.portal = portal;
  5. Ext.dd.ScrollManager.register(portal.body);
  6. erp.view.common.DeskTop.DropZone.superclass.constructor.call(this, portal.body, cfg);
  7. portal.body.ddScrollConfig = this.ddScrollConfig;
  8. },
  9. ddScrollConfig: {
  10. vthresh: 50,
  11. hthresh: -1,
  12. animate: true,
  13. increment: 200
  14. },
  15. createEvent: function(dd, e, data, col, c, pos) {
  16. return {
  17. portal: this.portal,
  18. panel: data.panel,
  19. columnIndex: col,
  20. column: c,
  21. position: pos,
  22. data: data,
  23. source: dd,
  24. rawEvent: e,
  25. status: this.dropAllowed
  26. };
  27. },
  28. notifyOver: function(dd, e, data) {
  29. var xy = e.getXY(),
  30. portal = this.portal,
  31. proxy = dd.proxy;
  32. // case column widths
  33. if (!this.grid) {
  34. this.grid = this.getGrid();
  35. }
  36. // handle case scroll where scrollbars appear during drag
  37. var cw = portal.body.dom.clientWidth;
  38. if (!this.lastCW) {
  39. // set initial client width
  40. this.lastCW = cw;
  41. } else if (this.lastCW != cw) {
  42. // client width has changed, so refresh layout & grid calcs
  43. this.lastCW = cw;
  44. //portal.doLayout();
  45. this.grid = this.getGrid();
  46. }
  47. // determine column
  48. var colIndex = 0,
  49. colRight = 0,
  50. cols = this.grid.columnX,
  51. len = cols.length,
  52. cmatch = false;
  53. for (len; colIndex < len; colIndex++) {
  54. colRight = cols[colIndex].x + cols[colIndex].w;
  55. if (xy[0] < colRight) {
  56. cmatch = true;
  57. break;
  58. }
  59. }
  60. // no match, fix last index
  61. if (!cmatch) {
  62. colIndex--;
  63. }
  64. // find insert position
  65. var overPortlet, pos = 0,
  66. h = 0,
  67. match = false,
  68. overColumn = portal.items.getAt(colIndex),
  69. portlets = overColumn.items.items,
  70. overSelf = false;
  71. len = portlets.length;
  72. for (len; pos < len; pos++) {
  73. overPortlet = portlets[pos];
  74. h = overPortlet.el.getHeight();
  75. if (h === 0) {
  76. overSelf = true;
  77. } else if ((overPortlet.el.getY() + (h / 2)) > xy[1]) {
  78. match = true;
  79. break;
  80. }
  81. }
  82. pos = (match && overPortlet ? pos : overColumn.items.getCount()) + (overSelf ? -1 : 0);
  83. var overEvent = this.createEvent(dd, e, data, colIndex, overColumn, pos);
  84. if (portal.fireEvent('validatedrop', overEvent) !== false && portal.fireEvent('beforedragover', overEvent) !== false) {
  85. // make sure proxy width is fluid in different width columns
  86. proxy.getProxy().setWidth('auto');
  87. if (overPortlet) {
  88. proxy.moveProxy(overPortlet.el.dom.parentNode, match ? overPortlet.el.dom : null);
  89. } else {
  90. proxy.moveProxy(overColumn.el.dom, null);
  91. }
  92. this.lastPos = {
  93. c: overColumn,
  94. col: colIndex,
  95. p: overSelf || (match && overPortlet) ? pos : false
  96. };
  97. this.scrollPos = portal.body.getScroll();
  98. portal.fireEvent('dragover', overEvent);
  99. return overEvent.status;
  100. } else {
  101. return overEvent.status;
  102. }
  103. },
  104. notifyOut: function() {
  105. delete this.grid;
  106. },
  107. notifyDrop: function(dd, e, data) {
  108. delete this.grid;
  109. if (!this.lastPos) {
  110. return;
  111. }
  112. var c = this.lastPos.c,
  113. col = this.lastPos.col,
  114. pos = this.lastPos.p,
  115. panel = dd.panel,
  116. dropEvent = this.createEvent(dd, e, data, col, c, pos !== false ? pos : c.items.getCount());
  117. if (this.portal.fireEvent('validatedrop', dropEvent) !== false && this.portal.fireEvent('beforedrop', dropEvent) !== false) {
  118. // make sure panel is visible prior to inserting so that the layout doesn't ignore it
  119. panel.el.dom.style.display = '';
  120. if (pos !== false) {
  121. c.insert(pos, panel);
  122. } else {
  123. c.add(panel);
  124. }
  125. dd.proxy.hide();
  126. this.portal.fireEvent('drop', dropEvent);
  127. // scroll position is lost on drop, fix it
  128. var st = this.scrollPos.top;
  129. if (st) {
  130. var d = this.portal.body.dom;
  131. setTimeout(function() {
  132. d.scrollTop = st;
  133. },
  134. 10);
  135. }
  136. }
  137. delete this.lastPos;
  138. return true;
  139. },
  140. // internal cache of body and column coords
  141. getGrid: function() {
  142. var box = this.portal.body.getBox();
  143. box.columnX = [];
  144. this.portal.items.each(function(c) {
  145. box.columnX.push({
  146. x: c.el.getX(),
  147. w: c.el.getWidth()
  148. });
  149. });
  150. return box;
  151. },
  152. // unregister the dropzone from ScrollManager
  153. unreg: function() {
  154. Ext.dd.ScrollManager.unregister(this.portal.body);
  155. erp.view.common.DeskTop.DropZone.superclass.unreg.call(this);
  156. }
  157. });