[483] | 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 | // summary: |
---|
| 14 | // Controls grid cell layout. Owned by grid and used internally. |
---|
| 15 | constructor: function(inGrid){ |
---|
| 16 | this.grid = inGrid; |
---|
| 17 | }, |
---|
| 18 | // flat array of grid cells |
---|
| 19 | cells: [], |
---|
| 20 | // structured array of grid cells |
---|
| 21 | structure: null, |
---|
| 22 | // default cell width |
---|
| 23 | defaultWidth: '6em', |
---|
| 24 | |
---|
| 25 | // methods |
---|
| 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 | //Fix #9481 - reset idx in cell markup |
---|
| 79 | array.forEach(this.cells, function(c){ |
---|
| 80 | var marks = c.markup[2].split(" "); |
---|
| 81 | var oldIdx = parseInt(marks[1].substring(5));//get old "idx" |
---|
| 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 | //this.grid.renderOnIdle(); |
---|
| 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 | // Check and calculate the sum of all relative widths |
---|
| 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 | // relative widths doesn't play nice with auto - since we |
---|
| 162 | // don't have a way of knowing how much space the auto is |
---|
| 163 | // supposed to take up. |
---|
| 164 | doRel = false; |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | if(relSum && doRel){ |
---|
| 169 | // We have some kind of relWidths specified - so change them to % |
---|
| 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 | if(lang.isArray(inDef[0])){ |
---|
| 184 | for(var i=0, row; inDef && (row=inDef[i]); i++){ |
---|
| 185 | result.push(this.addRowDef(i, row)); |
---|
| 186 | } |
---|
| 187 | }else{ |
---|
| 188 | result.push(this.addRowDef(0, inDef)); |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | return result; |
---|
| 192 | }, |
---|
| 193 | |
---|
| 194 | addViewDef: function(inDef){ |
---|
| 195 | this._defaultCellProps = inDef.defaultCell || {}; |
---|
| 196 | if(inDef.width && inDef.width == "auto"){ |
---|
| 197 | delete inDef.width; |
---|
| 198 | } |
---|
| 199 | return lang.mixin({}, inDef, {cells: this.addRowsDef(inDef.rows || inDef.cells)}); |
---|
| 200 | }, |
---|
| 201 | |
---|
| 202 | setStructure: function(inStructure){ |
---|
| 203 | this.fieldIndex = 0; |
---|
| 204 | this.cells = []; |
---|
| 205 | var s = this.structure = []; |
---|
| 206 | |
---|
| 207 | if(this.grid.rowSelector){ |
---|
| 208 | var sel = { type: dojox._scopeName + ".grid._RowSelector" }; |
---|
| 209 | |
---|
| 210 | if(lang.isString(this.grid.rowSelector)){ |
---|
| 211 | var width = this.grid.rowSelector; |
---|
| 212 | |
---|
| 213 | if(width == "false"){ |
---|
| 214 | sel = null; |
---|
| 215 | }else if(width != "true"){ |
---|
| 216 | sel['width'] = width; |
---|
| 217 | } |
---|
| 218 | }else{ |
---|
| 219 | if(!this.grid.rowSelector){ |
---|
| 220 | sel = null; |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | if(sel){ |
---|
| 225 | s.push(this.addViewDef(sel)); |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | var isCell = function(def){ |
---|
| 230 | return ("name" in def || "field" in def || "get" in def); |
---|
| 231 | }; |
---|
| 232 | |
---|
| 233 | var isRowDef = function(def){ |
---|
| 234 | if(lang.isArray(def)){ |
---|
| 235 | if(lang.isArray(def[0]) || isCell(def[0])){ |
---|
| 236 | return true; |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | return false; |
---|
| 240 | }; |
---|
| 241 | |
---|
| 242 | var isView = function(def){ |
---|
| 243 | return (def !== null && lang.isObject(def) && |
---|
| 244 | ("cells" in def || "rows" in def || ("type" in def && !isCell(def)))); |
---|
| 245 | }; |
---|
| 246 | |
---|
| 247 | if(lang.isArray(inStructure)){ |
---|
| 248 | var hasViews = false; |
---|
| 249 | for(var i=0, st; (st=inStructure[i]); i++){ |
---|
| 250 | if(isView(st)){ |
---|
| 251 | hasViews = true; |
---|
| 252 | break; |
---|
| 253 | } |
---|
| 254 | } |
---|
| 255 | if(!hasViews){ |
---|
| 256 | s.push(this.addViewDef({ cells: inStructure })); |
---|
| 257 | }else{ |
---|
| 258 | for(i=0; (st=inStructure[i]); i++){ |
---|
| 259 | if(isRowDef(st)){ |
---|
| 260 | s.push(this.addViewDef({ cells: st })); |
---|
| 261 | }else if(isView(st)){ |
---|
| 262 | s.push(this.addViewDef(st)); |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | } |
---|
| 266 | }else if(isView(inStructure)){ |
---|
| 267 | // it's a view object |
---|
| 268 | s.push(this.addViewDef(inStructure)); |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | this.cellCount = this.cells.length; |
---|
| 272 | this.grid.setupHeaderMenu(); |
---|
| 273 | } |
---|
| 274 | }); |
---|
| 275 | }); |
---|