| 1 | define([ |
| 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/Deferred", |
| 6 | "dojo/_base/lang", // lang.hitch |
| 7 | "dojo/when", |
| 8 | "../Destroyable" |
| 9 | ], function(array, aspect, declare, Deferred, lang, when, Destroyable){ |
| 10 | |
| 11 | // module: |
| 12 | // dijit/tree/ObjectStoreModel |
| 13 | |
| 14 | return declare("dijit.tree.ObjectStoreModel", Destroyable, { |
| 15 | // summary: |
| 16 | // Implements dijit/tree/model connecting dijit/Tree to a dojo/store/api/Store that implements |
| 17 | // getChildren(). |
| 18 | // |
| 19 | // If getChildren() returns an array with an observe() method, then it will be leveraged to reflect |
| 20 | // store updates to the tree. So, this class will work best when: |
| 21 | // |
| 22 | // 1. the store implements dojo/store/Observable |
| 23 | // 2. getChildren() is implemented as a query to the server (i.e. it calls store.query()) |
| 24 | // |
| 25 | // Drag and Drop: To support drag and drop, besides implementing getChildren() |
| 26 | // and dojo/store/Observable, the store must support the parent option to put(). |
| 27 | // And in order to have child elements ordered according to how the user dropped them, |
| 28 | // put() must support the before option. |
| 29 | |
| 30 | // store: dojo/store/api/Store |
| 31 | // Underlying store |
| 32 | store: null, |
| 33 | |
| 34 | // labelAttr: String |
| 35 | // Get label for tree node from this attribute |
| 36 | labelAttr: "name", |
| 37 | |
| 38 | // labelType: [const] String |
| 39 | // Specifies how to interpret the labelAttr in the data store items. |
| 40 | // Can be "html" or "text". |
| 41 | labelType: "text", |
| 42 | |
| 43 | // root: [readonly] Object |
| 44 | // Pointer to the root item from the dojo/store/api/Store (read only, not a parameter) |
| 45 | root: null, |
| 46 | |
| 47 | // query: anything |
| 48 | // Specifies datastore query to return the root item for the tree. |
| 49 | // Must only return a single item. Alternately can just pass in pointer |
| 50 | // to root item. |
| 51 | // example: |
| 52 | // | {id:'ROOT'} |
| 53 | query: null, |
| 54 | |
| 55 | constructor: function(/* Object */ args){ |
| 56 | // summary: |
| 57 | // Passed the arguments listed above (store, etc) |
| 58 | // tags: |
| 59 | // private |
| 60 | |
| 61 | lang.mixin(this, args); |
| 62 | |
| 63 | // Map from id of each parent node to array of its children, or to Promise for that array of children. |
| 64 | this.childrenCache = {}; |
| 65 | }, |
| 66 | |
| 67 | // ======================================================================= |
| 68 | // Methods for traversing hierarchy |
| 69 | |
| 70 | getRoot: function(onItem, onError){ |
| 71 | // summary: |
| 72 | // Calls onItem with the root item for the tree, possibly a fabricated item. |
| 73 | // Calls onError on error. |
| 74 | if(this.root){ |
| 75 | onItem(this.root); |
| 76 | }else{ |
| 77 | var res = this.store.query(this.query); |
| 78 | if(res.then){ |
| 79 | this.own(res); // in case app calls destroy() before query completes |
| 80 | } |
| 81 | |
| 82 | when(res, |
| 83 | lang.hitch(this, function(items){ |
| 84 | //console.log("queried root: ", res); |
| 85 | if(items.length != 1){ |
| 86 | throw new Error("dijit.tree.ObjectStoreModel: root query returned " + items.length + |
| 87 | " items, but must return exactly one"); |
| 88 | } |
| 89 | this.root = items[0]; |
| 90 | onItem(this.root); |
| 91 | |
| 92 | // Setup listener to detect if root item changes |
| 93 | if(res.observe){ |
| 94 | res.observe(lang.hitch(this, function(obj){ |
| 95 | // Presumably removedFrom == insertedInto == 1, and this call indicates item has changed. |
| 96 | //console.log("root changed: ", obj); |
| 97 | this.onChange(obj); |
| 98 | }), true); // true to listen for updates to obj |
| 99 | } |
| 100 | }), |
| 101 | onError |
| 102 | ); |
| 103 | } |
| 104 | }, |
| 105 | |
| 106 | mayHaveChildren: function(/*===== item =====*/){ |
| 107 | // summary: |
| 108 | // Tells if an item has or might have children. Implementing logic here |
| 109 | // avoids showing +/- expando icon for nodes that we know won't have children. |
| 110 | // (For efficiency reasons we may not want to check if an element actually |
| 111 | // has children until user clicks the expando node). |
| 112 | // |
| 113 | // Application code should override this method based on the data, for example |
| 114 | // it could be `return item.leaf == true;`. |
| 115 | // |
| 116 | // Note that mayHaveChildren() must return true for an item if it could possibly |
| 117 | // have children in the future, due to drag-an-drop or some other data store update. |
| 118 | // Also note that it may return true if it's just too expensive to check during tree |
| 119 | // creation whether or not the item has children. |
| 120 | // item: Object |
| 121 | // Item from the dojo/store |
| 122 | return true; |
| 123 | }, |
| 124 | |
| 125 | getChildren: function(/*Object*/ parentItem, /*function(items)*/ onComplete, /*function*/ onError){ |
| 126 | // summary: |
| 127 | // Calls onComplete() with array of child items of given parent item. |
| 128 | // parentItem: |
| 129 | // Item from the dojo/store |
| 130 | |
| 131 | // TODO: |
| 132 | // For 2.0, change getChildren(), getRoot(), etc. to return a cancelable promise, rather than taking |
| 133 | // onComplete() and onError() callbacks. Also, probably get rid of the caching. |
| 134 | // |
| 135 | // But be careful if we continue to maintain ObjectStoreModel as a separate class |
| 136 | // from Tree, because in that case ObjectStoreModel can be shared by two trees, and destroying one tree |
| 137 | // should not interfere with an in-progress getChildren() call from another tree. Also, need to make |
| 138 | // sure that multiple calls to getChildren() for the same parentItem don't trigger duplicate calls |
| 139 | // to onChildrenChange() and onChange(). |
| 140 | // |
| 141 | // I think for 2.0 though that ObjectStoreModel should be rolled into Tree itself. |
| 142 | |
| 143 | var id = this.store.getIdentity(parentItem); |
| 144 | |
| 145 | if(this.childrenCache[id]){ |
| 146 | // If this.childrenCache[id] is defined, then it always has the latest list of children |
| 147 | // (like a live collection), so just return it. |
| 148 | when(this.childrenCache[id], onComplete, onError); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | // Query the store. |
| 153 | // Cache result so that we can close the query on destroy(), and to avoid setting up multiple observers |
| 154 | // when getChildren() is called multiple times for the same parent. |
| 155 | // The only problem is that getChildren() on non-Observable stores may return a stale value. |
| 156 | var res = this.childrenCache[id] = this.store.getChildren(parentItem); |
| 157 | if(res.then){ |
| 158 | this.own(res); // in case app calls destroy() before query completes |
| 159 | } |
| 160 | |
| 161 | // Setup observer in case children list changes, or the item(s) in the children list are updated. |
| 162 | if(res.observe){ |
| 163 | this.own(res.observe(lang.hitch(this, function(obj, removedFrom, insertedInto){ |
| 164 | //console.log("observe on children of ", id, ": ", obj, removedFrom, insertedInto); |
| 165 | |
| 166 | // If removedFrom == insertedInto, this call indicates that the item has changed. |
| 167 | // Even if removedFrom != insertedInto, the item may have changed. |
| 168 | this.onChange(obj); |
| 169 | |
| 170 | if(removedFrom != insertedInto){ |
| 171 | // Indicates an item was added, removed, or re-parented. The children[] array (returned from |
| 172 | // res.then(...)) has already been updated (like a live collection), so just use it. |
| 173 | when(res, lang.hitch(this, "onChildrenChange", parentItem)); |
| 174 | } |
| 175 | }), true)); // true means to notify on item changes |
| 176 | } |
| 177 | |
| 178 | // User callback |
| 179 | when(res, onComplete, onError); |
| 180 | }, |
| 181 | |
| 182 | // ======================================================================= |
| 183 | // Inspecting items |
| 184 | |
| 185 | isItem: function(/*===== something =====*/){ |
| 186 | return true; // Boolean |
| 187 | }, |
| 188 | |
| 189 | getIdentity: function(/* item */ item){ |
| 190 | return this.store.getIdentity(item); // Object |
| 191 | }, |
| 192 | |
| 193 | getLabel: function(/*dojo/data/Item*/ item){ |
| 194 | // summary: |
| 195 | // Get the label for an item |
| 196 | return item[this.labelAttr]; // String |
| 197 | }, |
| 198 | |
| 199 | // ======================================================================= |
| 200 | // Write interface, for DnD |
| 201 | |
| 202 | newItem: function(/* dijit/tree/dndSource.__Item */ args, /*Item*/ parent, /*int?*/ insertIndex, /*Item*/ before){ |
| 203 | // summary: |
| 204 | // Creates a new item. See `dojo/data/api/Write` for details on args. |
| 205 | // Used in drag & drop when item from external source dropped onto tree. |
| 206 | |
| 207 | return this.store.put(args, { |
| 208 | parent: parent, |
| 209 | before: before |
| 210 | }); |
| 211 | }, |
| 212 | |
| 213 | pasteItem: function(/*Item*/ childItem, /*Item*/ oldParentItem, /*Item*/ newParentItem, |
| 214 | /*Boolean*/ bCopy, /*int?*/ insertIndex, /*Item*/ before){ |
| 215 | // summary: |
| 216 | // Move or copy an item from one parent item to another. |
| 217 | // Used in drag & drop. |
| 218 | |
| 219 | var d = new Deferred(); |
| 220 | |
| 221 | if(oldParentItem === newParentItem && !bCopy && !before){ |
| 222 | // Avoid problem when items visually disappear when dropped onto their parent. |
| 223 | // Happens because the (no-op) store.put() call doesn't generate any notification |
| 224 | // that the childItem was added/moved. |
| 225 | d.resolve(true); |
| 226 | return d; |
| 227 | } |
| 228 | |
| 229 | if(oldParentItem && !bCopy){ |
| 230 | // In order for DnD moves to work correctly, childItem needs to be orphaned from oldParentItem |
| 231 | // before being adopted by newParentItem. That way, the TreeNode is moved rather than |
| 232 | // an additional TreeNode being created, and the old TreeNode subsequently being deleted. |
| 233 | // The latter loses information such as selection and opened/closed children TreeNodes. |
| 234 | // Unfortunately simply calling this.store.put() will send notifications in a random order, based |
| 235 | // on when the TreeNodes in question originally appeared, and not based on the drag-from |
| 236 | // TreeNode vs. the drop-onto TreeNode. |
| 237 | |
| 238 | this.getChildren(oldParentItem, lang.hitch(this, function(oldParentChildren){ |
| 239 | oldParentChildren = [].concat(oldParentChildren); // concat to make copy |
| 240 | var index = array.indexOf(oldParentChildren, childItem); |
| 241 | oldParentChildren.splice(index, 1); |
| 242 | this.onChildrenChange(oldParentItem, oldParentChildren); |
| 243 | |
| 244 | d.resolve(this.store.put(childItem, { |
| 245 | overwrite: true, |
| 246 | parent: newParentItem, |
| 247 | oldParent: oldParentItem, |
| 248 | before: before, |
| 249 | isCopy: false |
| 250 | })); |
| 251 | })); |
| 252 | }else{ |
| 253 | d.resolve(this.store.put(childItem, { |
| 254 | overwrite: true, |
| 255 | parent: newParentItem, |
| 256 | oldParent: oldParentItem, |
| 257 | before: before, |
| 258 | isCopy: true |
| 259 | })); |
| 260 | } |
| 261 | |
| 262 | return d; |
| 263 | }, |
| 264 | |
| 265 | // ======================================================================= |
| 266 | // Callbacks |
| 267 | |
| 268 | onChange: function(/*dojo/data/Item*/ /*===== item =====*/){ |
| 269 | // summary: |
| 270 | // Callback whenever an item has changed, so that Tree |
| 271 | // can update the label, icon, etc. Note that changes |
| 272 | // to an item's children or parent(s) will trigger an |
| 273 | // onChildrenChange() so you can ignore those changes here. |
| 274 | // tags: |
| 275 | // callback |
| 276 | }, |
| 277 | |
| 278 | onChildrenChange: function(/*===== parent, newChildrenList =====*/){ |
| 279 | // summary: |
| 280 | // Callback to do notifications about new, updated, or deleted items. |
| 281 | // parent: dojo/data/Item |
| 282 | // newChildrenList: Object[] |
| 283 | // Items from the store |
| 284 | // tags: |
| 285 | // callback |
| 286 | }, |
| 287 | |
| 288 | onDelete: function(/*dojo/data/Item*/ /*===== item =====*/){ |
| 289 | // summary: |
| 290 | // Callback when an item has been deleted. |
| 291 | // Actually we have no way of knowing this with the new dojo.store API, |
| 292 | // so this method is never called (but it's left here since Tree connects |
| 293 | // to it). |
| 294 | // tags: |
| 295 | // callback |
| 296 | } |
| 297 | }); |
| 298 | }); |