| 1 |
define([ |
| 2 |
"dojo/_base/declare", |
| 3 |
"dojo/_base/array", |
| 4 |
"dojo/_base/lang", |
| 5 |
"dojo/_base/xhr" |
| 6 |
], function(declare, array, lang, xhr){ |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
var ns = lang.getObject("grid.enhanced.plugins", true, dojox); |
| 44 |
|
| 45 |
var getPrevTags = function(tags){ |
| 46 |
var tagList = ["reorder", "sizeChange", "normal", "presentation"]; |
| 47 |
var idx = tagList.length; |
| 48 |
for(var i = tags.length - 1; i >= 0; --i){ |
| 49 |
var p = array.indexOf(tagList, tags[i]); |
| 50 |
if(p >= 0 && p <= idx){ |
| 51 |
idx = p; |
| 52 |
} |
| 53 |
} |
| 54 |
if(idx < tagList.length - 1){ |
| 55 |
return tagList.slice(0, idx + 1); |
| 56 |
}else{ |
| 57 |
return tagList; |
| 58 |
} |
| 59 |
}, |
| 60 |
|
| 61 |
unwrap = function(layerName){ |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
var i, layers = this._layers, len = layers.length; |
| 69 |
if(layerName){ |
| 70 |
for(i = len-1; i >= 0; --i){ |
| 71 |
if(layers[i].name() == layerName){ |
| 72 |
layers[i]._unwrap(layers[i + 1]); |
| 73 |
break; |
| 74 |
} |
| 75 |
} |
| 76 |
layers.splice(i, 1); |
| 77 |
}else{ |
| 78 |
for(i = len - 1; i >= 0; --i){ |
| 79 |
layers[i]._unwrap(); |
| 80 |
} |
| 81 |
} |
| 82 |
if(!layers.length){ |
| 83 |
delete this._layers; |
| 84 |
delete this.layer; |
| 85 |
delete this.unwrap; |
| 86 |
delete this.forEachLayer; |
| 87 |
} |
| 88 |
|
| 89 |
return this; |
| 90 |
}, |
| 91 |
|
| 92 |
getLayer = function(layerName){ |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
var i, layers = this._layers; |
| 102 |
if(typeof layerName == "undefined"){ |
| 103 |
return layers.length; |
| 104 |
} |
| 105 |
if(typeof layerName == "number"){ |
| 106 |
return layers[layerName]; |
| 107 |
} |
| 108 |
for(i = layers.length - 1; i >= 0; --i){ |
| 109 |
if(layers[i].name() == layerName){ |
| 110 |
return layers[i]; |
| 111 |
} |
| 112 |
} |
| 113 |
return null; |
| 114 |
}, |
| 115 |
|
| 116 |
forEachLayer = function(callback, isInnerToOuter){ |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
var len = this._layers.length, start, end, dir; |
| 125 |
if(isInnerToOuter){ |
| 126 |
start = 0; |
| 127 |
end = len; |
| 128 |
dir = 1; |
| 129 |
}else{ |
| 130 |
start = len - 1; |
| 131 |
end = -1; |
| 132 |
dir = -1; |
| 133 |
} |
| 134 |
for(var i = start; i != end; i += dir){ |
| 135 |
if(callback(this._layers[i], i) === false){ |
| 136 |
return i; |
| 137 |
} |
| 138 |
} |
| 139 |
return end; |
| 140 |
}; |
| 141 |
ns.wrap = function(store, funcName, layer, layerFuncName){ |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
if(!store._layers){ |
| 153 |
store._layers = []; |
| 154 |
store.layer = lang.hitch(store, getLayer); |
| 155 |
store.unwrap = lang.hitch(store, unwrap); |
| 156 |
store.forEachLayer = lang.hitch(store, forEachLayer); |
| 157 |
} |
| 158 |
var prevTags = getPrevTags(layer.tags); |
| 159 |
if(!array.some(store._layers, function(lyr, i){ |
| 160 |
if(array.some(lyr.tags, function(tag){ |
| 161 |
return array.indexOf(prevTags, tag) >= 0; |
| 162 |
})){ |
| 163 |
return false; |
| 164 |
}else{ |
| 165 |
store._layers.splice(i, 0, layer); |
| 166 |
layer._wrap(store, funcName, layerFuncName, lyr); |
| 167 |
return true; |
| 168 |
} |
| 169 |
})){ |
| 170 |
store._layers.push(layer); |
| 171 |
layer._wrap(store, funcName, layerFuncName); |
| 172 |
} |
| 173 |
|
| 174 |
return store; |
| 175 |
}; |
| 176 |
|
| 177 |
var _StoreLayer = declare("dojox.grid.enhanced.plugins._StoreLayer", null, { |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
tags: ["normal"], |
| 196 |
|
| 197 |
layerFuncName: "_fetch", |
| 198 |
|
| 199 |
constructor: function(){ |
| 200 |
this._store = null; |
| 201 |
this._originFetch = null; |
| 202 |
this.__enabled = true; |
| 203 |
}, |
| 204 |
initialize: function(store){ |
| 205 |
}, |
| 206 |
uninitialize: function(store){ |
| 207 |
}, |
| 208 |
invalidate: function(){ |
| 209 |
|
| 210 |
}, |
| 211 |
_wrap: function(store, funcName, layerFuncName, nextLayer){ |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
this._store = store; |
| 219 |
this._funcName = funcName; |
| 220 |
var fetchFunc = lang.hitch(this, function(){ |
| 221 |
return (this.enabled() ? this[layerFuncName || this.layerFuncName] : this.originFetch).apply(this, arguments); |
| 222 |
}); |
| 223 |
if(nextLayer){ |
| 224 |
this._originFetch = nextLayer._originFetch; |
| 225 |
nextLayer._originFetch = fetchFunc; |
| 226 |
}else{ |
| 227 |
this._originFetch = store[funcName] || function(){}; |
| 228 |
store[funcName] = fetchFunc; |
| 229 |
} |
| 230 |
this.initialize(store); |
| 231 |
}, |
| 232 |
_unwrap: function(nextLayer){ |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
this.uninitialize(this._store); |
| 240 |
if(nextLayer){ |
| 241 |
nextLayer._originFetch = this._originFetch; |
| 242 |
}else{ |
| 243 |
this._store[this._funcName] = this._originFetch; |
| 244 |
} |
| 245 |
this._originFetch = null; |
| 246 |
this._store = null; |
| 247 |
}, |
| 248 |
enabled: function(toEnable){ |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
if(typeof toEnable != "undefined"){ |
| 256 |
this.__enabled = !!toEnable; |
| 257 |
} |
| 258 |
return this.__enabled; |
| 259 |
}, |
| 260 |
name: function(){ |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
if(!this.__name){ |
| 271 |
var m = this.declaredClass.match(/(?:\.(?:_*)([^\.]+)Layer$)|(?:\.([^\.]+)$)/i); |
| 272 |
this.__name = m ? (m[1] || m[2]).toLowerCase() : this.declaredClass; |
| 273 |
} |
| 274 |
return this.__name; |
| 275 |
}, |
| 276 |
originFetch: function(){ |
| 277 |
return (lang.hitch(this._store, this._originFetch)).apply(this, arguments); |
| 278 |
} |
| 279 |
}); |
| 280 |
var _ServerSideLayer = declare("dojox.grid.enhanced.plugins._ServerSideLayer", _StoreLayer, { |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
constructor: function(args){ |
| 294 |
args = args || {}; |
| 295 |
this._url = args.url || ""; |
| 296 |
this._isStateful = !!args.isStateful; |
| 297 |
this._onUserCommandLoad = args.onCommandLoad || function(){}; |
| 298 |
this.__cmds = {cmdlayer:this.name(), enable:true}; |
| 299 |
|
| 300 |
|
| 301 |
this.useCommands(this._isStateful); |
| 302 |
}, |
| 303 |
enabled: function(toEnable){ |
| 304 |
|
| 305 |
|
| 306 |
var res = this.inherited(arguments); |
| 307 |
this.__cmds.enable = this.__enabled; |
| 308 |
return res; |
| 309 |
}, |
| 310 |
useCommands: function(toUse){ |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
if(typeof toUse != "undefined"){ |
| 320 |
this.__cmds.cmdlayer = (toUse && this._isStateful) ? this.name() : null; |
| 321 |
} |
| 322 |
return !!(this.__cmds.cmdlayer); |
| 323 |
}, |
| 324 |
_fetch: function(userRequest){ |
| 325 |
|
| 326 |
|
| 327 |
if(this.__cmds.cmdlayer){ |
| 328 |
|
| 329 |
xhr.post({ |
| 330 |
url: this._url || this._store.url, |
| 331 |
content: this.__cmds, |
| 332 |
load: lang.hitch(this, function(responce){ |
| 333 |
this.onCommandLoad(responce, userRequest); |
| 334 |
this.originFetch(userRequest); |
| 335 |
}), |
| 336 |
error: lang.hitch(this, this.onCommandError) |
| 337 |
}); |
| 338 |
}else{ |
| 339 |
|
| 340 |
this.onCommandLoad("", userRequest); |
| 341 |
this.originFetch(userRequest); |
| 342 |
} |
| 343 |
return userRequest; |
| 344 |
}, |
| 345 |
command: function(cmdName,cmdContent){ |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
var cmds = this.__cmds; |
| 357 |
if(cmdContent === null){ |
| 358 |
delete cmds[cmdName]; |
| 359 |
}else if(typeof cmdContent !== "undefined"){ |
| 360 |
cmds[cmdName] = cmdContent; |
| 361 |
} |
| 362 |
return cmds[cmdName]; |
| 363 |
}, |
| 364 |
onCommandLoad: function(response, userRequest){ |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
this._onUserCommandLoad(this.__cmds, userRequest, response); |
| 375 |
}, |
| 376 |
onCommandError: function(error){ |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
console.log(error); |
| 383 |
throw error; |
| 384 |
} |
| 385 |
}); |
| 386 |
|
| 387 |
return { |
| 388 |
_StoreLayer: _StoreLayer, |
| 389 |
_ServerSideLayer: _ServerSideLayer, |
| 390 |
wrap: ns.wrap |
| 391 |
}; |
| 392 |
}); |