123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- define("dijit/tree/TreeStoreModel", [
- "dojo/_base/array",
- "dojo/aspect",
- "dojo/_base/declare",
- "dojo/_base/json",
- "dojo/_base/lang"
- ], function(array, aspect, declare, json, lang){
-
-
-
-
-
- return declare("dijit.tree.TreeStoreModel", null, {
-
-
-
-
-
-
- store: null,
-
-
- childrenAttrs: ["children"],
-
-
-
-
-
-
-
-
- newItemIdAttr: "id",
-
-
-
- labelAttr: "",
-
-
- root: null,
-
-
-
-
-
-
- query: null,
-
-
-
-
-
-
-
- deferItemLoadingUntilExpand: false,
- constructor: function(/* Object */ args){
-
-
-
-
- lang.mixin(this, args);
- this.connects = [];
- var store = this.store;
- if(!store.getFeatures()['dojo.data.api.Identity']){
- throw new Error("dijit.Tree: store must support dojo.data.Identity");
- }
-
- if(store.getFeatures()['dojo.data.api.Notification']){
- this.connects = this.connects.concat([
- aspect.after(store, "onNew", lang.hitch(this, "onNewItem"), true),
- aspect.after(store, "onDelete", lang.hitch(this, "onDeleteItem"), true),
- aspect.after(store, "onSet", lang.hitch(this, "onSetItem"), true)
- ]);
- }
- },
- destroy: function(){
- var h;
- while(h = this.connects.pop()){ h.remove(); }
-
- },
-
-
- getRoot: function(onItem, onError){
-
-
-
- if(this.root){
- onItem(this.root);
- }else{
- this.store.fetch({
- query: this.query,
- onComplete: lang.hitch(this, function(items){
- if(items.length != 1){
- throw new Error(this.declaredClass + ": query " + json.stringify(this.query) + " returned " + items.length +
- " items, but must return exactly one item");
- }
- this.root = items[0];
- onItem(this.root);
- }),
- onError: onError
- });
- }
- },
- mayHaveChildren: function(/*dojo.data.Item*/ item){
-
-
-
-
-
- return array.some(this.childrenAttrs, function(attr){
- return this.store.hasAttribute(item, attr);
- }, this);
- },
- getChildren: function(/*dojo.data.Item*/ parentItem, /*function(items)*/ onComplete, /*function*/ onError){
-
-
- var store = this.store;
- if(!store.isItemLoaded(parentItem)){
-
-
-
- var getChildren = lang.hitch(this, arguments.callee);
- store.loadItem({
- item: parentItem,
- onItem: function(parentItem){
- getChildren(parentItem, onComplete, onError);
- },
- onError: onError
- });
- return;
- }
-
- var childItems = [];
- for(var i=0; i<this.childrenAttrs.length; i++){
- var vals = store.getValues(parentItem, this.childrenAttrs[i]);
- childItems = childItems.concat(vals);
- }
-
- var _waitCount = 0;
- if(!this.deferItemLoadingUntilExpand){
- array.forEach(childItems, function(item){ if(!store.isItemLoaded(item)){ _waitCount++; } });
- }
- if(_waitCount == 0){
-
- onComplete(childItems);
- }else{
-
- array.forEach(childItems, function(item, idx){
- if(!store.isItemLoaded(item)){
- store.loadItem({
- item: item,
- onItem: function(item){
- childItems[idx] = item;
- if(--_waitCount == 0){
-
- onComplete(childItems);
- }
- },
- onError: onError
- });
- }
- });
- }
- },
-
-
- isItem: function(/* anything */ something){
- return this.store.isItem(something);
- },
- fetchItemByIdentity: function(/* object */ keywordArgs){
- this.store.fetchItemByIdentity(keywordArgs);
- },
- getIdentity: function(/* item */ item){
- return this.store.getIdentity(item);
- },
- getLabel: function(/*dojo.data.Item*/ item){
-
-
- if(this.labelAttr){
- return this.store.getValue(item,this.labelAttr);
- }else{
- return this.store.getLabel(item);
- }
- },
-
-
- newItem: function(/* dojo.dnd.Item */ args, /*Item*/ parent, /*int?*/ insertIndex){
-
-
-
-
-
-
-
- var pInfo = {parent: parent, attribute: this.childrenAttrs[0]}, LnewItem;
- if(this.newItemIdAttr && args[this.newItemIdAttr]){
-
- this.fetchItemByIdentity({identity: args[this.newItemIdAttr], scope: this, onItem: function(item){
- if(item){
-
- this.pasteItem(item, null, parent, true, insertIndex);
- }else{
-
- LnewItem=this.store.newItem(args, pInfo);
- if(LnewItem && (insertIndex!=undefined)){
-
- this.pasteItem(LnewItem, parent, parent, false, insertIndex);
- }
- }
- }});
- }else{
-
- LnewItem=this.store.newItem(args, pInfo);
- if(LnewItem && (insertIndex!=undefined)){
-
- this.pasteItem(LnewItem, parent, parent, false, insertIndex);
- }
- }
- },
- pasteItem: function(/*Item*/ childItem, /*Item*/ oldParentItem, /*Item*/ newParentItem, /*Boolean*/ bCopy, /*int?*/ insertIndex){
-
-
-
- var store = this.store,
- parentAttr = this.childrenAttrs[0];
-
- if(oldParentItem){
- array.forEach(this.childrenAttrs, function(attr){
- if(store.containsValue(oldParentItem, attr, childItem)){
- if(!bCopy){
- var values = array.filter(store.getValues(oldParentItem, attr), function(x){
- return x != childItem;
- });
- store.setValues(oldParentItem, attr, values);
- }
- parentAttr = attr;
- }
- });
- }
-
- if(newParentItem){
- if(typeof insertIndex == "number"){
-
- var childItems = store.getValues(newParentItem, parentAttr).slice();
- childItems.splice(insertIndex, 0, childItem);
- store.setValues(newParentItem, parentAttr, childItems);
- }else{
- store.setValues(newParentItem, parentAttr,
- store.getValues(newParentItem, parentAttr).concat(childItem));
- }
- }
- },
-
-
- onChange: function(/*dojo.data.Item*/ /*===== item =====*/){
-
-
-
-
-
-
-
- },
- onChildrenChange: function(/*===== parent, newChildrenList =====*/){
-
-
-
-
-
-
- },
- onDelete: function(/*dojo.data.Item*/ /*===== item =====*/){
-
-
-
-
-
-
-
- },
-
-
- onNewItem: function(/* dojo.data.Item */ item, /* Object */ parentInfo){
-
-
-
-
-
-
-
-
-
-
-
-
- if(!parentInfo){
- return;
- }
-
-
-
-
-
- this.getChildren(parentInfo.item, lang.hitch(this, function(children){
- this.onChildrenChange(parentInfo.item, children);
- }));
- },
- onDeleteItem: function(/*Object*/ item){
-
-
- this.onDelete(item);
- },
- onSetItem: function(item, attribute /*===== , oldValue, newValue =====*/){
-
-
-
-
-
-
-
-
-
-
-
-
-
- if(array.indexOf(this.childrenAttrs, attribute) != -1){
-
- this.getChildren(item, lang.hitch(this, function(children){
-
- this.onChildrenChange(item, children);
- }));
- }else{
-
- this.onChange(item);
- }
- }
- });
- });
|