| 1 |
define([ |
| 2 |
"dojo/_base/array", |
| 3 |
"dojo/_base/declare", |
| 4 |
"dojo/_base/kernel", |
| 5 |
"dojo/_base/lang", |
| 6 |
"./_StoreLayer" |
| 7 |
], function(array, declare, kernel, lang, layers){ |
| 8 |
|
| 9 |
var _devideToArrays = function(a){ |
| 10 |
a.sort(function(v1, v2){ |
| 11 |
return v1 - v2; |
| 12 |
}); |
| 13 |
var arr = [[a[0]]]; |
| 14 |
for(var i = 1, j = 0; i < a.length; ++i){ |
| 15 |
if(a[i] == a[i-1] + 1){ |
| 16 |
arr[j].push(a[i]); |
| 17 |
}else{ |
| 18 |
arr[++j] = [a[i]]; |
| 19 |
} |
| 20 |
} |
| 21 |
return arr; |
| 22 |
}, |
| 23 |
hitchIfCan = function(scope, func){ |
| 24 |
return func ? lang.hitch(scope || kernel.global, func) : function(){}; |
| 25 |
}; |
| 26 |
|
| 27 |
return declare("dojox.grid.enhanced.plugins._RowMapLayer", layers._StoreLayer, { |
| 28 |
tags: ["reorder"], |
| 29 |
constructor: function(grid){ |
| 30 |
this._map = {}; |
| 31 |
this._revMap = {}; |
| 32 |
this.grid = grid; |
| 33 |
this._oldOnDelete = grid._onDelete; |
| 34 |
var _this = this; |
| 35 |
grid._onDelete = function(item){ |
| 36 |
_this._onDelete(item); |
| 37 |
_this._oldOnDelete.call(grid, item); |
| 38 |
}; |
| 39 |
this._oldSort = grid.sort; |
| 40 |
grid.sort = function(){ |
| 41 |
_this.clearMapping(); |
| 42 |
_this._oldSort.apply(grid, arguments); |
| 43 |
}; |
| 44 |
}, |
| 45 |
uninitialize: function(){ |
| 46 |
this.grid._onDelete = this._oldOnDelete; |
| 47 |
this.grid.sort = this._oldSort; |
| 48 |
}, |
| 49 |
setMapping: function(mapping){ |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
this._store.forEachLayer(function(layer){ |
| 55 |
if(layer.name() === "rowmap"){ |
| 56 |
return false; |
| 57 |
}else if(layer.onRowMappingChange){ |
| 58 |
layer.onRowMappingChange(mapping); |
| 59 |
} |
| 60 |
return true; |
| 61 |
}, false); |
| 62 |
var from, to, origin, revmap = {}; |
| 63 |
for(from in mapping){ |
| 64 |
from = parseInt(from, 10); |
| 65 |
to = mapping[from]; |
| 66 |
if(typeof to == "number"){ |
| 67 |
if(from in this._revMap){ |
| 68 |
origin = this._revMap[from]; |
| 69 |
delete this._revMap[from]; |
| 70 |
}else{ |
| 71 |
origin = from; |
| 72 |
} |
| 73 |
if(origin == to){ |
| 74 |
delete this._map[origin]; |
| 75 |
revmap[to] = "eq"; |
| 76 |
}else{ |
| 77 |
this._map[origin] = to; |
| 78 |
revmap[to] = origin; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
for(to in revmap){ |
| 83 |
if(revmap[to] === "eq"){ |
| 84 |
delete this._revMap[parseInt(to, 10)]; |
| 85 |
}else{ |
| 86 |
this._revMap[parseInt(to, 10)] = revmap[to]; |
| 87 |
} |
| 88 |
} |
| 89 |
}, |
| 90 |
clearMapping: function(){ |
| 91 |
this._map = {}; |
| 92 |
this._revMap = {}; |
| 93 |
}, |
| 94 |
_onDelete: function(item){ |
| 95 |
var idx = this.grid._getItemIndex(item, true); |
| 96 |
if(idx in this._revMap){ |
| 97 |
var rowIdxArr = [], r, i, origin = this._revMap[idx]; |
| 98 |
delete this._map[origin]; |
| 99 |
delete this._revMap[idx]; |
| 100 |
for(r in this._revMap){ |
| 101 |
r = parseInt(r, 10); |
| 102 |
if(this._revMap[r] > origin){ |
| 103 |
--this._revMap[r]; |
| 104 |
} |
| 105 |
} |
| 106 |
for(r in this._revMap){ |
| 107 |
r = parseInt(r, 10); |
| 108 |
if(r > idx){ |
| 109 |
rowIdxArr.push(r); |
| 110 |
} |
| 111 |
} |
| 112 |
rowIdxArr.sort(function(a, b){ |
| 113 |
return b - a; |
| 114 |
}); |
| 115 |
for(i = rowIdxArr.length - 1; i >= 0; --i){ |
| 116 |
r = rowIdxArr[i]; |
| 117 |
this._revMap[r - 1] = this._revMap[r]; |
| 118 |
delete this._revMap[r]; |
| 119 |
} |
| 120 |
this._map = {}; |
| 121 |
for(r in this._revMap){ |
| 122 |
this._map[this._revMap[r]] = r; |
| 123 |
} |
| 124 |
} |
| 125 |
}, |
| 126 |
_fetch: function(userRequest){ |
| 127 |
var mapCount = 0, r; |
| 128 |
var start = userRequest.start || 0; |
| 129 |
for(r in this._revMap){ |
| 130 |
r = parseInt(r, 10); |
| 131 |
if(r >= start){ |
| 132 |
++mapCount; |
| 133 |
} |
| 134 |
} |
| 135 |
if(mapCount > 0){ |
| 136 |
|
| 137 |
var rows = [], i, map = {}, |
| 138 |
count = userRequest.count > 0 ? userRequest.count : -1; |
| 139 |
if(count > 0){ |
| 140 |
for(i = 0; i < count; ++i){ |
| 141 |
r = start + i; |
| 142 |
r = r in this._revMap ? this._revMap[r] : r; |
| 143 |
map[r] = i; |
| 144 |
rows.push(r); |
| 145 |
} |
| 146 |
}else{ |
| 147 |
|
| 148 |
for(i = 0;; ++i){ |
| 149 |
r = start + i; |
| 150 |
if(r in this._revMap){ |
| 151 |
--mapCount; |
| 152 |
r = this._revMap[r]; |
| 153 |
} |
| 154 |
map[r] = i; |
| 155 |
rows.push(r); |
| 156 |
if(mapCount <= 0){ |
| 157 |
break; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
this._subFetch(userRequest, this._getRowArrays(rows), 0, [], map, userRequest.onComplete, start, count); |
| 162 |
return userRequest; |
| 163 |
}else{ |
| 164 |
|
| 165 |
return lang.hitch(this._store, this._originFetch)(userRequest); |
| 166 |
} |
| 167 |
}, |
| 168 |
_getRowArrays: function(rows){ |
| 169 |
return _devideToArrays(rows); |
| 170 |
}, |
| 171 |
_subFetch: function(userRequest, rowArrays, index, result, map, oldOnComplete, start, count){ |
| 172 |
var arr = rowArrays[index], _this = this; |
| 173 |
var urstart = userRequest.start = arr[0]; |
| 174 |
userRequest.count = arr[arr.length - 1] - arr[0] + 1; |
| 175 |
userRequest.onComplete = function(items){ |
| 176 |
array.forEach(items, function(item, i){ |
| 177 |
var r = urstart + i; |
| 178 |
if(r in map){ |
| 179 |
result[map[r]] = item; |
| 180 |
} |
| 181 |
}); |
| 182 |
if(++index == rowArrays.length){ |
| 183 |
|
| 184 |
if(count > 0){ |
| 185 |
userRequest.start = start; |
| 186 |
userRequest.count = count; |
| 187 |
userRequest.onComplete = oldOnComplete; |
| 188 |
hitchIfCan(userRequest.scope, oldOnComplete)(result, userRequest); |
| 189 |
}else{ |
| 190 |
userRequest.start = userRequest.start + items.length; |
| 191 |
delete userRequest.count; |
| 192 |
userRequest.onComplete = function(items){ |
| 193 |
result = result.concat(items); |
| 194 |
userRequest.start = start; |
| 195 |
userRequest.onComplete = oldOnComplete; |
| 196 |
hitchIfCan(userRequest.scope, oldOnComplete)(result, userRequest); |
| 197 |
}; |
| 198 |
_this.originFetch(userRequest); |
| 199 |
} |
| 200 |
}else{ |
| 201 |
_this._subFetch(userRequest, rowArrays, index, result, map, oldOnComplete, start, count); |
| 202 |
} |
| 203 |
}; |
| 204 |
_this.originFetch(userRequest); |
| 205 |
} |
| 206 |
}); |
| 207 |
}); |