root / trunk / web / dojo / dijit / form / _FormSelectWidget.js
History | View | Annotate | Download (7.4 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dijit.form._FormSelectWidget"]){ |
||
9 | dojo._hasResource["dijit.form._FormSelectWidget"]=true; |
||
10 | dojo.provide("dijit.form._FormSelectWidget");
|
||
11 | dojo.require("dijit.form._FormWidget");
|
||
12 | dojo.require("dojo.data.util.sorter");
|
||
13 | dojo.declare("dijit.form._FormSelectWidget",dijit.form._FormValueWidget,{multiple:false,options:null,store:null,query:null,queryOptions:null,onFetch:null,sortByLabel:true,loadChildrenOnOpen:false,getOptions:function(_1){ |
||
14 | var _2=_1,_3=this.options||[],l=_3.length; |
||
15 | if(_2===undefined){ |
||
16 | return _3;
|
||
17 | } |
||
18 | if(dojo.isArray(_2)){
|
||
19 | return dojo.map(_2,"return this.getOptions(item);",this); |
||
20 | } |
||
21 | if(dojo.isObject(_1)){
|
||
22 | if(!dojo.some(this.options,function(o,_4){ |
||
23 | if(o===_2||(o.value&&o.value===_2.value)){
|
||
24 | _2=_4; |
||
25 | return true; |
||
26 | } |
||
27 | return false; |
||
28 | })){ |
||
29 | _2=-1;
|
||
30 | } |
||
31 | } |
||
32 | if(typeof _2=="string"){ |
||
33 | for(var i=0;i<l;i++){ |
||
34 | if(_3[i].value===_2){
|
||
35 | _2=i; |
||
36 | break;
|
||
37 | } |
||
38 | } |
||
39 | } |
||
40 | if(typeof _2=="number"&&_2>=0&&_2<l){ |
||
41 | return this.options[_2]; |
||
42 | } |
||
43 | return null; |
||
44 | },addOption:function(_5){ |
||
45 | if(!dojo.isArray(_5)){
|
||
46 | _5=[_5]; |
||
47 | } |
||
48 | dojo.forEach(_5,function(i){
|
||
49 | if(i&&dojo.isObject(i)){
|
||
50 | this.options.push(i);
|
||
51 | } |
||
52 | },this);
|
||
53 | this._loadChildren();
|
||
54 | },removeOption:function(_6){ |
||
55 | if(!dojo.isArray(_6)){
|
||
56 | _6=[_6]; |
||
57 | } |
||
58 | var _7=this.getOptions(_6); |
||
59 | dojo.forEach(_7,function(i){
|
||
60 | if(i){
|
||
61 | this.options=dojo.filter(this.options,function(_8,_9){ |
||
62 | return (_8.value!==i.value);
|
||
63 | }); |
||
64 | this._removeOptionItem(i);
|
||
65 | } |
||
66 | },this);
|
||
67 | this._loadChildren();
|
||
68 | },updateOption:function(_a){ |
||
69 | if(!dojo.isArray(_a)){
|
||
70 | _a=[_a]; |
||
71 | } |
||
72 | dojo.forEach(_a,function(i){
|
||
73 | var _b=this.getOptions(i),k; |
||
74 | if(_b){
|
||
75 | for(k in i){ |
||
76 | _b[k]=i[k]; |
||
77 | } |
||
78 | } |
||
79 | },this);
|
||
80 | this._loadChildren();
|
||
81 | },setStore:function(_c,_d,_e){ |
||
82 | var _f=this.store; |
||
83 | _e=_e||{}; |
||
84 | if(_f!==_c){
|
||
85 | dojo.forEach(this._notifyConnections||[],dojo.disconnect);
|
||
86 | delete this._notifyConnections; |
||
87 | if(_c&&_c.getFeatures()["dojo.data.api.Notification"]){ |
||
88 | this._notifyConnections=[dojo.connect(_c,"onNew",this,"_onNewItem"),dojo.connect(_c,"onDelete",this,"_onDeleteItem"),dojo.connect(_c,"onSet",this,"_onSetItem")]; |
||
89 | } |
||
90 | this.store=_c;
|
||
91 | } |
||
92 | this._onChangeActive=false; |
||
93 | if(this.options&&this.options.length){ |
||
94 | this.removeOption(this.options); |
||
95 | } |
||
96 | if(_c){
|
||
97 | var cb=function(_10){ |
||
98 | if(this.sortByLabel&&!_e.sort&&_10.length){ |
||
99 | _10.sort(dojo.data.util.sorter.createSortFunction([{attribute:_c.getLabelAttributes(_10[0])[0]}],_c)); |
||
100 | } |
||
101 | if(_e.onFetch){
|
||
102 | _10=_e.onFetch(_10); |
||
103 | } |
||
104 | dojo.forEach(_10,function(i){
|
||
105 | this._addOptionForItem(i);
|
||
106 | },this);
|
||
107 | this._loadingStore=false; |
||
108 | this.set("value",(("_pendingValue" in this)?this._pendingValue:_d)); |
||
109 | delete this._pendingValue; |
||
110 | if(!this.loadChildrenOnOpen){ |
||
111 | this._loadChildren();
|
||
112 | }else{
|
||
113 | this._pseudoLoadChildren(_10);
|
||
114 | } |
||
115 | this._fetchedWith=_11;
|
||
116 | this._lastValueReported=this.multiple?[]:null; |
||
117 | this._onChangeActive=true; |
||
118 | this.onSetStore();
|
||
119 | this._handleOnChange(this.value); |
||
120 | }; |
||
121 | var _11=dojo.mixin({onComplete:cb,scope:this},_e); |
||
122 | this._loadingStore=true; |
||
123 | _c.fetch(_11); |
||
124 | }else{
|
||
125 | delete this._fetchedWith; |
||
126 | } |
||
127 | return _f;
|
||
128 | },_setValueAttr:function(_12,_13){ |
||
129 | if(this._loadingStore){ |
||
130 | this._pendingValue=_12;
|
||
131 | return;
|
||
132 | } |
||
133 | var _14=this.getOptions()||[]; |
||
134 | if(!dojo.isArray(_12)){
|
||
135 | _12=[_12]; |
||
136 | } |
||
137 | dojo.forEach(_12,function(i,idx){
|
||
138 | if(!dojo.isObject(i)){
|
||
139 | i=i+"";
|
||
140 | } |
||
141 | if(typeof i==="string"){ |
||
142 | _12[idx]=dojo.filter(_14,function(_15){
|
||
143 | return _15.value===i;
|
||
144 | })[0]||{value:"",label:""}; |
||
145 | } |
||
146 | },this);
|
||
147 | _12=dojo.filter(_12,function(i){
|
||
148 | return i&&i.value;
|
||
149 | }); |
||
150 | if(!this.multiple&&(!_12[0]||!_12[0].value)&&_14.length){ |
||
151 | _12[0]=_14[0]; |
||
152 | } |
||
153 | dojo.forEach(_14,function(i){
|
||
154 | i.selected=dojo.some(_12,function(v){
|
||
155 | return v.value===i.value;
|
||
156 | }); |
||
157 | }); |
||
158 | var val=dojo.map(_12,function(i){ |
||
159 | return i.value;
|
||
160 | }),_16=dojo.map(_12,function(i){
|
||
161 | return i.label;
|
||
162 | }); |
||
163 | this.value=this.multiple?val:val[0]; |
||
164 | this._setDisplay(this.multiple?_16:_16[0]); |
||
165 | this._updateSelection();
|
||
166 | this._handleOnChange(this.value,_13); |
||
167 | },_getDisplayedValueAttr:function(){ |
||
168 | var val=this.get("value"); |
||
169 | if(!dojo.isArray(val)){
|
||
170 | val=[val]; |
||
171 | } |
||
172 | var ret=dojo.map(this.getOptions(val),function(v){ |
||
173 | if(v&&"label" in v){ |
||
174 | return v.label;
|
||
175 | }else{
|
||
176 | if(v){
|
||
177 | return v.value;
|
||
178 | } |
||
179 | } |
||
180 | return null; |
||
181 | },this);
|
||
182 | return this.multiple?ret:ret[0]; |
||
183 | },_getValueDeprecated:false,getValue:function(){ |
||
184 | return this._lastValue; |
||
185 | },undo:function(){ |
||
186 | this._setValueAttr(this._lastValueReported,false); |
||
187 | },_loadChildren:function(){ |
||
188 | if(this._loadingStore){ |
||
189 | return;
|
||
190 | } |
||
191 | dojo.forEach(this._getChildren(),function(_17){ |
||
192 | _17.destroyRecursive(); |
||
193 | }); |
||
194 | dojo.forEach(this.options,this._addOptionItem,this); |
||
195 | this._updateSelection();
|
||
196 | },_updateSelection:function(){ |
||
197 | this.value=this._getValueFromOpts(); |
||
198 | var val=this.value; |
||
199 | if(!dojo.isArray(val)){
|
||
200 | val=[val]; |
||
201 | } |
||
202 | if(val&&val[0]){ |
||
203 | dojo.forEach(this._getChildren(),function(_18){ |
||
204 | var _19=dojo.some(val,function(v){ |
||
205 | return _18.option&&(v===_18.option.value);
|
||
206 | }); |
||
207 | dojo.toggleClass(_18.domNode,this.baseClass+"SelectedOption",_19); |
||
208 | dijit.setWaiState(_18.domNode,"selected",_19);
|
||
209 | },this);
|
||
210 | } |
||
211 | this._handleOnChange(this.value); |
||
212 | },_getValueFromOpts:function(){ |
||
213 | var _1a=this.getOptions()||[]; |
||
214 | if(!this.multiple&&_1a.length){ |
||
215 | var opt=dojo.filter(_1a,function(i){ |
||
216 | return i.selected;
|
||
217 | })[0];
|
||
218 | if(opt&&opt.value){
|
||
219 | return opt.value;
|
||
220 | }else{
|
||
221 | _1a[0].selected=true; |
||
222 | return _1a[0].value; |
||
223 | } |
||
224 | }else{
|
||
225 | if(this.multiple){ |
||
226 | return dojo.map(dojo.filter(_1a,function(i){ |
||
227 | return i.selected;
|
||
228 | }),function(i){
|
||
229 | return i.value;
|
||
230 | })||[]; |
||
231 | } |
||
232 | } |
||
233 | return ""; |
||
234 | },_onNewItem:function(_1b,_1c){ |
||
235 | if(!_1c||!_1c.parent){
|
||
236 | this._addOptionForItem(_1b);
|
||
237 | } |
||
238 | },_onDeleteItem:function(_1d){ |
||
239 | var _1e=this.store; |
||
240 | this.removeOption(_1e.getIdentity(_1d));
|
||
241 | },_onSetItem:function(_1f){ |
||
242 | this.updateOption(this._getOptionObjForItem(_1f)); |
||
243 | },_getOptionObjForItem:function(_20){ |
||
244 | var _21=this.store,_22=_21.getLabel(_20),_23=(_22?_21.getIdentity(_20):null); |
||
245 | return {value:_23,label:_22,item:_20}; |
||
246 | },_addOptionForItem:function(_24){ |
||
247 | var _25=this.store; |
||
248 | if(!_25.isItemLoaded(_24)){
|
||
249 | _25.loadItem({item:_24,onComplete:function(i){ |
||
250 | this._addOptionForItem(_24);
|
||
251 | },scope:this}); |
||
252 | return;
|
||
253 | } |
||
254 | var _26=this._getOptionObjForItem(_24); |
||
255 | this.addOption(_26);
|
||
256 | },constructor:function(_27){ |
||
257 | this._oValue=(_27||{}).value||null; |
||
258 | },_fillContent:function(){ |
||
259 | var _28=this.options; |
||
260 | if(!_28){
|
||
261 | _28=this.options=this.srcNodeRef?dojo.query(">",this.srcNodeRef).map(function(_29){ |
||
262 | if(_29.getAttribute("type")==="separator"){ |
||
263 | return {value:"",label:"",selected:false,disabled:false}; |
||
264 | } |
||
265 | return {value:_29.getAttribute("value"),label:String(_29.innerHTML),selected:_29.getAttribute("selected")||false,disabled:_29.getAttribute("disabled")||false}; |
||
266 | },this):[];
|
||
267 | } |
||
268 | if(!this.value){ |
||
269 | this.value=this._getValueFromOpts(); |
||
270 | }else{
|
||
271 | if(this.multiple&&typeof this.value=="string"){ |
||
272 | this.value=this.value.split(","); |
||
273 | } |
||
274 | } |
||
275 | },postCreate:function(){ |
||
276 | dojo.setSelectable(this.focusNode,false); |
||
277 | this.inherited(arguments); |
||
278 | this.connect(this,"onChange","_updateSelection"); |
||
279 | this.connect(this,"startup","_loadChildren"); |
||
280 | this._setValueAttr(this.value,null); |
||
281 | },startup:function(){ |
||
282 | this.inherited(arguments); |
||
283 | var _2a=this.store,_2b={}; |
||
284 | dojo.forEach(["query","queryOptions","onFetch"],function(i){ |
||
285 | if(this[i]){ |
||
286 | _2b[i]=this[i];
|
||
287 | } |
||
288 | delete this[i]; |
||
289 | },this);
|
||
290 | if(_2a&&_2a.getFeatures()["dojo.data.api.Identity"]){ |
||
291 | this.store=null; |
||
292 | this.setStore(_2a,this._oValue,_2b); |
||
293 | } |
||
294 | },destroy:function(){ |
||
295 | dojo.forEach(this._notifyConnections||[],dojo.disconnect);
|
||
296 | this.inherited(arguments); |
||
297 | },_addOptionItem:function(_2c){ |
||
298 | },_removeOptionItem:function(_2d){ |
||
299 | },_setDisplay:function(_2e){ |
||
300 | },_getChildren:function(){ |
||
301 | return [];
|
||
302 | },_getSelectedOptionsAttr:function(){ |
||
303 | return this.getOptions(this.get("value")); |
||
304 | },_pseudoLoadChildren:function(_2f){ |
||
305 | },onSetStore:function(){ |
||
306 | }}); |
||
307 | } |