root / trunk / web / dojo / dijit / tree / TreeStoreModel.js @ 13
History | View | Annotate | Download (3.76 KB)
| 1 |
/*
|
|---|---|
| 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.tree.TreeStoreModel"]){ |
| 9 |
dojo._hasResource["dijit.tree.TreeStoreModel"]=true; |
| 10 |
dojo.provide("dijit.tree.TreeStoreModel");
|
| 11 |
dojo.declare("dijit.tree.TreeStoreModel",null,{store:null,childrenAttrs:["children"],newItemIdAttr:"id",labelAttr:"",root:null,query:null,deferItemLoadingUntilExpand:false,constructor:function(_1){ |
| 12 |
dojo.mixin(this,_1);
|
| 13 |
this.connects=[];
|
| 14 |
var _2=this.store; |
| 15 |
if(!_2.getFeatures()["dojo.data.api.Identity"]){ |
| 16 |
throw new Error("dijit.Tree: store must support dojo.data.Identity"); |
| 17 |
} |
| 18 |
if(_2.getFeatures()["dojo.data.api.Notification"]){ |
| 19 |
this.connects=this.connects.concat([dojo.connect(_2,"onNew",this,"onNewItem"),dojo.connect(_2,"onDelete",this,"onDeleteItem"),dojo.connect(_2,"onSet",this,"onSetItem")]); |
| 20 |
} |
| 21 |
},destroy:function(){ |
| 22 |
dojo.forEach(this.connects,dojo.disconnect);
|
| 23 |
},getRoot:function(_3,_4){ |
| 24 |
if(this.root){ |
| 25 |
_3(this.root);
|
| 26 |
}else{
|
| 27 |
this.store.fetch({query:this.query,onComplete:dojo.hitch(this,function(_5){ |
| 28 |
if(_5.length!=1){ |
| 29 |
throw new Error(this.declaredClass+": query "+dojo.toJson(this.query)+" returned "+_5.length+" items, but must return exactly one item"); |
| 30 |
} |
| 31 |
this.root=_5[0]; |
| 32 |
_3(this.root);
|
| 33 |
}),onError:_4});
|
| 34 |
} |
| 35 |
},mayHaveChildren:function(_6){ |
| 36 |
return dojo.some(this.childrenAttrs,function(_7){ |
| 37 |
return this.store.hasAttribute(_6,_7); |
| 38 |
},this);
|
| 39 |
},getChildren:function(_8,_9,_a){ |
| 40 |
var _b=this.store; |
| 41 |
if(!_b.isItemLoaded(_8)){
|
| 42 |
var _c=dojo.hitch(this,arguments.callee); |
| 43 |
_b.loadItem({item:_8,onItem:function(_d){
|
| 44 |
_c(_d,_9,_a); |
| 45 |
},onError:_a});
|
| 46 |
return;
|
| 47 |
} |
| 48 |
var _e=[];
|
| 49 |
for(var i=0;i<this.childrenAttrs.length;i++){ |
| 50 |
var _f=_b.getValues(_8,this.childrenAttrs[i]); |
| 51 |
_e=_e.concat(_f); |
| 52 |
} |
| 53 |
var _10=0; |
| 54 |
if(!this.deferItemLoadingUntilExpand){ |
| 55 |
dojo.forEach(_e,function(_11){
|
| 56 |
if(!_b.isItemLoaded(_11)){
|
| 57 |
_10++; |
| 58 |
} |
| 59 |
}); |
| 60 |
} |
| 61 |
if(_10==0){ |
| 62 |
_9(_e); |
| 63 |
}else{
|
| 64 |
dojo.forEach(_e,function(_12,idx){
|
| 65 |
if(!_b.isItemLoaded(_12)){
|
| 66 |
_b.loadItem({item:_12,onItem:function(_13){
|
| 67 |
_e[idx]=_13; |
| 68 |
if(--_10==0){ |
| 69 |
_9(_e); |
| 70 |
} |
| 71 |
},onError:_a});
|
| 72 |
} |
| 73 |
}); |
| 74 |
} |
| 75 |
},isItem:function(_14){ |
| 76 |
return this.store.isItem(_14); |
| 77 |
},fetchItemByIdentity:function(_15){ |
| 78 |
this.store.fetchItemByIdentity(_15);
|
| 79 |
},getIdentity:function(_16){ |
| 80 |
return this.store.getIdentity(_16); |
| 81 |
},getLabel:function(_17){ |
| 82 |
if(this.labelAttr){ |
| 83 |
return this.store.getValue(_17,this.labelAttr); |
| 84 |
}else{
|
| 85 |
return this.store.getLabel(_17); |
| 86 |
} |
| 87 |
},newItem:function(_18,_19,_1a){ |
| 88 |
var _1b={parent:_19,attribute:this.childrenAttrs[0],insertIndex:_1a}; |
| 89 |
if(this.newItemIdAttr&&_18[this.newItemIdAttr]){ |
| 90 |
this.fetchItemByIdentity({identity:_18[this.newItemIdAttr],scope:this,onItem:function(_1c){ |
| 91 |
if(_1c){
|
| 92 |
this.pasteItem(_1c,null,_19,true,_1a); |
| 93 |
}else{
|
| 94 |
this.store.newItem(_18,_1b);
|
| 95 |
} |
| 96 |
}}); |
| 97 |
}else{
|
| 98 |
this.store.newItem(_18,_1b);
|
| 99 |
} |
| 100 |
},pasteItem:function(_1d,_1e,_1f,_20,_21){ |
| 101 |
var _22=this.store,_23=this.childrenAttrs[0]; |
| 102 |
if(_1e){
|
| 103 |
dojo.forEach(this.childrenAttrs,function(_24){ |
| 104 |
if(_22.containsValue(_1e,_24,_1d)){
|
| 105 |
if(!_20){
|
| 106 |
var _25=dojo.filter(_22.getValues(_1e,_24),function(x){ |
| 107 |
return x!=_1d;
|
| 108 |
}); |
| 109 |
_22.setValues(_1e,_24,_25); |
| 110 |
} |
| 111 |
_23=_24; |
| 112 |
} |
| 113 |
}); |
| 114 |
} |
| 115 |
if(_1f){
|
| 116 |
if(typeof _21=="number"){ |
| 117 |
var _26=_22.getValues(_1f,_23).slice();
|
| 118 |
_26.splice(_21,0,_1d);
|
| 119 |
_22.setValues(_1f,_23,_26); |
| 120 |
}else{
|
| 121 |
_22.setValues(_1f,_23,_22.getValues(_1f,_23).concat(_1d)); |
| 122 |
} |
| 123 |
} |
| 124 |
},onChange:function(_27){ |
| 125 |
},onChildrenChange:function(_28,_29){ |
| 126 |
},onDelete:function(_2a,_2b){ |
| 127 |
},onNewItem:function(_2c,_2d){ |
| 128 |
if(!_2d){
|
| 129 |
return;
|
| 130 |
} |
| 131 |
this.getChildren(_2d.item,dojo.hitch(this,function(_2e){ |
| 132 |
this.onChildrenChange(_2d.item,_2e);
|
| 133 |
})); |
| 134 |
},onDeleteItem:function(_2f){ |
| 135 |
this.onDelete(_2f);
|
| 136 |
},onSetItem:function(_30,_31,_32,_33){ |
| 137 |
if(dojo.indexOf(this.childrenAttrs,_31)!=-1){ |
| 138 |
this.getChildren(_30,dojo.hitch(this,function(_34){ |
| 139 |
this.onChildrenChange(_30,_34);
|
| 140 |
})); |
| 141 |
}else{
|
| 142 |
this.onChange(_30);
|
| 143 |
} |
| 144 |
}}); |
| 145 |
} |