| 1 |
define([ |
| 2 |
"dojo/_base/array", |
| 3 |
"dojo/_base/lang", |
| 4 |
"dojo/_base/declare", |
| 5 |
"./ModelRefController" |
| 6 |
], function(array, lang, declare, ModelRefController){ |
| 7 |
function unwatchHandles( c){ |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
for(var s in {"_listModelWatchHandle": 1, "_tableModelWatchHandle": 1}){ |
| 12 |
if(c[s]){ |
| 13 |
c[s].unwatch(); |
| 14 |
c[s] = null; |
| 15 |
} |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
function setRefInModel( ctrl, old, current){ |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
unwatchHandles(ctrl); |
| 24 |
if(current && old !== current){ |
| 25 |
if(current.watchElements){ |
| 26 |
ctrl._listModelWatchHandle = current.watchElements(function(idx, removals, adds){ |
| 27 |
if(removals && adds){ |
| 28 |
var curIdx = ctrl.get("cursorIndex"); |
| 29 |
|
| 30 |
if(removals && curIdx >= idx && curIdx < idx + removals.length){ |
| 31 |
ctrl.set("cursorIndex", -1); |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
if((removals.length || adds.length) && curIdx >= idx){ |
| 36 |
ctrl.set(ctrl._refCursorProp, ctrl.get("cursor")); |
| 37 |
} |
| 38 |
}else{ |
| 39 |
|
| 40 |
ctrl.set(ctrl._refCursorProp, ctrl.get(ctrl._refCursorProp)); |
| 41 |
} |
| 42 |
}); |
| 43 |
}else if(current.set && current.watch){ |
| 44 |
if(ctrl.get("cursorIndex") < 0){ ctrl._set("cursorIndex", ""); } |
| 45 |
ctrl._tableModelWatchHandle = current.watch(function(name, old, current){ |
| 46 |
if(old !== current && name == ctrl.get("cursorIndex")){ |
| 47 |
ctrl.set(ctrl._refCursorProp, current); |
| 48 |
} |
| 49 |
}); |
| 50 |
} |
| 51 |
} |
| 52 |
ctrl._setCursorIndexAttr(ctrl.cursorIndex); |
| 53 |
} |
| 54 |
|
| 55 |
function setRefCursor( ctrl, old, current){ |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
var model = ctrl[ctrl._refInModelProp]; |
| 62 |
if(!model){ return; } |
| 63 |
if(old !== current){ |
| 64 |
if(lang.isArray(model)){ |
| 65 |
var foundIdx = array.indexOf(model, current); |
| 66 |
if(foundIdx < 0){ |
| 67 |
var targetIdx = ctrl.get("cursorIndex"); |
| 68 |
if(targetIdx >= 0 && targetIdx < model.length){ |
| 69 |
model.set(targetIdx, current); |
| 70 |
} |
| 71 |
}else{ |
| 72 |
ctrl.set("cursorIndex", foundIdx); |
| 73 |
} |
| 74 |
}else{ |
| 75 |
for(var s in model){ |
| 76 |
if(model[s] == current){ |
| 77 |
ctrl.set("cursorIndex", s); |
| 78 |
return; |
| 79 |
} |
| 80 |
} |
| 81 |
var targetIdx = ctrl.get("cursorIndex"); |
| 82 |
if(targetIdx){ |
| 83 |
model.set(targetIdx, current); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return declare("dojox.mvc.ListController", ModelRefController, { |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
idProperty: "uniqueId", |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
cursorId: null, |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
cursorIndex: -1, |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
cursor: null, |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
model: null, |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
_listModelWatchHandle: null, |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
_tableModelWatchHandle: null, |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
_refCursorProp: "cursor", |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
_refModelProp: "cursor", |
| 152 |
|
| 153 |
destroy: function(){ |
| 154 |
unwatchHandles(this); |
| 155 |
this.inherited(arguments); |
| 156 |
}, |
| 157 |
|
| 158 |
set: function( name, value){ |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
var oldRefInCursor = this[this._refCursorProp]; |
| 167 |
var oldRefInModel = this[this._refInModelProp]; |
| 168 |
this.inherited(arguments); |
| 169 |
if(name == this._refCursorProp){ |
| 170 |
setRefCursor(this, oldRefInCursor, value); |
| 171 |
} |
| 172 |
if(name == this._refInModelProp){ |
| 173 |
setRefInModel(this, oldRefInModel, value); |
| 174 |
} |
| 175 |
}, |
| 176 |
|
| 177 |
_setCursorIdAttr: function( value){ |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
var old = this.cursorId; |
| 184 |
this._set("cursorId", value); |
| 185 |
var model = this[this._refInModelProp]; |
| 186 |
if(!model){ return; } |
| 187 |
if(old !== value){ |
| 188 |
if(lang.isArray(model)){ |
| 189 |
for(var i = 0; i < model.length; i++){ |
| 190 |
if(model[i][this.idProperty] == value){ |
| 191 |
this.set("cursorIndex", i); |
| 192 |
return; |
| 193 |
} |
| 194 |
} |
| 195 |
this._set("cursorIndex", -1); |
| 196 |
}else{ |
| 197 |
for(var s in model){ |
| 198 |
if(model[s][this.idProperty] == value){ |
| 199 |
this.set("cursorIndex", s); |
| 200 |
return; |
| 201 |
} |
| 202 |
} |
| 203 |
this._set("cursorIndex", ""); |
| 204 |
} |
| 205 |
} |
| 206 |
}, |
| 207 |
|
| 208 |
_setCursorIndexAttr: function( value){ |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
this._set("cursorIndex", value); |
| 215 |
if(!this[this._refInModelProp]){ return; } |
| 216 |
this.set(this._refCursorProp, this[this._refInModelProp][value]); |
| 217 |
this.set("cursorId", this[this._refInModelProp][value] && this[this._refInModelProp][value][this.idProperty]); |
| 218 |
}, |
| 219 |
|
| 220 |
hasControllerProperty: function( name){ |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
return this.inherited(arguments) || name == this._refCursorProp; |
| 227 |
} |
| 228 |
}); |
| 229 |
}); |