| 1 |
define([ |
| 2 |
"dojo/_base/declare", |
| 3 |
"dojo/_base/lang", |
| 4 |
"dojo/_base/kernel", |
| 5 |
"dojo/_base/json", |
| 6 |
"../_StoreLayer" |
| 7 |
], function(declare, lang, kernel, json, layers){ |
| 8 |
|
| 9 |
var cmdSetFilter = "filter", |
| 10 |
cmdClearFilter = "clear", |
| 11 |
hitchIfCan = function(scope, func){ |
| 12 |
return func ? lang.hitch(scope || kernel.global, func) : function(){}; |
| 13 |
}, |
| 14 |
shallowClone = function(obj){ |
| 15 |
var res = {}; |
| 16 |
if(obj && lang.isObject(obj)){ |
| 17 |
for(var name in obj){ |
| 18 |
res[name] = obj[name]; |
| 19 |
} |
| 20 |
} |
| 21 |
return res; |
| 22 |
}; |
| 23 |
var _FilterLayerMixin = declare("dojox.grid.enhanced.plugins.filter._FilterLayerMixin", null, { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
tags: ["sizeChange"], |
| 42 |
name: function(){ |
| 43 |
|
| 44 |
|
| 45 |
return "filter"; |
| 46 |
}, |
| 47 |
onFilterDefined: function(filter){}, |
| 48 |
|
| 49 |
onFiltered: function(filteredSize, totalSize){ |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
} |
| 59 |
}); |
| 60 |
var ServerSideFilterLayer = declare("dojox.grid.enhanced.plugins.filter.ServerSideFilterLayer", [layers._ServerSideLayer, _FilterLayerMixin], { |
| 61 |
constructor: function(args){ |
| 62 |
this._onUserCommandLoad = args.setupFilterQuery || this._onUserCommandLoad; |
| 63 |
this.filterDef(null); |
| 64 |
}, |
| 65 |
filterDef: function(filter){ |
| 66 |
|
| 67 |
|
| 68 |
if(filter){ |
| 69 |
this._filter = filter; |
| 70 |
var obj = filter.toObject(); |
| 71 |
|
| 72 |
this.command(cmdSetFilter, this._isStateful ? json.toJson(obj) : obj); |
| 73 |
this.command(cmdClearFilter, null); |
| 74 |
this.useCommands(true); |
| 75 |
this.onFilterDefined(filter); |
| 76 |
}else if(filter === null){ |
| 77 |
this._filter = null; |
| 78 |
this.command(cmdSetFilter, null); |
| 79 |
this.command(cmdClearFilter, true); |
| 80 |
this.useCommands(true); |
| 81 |
this.onFilterDefined(null); |
| 82 |
} |
| 83 |
return this._filter; |
| 84 |
}, |
| 85 |
onCommandLoad: function(responce, userRequest){ |
| 86 |
|
| 87 |
|
| 88 |
this.inherited(arguments); |
| 89 |
var oldOnBegin = userRequest.onBegin; |
| 90 |
if(this._isStateful){ |
| 91 |
var filteredSize; |
| 92 |
if(responce){ |
| 93 |
this.command(cmdSetFilter, null); |
| 94 |
this.command(cmdClearFilter, null); |
| 95 |
this.useCommands(false); |
| 96 |
var sizes = responce.split(','); |
| 97 |
if(sizes.length >= 2){ |
| 98 |
filteredSize = this._filteredSize = parseInt(sizes[0], 10); |
| 99 |
this.onFiltered(filteredSize, parseInt(sizes[1], 10)); |
| 100 |
}else{ |
| 101 |
|
| 102 |
return; |
| 103 |
} |
| 104 |
}else{ |
| 105 |
filteredSize = this._filteredSize; |
| 106 |
} |
| 107 |
if(this.enabled()){ |
| 108 |
userRequest.onBegin = function(size, req){ |
| 109 |
hitchIfCan(userRequest.scope, oldOnBegin)(filteredSize, req); |
| 110 |
}; |
| 111 |
} |
| 112 |
}else{ |
| 113 |
var _this = this; |
| 114 |
userRequest.onBegin = function(size, req){ |
| 115 |
if(!_this._filter){ |
| 116 |
_this._storeSize = size; |
| 117 |
} |
| 118 |
_this.onFiltered(size, _this._storeSize || size); |
| 119 |
req.onBegin = oldOnBegin; |
| 120 |
hitchIfCan(userRequest.scope, oldOnBegin)(size, req); |
| 121 |
}; |
| 122 |
} |
| 123 |
} |
| 124 |
}); |
| 125 |
var ClientSideFilterLayer = declare("dojox.grid.enhanced.plugins.filter.ClientSideFilterLayer", [layers._StoreLayer, _FilterLayerMixin], { |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
_storeSize: -1, |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
_fetchAll: true, |
| 162 |
|
| 163 |
constructor: function(args){ |
| 164 |
this.filterDef(null); |
| 165 |
args = lang.isObject(args) ? args : {}; |
| 166 |
this.fetchAllOnFirstFilter(args.fetchAll); |
| 167 |
this._getter = lang.isFunction(args.getter) ? args.getter : this._defaultGetter; |
| 168 |
}, |
| 169 |
_defaultGetter: function(datarow, colName, rowIndex, store){ |
| 170 |
return store.getValue(datarow, colName); |
| 171 |
}, |
| 172 |
filterDef: function(filter){ |
| 173 |
|
| 174 |
|
| 175 |
if(filter !== undefined){ |
| 176 |
this._filter = filter; |
| 177 |
this.invalidate(); |
| 178 |
this.onFilterDefined(filter); |
| 179 |
} |
| 180 |
return this._filter; |
| 181 |
}, |
| 182 |
setGetter: function(getter){ |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
if(lang.isFunction(getter)){ |
| 189 |
this._getter = getter; |
| 190 |
} |
| 191 |
}, |
| 192 |
fetchAllOnFirstFilter: function(toFetchAll){ |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
if(toFetchAll !== undefined){ |
| 202 |
this._fetchAll = !!toFetchAll; |
| 203 |
} |
| 204 |
return this._fetchAll; |
| 205 |
}, |
| 206 |
invalidate: function(){ |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
this._items = []; |
| 212 |
this._nextUnfetchedIdx = 0; |
| 213 |
this._result = []; |
| 214 |
this._indexMap = []; |
| 215 |
this._resultStartIdx = 0; |
| 216 |
}, |
| 217 |
|
| 218 |
_fetch: function(userRequest,filterRequest){ |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
if(!this._filter){ |
| 229 |
|
| 230 |
var old_onbegin = userRequest.onBegin, _this = this; |
| 231 |
userRequest.onBegin = function(size, r){ |
| 232 |
hitchIfCan(userRequest.scope, old_onbegin)(size, r); |
| 233 |
_this.onFiltered(size, size); |
| 234 |
}; |
| 235 |
this.originFetch(userRequest); |
| 236 |
return userRequest; |
| 237 |
} |
| 238 |
try{ |
| 239 |
|
| 240 |
|
| 241 |
var start = filterRequest ? filterRequest._nextResultItemIdx : userRequest.start; |
| 242 |
start = start || 0; |
| 243 |
if(!filterRequest){ |
| 244 |
|
| 245 |
this._result = []; |
| 246 |
this._resultStartIdx = start; |
| 247 |
var sortStr; |
| 248 |
if(lang.isArray(userRequest.sort) && userRequest.sort.length > 0 && |
| 249 |
|
| 250 |
(sortStr = json.toJson(userRequest.sort)) != this._lastSortInfo){ |
| 251 |
|
| 252 |
this.invalidate(); |
| 253 |
this._lastSortInfo = sortStr; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
var end = typeof userRequest.count == "number" ? |
| 258 |
start + userRequest.count - this._result.length : this._items.length; |
| 259 |
|
| 260 |
|
| 261 |
if(this._result.length){ |
| 262 |
this._result = this._result.concat(this._items.slice(start, end)); |
| 263 |
}else{ |
| 264 |
this._result = this._items.slice(userRequest.start, typeof userRequest.count == "number" ? |
| 265 |
userRequest.start + userRequest.count : this._items.length); |
| 266 |
} |
| 267 |
if(this._result.length >= userRequest.count || this._hasReachedStoreEnd()){ |
| 268 |
|
| 269 |
this._completeQuery(userRequest); |
| 270 |
}else{ |
| 271 |
|
| 272 |
if(!filterRequest){ |
| 273 |
|
| 274 |
filterRequest = shallowClone(userRequest); |
| 275 |
|
| 276 |
filterRequest.onBegin = lang.hitch(this, this._onFetchBegin); |
| 277 |
filterRequest.onComplete = lang.hitch(this, function(items, req){ |
| 278 |
|
| 279 |
this._nextUnfetchedIdx += items.length; |
| 280 |
|
| 281 |
|
| 282 |
this._doFilter(items, req.start, userRequest); |
| 283 |
|
| 284 |
this._fetch(userRequest, req); |
| 285 |
}); |
| 286 |
} |
| 287 |
|
| 288 |
filterRequest.start = this._nextUnfetchedIdx; |
| 289 |
|
| 290 |
if(this._fetchAll){ |
| 291 |
delete filterRequest.count; |
| 292 |
} |
| 293 |
|
| 294 |
filterRequest._nextResultItemIdx = end < this._items.length ? end : this._items.length; |
| 295 |
|
| 296 |
this.originFetch(filterRequest); |
| 297 |
} |
| 298 |
}catch(e){ |
| 299 |
if(userRequest.onError){ |
| 300 |
hitchIfCan(userRequest.scope, userRequest.onError)(e, userRequest); |
| 301 |
}else{ |
| 302 |
throw e; |
| 303 |
} |
| 304 |
} |
| 305 |
return userRequest; |
| 306 |
}, |
| 307 |
_hasReachedStoreEnd: function(){ |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
return this._storeSize >= 0 && this._nextUnfetchedIdx >= this._storeSize; |
| 313 |
}, |
| 314 |
_applyFilter: function(datarow,rowIndex){ |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
var g = this._getter, s = this._store; |
| 322 |
try{ |
| 323 |
return !!(this._filter.applyRow(datarow, function(item, arg){ |
| 324 |
return g(item, arg, rowIndex, s); |
| 325 |
}).getValue()); |
| 326 |
}catch(e){ |
| 327 |
console.warn("FilterLayer._applyFilter() error: ", e); |
| 328 |
return false; |
| 329 |
} |
| 330 |
}, |
| 331 |
_doFilter: function(items,startIdx,userRequest){ |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
for(var i = 0, cnt = 0; i < items.length; ++i){ |
| 338 |
if(this._applyFilter(items[i], startIdx + i)){ |
| 339 |
hitchIfCan(userRequest.scope, userRequest.onItem)(items[i], userRequest); |
| 340 |
cnt += this._addCachedItems(items[i], this._items.length); |
| 341 |
this._indexMap.push(startIdx + i); |
| 342 |
} |
| 343 |
} |
| 344 |
}, |
| 345 |
_onFetchBegin: function(size,req){ |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
this._storeSize = size; |
| 351 |
}, |
| 352 |
_completeQuery: function(userRequest){ |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
var size = this._items.length; |
| 359 |
if(this._nextUnfetchedIdx < this._storeSize){ |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
size++; |
| 364 |
} |
| 365 |
hitchIfCan(userRequest.scope, userRequest.onBegin)(size,userRequest); |
| 366 |
this.onFiltered(this._items.length, this._storeSize); |
| 367 |
hitchIfCan(userRequest.scope, userRequest.onComplete)(this._result, userRequest); |
| 368 |
}, |
| 369 |
_addCachedItems: function(items,filterStartIdx){ |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
if(!lang.isArray(items)){ |
| 379 |
items = [items]; |
| 380 |
} |
| 381 |
for(var k = 0; k < items.length; ++k){ |
| 382 |
this._items[filterStartIdx + k] = items[k]; |
| 383 |
} |
| 384 |
return items.length; |
| 385 |
}, |
| 386 |
onRowMappingChange: function(mapping){ |
| 387 |
|
| 388 |
if(this._filter){ |
| 389 |
var m = lang.clone(mapping), |
| 390 |
alreadyUpdated = {}; |
| 391 |
for(var r in m){ |
| 392 |
r = parseInt(r, 10); |
| 393 |
mapping[this._indexMap[r]] = this._indexMap[m[r]]; |
| 394 |
if(!alreadyUpdated[this._indexMap[r]]){ |
| 395 |
alreadyUpdated[this._indexMap[r]] = true; |
| 396 |
} |
| 397 |
if(!alreadyUpdated[r]){ |
| 398 |
alreadyUpdated[r] = true; |
| 399 |
delete mapping[r]; |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
}); |
| 405 |
|
| 406 |
return lang.mixin({ |
| 407 |
ServerSideFilterLayer: ServerSideFilterLayer, |
| 408 |
ClientSideFilterLayer: ClientSideFilterLayer |
| 409 |
}, layers); |
| 410 |
}); |