| 1 |
define([ |
| 2 |
"dojo/_base/lang", |
| 3 |
"dojo/_base/array", |
| 4 |
"dijit/_base/manager", |
| 5 |
"dojo/_base/connect", |
| 6 |
"dojo/_base/declare" |
| 7 |
], function(lang, array, manager, connect, declare){ |
| 8 |
|
| 9 |
return declare("dojox.form._SelectStackMixin", null, { |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
stackId: "", |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
stackPrefix: "", |
| 37 |
|
| 38 |
_paneIdFromOption: function( oVal){ |
| 39 |
|
| 40 |
|
| 41 |
return (this.stackPrefix || "") + oVal; |
| 42 |
}, |
| 43 |
|
| 44 |
_optionValFromPane: function( id){ |
| 45 |
|
| 46 |
|
| 47 |
var sp = this.stackPrefix; |
| 48 |
if(sp && id.indexOf(sp) === 0){ |
| 49 |
return id.substring(sp.length); |
| 50 |
} |
| 51 |
return id; |
| 52 |
}, |
| 53 |
|
| 54 |
_togglePane: function( pane, shown){ |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
if(pane._shown != undefined && pane._shown == shown){ return; } |
| 60 |
var widgets = array.filter(pane.getDescendants(), "return item.name;"); |
| 61 |
if(!shown){ |
| 62 |
|
| 63 |
savedStates = {}; |
| 64 |
array.forEach(widgets, function(w){ |
| 65 |
savedStates[w.id] = w.disabled; |
| 66 |
w.set("disabled", true); |
| 67 |
}); |
| 68 |
pane._savedStates = savedStates; |
| 69 |
}else{ |
| 70 |
|
| 71 |
var savedStates = pane._savedStates||{}; |
| 72 |
array.forEach(widgets, function(w){ |
| 73 |
var state = savedStates[w.id]; |
| 74 |
if(state == undefined){ |
| 75 |
state = false; |
| 76 |
} |
| 77 |
w.set("disabled", state); |
| 78 |
}); |
| 79 |
delete pane._savedStates; |
| 80 |
} |
| 81 |
pane._shown = shown; |
| 82 |
}, |
| 83 |
|
| 84 |
_connectTitle: function( pane, value){ |
| 85 |
var fx = lang.hitch(this, function(title){ |
| 86 |
this.updateOption({value: value, label: title}); |
| 87 |
}); |
| 88 |
if(pane._setTitleAttr){ |
| 89 |
this.connect(pane, "_setTitleAttr", fx); |
| 90 |
}else{ |
| 91 |
this.connect(pane, "attr", function(attr, val){ |
| 92 |
if(attr == "title" && arguments.length > 1){ |
| 93 |
fx(val); |
| 94 |
} |
| 95 |
}); |
| 96 |
} |
| 97 |
}, |
| 98 |
|
| 99 |
onAddChild: function( pane, insertIndex){ |
| 100 |
|
| 101 |
|
| 102 |
if(!this._panes[pane.id]){ |
| 103 |
this._panes[pane.id] = pane; |
| 104 |
var v = this._optionValFromPane(pane.id); |
| 105 |
this.addOption({value: v, label: pane.title}); |
| 106 |
this._connectTitle(pane, v); |
| 107 |
} |
| 108 |
if(!pane.onShow || !pane.onHide || pane._shown == undefined){ |
| 109 |
pane.onShow = lang.hitch(this, "_togglePane", pane, true); |
| 110 |
pane.onHide = lang.hitch(this, "_togglePane", pane, false); |
| 111 |
pane.onHide(); |
| 112 |
} |
| 113 |
}, |
| 114 |
|
| 115 |
_setValueAttr: function(v){ |
| 116 |
if("_savedValue" in this){ |
| 117 |
return; |
| 118 |
} |
| 119 |
this.inherited(arguments); |
| 120 |
}, |
| 121 |
attr: function( name, value){ |
| 122 |
if(name == "value" && arguments.length == 2 && "_savedValue" in this){ |
| 123 |
this._savedValue = value; |
| 124 |
} |
| 125 |
return this.inherited(arguments); |
| 126 |
}, |
| 127 |
|
| 128 |
onRemoveChild: function( pane){ |
| 129 |
|
| 130 |
|
| 131 |
if(this._panes[pane.id]){ |
| 132 |
delete this._panes[pane.id]; |
| 133 |
this.removeOption(this._optionValFromPane(pane.id)); |
| 134 |
} |
| 135 |
}, |
| 136 |
|
| 137 |
onSelectChild: function( pane){ |
| 138 |
|
| 139 |
|
| 140 |
this._setValueAttr(this._optionValFromPane(pane.id)); |
| 141 |
}, |
| 142 |
|
| 143 |
onStartup: function( info){ |
| 144 |
|
| 145 |
|
| 146 |
var selPane = info.selected; |
| 147 |
this.addOption(array.filter(array.map(info.children, function(c){ |
| 148 |
var v = this._optionValFromPane(c.id); |
| 149 |
this._connectTitle(c, v); |
| 150 |
var toAdd = null; |
| 151 |
if(!this._panes[c.id]){ |
| 152 |
this._panes[c.id] = c; |
| 153 |
toAdd = {value: v, label: c.title}; |
| 154 |
} |
| 155 |
if(!c.onShow || !c.onHide || c._shown == undefined){ |
| 156 |
c.onShow = lang.hitch(this, "_togglePane", c, true); |
| 157 |
c.onHide = lang.hitch(this, "_togglePane", c, false); |
| 158 |
c.onHide(); |
| 159 |
} |
| 160 |
if("_savedValue" in this && v === this._savedValue){ |
| 161 |
selPane = c; |
| 162 |
} |
| 163 |
return toAdd; |
| 164 |
}, this), function(i){ return i;})); |
| 165 |
var _this = this; |
| 166 |
var fx = function(){ |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
delete _this._savedValue; |
| 171 |
_this.onSelectChild(selPane); |
| 172 |
if(!selPane._shown){ |
| 173 |
_this._togglePane(selPane, true); |
| 174 |
} |
| 175 |
}; |
| 176 |
if(selPane !== info.selected){ |
| 177 |
var stack = manager.byId(this.stackId); |
| 178 |
var c = this.connect(stack, "_showChild", function(sel){ |
| 179 |
this.disconnect(c); |
| 180 |
fx(); |
| 181 |
}); |
| 182 |
}else{ |
| 183 |
fx(); |
| 184 |
} |
| 185 |
}, |
| 186 |
|
| 187 |
postMixInProperties: function(){ |
| 188 |
this._savedValue = this.value; |
| 189 |
this.inherited(arguments); |
| 190 |
this.connect(this, "onChange", "_handleSelfOnChange"); |
| 191 |
}, |
| 192 |
|
| 193 |
postCreate: function(){ |
| 194 |
this.inherited(arguments); |
| 195 |
this._panes = {}; |
| 196 |
this._subscriptions = [ |
| 197 |
connect.subscribe(this.stackId + "-startup", this, "onStartup"), |
| 198 |
connect.subscribe(this.stackId + "-addChild", this, "onAddChild"), |
| 199 |
connect.subscribe(this.stackId + "-removeChild", this, "onRemoveChild"), |
| 200 |
connect.subscribe(this.stackId + "-selectChild", this, "onSelectChild") |
| 201 |
]; |
| 202 |
var stack = manager.byId(this.stackId); |
| 203 |
if(stack && stack._started){ |
| 204 |
|
| 205 |
this.onStartup({children: stack.getChildren(), selected: stack.selectedChildWidget}); |
| 206 |
} |
| 207 |
}, |
| 208 |
|
| 209 |
destroy: function(){ |
| 210 |
array.forEach(this._subscriptions, connect.unsubscribe); |
| 211 |
delete this._panes; |
| 212 |
this.inherited("destroy", arguments); |
| 213 |
}, |
| 214 |
|
| 215 |
_handleSelfOnChange: function( val){ |
| 216 |
|
| 217 |
|
| 218 |
var pane = this._panes[this._paneIdFromOption(val)]; |
| 219 |
if(pane){ |
| 220 |
var s = manager.byId(this.stackId); |
| 221 |
if(pane == s.selectedChildWidget){ |
| 222 |
s._transition(pane); |
| 223 |
}else{ |
| 224 |
s.selectChild(pane); |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
}); |
| 229 |
}); |