| 1 |
define([ |
| 2 |
"dojo/_base/declare", |
| 3 |
"dojo/_base/array", |
| 4 |
"dojo/_base/lang", |
| 5 |
"dojo/_base/html", |
| 6 |
"../_Plugin", |
| 7 |
"../../EnhancedGrid" |
| 8 |
], function(declare, array, lang, html, _Plugin, EnhancedGrid){ |
| 9 |
|
| 10 |
var CellMerge = declare("dojox.grid.enhanced.plugins.CellMerge", _Plugin, { |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
name: "cellMerge", |
| 34 |
|
| 35 |
constructor: function(grid, args){ |
| 36 |
this.grid = grid; |
| 37 |
this._records = []; |
| 38 |
this._merged = {}; |
| 39 |
if(args && lang.isObject(args)){ |
| 40 |
this._setupConfig(args.mergedCells); |
| 41 |
} |
| 42 |
this._initEvents(); |
| 43 |
this._mixinGrid(); |
| 44 |
}, |
| 45 |
|
| 46 |
mergeCells: function(rowTester, startColumnIndex, endColumnIndex, majorColumnIndex){ |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
var item = this._createRecord({ |
| 68 |
"row": rowTester, |
| 69 |
"start": startColumnIndex, |
| 70 |
"end": endColumnIndex, |
| 71 |
"major": majorColumnIndex |
| 72 |
}); |
| 73 |
if(item){ |
| 74 |
this._updateRows(item); |
| 75 |
} |
| 76 |
return item; |
| 77 |
}, |
| 78 |
unmergeCells: function(mergeHandler){ |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
var idx; |
| 86 |
if(mergeHandler && (idx = array.indexOf(this._records, mergeHandler)) >= 0){ |
| 87 |
this._records.splice(idx, 1); |
| 88 |
this._updateRows(mergeHandler); |
| 89 |
} |
| 90 |
}, |
| 91 |
getMergedCells: function(){ |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
var res = []; |
| 107 |
for(var i in this._merged){ |
| 108 |
res = res.concat(this._merged[i]); |
| 109 |
} |
| 110 |
return res; |
| 111 |
}, |
| 112 |
getMergedCellsByRow: function(rowIndex){ |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
return this._merged[rowIndex] || []; |
| 120 |
}, |
| 121 |
|
| 122 |
|
| 123 |
_setupConfig: function(config){ |
| 124 |
array.forEach(config, this._createRecord, this); |
| 125 |
}, |
| 126 |
_initEvents: function(){ |
| 127 |
array.forEach(this.grid.views.views, function(view){ |
| 128 |
this.connect(view, "onAfterRow", lang.hitch(this, "_onAfterRow", view.index)); |
| 129 |
}, this); |
| 130 |
}, |
| 131 |
_mixinGrid: function(){ |
| 132 |
var g = this.grid; |
| 133 |
g.mergeCells = lang.hitch(this, "mergeCells"); |
| 134 |
g.unmergeCells = lang.hitch(this, "unmergeCells"); |
| 135 |
g.getMergedCells = lang.hitch(this, "getMergedCells"); |
| 136 |
g.getMergedCellsByRow = lang.hitch(this, "getMergedCellsByRow"); |
| 137 |
}, |
| 138 |
_getWidth: function(colIndex){ |
| 139 |
var node = this.grid.layout.cells[colIndex].getHeaderNode(); |
| 140 |
return html.position(node).w; |
| 141 |
}, |
| 142 |
_onAfterRow: function(viewIdx, rowIndex, subrows){ |
| 143 |
try{ |
| 144 |
if(rowIndex < 0){ |
| 145 |
return; |
| 146 |
} |
| 147 |
var result = [], i, j, len = this._records.length, |
| 148 |
cells = this.grid.layout.cells; |
| 149 |
|
| 150 |
for(i = 0; i < len; ++i){ |
| 151 |
var item = this._records[i]; |
| 152 |
var storeItem = this.grid._by_idx[rowIndex]; |
| 153 |
if(item.view == viewIdx && item.row(rowIndex, storeItem && storeItem.item, this.grid.store)){ |
| 154 |
var res = { |
| 155 |
record: item, |
| 156 |
hiddenCells: [], |
| 157 |
totalWidth: 0, |
| 158 |
majorNode: cells[item.major].getNode(rowIndex), |
| 159 |
majorHeaderNode: cells[item.major].getHeaderNode() |
| 160 |
}; |
| 161 |
|
| 162 |
for(j = item.start; j <= item.end; ++j){ |
| 163 |
var w = this._getWidth(j, rowIndex); |
| 164 |
res.totalWidth += w; |
| 165 |
if(j != item.major){ |
| 166 |
res.hiddenCells.push(cells[j].getNode(rowIndex)); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
if(subrows.length != 1 || res.totalWidth > 0){ |
| 171 |
|
| 172 |
for(j = result.length - 1; j >= 0; --j){ |
| 173 |
var r = result[j].record; |
| 174 |
if((r.start >= item.start && r.start <= item.end) || |
| 175 |
(r.end >= item.start && r.end <= item.end)){ |
| 176 |
result.splice(j, 1); |
| 177 |
} |
| 178 |
} |
| 179 |
result.push(res); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
this._merged[rowIndex] = []; |
| 184 |
array.forEach(result, function(res){ |
| 185 |
array.forEach(res.hiddenCells, function(node){ |
| 186 |
html.style(node, "display", "none"); |
| 187 |
}); |
| 188 |
var pbm = html.marginBox(res.majorHeaderNode).w - html.contentBox(res.majorHeaderNode).w; |
| 189 |
var tw = res.totalWidth; |
| 190 |
|
| 191 |
|
| 192 |
if(!html.isWebKit){ |
| 193 |
tw -= pbm; |
| 194 |
} |
| 195 |
|
| 196 |
html.style(res.majorNode, "width", tw + "px"); |
| 197 |
|
| 198 |
res.majorNode.setAttribute("colspan", res.hiddenCells.length + 1); |
| 199 |
|
| 200 |
this._merged[rowIndex].push({ |
| 201 |
"row": rowIndex, |
| 202 |
"start": res.record.start, |
| 203 |
"end": res.record.end, |
| 204 |
"major": res.record.major, |
| 205 |
"handle": res.record |
| 206 |
}); |
| 207 |
}, this); |
| 208 |
}catch(e){ |
| 209 |
console.warn("CellMerge._onAfterRow() error: ", rowIndex, e); |
| 210 |
} |
| 211 |
}, |
| 212 |
_createRecord: function(item){ |
| 213 |
if(this._isValid(item)){ |
| 214 |
item = { |
| 215 |
"row": item.row, |
| 216 |
"start": item.start, |
| 217 |
"end": item.end, |
| 218 |
"major": item.major |
| 219 |
}; |
| 220 |
var cells = this.grid.layout.cells; |
| 221 |
item.view = cells[item.start].view.index; |
| 222 |
item.major = typeof item.major == "number" && !isNaN(item.major) ? item.major : item.start; |
| 223 |
if(typeof item.row == "number"){ |
| 224 |
var r = item.row; |
| 225 |
item.row = function(rowIndex){ |
| 226 |
return rowIndex === r; |
| 227 |
}; |
| 228 |
}else if(typeof item.row == "string"){ |
| 229 |
var id = item.row; |
| 230 |
item.row = function(rowIndex, storeItem, store){ |
| 231 |
try{ |
| 232 |
if(store && storeItem && store.getFeatures()['dojo.data.api.Identity']){ |
| 233 |
return store.getIdentity(storeItem) == id; |
| 234 |
} |
| 235 |
}catch(e){ |
| 236 |
console.error(e); |
| 237 |
} |
| 238 |
return false; |
| 239 |
}; |
| 240 |
} |
| 241 |
if(lang.isFunction(item.row)){ |
| 242 |
this._records.push(item); |
| 243 |
return item; |
| 244 |
} |
| 245 |
} |
| 246 |
return null; |
| 247 |
}, |
| 248 |
_isValid: function(item){ |
| 249 |
var cells = this.grid.layout.cells, |
| 250 |
colCount = cells.length; |
| 251 |
return (lang.isObject(item) && ("row" in item) && ("start" in item) && ("end" in item) && |
| 252 |
item.start >= 0 && item.start < colCount && |
| 253 |
item.end > item.start && item.end < colCount && |
| 254 |
cells[item.start].view.index == cells[item.end].view.index && |
| 255 |
cells[item.start].subrow == cells[item.end].subrow && |
| 256 |
!(typeof item.major == "number" && (item.major < item.start || item.major > item.end))); |
| 257 |
}, |
| 258 |
_updateRows: function(item){ |
| 259 |
var min = null; |
| 260 |
for(var i = 0, count = this.grid.rowCount; i < count; ++i){ |
| 261 |
var storeItem = this.grid._by_idx[i]; |
| 262 |
if(storeItem && item.row(i, storeItem && storeItem.item, this.grid.store)){ |
| 263 |
this.grid.views.updateRow(i); |
| 264 |
if(min === null){ min = i; } |
| 265 |
} |
| 266 |
} |
| 267 |
if(min >= 0){ |
| 268 |
this.grid.scroller.rowHeightChanged(min); |
| 269 |
} |
| 270 |
} |
| 271 |
}); |
| 272 |
|
| 273 |
EnhancedGrid.registerPlugin(CellMerge); |
| 274 |
|
| 275 |
return CellMerge; |
| 276 |
}); |