GridSource.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. define("dojox/grid/enhanced/plugins/GridSource", [
  2. "dojo/_base/declare",
  3. "dojo/_base/array",
  4. "dojo/_base/lang",
  5. "dojo/dnd/Source",
  6. "./DnD"
  7. ], function(declare, array, lang, Source, DnD){
  8. var _joinToArray = function(arrays){
  9. var a = arrays[0];
  10. for(var i = 1; i < arrays.length; ++i){
  11. a = a.concat(arrays[i]);
  12. }
  13. return a;
  14. };
  15. var GridDnDSource = lang.getObject("dojox.grid.enhanced.plugins.GridDnDSource");
  16. return declare("dojox.grid.enhanced.plugins.GridSource", Source, {
  17. // summary:
  18. // A special source that can accept grid contents.
  19. // Only for non-grid widgets or domNodes.
  20. accept: ["grid/cells", "grid/rows", "grid/cols", "text"],
  21. // insertNodesForGrid:
  22. // If you'd like to insert some sort of nodes into your dnd source, turn this on,
  23. // and override getCellContent/getRowContent/getColumnContent
  24. // to populate the dnd data in your desired format.
  25. insertNodesForGrid: false,
  26. markupFactory: function(params, node){
  27. cls = lang.getObject("dojox.grid.enhanced.plugins.GridSource");
  28. return new cls(node, params);
  29. },
  30. checkAcceptance: function(source, nodes){
  31. if(source instanceof GridDnDSource){
  32. if(nodes[0]){
  33. var item = source.getItem(nodes[0].id);
  34. if(item && (array.indexOf(item.type, "grid/rows") >= 0 || array.indexOf(item.type, "grid/cells") >= 0) &&
  35. !source.dndPlugin._allDnDItemsLoaded()){
  36. return false;
  37. }
  38. }
  39. this.sourcePlugin = source.dndPlugin;
  40. }
  41. return this.inherited(arguments);
  42. },
  43. onDraggingOver: function(){
  44. if(this.sourcePlugin){
  45. this.sourcePlugin._isSource = true;
  46. }
  47. },
  48. onDraggingOut: function(){
  49. if(this.sourcePlugin){
  50. this.sourcePlugin._isSource = false;
  51. }
  52. },
  53. onDropExternal: function(source, nodes, copy){
  54. if(source instanceof GridDnDSource){
  55. var ranges = array.map(nodes, function(node){
  56. return source.getItem(node.id).data;
  57. });
  58. var item = source.getItem(nodes[0].id);
  59. var grid = item.dndPlugin.grid;
  60. var type = item.type[0];
  61. var range;
  62. try{
  63. switch(type){
  64. case "grid/cells":
  65. nodes[0].innerHTML = this.getCellContent(grid, ranges[0].min, ranges[0].max) || "";
  66. this.onDropGridCells(grid, ranges[0].min, ranges[0].max);
  67. break;
  68. case "grid/rows":
  69. range = _joinToArray(ranges);
  70. nodes[0].innerHTML = this.getRowContent(grid, range) || "";
  71. this.onDropGridRows(grid, range);
  72. break;
  73. case "grid/cols":
  74. range = _joinToArray(ranges);
  75. nodes[0].innerHTML = this.getColumnContent(grid, range) || "";
  76. this.onDropGridColumns(grid, range);
  77. break;
  78. }
  79. if(this.insertNodesForGrid){
  80. this.selectNone();
  81. this.insertNodes(true, [nodes[0]], this.before, this.current);
  82. }
  83. item.dndPlugin.onDragOut(!copy);
  84. }catch(e){
  85. console.warn("GridSource.onDropExternal() error:",e);
  86. }
  87. }else{
  88. this.inherited(arguments);
  89. }
  90. },
  91. getCellContent: function(grid, leftTopCell, rightBottomCell){
  92. // summary:
  93. // Fill node innerHTML for dnd grid cells.
  94. // sample code:
  95. // var cells = grid.layout.cells;
  96. // var store = grid.store;
  97. // var cache = grid._by_idx;
  98. // var res = "Grid Cells from " + grid.id + ":<br/>";
  99. // for(var r = leftTopCell.row; r <= rightBottomCell.row; ++r){
  100. // for(var c = leftTopCell.col; c <= rightBottomCell.col; ++c){
  101. // res += store.getValue(cache[r].item, cells[c].field) + ", ";
  102. // }
  103. // res = res.substring(0, res.length - 2) + ";<br/>";
  104. // }
  105. // return res;
  106. },
  107. getRowContent: function(grid, rowIndexes){
  108. // summary:
  109. // Fill node innerHTML for dnd grid rows.
  110. // sample code:
  111. // var cells = grid.layout.cells;
  112. // var store = grid.store;
  113. // var cache = grid._by_idx;
  114. // var res = "Grid Rows from " + grid.id + ":<br/>";
  115. // for(var i = 0; i < rowIndexes.length; ++i){
  116. // var r = rowIndexes[i];
  117. // res += "Row " + r + ": ";
  118. // for(var j = 0; j < cells.length; ++j){
  119. // if(!cells[j].hidden){
  120. // res += store.getValue(cache[r].item, cells[j].field) + ", ";
  121. // }
  122. // }
  123. // res = res.substring(0, res.length - 2) + ";<br/>";
  124. // }
  125. // return res;
  126. },
  127. getColumnContent: function(grid, colIndexes){
  128. // summary:
  129. // Fill node innerHTML for dnd grid columns.
  130. // sample code:
  131. // var cells = grid.layout.cells;
  132. // var res = "Grid Columns from " + grid.id + ":";
  133. // for(var i = 0; i < colIndexes.length; ++i){
  134. // var c = colIndexes[i];
  135. // res += (cells[c].name || cells[c].field) + ", ";
  136. // }
  137. // return res.substring(0, res.length - 2);
  138. },
  139. onDropGridCells: function(grid, leftTopCell, rightBottomCell){
  140. },
  141. onDropGridRows: function(grid, rowIndexes){
  142. },
  143. onDropGridColumns: function(grid, colIndexes){
  144. }
  145. });
  146. });