root / trunk / web / dojo / dojox / data / ItemExplorer.js @ 12
History | View | Annotate | Download (12.2 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["dojox.data.ItemExplorer"]){ |
||
9 | dojo._hasResource["dojox.data.ItemExplorer"]=true; |
||
10 | dojo.provide("dojox.data.ItemExplorer");
|
||
11 | dojo.require("dijit.Tree");
|
||
12 | dojo.require("dijit.Dialog");
|
||
13 | dojo.require("dijit.Menu");
|
||
14 | dojo.require("dijit.form.ValidationTextBox");
|
||
15 | dojo.require("dijit.form.Textarea");
|
||
16 | dojo.require("dijit.form.Button");
|
||
17 | dojo.require("dijit.form.CheckBox");
|
||
18 | dojo.require("dijit.form.FilteringSelect");
|
||
19 | (function(){
|
||
20 | var _1=function(_2,_3,_4){ |
||
21 | var _5=_2.getValues(_3,_4);
|
||
22 | if(_5.length<2){ |
||
23 | _5=_2.getValue(_3,_4); |
||
24 | } |
||
25 | return _5;
|
||
26 | }; |
||
27 | dojo.declare("dojox.data.ItemExplorer",dijit.Tree,{useSelect:false,refSelectSearchAttr:null,constructor:function(_6){ |
||
28 | dojo.mixin(this,_6);
|
||
29 | var _7=this; |
||
30 | var _8={};
|
||
31 | var _9=this.rootModelNode={value:_8,id:"root"}; |
||
32 | this._modelNodeIdMap={};
|
||
33 | this._modelNodePropMap={};
|
||
34 | var _a=1; |
||
35 | this.model={getRoot:function(_b){ |
||
36 | _b(_9); |
||
37 | },mayHaveChildren:function(_c){ |
||
38 | return _c.value&&typeof _c.value=="object"&&!(_c.value instanceof Date); |
||
39 | },getChildren:function(_d,_e,_f){ |
||
40 | var _10,_11,_12=_d.value;
|
||
41 | var _13=[];
|
||
42 | if(_12==_8){
|
||
43 | _e([]); |
||
44 | return;
|
||
45 | } |
||
46 | var _14=_7.store&&_7.store.isItem(_12,true); |
||
47 | if(_14&&!_7.store.isItemLoaded(_12)){
|
||
48 | _7.store.loadItem({item:_12,onItem:function(_15){ |
||
49 | _12=_15; |
||
50 | _16(); |
||
51 | }}); |
||
52 | }else{
|
||
53 | _16(); |
||
54 | } |
||
55 | function _16(){ |
||
56 | if(_14){
|
||
57 | _10=_7.store.getAttributes(_12); |
||
58 | _11=_12; |
||
59 | }else{
|
||
60 | if(_12&&typeof _12=="object"){ |
||
61 | _11=_d.value; |
||
62 | _10=[]; |
||
63 | for(var i in _12){ |
||
64 | if(_12.hasOwnProperty(i)&&i!="__id"&&i!="__clientId"){ |
||
65 | _10.push(i); |
||
66 | } |
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | if(_10){
|
||
71 | for(var key,k=0;key=_10[k++];){ |
||
72 | _13.push({property:key,value:_14?_1(_7.store,_12,key):_12[key],parent:_11}); |
||
73 | } |
||
74 | _13.push({addNew:true,parent:_11,parentNode:_d}); |
||
75 | } |
||
76 | _e(_13); |
||
77 | }; |
||
78 | },getIdentity:function(_17){ |
||
79 | if(!_17.id){
|
||
80 | if(_17.addNew){
|
||
81 | _17.property="--addNew";
|
||
82 | } |
||
83 | _17.id=_a++; |
||
84 | if(_7.store){
|
||
85 | if(_7.store.isItem(_17.value)){
|
||
86 | var _18=_7.store.getIdentity(_17.value);
|
||
87 | (_7._modelNodeIdMap[_18]=_7._modelNodeIdMap[_18]||[]).push(_17); |
||
88 | } |
||
89 | if(_17.parent){
|
||
90 | _18=_7.store.getIdentity(_17.parent)+"."+_17.property;
|
||
91 | (_7._modelNodePropMap[_18]=_7._modelNodePropMap[_18]||[]).push(_17); |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | return _17.id;
|
||
96 | },getLabel:function(_19){ |
||
97 | return _19===_9?"Object Properties":_19.addNew?(_19.parent instanceof Array?"Add new value":"Add new property"):_19.property+": "+(_19.value instanceof Array?"("+_19.value.length+" elements)":_19.value); |
||
98 | },onChildrenChange:function(_1a){ |
||
99 | },onChange:function(_1b){ |
||
100 | }}; |
||
101 | },postCreate:function(){ |
||
102 | this.inherited(arguments); |
||
103 | dojo.connect(this,"onClick",function(_1c,_1d){ |
||
104 | this.lastFocused=_1d;
|
||
105 | if(_1c.addNew){
|
||
106 | this._addProperty();
|
||
107 | }else{
|
||
108 | this._editProperty();
|
||
109 | } |
||
110 | }); |
||
111 | var _1e=new dijit.Menu({targetNodeIds:[this.rootNode.domNode],id:"contextMenu"}); |
||
112 | dojo.connect(_1e,"_openMyself",this,function(e){ |
||
113 | var _1f=dijit.getEnclosingWidget(e.target);
|
||
114 | if(_1f){
|
||
115 | var _20=_1f.item;
|
||
116 | if(this.store.isItem(_20.value,true)&&!_20.parent){ |
||
117 | dojo.forEach(_1e.getChildren(),function(_21){
|
||
118 | _21.attr("disabled",(_21.label!="Add")); |
||
119 | }); |
||
120 | this.lastFocused=_1f;
|
||
121 | }else{
|
||
122 | if(_20.value&&typeof _20.value=="object"&&!(_20.value instanceof Date)){ |
||
123 | dojo.forEach(_1e.getChildren(),function(_22){
|
||
124 | _22.attr("disabled",(_22.label!="Add")&&(_22.label!="Delete")); |
||
125 | }); |
||
126 | this.lastFocused=_1f;
|
||
127 | }else{
|
||
128 | if(_20.property&&dojo.indexOf(this.store.getIdentityAttributes(),_20.property)>=0){ |
||
129 | this.focusNode(_1f);
|
||
130 | alert("Cannot modify an Identifier node.");
|
||
131 | }else{
|
||
132 | if(_20.addNew){
|
||
133 | this.focusNode(_1f);
|
||
134 | }else{
|
||
135 | dojo.forEach(_1e.getChildren(),function(_23){
|
||
136 | _23.attr("disabled",(_23.label!="Edit")&&(_23.label!="Delete")); |
||
137 | }); |
||
138 | this.lastFocused=_1f;
|
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | } |
||
144 | }); |
||
145 | _1e.addChild(new dijit.MenuItem({label:"Add",onClick:dojo.hitch(this,"_addProperty")})); |
||
146 | _1e.addChild(new dijit.MenuItem({label:"Edit",onClick:dojo.hitch(this,"_editProperty")})); |
||
147 | _1e.addChild(new dijit.MenuItem({label:"Delete",onClick:dojo.hitch(this,"_destroyProperty")})); |
||
148 | _1e.startup(); |
||
149 | },store:null,setStore:function(_24){ |
||
150 | this.store=_24;
|
||
151 | var _25=this; |
||
152 | if(this._editDialog){ |
||
153 | this._editDialog.destroyRecursive();
|
||
154 | delete this._editDialog; |
||
155 | } |
||
156 | dojo.connect(_24,"onSet",function(_26,_27,_28,_29){ |
||
157 | var _2a,i,_2b=_25.store.getIdentity(_26);
|
||
158 | _2a=_25._modelNodeIdMap[_2b]; |
||
159 | if(_2a&&(_28===undefined||_29===undefined||_28 instanceof Array||_29 instanceof Array||typeof _28=="object"||typeof _29=="object")){ |
||
160 | for(i=0;i<_2a.length;i++){ |
||
161 | (function(_2c){
|
||
162 | _25.model.getChildren(_2c,function(_2d){
|
||
163 | _25.model.onChildrenChange(_2c,_2d); |
||
164 | }); |
||
165 | })(_2a[i]); |
||
166 | } |
||
167 | } |
||
168 | _2a=_25._modelNodePropMap[_2b+"."+_27];
|
||
169 | if(_2a){
|
||
170 | for(i=0;i<_2a.length;i++){ |
||
171 | _2a[i].value=_29; |
||
172 | _25.model.onChange(_2a[i]); |
||
173 | } |
||
174 | } |
||
175 | }); |
||
176 | this.rootNode.setChildItems([]);
|
||
177 | },setItem:function(_2e){ |
||
178 | (this._modelNodeIdMap={})[this.store.getIdentity(_2e)]=[this.rootModelNode]; |
||
179 | this._modelNodePropMap={};
|
||
180 | this.rootModelNode.value=_2e;
|
||
181 | var _2f=this; |
||
182 | this.model.getChildren(this.rootModelNode,function(_30){ |
||
183 | _2f.rootNode.setChildItems(_30); |
||
184 | }); |
||
185 | },refreshItem:function(){ |
||
186 | this.setItem(this.rootModelNode.value); |
||
187 | },_createEditDialog:function(){ |
||
188 | this._editDialog=new dijit.Dialog({title:"Edit Property",execute:dojo.hitch(this,"_updateItem"),preload:true}); |
||
189 | this._editDialog.placeAt(dojo.body());
|
||
190 | this._editDialog.startup();
|
||
191 | var _31=dojo.doc.createElement("div"); |
||
192 | var _32=dojo.doc.createElement("label"); |
||
193 | dojo.attr(_32,"for","property"); |
||
194 | dojo.style(_32,"fontWeight","bold"); |
||
195 | dojo.attr(_32,"innerHTML","Property:"); |
||
196 | _31.appendChild(_32); |
||
197 | var _33=new dijit.form.ValidationTextBox({name:"property",value:"",required:true,disabled:true}).placeAt(_31); |
||
198 | _31.appendChild(dojo.doc.createElement("br"));
|
||
199 | _31.appendChild(dojo.doc.createElement("br"));
|
||
200 | var _34=new dijit.form.RadioButton({name:"itemType",value:"value",onClick:dojo.hitch(this,function(){ |
||
201 | this._enableFields("value"); |
||
202 | })}).placeAt(_31); |
||
203 | var _35=dojo.doc.createElement("label"); |
||
204 | dojo.attr(_35,"for","value"); |
||
205 | dojo.attr(_35,"innerHTML","Value (JSON):"); |
||
206 | _31.appendChild(_35); |
||
207 | var _36=dojo.doc.createElement("div"); |
||
208 | dojo.addClass(_36,"value");
|
||
209 | var _37=new dijit.form.Textarea({name:"jsonVal"}).placeAt(_36); |
||
210 | _31.appendChild(_36); |
||
211 | var _38=new dijit.form.RadioButton({name:"itemType",value:"reference",onClick:dojo.hitch(this,function(){ |
||
212 | this._enableFields("reference"); |
||
213 | })}).placeAt(_31); |
||
214 | var _39=dojo.doc.createElement("label"); |
||
215 | dojo.attr(_39,"for","_reference"); |
||
216 | dojo.attr(_39,"innerHTML","Reference (ID):"); |
||
217 | _31.appendChild(_39); |
||
218 | _31.appendChild(dojo.doc.createElement("br"));
|
||
219 | var _3a=dojo.doc.createElement("div"); |
||
220 | dojo.addClass(_3a,"reference");
|
||
221 | if(this.useSelect){ |
||
222 | var _3b=new dijit.form.FilteringSelect({name:"_reference",store:this.store,searchAttr:this.refSelectSearchAttr||this.store.getIdentityAttributes()[0],required:false,value:null,pageSize:10}).placeAt(_3a); |
||
223 | }else{
|
||
224 | var _3c=new dijit.form.ValidationTextBox({name:"_reference",value:"",promptMessage:"Enter the ID of the item to reference",isValid:dojo.hitch(this,function(_3d){ |
||
225 | return true; |
||
226 | })}).placeAt(_3a); |
||
227 | } |
||
228 | _31.appendChild(_3a); |
||
229 | _31.appendChild(dojo.doc.createElement("br"));
|
||
230 | _31.appendChild(dojo.doc.createElement("br"));
|
||
231 | var _3e=document.createElement("div"); |
||
232 | _3e.setAttribute("dir","rtl"); |
||
233 | var _3f=new dijit.form.Button({type:"reset",label:"Cancel"}).placeAt(_3e); |
||
234 | _3f.onClick=dojo.hitch(this._editDialog,"onCancel"); |
||
235 | var _40=new dijit.form.Button({type:"submit",label:"OK"}).placeAt(_3e); |
||
236 | _31.appendChild(_3e); |
||
237 | this._editDialog.attr("content",_31); |
||
238 | },_enableFields:function(_41){ |
||
239 | switch(_41){
|
||
240 | case "reference": |
||
241 | dojo.query(".value [widgetId]",this._editDialog.containerNode).forEach(function(_42){ |
||
242 | dijit.getEnclosingWidget(_42).attr("disabled",true); |
||
243 | }); |
||
244 | dojo.query(".reference [widgetId]",this._editDialog.containerNode).forEach(function(_43){ |
||
245 | dijit.getEnclosingWidget(_43).attr("disabled",false); |
||
246 | }); |
||
247 | break;
|
||
248 | case "value": |
||
249 | dojo.query(".value [widgetId]",this._editDialog.containerNode).forEach(function(_44){ |
||
250 | dijit.getEnclosingWidget(_44).attr("disabled",false); |
||
251 | }); |
||
252 | dojo.query(".reference [widgetId]",this._editDialog.containerNode).forEach(function(_45){ |
||
253 | dijit.getEnclosingWidget(_45).attr("disabled",true); |
||
254 | }); |
||
255 | break;
|
||
256 | } |
||
257 | },_updateItem:function(_46){ |
||
258 | var _47,_48,val,_49,_4a=this._editDialog.attr("title")=="Edit Property"; |
||
259 | var _4b=this._editDialog; |
||
260 | var _4c=this.store; |
||
261 | function _4d(){ |
||
262 | try{
|
||
263 | var _4e,_4f=[];
|
||
264 | var _50=_46.property;
|
||
265 | if(_4a){
|
||
266 | while(!_4c.isItem(_48.parent,true)){ |
||
267 | _47=_47.getParent(); |
||
268 | _4f.push(_48.property); |
||
269 | _48=_47.item; |
||
270 | } |
||
271 | if(_4f.length==0){ |
||
272 | _4c.setValue(_48.parent,_48.property,val); |
||
273 | }else{
|
||
274 | _49=_1(_4c,_48.parent,_48.property); |
||
275 | if(_49 instanceof Array){ |
||
276 | _49=_49.concat(); |
||
277 | } |
||
278 | _4e=_49; |
||
279 | while(_4f.length>1){ |
||
280 | _4e=_4e[_4f.pop()]; |
||
281 | } |
||
282 | _4e[_4f]=val; |
||
283 | _4c.setValue(_48.parent,_48.property,_49); |
||
284 | } |
||
285 | }else{
|
||
286 | if(_4c.isItem(_51,true)){ |
||
287 | if(!_4c.isItemLoaded(_51)){
|
||
288 | _4c.loadItem({item:_51,onItem:function(_52){ |
||
289 | if(_52 instanceof Array){ |
||
290 | _50=_52.length; |
||
291 | } |
||
292 | _4c.setValue(_52,_50,val); |
||
293 | }}); |
||
294 | }else{
|
||
295 | if(_51 instanceof Array){ |
||
296 | _50=_51.length; |
||
297 | } |
||
298 | _4c.setValue(_51,_50,val); |
||
299 | } |
||
300 | }else{
|
||
301 | if(_48.value instanceof Array){ |
||
302 | _4f.push(_48.value.length); |
||
303 | }else{
|
||
304 | _4f.push(_46.property); |
||
305 | } |
||
306 | while(!_4c.isItem(_48.parent,true)){ |
||
307 | _47=_47.getParent(); |
||
308 | _4f.push(_48.property); |
||
309 | _48=_47.item; |
||
310 | } |
||
311 | _49=_1(_4c,_48.parent,_48.property); |
||
312 | _4e=_49; |
||
313 | while(_4f.length>1){ |
||
314 | _4e=_4e[_4f.pop()]; |
||
315 | } |
||
316 | _4e[_4f]=val; |
||
317 | _4c.setValue(_48.parent,_48.property,_49); |
||
318 | } |
||
319 | } |
||
320 | } |
||
321 | catch(e){
|
||
322 | alert(e); |
||
323 | } |
||
324 | }; |
||
325 | if(_4b.validate()){
|
||
326 | _47=this.lastFocused;
|
||
327 | _48=_47.item; |
||
328 | var _51=_48.value;
|
||
329 | if(_48.addNew){
|
||
330 | _51=_47.item.parent; |
||
331 | _47=_47.getParent(); |
||
332 | _48=_47.item; |
||
333 | } |
||
334 | val=null;
|
||
335 | switch(_46.itemType){
|
||
336 | case "reference": |
||
337 | this.store.fetchItemByIdentity({identity:_46._reference,onItem:function(_53){ |
||
338 | val=_53; |
||
339 | _4d(); |
||
340 | },onError:function(){ |
||
341 | alert("The id could not be found");
|
||
342 | }}); |
||
343 | break;
|
||
344 | case "value": |
||
345 | var _54=_46.jsonVal;
|
||
346 | val=dojo.fromJson(_54); |
||
347 | if(typeof val=="function"){ |
||
348 | val.toString=function(){ |
||
349 | return _54;
|
||
350 | }; |
||
351 | } |
||
352 | _4d(); |
||
353 | break;
|
||
354 | } |
||
355 | }else{
|
||
356 | _4b.show(); |
||
357 | } |
||
358 | },_editProperty:function(){ |
||
359 | var _55=dojo.mixin({},this.lastFocused.item); |
||
360 | if(!this._editDialog){ |
||
361 | this._createEditDialog();
|
||
362 | }else{
|
||
363 | this._editDialog.reset();
|
||
364 | } |
||
365 | if(dojo.indexOf(this.store.getIdentityAttributes(),_55.property)>=0){ |
||
366 | alert("Cannot Edit an Identifier!");
|
||
367 | }else{
|
||
368 | this._editDialog.attr("title","Edit Property"); |
||
369 | dijit.getEnclosingWidget(dojo.query("input",this._editDialog.containerNode)[0]).attr("disabled",true); |
||
370 | if(this.store.isItem(_55.value,true)){ |
||
371 | if(_55.parent){
|
||
372 | _55.itemType="reference";
|
||
373 | this._enableFields(_55.itemType);
|
||
374 | _55._reference=this.store.getIdentity(_55.value);
|
||
375 | this._editDialog.attr("value",_55); |
||
376 | this._editDialog.show();
|
||
377 | } |
||
378 | }else{
|
||
379 | if(_55.value&&typeof _55.value=="object"&&!(_55.value instanceof Date)){ |
||
380 | }else{
|
||
381 | _55.itemType="value";
|
||
382 | this._enableFields(_55.itemType);
|
||
383 | _55.jsonVal=typeof _55.value=="function"?_55.value.toString():_55.value instanceof Date?"new Date(\""+_55.value+"\")":dojo.toJson(_55.value); |
||
384 | this._editDialog.attr("value",_55); |
||
385 | this._editDialog.show();
|
||
386 | } |
||
387 | } |
||
388 | } |
||
389 | },_destroyProperty:function(){ |
||
390 | var _56=this.lastFocused; |
||
391 | var _57=_56.item;
|
||
392 | var _58=[];
|
||
393 | while(!this.store.isItem(_57.parent,true)||_57.parent instanceof Array){ |
||
394 | _56=_56.getParent(); |
||
395 | _58.push(_57.property); |
||
396 | _57=_56.item; |
||
397 | } |
||
398 | if(dojo.indexOf(this.store.getIdentityAttributes(),_57.property)>=0){ |
||
399 | alert("Cannot Delete an Identifier!");
|
||
400 | }else{
|
||
401 | try{
|
||
402 | if(_58.length>0){ |
||
403 | var _59,_5a=_1(this.store,_57.parent,_57.property); |
||
404 | _59=_5a; |
||
405 | while(_58.length>1){ |
||
406 | _59=_59[_58.pop()]; |
||
407 | } |
||
408 | if(dojo.isArray(_59)){
|
||
409 | _59.splice(_58,1);
|
||
410 | }else{
|
||
411 | delete _59[_58];
|
||
412 | } |
||
413 | this.store.setValue(_57.parent,_57.property,_5a);
|
||
414 | }else{
|
||
415 | this.store.unsetAttribute(_57.parent,_57.property);
|
||
416 | } |
||
417 | } |
||
418 | catch(e){
|
||
419 | alert(e); |
||
420 | } |
||
421 | } |
||
422 | },_addProperty:function(){ |
||
423 | var _5b=this.lastFocused.item; |
||
424 | var _5c=_5b.value;
|
||
425 | var _5d=dojo.hitch(this,function(){ |
||
426 | var _5e=null; |
||
427 | if(!this._editDialog){ |
||
428 | this._createEditDialog();
|
||
429 | }else{
|
||
430 | this._editDialog.reset();
|
||
431 | } |
||
432 | if(_5c instanceof Array){ |
||
433 | _5e=_5c.length; |
||
434 | dijit.getEnclosingWidget(dojo.query("input",this._editDialog.containerNode)[0]).attr("disabled",true); |
||
435 | }else{
|
||
436 | dijit.getEnclosingWidget(dojo.query("input",this._editDialog.containerNode)[0]).attr("disabled",false); |
||
437 | } |
||
438 | this._editDialog.attr("title","Add Property"); |
||
439 | this._enableFields("value"); |
||
440 | this._editDialog.attr("value",{itemType:"value",property:_5e}); |
||
441 | this._editDialog.show();
|
||
442 | }); |
||
443 | if(_5b.addNew){
|
||
444 | _5b=this.lastFocused.getParent().item;
|
||
445 | _5c=this.lastFocused.item.parent;
|
||
446 | } |
||
447 | if(_5b.property&&dojo.indexOf(this.store.getIdentityAttributes(),_5b.property)>=0){ |
||
448 | alert("Cannot add properties to an ID node!");
|
||
449 | }else{
|
||
450 | if(this.store.isItem(_5c,true)&&!this.store.isItemLoaded(_5c)){ |
||
451 | this.store.loadItem({item:_5c,onItem:function(_5f){ |
||
452 | _5c=_5f; |
||
453 | _5d(); |
||
454 | }}); |
||
455 | }else{
|
||
456 | _5d(); |
||
457 | } |
||
458 | } |
||
459 | }}); |
||
460 | })(); |
||
461 | } |