griddraganddrop.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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: 'menuindex'
  6. ,gridDropTarget: this
  7. ,notifyDrop: function(dd, e, data){
  8. if (!data.grid.parseSortField) {
  9. data.grid.parseSortField = function (value) {return value;}
  10. }
  11. if (!data.grid.parsePermanentSort) {
  12. data.grid.parsePermanentSort = function (value) {return false;}
  13. }
  14. if (data.grid.parseSortField(data.grid.config.baseParams.sort) != this.sortCol) {
  15. if (data.grid.store.sortInfo == undefined || data.grid.parseSortField(data.grid.store.sortInfo.field) != this.sortCol) {
  16. return false;
  17. }
  18. } else {
  19. if (data.grid.store.sortInfo != undefined && data.grid.parseSortField(data.grid.store.sortInfo.field) != this.sortCol) {
  20. return false;
  21. }
  22. }
  23. if (data.grid.parsePermanentSort(this.sortCol)) {
  24. return false;
  25. }
  26. var search = Ext.getCmp('collections-child-search');
  27. var filter = Ext.getCmp('collections-grid-filter-status');
  28. if (search != undefined && filter != undefined) {
  29. if (search.getValue() != '' || filter.getValue() != '') {
  30. return false;
  31. }
  32. }
  33. // determine the row
  34. var t = Ext.lib.Event.getTarget(e);
  35. var rindex = this.grid.getView().findRowIndex(t);
  36. if (rindex === false) return false;
  37. if (rindex == data.rowIndex) return false;
  38. var menuIndexes = {};
  39. menuIndexes.oldIndex = this.grid.store.data.items[data.rowIndex].data[this.sortCol];
  40. menuIndexes.newIndex = this.grid.store.data.items[rindex].data[this.sortCol];
  41. // fire the before move/copy event
  42. if (this.gridDropTarget.fireEvent(this.copy?'beforerowcopy':'beforerowmove', this.gridDropTarget, menuIndexes.oldIndex, menuIndexes.newIndex, data.selections) === false) return false;
  43. // update the store
  44. var ds = this.grid.getStore();
  45. if (!this.copy) {
  46. for(i = 0; i < data.selections.length; i++) {
  47. ds.remove(ds.getById(data.selections[i].id));
  48. }
  49. }
  50. ds.insert(rindex,data.selections);
  51. // re-select the row(s)
  52. var sm = this.grid.getSelectionModel();
  53. if (sm) sm.selectRecords(data.selections);
  54. // fire the after move/copy event
  55. this.gridDropTarget.fireEvent(this.copy?'afterrowcopy':'afterrowmove', this.gridDropTarget, menuIndexes.oldIndex, menuIndexes.newIndex, data.selections);
  56. return true;
  57. }
  58. ,notifyOver: function(dd, e, data) {
  59. this.grid.getView().dragZone.ddel.innerHTML = this.grid.getDragDropText();
  60. this.grid.getView().dragZone.proxy.update(this.grid.getView().dragZone.ddel);
  61. if (!data.grid.parseSortField) {
  62. data.grid.parseSortField = function (value) {return value;}
  63. }
  64. if (!data.grid.parsePermanentSort) {
  65. data.grid.parsePermanentSort = function (value) {return false;}
  66. }
  67. if (data.grid.parseSortField(data.grid.config.baseParams.sort) != this.sortCol) {
  68. if (data.grid.store.sortInfo == undefined || data.grid.parseSortField(data.grid.store.sortInfo.field) != this.sortCol) {
  69. return this.dropNotAllowed;
  70. }
  71. } else {
  72. if (data.grid.store.sortInfo != undefined && data.grid.parseSortField(data.grid.store.sortInfo.field) != this.sortCol) {
  73. return this.dropNotAllowed;
  74. }
  75. }
  76. if (data.grid.parsePermanentSort(this.sortCol)) {
  77. return false;
  78. }
  79. var search = Ext.getCmp('collections-child-search');
  80. var filter = Ext.getCmp('collections-grid-filter-status');
  81. if (search != undefined && filter != undefined) {
  82. if (search.getValue() != '' || filter.getValue() != '') {
  83. return this.dropNotAllowed;
  84. }
  85. }
  86. var t = Ext.lib.Event.getTarget(e);
  87. var rindex = this.grid.getView().findRowIndex(t);
  88. if (rindex == data.rowIndex) rindex = false;
  89. return (rindex === false)? this.dropNotAllowed : this.dropAllowed;
  90. }
  91. });
  92. if (config) {
  93. Ext.apply(this.target, config);
  94. if (config.listeners) Ext.apply(this,{listeners: config.listeners});
  95. }
  96. this.addEvents({
  97. "beforerowmove": true
  98. ,"afterrowmove": true
  99. ,"beforerowcopy": true
  100. ,"afterrowcopy": true
  101. });
  102. Ext.ux.dd.GridReorderDropTarget.superclass.constructor.call(this);
  103. };
  104. Ext.extend(Ext.ux.dd.GridReorderDropTarget, Ext.util.Observable, {
  105. getTarget: function() {
  106. return this.target;
  107. }
  108. ,getGrid: function() {
  109. return this.target.grid;
  110. }
  111. ,getCopy: function() {
  112. return this.target.copy?true:false;
  113. }
  114. ,setCopy: function(b) {
  115. this.target.copy = b?true:false;
  116. }
  117. });