]>
git.wh0rd.org - tt-rss.git/blob - lib/dijit/tree/ObjectStoreModel.js.uncompressed.js
1 define("dijit/tree/ObjectStoreModel", [
2 "dojo/_base/array", // array.filter array.forEach array.indexOf array.some
3 "dojo/aspect", // aspect.before, aspect.after
4 "dojo/_base/declare", // declare
5 "dojo/_base/lang", // lang.hitch
7 ], function(array
, aspect
, declare
, lang
, when
){
10 // dijit/tree/ObjectStoreModel
12 return declare("dijit.tree.ObjectStoreModel", null, {
14 // Implements dijit/tree/model connecting dijit/Tree to a dojo/store/api/Store that implements
17 // If getChildren() returns an array with an observe() method, then it will be leveraged to reflect
18 // store updates to the tree. So, this class will work best when:
20 // 1. the store implements dojo/store/Observable
21 // 2. getChildren() is implemented as a query to the server (i.e. it calls store.query())
23 // Drag and Drop: To support drag and drop, besides implementing getChildren()
24 // and dojo/store/Observable, the store must support the parent option to put().
25 // And in order to have child elements ordered according to how the user dropped them,
26 // put() must support the before option.
28 // store: dojo/store/api/Store
33 // Get label for tree node from this attribute
36 // root: [readonly] Object
37 // Pointer to the root item from the dojo/store/api/Store (read only, not a parameter)
41 // Specifies datastore query to return the root item for the tree.
42 // Must only return a single item. Alternately can just pass in pointer
48 constructor: function(/* Object */ args
){
50 // Passed the arguments listed above (store, etc)
54 lang
.mixin(this, args
);
56 this.childrenCache
= {}; // map from id to array of children
60 // TODO: should cancel any in-progress processing of getRoot(), getChildren()
61 for(var id
in this.childrenCache
){
62 this.childrenCache
[id
].close
&& this.childrenCache
[id
].close();
66 // =======================================================================
67 // Methods for traversing hierarchy
69 getRoot: function(onItem
, onError
){
71 // Calls onItem with the root item for the tree, possibly a fabricated item.
72 // Calls onError on error.
77 when(res
= this.store
.query(this.query
),
78 lang
.hitch(this, function(items
){
79 //console.log("queried root: ", res);
80 if(items
.length
!= 1){
81 throw new Error("dijit.tree.ObjectStoreModel: root query returned " + items
.length
+
82 " items, but must return exactly one");
87 // Setup listener to detect if root item changes
89 res
.observe(lang
.hitch(this, function(obj
){
90 // Presumably removedFrom == insertedInto == 1, and this call indicates item has changed.
91 //console.log("root changed: ", obj);
93 }), true); // true to listen for updates to obj
101 mayHaveChildren: function(/*===== item =====*/){
103 // Tells if an item has or may have children. Implementing logic here
104 // avoids showing +/- expando icon for nodes that we know don't have children.
105 // (For efficiency reasons we may not want to check if an element actually
106 // has children until user clicks the expando node).
108 // Application code should override this method based on the data, for example
109 // it could be `return item.leaf == true;`.
111 // Item from the dojo/store
115 getChildren: function(/*Object*/ parentItem
, /*function(items)*/ onComplete
, /*function*/ onError
){
117 // Calls onComplete() with array of child items of given parent item.
119 // Item from the dojo/store
121 var id
= this.store
.getIdentity(parentItem
);
122 if(this.childrenCache
[id
]){
123 when(this.childrenCache
[id
], onComplete
, onError
);
127 var res
= this.childrenCache
[id
] = this.store
.getChildren(parentItem
);
130 when(res
, onComplete
, onError
);
132 // Setup listener in case children list changes, or the item(s) in the children list are
133 // updated in some way.
135 res
.observe(lang
.hitch(this, function(obj
, removedFrom
, insertedInto
){
136 //console.log("observe on children of ", id, ": ", obj, removedFrom, insertedInto);
138 // If removedFrom == insertedInto, this call indicates that the item has changed.
139 // Even if removedFrom != insertedInto, the item may have changed.
142 if(removedFrom
!= insertedInto
){
143 // Indicates an item was added, removed, or re-parented. The children[] array (returned from
144 // res.then(...)) has already been updated (like a live collection), so just use it.
145 when(res
, lang
.hitch(this, "onChildrenChange", parentItem
));
147 }), true); // true means to notify on item changes
151 // =======================================================================
154 isItem: function(/*===== something =====*/){
155 return true; // Boolean
158 fetchItemByIdentity: function(/* object */ keywordArgs
){
159 this.store
.get(keywordArgs
.identity
).then(
160 lang
.hitch(keywordArgs
.scope
, keywordArgs
.onItem
),
161 lang
.hitch(keywordArgs
.scope
, keywordArgs
.onError
)
165 getIdentity: function(/* item */ item
){
166 return this.store
.getIdentity(item
); // Object
169 getLabel: function(/*dojo/data/Item*/ item
){
171 // Get the label for an item
172 return item
[this.labelAttr
]; // String
175 // =======================================================================
176 // Write interface, for DnD
178 newItem: function(/* dijit/tree/dndSource.__Item */ args
, /*Item*/ parent
, /*int?*/ insertIndex
, /*Item*/ before
){
180 // Creates a new item. See `dojo/data/api/Write` for details on args.
181 // Used in drag & drop when item from external source dropped onto tree.
183 return this.store
.put(args
, {
189 pasteItem: function(/*Item*/ childItem
, /*Item*/ oldParentItem
, /*Item*/ newParentItem
,
190 /*Boolean*/ bCopy
, /*int?*/ insertIndex
, /*Item*/ before
){
192 // Move or copy an item from one parent item to another.
193 // Used in drag & drop
196 // In order for DnD moves to work correctly, childItem needs to be orphaned from oldParentItem
197 // before being adopted by newParentItem. That way, the TreeNode is moved rather than
198 // an additional TreeNode being created, and the old TreeNode subsequently being deleted.
199 // The latter loses information such as selection and opened/closed children TreeNodes.
200 // Unfortunately simply calling this.store.put() will send notifications in a random order, based
201 // on when the TreeNodes in question originally appeared, and not based on the drag-from
202 // TreeNode vs. the drop-onto TreeNode.
204 var oldParentChildren
= [].concat(this.childrenCache
[this.getIdentity(oldParentItem
)]), // concat to make copy
205 index
= array
.indexOf(oldParentChildren
, childItem
);
206 oldParentChildren
.splice(index
, 1);
207 this.onChildrenChange(oldParentItem
, oldParentChildren
);
210 return this.store
.put(childItem
, {
212 parent
: newParentItem
,
217 // =======================================================================
220 onChange: function(/*dojo/data/Item*/ /*===== item =====*/){
222 // Callback whenever an item has changed, so that Tree
223 // can update the label, icon, etc. Note that changes
224 // to an item's children or parent(s) will trigger an
225 // onChildrenChange() so you can ignore those changes here.
230 onChildrenChange: function(/*===== parent, newChildrenList =====*/){
232 // Callback to do notifications about new, updated, or deleted items.
233 // parent: dojo/data/Item
234 // newChildrenList: Object[]
235 // Items from the store
240 onDelete: function(/*dojo/data/Item*/ /*===== item =====*/){
242 // Callback when an item has been deleted.
243 // Actually we have no way of knowing this with the new dojo.store API,
244 // so this method is never called (but it's left here since Tree connects