| 1 |
define([ |
| 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 |
|
| 12 |
return declare("dojox.grid._Layout", null, { |
| 13 |
|
| 14 |
|
| 15 |
constructor: function(inGrid){ |
| 16 |
this.grid = inGrid; |
| 17 |
}, |
| 18 |
|
| 19 |
cells: [], |
| 20 |
|
| 21 |
structure: null, |
| 22 |
|
| 23 |
defaultWidth: '6em', |
| 24 |
|
| 25 |
|
| 26 |
moveColumn: function(sourceViewIndex, destViewIndex, cellIndex, targetIndex, before){ |
| 27 |
var source_cells = this.structure[sourceViewIndex].cells[0]; |
| 28 |
var dest_cells = this.structure[destViewIndex].cells[0]; |
| 29 |
|
| 30 |
var cell = null; |
| 31 |
var cell_ri = 0; |
| 32 |
var target_ri = 0; |
| 33 |
|
| 34 |
for(var i=0, c; c=source_cells[i]; i++){ |
| 35 |
if(c.index == cellIndex){ |
| 36 |
cell_ri = i; |
| 37 |
break; |
| 38 |
} |
| 39 |
} |
| 40 |
cell = source_cells.splice(cell_ri, 1)[0]; |
| 41 |
cell.view = this.grid.views.views[destViewIndex]; |
| 42 |
|
| 43 |
for(i=0, c=null; c=dest_cells[i]; i++){ |
| 44 |
if(c.index == targetIndex){ |
| 45 |
target_ri = i; |
| 46 |
break; |
| 47 |
} |
| 48 |
} |
| 49 |
if(!before){ |
| 50 |
target_ri += 1; |
| 51 |
} |
| 52 |
dest_cells.splice(target_ri, 0, cell); |
| 53 |
|
| 54 |
var sortedCell = this.grid.getCell(this.grid.getSortIndex()); |
| 55 |
if(sortedCell){ |
| 56 |
sortedCell._currentlySorted = this.grid.getSortAsc(); |
| 57 |
} |
| 58 |
|
| 59 |
this.cells = []; |
| 60 |
cellIndex = 0; |
| 61 |
var v; |
| 62 |
for(i=0; v=this.structure[i]; i++){ |
| 63 |
for(var j=0, cs; cs=v.cells[j]; j++){ |
| 64 |
for(var k=0; c=cs[k]; k++){ |
| 65 |
c.index = cellIndex; |
| 66 |
this.cells.push(c); |
| 67 |
if("_currentlySorted" in c){ |
| 68 |
var si = cellIndex + 1; |
| 69 |
si *= c._currentlySorted ? 1 : -1; |
| 70 |
this.grid.sortInfo = si; |
| 71 |
delete c._currentlySorted; |
| 72 |
} |
| 73 |
cellIndex++; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
array.forEach(this.cells, function(c){ |
| 80 |
var marks = c.markup[2].split(" "); |
| 81 |
var oldIdx = parseInt(marks[1].substring(5)); |
| 82 |
if(oldIdx != c.index){ |
| 83 |
marks[1] = "idx=\"" + c.index + "\""; |
| 84 |
c.markup[2] = marks.join(" "); |
| 85 |
} |
| 86 |
}); |
| 87 |
|
| 88 |
this.grid.setupHeaderMenu(); |
| 89 |
|
| 90 |
}, |
| 91 |
|
| 92 |
setColumnVisibility: function(columnIndex, visible){ |
| 93 |
var cell = this.cells[columnIndex]; |
| 94 |
if(cell.hidden == visible){ |
| 95 |
cell.hidden = !visible; |
| 96 |
var v = cell.view, w = v.viewWidth; |
| 97 |
if(w && w != "auto"){ |
| 98 |
v._togglingColumn = domGeometry.getMarginBox(cell.getHeaderNode()).w || 0; |
| 99 |
} |
| 100 |
v.update(); |
| 101 |
return true; |
| 102 |
}else{ |
| 103 |
return false; |
| 104 |
} |
| 105 |
}, |
| 106 |
|
| 107 |
addCellDef: function(inRowIndex, inCellIndex, inDef){ |
| 108 |
var self = this; |
| 109 |
var getCellWidth = function(inDef){ |
| 110 |
var w = 0; |
| 111 |
if(inDef.colSpan > 1){ |
| 112 |
w = 0; |
| 113 |
}else{ |
| 114 |
w = inDef.width || self._defaultCellProps.width || self.defaultWidth; |
| 115 |
|
| 116 |
if(!isNaN(w)){ |
| 117 |
w = w + "em"; |
| 118 |
} |
| 119 |
} |
| 120 |
return w; |
| 121 |
}; |
| 122 |
|
| 123 |
var props = { |
| 124 |
grid: this.grid, |
| 125 |
subrow: inRowIndex, |
| 126 |
layoutIndex: inCellIndex, |
| 127 |
index: this.cells.length |
| 128 |
}; |
| 129 |
|
| 130 |
if(inDef && inDef instanceof dojox.grid.cells._Base){ |
| 131 |
var new_cell = lang.clone(inDef); |
| 132 |
props.unitWidth = getCellWidth(new_cell._props); |
| 133 |
new_cell = lang.mixin(new_cell, this._defaultCellProps, inDef._props, props); |
| 134 |
return new_cell; |
| 135 |
} |
| 136 |
|
| 137 |
var cell_type = inDef.type || inDef.cellType || this._defaultCellProps.type || this._defaultCellProps.cellType || dojox.grid.cells.Cell; |
| 138 |
if(lang.isString(cell_type)){ |
| 139 |
cell_type = lang.getObject(cell_type); |
| 140 |
} |
| 141 |
|
| 142 |
props.unitWidth = getCellWidth(inDef); |
| 143 |
return new cell_type(lang.mixin({}, this._defaultCellProps, inDef, props)); |
| 144 |
}, |
| 145 |
|
| 146 |
addRowDef: function(inRowIndex, inDef){ |
| 147 |
var result = []; |
| 148 |
var relSum = 0, pctSum = 0, doRel = true; |
| 149 |
for(var i=0, def, cell; (def=inDef[i]); i++){ |
| 150 |
cell = this.addCellDef(inRowIndex, i, def); |
| 151 |
result.push(cell); |
| 152 |
this.cells.push(cell); |
| 153 |
|
| 154 |
if(doRel && cell.relWidth){ |
| 155 |
relSum += cell.relWidth; |
| 156 |
}else if(cell.width){ |
| 157 |
var w = cell.width; |
| 158 |
if(typeof w == "string" && w.slice(-1) == "%"){ |
| 159 |
pctSum += window.parseInt(w, 10); |
| 160 |
}else if(w == "auto"){ |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
doRel = false; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
if(relSum && doRel){ |
| 169 |
|
| 170 |
array.forEach(result, function(cell){ |
| 171 |
if(cell.relWidth){ |
| 172 |
cell.width = cell.unitWidth = ((cell.relWidth / relSum) * (100 - pctSum)) + "%"; |
| 173 |
} |
| 174 |
}); |
| 175 |
} |
| 176 |
return result; |
| 177 |
|
| 178 |
}, |
| 179 |
|
| 180 |
addRowsDef: function(inDef){ |
| 181 |
var result = []; |
| 182 |
if(lang.isArray(inDef)){ |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
if(lang.isArrayLike(inDef[0])){ |
| 187 |
for(var i=0, row; inDef && (row=inDef[i]); i++){ |
| 188 |
result.push(this.addRowDef(i, row)); |
| 189 |
} |
| 190 |
}else{ |
| 191 |
result.push(this.addRowDef(0, inDef)); |
| 192 |
} |
| 193 |
} |
| 194 |
return result; |
| 195 |
}, |
| 196 |
|
| 197 |
addViewDef: function(inDef){ |
| 198 |
this._defaultCellProps = inDef.defaultCell || {}; |
| 199 |
if(inDef.width && inDef.width == "auto"){ |
| 200 |
delete inDef.width; |
| 201 |
} |
| 202 |
return lang.mixin({}, inDef, {cells: this.addRowsDef(inDef.rows || inDef.cells)}); |
| 203 |
}, |
| 204 |
|
| 205 |
setStructure: function(inStructure){ |
| 206 |
this.fieldIndex = 0; |
| 207 |
this.cells = []; |
| 208 |
var s = this.structure = []; |
| 209 |
|
| 210 |
if(this.grid.rowSelector){ |
| 211 |
var sel = { type: dojox._scopeName + ".grid._RowSelector" }; |
| 212 |
|
| 213 |
if(lang.isString(this.grid.rowSelector)){ |
| 214 |
var width = this.grid.rowSelector; |
| 215 |
|
| 216 |
if(width == "false"){ |
| 217 |
sel = null; |
| 218 |
}else if(width != "true"){ |
| 219 |
sel['width'] = width; |
| 220 |
} |
| 221 |
}else{ |
| 222 |
if(!this.grid.rowSelector){ |
| 223 |
sel = null; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
if(sel){ |
| 228 |
s.push(this.addViewDef(sel)); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
var isCell = function(def){ |
| 233 |
return ("name" in def || "field" in def || "get" in def); |
| 234 |
}; |
| 235 |
|
| 236 |
var isRowDef = function(def){ |
| 237 |
if(lang.isArray(def)){ |
| 238 |
if(lang.isArray(def[0]) || isCell(def[0])){ |
| 239 |
return true; |
| 240 |
} |
| 241 |
} |
| 242 |
return false; |
| 243 |
}; |
| 244 |
|
| 245 |
var isView = function(def){ |
| 246 |
return (def !== null && lang.isObject(def) && |
| 247 |
("cells" in def || "rows" in def || ("type" in def && !isCell(def)))); |
| 248 |
}; |
| 249 |
|
| 250 |
if(lang.isArrayLike(inStructure)){ |
| 251 |
var hasViews = false; |
| 252 |
for(var i=0, st; (st=inStructure[i]); i++){ |
| 253 |
if(isView(st)){ |
| 254 |
hasViews = true; |
| 255 |
break; |
| 256 |
} |
| 257 |
} |
| 258 |
if(!hasViews){ |
| 259 |
s.push(this.addViewDef({ cells: inStructure })); |
| 260 |
}else{ |
| 261 |
for(i=0; (st=inStructure[i]); i++){ |
| 262 |
if(isRowDef(st)){ |
| 263 |
s.push(this.addViewDef({ cells: st })); |
| 264 |
}else if(isView(st)){ |
| 265 |
s.push(this.addViewDef(st)); |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
}else if(isView(inStructure)){ |
| 270 |
|
| 271 |
s.push(this.addViewDef(inStructure)); |
| 272 |
} |
| 273 |
|
| 274 |
this.cellCount = this.cells.length; |
| 275 |
this.grid.setupHeaderMenu(); |
| 276 |
} |
| 277 |
}); |
| 278 |
}); |