_Layout.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. define("dojox/grid/_Layout", [
  2. "dojo/_base/kernel",
  3. "../main",
  4. "dojo/_base/declare",
  5. "dojo/_base/array",
  6. "dojo/_base/lang",
  7. "dojo/dom-geometry",
  8. "./cells",
  9. "./_RowSelector"
  10. ], function(dojo, dojox, declare, array, lang, domGeometry){
  11. return declare("dojox.grid._Layout", null, {
  12. // summary:
  13. // Controls grid cell layout. Owned by grid and used internally.
  14. constructor: function(inGrid){
  15. this.grid = inGrid;
  16. },
  17. // flat array of grid cells
  18. cells: [],
  19. // structured array of grid cells
  20. structure: null,
  21. // default cell width
  22. defaultWidth: '6em',
  23. // methods
  24. moveColumn: function(sourceViewIndex, destViewIndex, cellIndex, targetIndex, before){
  25. var source_cells = this.structure[sourceViewIndex].cells[0];
  26. var dest_cells = this.structure[destViewIndex].cells[0];
  27. var cell = null;
  28. var cell_ri = 0;
  29. var target_ri = 0;
  30. for(var i=0, c; c=source_cells[i]; i++){
  31. if(c.index == cellIndex){
  32. cell_ri = i;
  33. break;
  34. }
  35. }
  36. cell = source_cells.splice(cell_ri, 1)[0];
  37. cell.view = this.grid.views.views[destViewIndex];
  38. for(i=0, c=null; c=dest_cells[i]; i++){
  39. if(c.index == targetIndex){
  40. target_ri = i;
  41. break;
  42. }
  43. }
  44. if(!before){
  45. target_ri += 1;
  46. }
  47. dest_cells.splice(target_ri, 0, cell);
  48. var sortedCell = this.grid.getCell(this.grid.getSortIndex());
  49. if(sortedCell){
  50. sortedCell._currentlySorted = this.grid.getSortAsc();
  51. }
  52. this.cells = [];
  53. cellIndex = 0;
  54. var v;
  55. for(i=0; v=this.structure[i]; i++){
  56. for(var j=0, cs; cs=v.cells[j]; j++){
  57. for(var k=0; c=cs[k]; k++){
  58. c.index = cellIndex;
  59. this.cells.push(c);
  60. if("_currentlySorted" in c){
  61. var si = cellIndex + 1;
  62. si *= c._currentlySorted ? 1 : -1;
  63. this.grid.sortInfo = si;
  64. delete c._currentlySorted;
  65. }
  66. cellIndex++;
  67. }
  68. }
  69. }
  70. //Fix #9481 - reset idx in cell markup
  71. array.forEach(this.cells, function(c){
  72. var marks = c.markup[2].split(" ");
  73. var oldIdx = parseInt(marks[1].substring(5));//get old "idx"
  74. if(oldIdx != c.index){
  75. marks[1] = "idx=\"" + c.index + "\"";
  76. c.markup[2] = marks.join(" ");
  77. }
  78. });
  79. this.grid.setupHeaderMenu();
  80. //this.grid.renderOnIdle();
  81. },
  82. setColumnVisibility: function(columnIndex, visible){
  83. var cell = this.cells[columnIndex];
  84. if(cell.hidden == visible){
  85. cell.hidden = !visible;
  86. var v = cell.view, w = v.viewWidth;
  87. if(w && w != "auto"){
  88. v._togglingColumn = domGeometry.getMarginBox(cell.getHeaderNode()).w || 0;
  89. }
  90. v.update();
  91. return true;
  92. }else{
  93. return false;
  94. }
  95. },
  96. addCellDef: function(inRowIndex, inCellIndex, inDef){
  97. var self = this;
  98. var getCellWidth = function(inDef){
  99. var w = 0;
  100. if(inDef.colSpan > 1){
  101. w = 0;
  102. }else{
  103. w = inDef.width || self._defaultCellProps.width || self.defaultWidth;
  104. if(!isNaN(w)){
  105. w = w + "em";
  106. }
  107. }
  108. return w;
  109. };
  110. var props = {
  111. grid: this.grid,
  112. subrow: inRowIndex,
  113. layoutIndex: inCellIndex,
  114. index: this.cells.length
  115. };
  116. if(inDef && inDef instanceof dojox.grid.cells._Base){
  117. var new_cell = lang.clone(inDef);
  118. props.unitWidth = getCellWidth(new_cell._props);
  119. new_cell = lang.mixin(new_cell, this._defaultCellProps, inDef._props, props);
  120. return new_cell;
  121. }
  122. var cell_type = inDef.type || inDef.cellType || this._defaultCellProps.type || this._defaultCellProps.cellType || dojox.grid.cells.Cell;
  123. if(lang.isString(cell_type)){
  124. cell_type = lang.getObject(cell_type);
  125. }
  126. props.unitWidth = getCellWidth(inDef);
  127. return new cell_type(lang.mixin({}, this._defaultCellProps, inDef, props));
  128. },
  129. addRowDef: function(inRowIndex, inDef){
  130. var result = [];
  131. var relSum = 0, pctSum = 0, doRel = true;
  132. for(var i=0, def, cell; (def=inDef[i]); i++){
  133. cell = this.addCellDef(inRowIndex, i, def);
  134. result.push(cell);
  135. this.cells.push(cell);
  136. // Check and calculate the sum of all relative widths
  137. if(doRel && cell.relWidth){
  138. relSum += cell.relWidth;
  139. }else if(cell.width){
  140. var w = cell.width;
  141. if(typeof w == "string" && w.slice(-1) == "%"){
  142. pctSum += window.parseInt(w, 10);
  143. }else if(w == "auto"){
  144. // relative widths doesn't play nice with auto - since we
  145. // don't have a way of knowing how much space the auto is
  146. // supposed to take up.
  147. doRel = false;
  148. }
  149. }
  150. }
  151. if(relSum && doRel){
  152. // We have some kind of relWidths specified - so change them to %
  153. array.forEach(result, function(cell){
  154. if(cell.relWidth){
  155. cell.width = cell.unitWidth = ((cell.relWidth / relSum) * (100 - pctSum)) + "%";
  156. }
  157. });
  158. }
  159. return result;
  160. },
  161. addRowsDef: function(inDef){
  162. var result = [];
  163. if(lang.isArray(inDef)){
  164. if(lang.isArray(inDef[0])){
  165. for(var i=0, row; inDef && (row=inDef[i]); i++){
  166. result.push(this.addRowDef(i, row));
  167. }
  168. }else{
  169. result.push(this.addRowDef(0, inDef));
  170. }
  171. }
  172. return result;
  173. },
  174. addViewDef: function(inDef){
  175. this._defaultCellProps = inDef.defaultCell || {};
  176. if(inDef.width && inDef.width == "auto"){
  177. delete inDef.width;
  178. }
  179. return lang.mixin({}, inDef, {cells: this.addRowsDef(inDef.rows || inDef.cells)});
  180. },
  181. setStructure: function(inStructure){
  182. this.fieldIndex = 0;
  183. this.cells = [];
  184. var s = this.structure = [];
  185. if(this.grid.rowSelector){
  186. var sel = { type: dojox._scopeName + ".grid._RowSelector" };
  187. if(lang.isString(this.grid.rowSelector)){
  188. var width = this.grid.rowSelector;
  189. if(width == "false"){
  190. sel = null;
  191. }else if(width != "true"){
  192. sel['width'] = width;
  193. }
  194. }else{
  195. if(!this.grid.rowSelector){
  196. sel = null;
  197. }
  198. }
  199. if(sel){
  200. s.push(this.addViewDef(sel));
  201. }
  202. }
  203. var isCell = function(def){
  204. return ("name" in def || "field" in def || "get" in def);
  205. };
  206. var isRowDef = function(def){
  207. if(lang.isArray(def)){
  208. if(lang.isArray(def[0]) || isCell(def[0])){
  209. return true;
  210. }
  211. }
  212. return false;
  213. };
  214. var isView = function(def){
  215. return (def !== null && lang.isObject(def) &&
  216. ("cells" in def || "rows" in def || ("type" in def && !isCell(def))));
  217. };
  218. if(lang.isArray(inStructure)){
  219. var hasViews = false;
  220. for(var i=0, st; (st=inStructure[i]); i++){
  221. if(isView(st)){
  222. hasViews = true;
  223. break;
  224. }
  225. }
  226. if(!hasViews){
  227. s.push(this.addViewDef({ cells: inStructure }));
  228. }else{
  229. for(i=0; (st=inStructure[i]); i++){
  230. if(isRowDef(st)){
  231. s.push(this.addViewDef({ cells: st }));
  232. }else if(isView(st)){
  233. s.push(this.addViewDef(st));
  234. }
  235. }
  236. }
  237. }else if(isView(inStructure)){
  238. // it's a view object
  239. s.push(this.addViewDef(inStructure));
  240. }
  241. this.cellCount = this.cells.length;
  242. this.grid.setupHeaderMenu();
  243. }
  244. });
  245. });