GridSource.js 4.9 KB

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