griddraganddrop.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. Ext.ux.dd.GridReorderDropTarget = function (grid, config) {
  2. this.target = new Ext.dd.DropTarget(grid.getEl(), {
  3. ddGroup: grid.ddGroup || 'GridDD',
  4. grid: grid,
  5. sortCol: 'position',
  6. isGridFiltered: null,
  7. gridDropTarget: this,
  8. notifyDrop: function (dd, e, data) {
  9. if (data.grid.store.sortInfo && data.grid.store.sortInfo.field != this.sortCol) {
  10. return false;
  11. }
  12. if (typeof this.isGridFiltered === "function") {
  13. if (this.isGridFiltered()) {
  14. return this.dropNotAllowed;
  15. }
  16. }
  17. // determine the row
  18. var t = Ext.lib.Event.getTarget(e);
  19. var rindex = this.grid.getView().findRowIndex(t);
  20. if (rindex === false) return false;
  21. if (rindex == data.rowIndex) return false;
  22. var menuIndexes = {};
  23. menuIndexes.oldIndex = this.grid.store.data.items[data.rowIndex].data[this.sortCol];
  24. menuIndexes.newIndex = this.grid.store.data.items[rindex].data[this.sortCol];
  25. // fire the before move/copy event
  26. if (this.gridDropTarget.fireEvent(this.copy ? 'beforerowcopy' : 'beforerowmove', this.gridDropTarget, menuIndexes.oldIndex, menuIndexes.newIndex, data.selections) === false) return false;
  27. // update the store
  28. var ds = this.grid.getStore();
  29. if (!this.copy) {
  30. for (i = 0; i < data.selections.length; i++) {
  31. ds.remove(ds.getById(data.selections[i].id));
  32. }
  33. }
  34. ds.insert(rindex, data.selections);
  35. // re-select the row(s)
  36. var sm = this.grid.getSelectionModel();
  37. if (sm) sm.selectRecords(data.selections);
  38. // fire the after move/copy event
  39. this.gridDropTarget.fireEvent(this.copy ? 'afterrowcopy' : 'afterrowmove', this.gridDropTarget, menuIndexes.oldIndex, menuIndexes.newIndex, data.selections);
  40. return true;
  41. },
  42. notifyOver: function (dd, e, data) {
  43. this.grid.getView().dragZone.ddel.innerHTML = this.grid.getDragDropText();
  44. this.grid.getView().dragZone.proxy.update(this.grid.getView().dragZone.ddel);
  45. if (typeof this.isGridFiltered === "function") {
  46. if (this.isGridFiltered()) {
  47. return this.dropNotAllowed;
  48. }
  49. }
  50. if (data.grid.store.sortInfo && data.grid.store.sortInfo.field != this.sortCol) {
  51. return this.dropNotAllowed;
  52. }
  53. var t = Ext.lib.Event.getTarget(e);
  54. var rindex = this.grid.getView().findRowIndex(t);
  55. if (rindex == data.rowIndex) rindex = false;
  56. return (rindex === false) ? this.dropNotAllowed : this.dropAllowed;
  57. }
  58. });
  59. if (config) {
  60. Ext.apply(this.target, config);
  61. if (config.listeners) Ext.apply(this, {listeners: config.listeners});
  62. }
  63. this.addEvents({
  64. "beforerowmove": true,
  65. "afterrowmove": true,
  66. "beforerowcopy": true,
  67. "afterrowcopy": true
  68. });
  69. Ext.ux.dd.GridReorderDropTarget.superclass.constructor.call(this);
  70. };
  71. Ext.extend(Ext.ux.dd.GridReorderDropTarget, Ext.util.Observable, {
  72. getTarget: function () {
  73. return this.target;
  74. },
  75. getGrid: function () {
  76. return this.target.grid;
  77. },
  78. getCopy: function () {
  79. return this.target.copy ? true : false;
  80. },
  81. setCopy: function (b) {
  82. this.target.copy = b ? true : false;
  83. }
  84. });