| 1 | <!DOCTYPE html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta http-equiv="Content-type" content="text/html; charset=utf-8"> |
| 5 | <title>Dijit Tree Test</title> |
| 6 | |
| 7 | <style type="text/css"> |
| 8 | @import "../../../dojo/resources/dnd.css"; |
| 9 | @import "../../../dojo/tests/dnd/dndDefault.css"; |
| 10 | </style> |
| 11 | |
| 12 | <style> |
| 13 | .myFolder{ |
| 14 | width: 16px; |
| 15 | height: 16px; |
| 16 | background: blue; |
| 17 | } |
| 18 | |
| 19 | .myItem{ |
| 20 | width: 16px; |
| 21 | height: 16px; |
| 22 | background: green; |
| 23 | |
| 24 | } |
| 25 | </style> |
| 26 | </head> |
| 27 | |
| 28 | <body class="claro" role="main"> |
| 29 | <h1 class="testTitle">Dijit Tree Test - Drag And Drop Support</h1> |
| 30 | |
| 31 | <h2>Dojo Object Store (<code>dojo/store</code>) Examples</h2> |
| 32 | |
| 33 | <p> |
| 34 | You can't drop items onto this tree, but you can reorder categories. The between threshold |
| 35 | is set to 5, so if you are near the top or bottom of a node the drop will be above or below it. |
| 36 | </p> |
| 37 | <div id="memoryStoreTree"></div> |
| 38 | |
| 39 | <h2>Dojo.data (<code>dojo/data</code>) Examples</h2> |
| 40 | <div> |
| 41 | <div role="presentation" style="display: inline-block; vertical-align: top; width: 49%;"> |
| 42 | <h3>Items: </h3> |
| 43 | <p>List of Items to be categorized</p> |
| 44 | <div data-dojo-id="c2" data-dojo-type="dojo/dnd/Source" class="container" style="height:175px; overflow:auto;"> |
| 45 | <div class="dojoDndItem" id="1001">Apple</div> |
| 46 | <div class="dojoDndItem" id="1002">Orange</div> |
| 47 | <div class="dojoDndItem" id="1003">Banana</div> |
| 48 | <div class="dojoDndItem" id="1004">Tomato</div> |
| 49 | <div class="dojoDndItem" id="1005">Pepper</div> |
| 50 | <div class="dojoDndItem" id="1006">Wheat</div> |
| 51 | <div class="dojoDndItem" id="1007">Corn</div> |
| 52 | <div class="dojoDndItem" id="1008">Spinach</div> |
| 53 | <div class="dojoDndItem" id="1009">Cucumber</div> |
| 54 | <div class="dojoDndItem" id="1010">Carrot</div> |
| 55 | <div class="dojoDndItem" id="1011">Potato</div> |
| 56 | <div class="dojoDndItem" id="1012">Grape</div> |
| 57 | <div class="dojoDndItem" id="1013">Lemon</div> |
| 58 | <div class="dojoDndItem" id="1014">Lettuce</div> |
| 59 | <div class="dojoDndItem" id="1015">Peanut</div> |
| 60 | </div> |
| 61 | </div> |
| 62 | |
| 63 | <div role="presentation" style="display: inline-block; width: 49%;"> |
| 64 | <h3>Collection</h3> |
| 65 | <p> |
| 66 | Drop items from table on left onto this tree, only on to categories or between other items; should fail to let you drop on other items. |
| 67 | Can also move items within this tree. The drag threshold is set to 8, between threshold is set to 5, so you have a few pixels |
| 68 | of buffer before drag operations start. |
| 69 | </p> |
| 70 | <div data-dojo-id="itemModel" |
| 71 | data-dojo-type="dijit/tree/TreeStoreModel" |
| 72 | data-dojo-props="store: myStore, |
| 73 | query: {id: '0'}, |
| 74 | childrenAttrs: ['items', 'children']"> |
| 75 | </div> |
| 76 | <div id="itemTree" |
| 77 | data-dojo-type="dijit/Tree" |
| 78 | data-dojo-props="'class': 'container', |
| 79 | model: itemModel, |
| 80 | checkItemAcceptance: itemTreeCheckItemAcceptance, |
| 81 | dragThreshold: 8, |
| 82 | betweenThreshold: 5, |
| 83 | getIconClass: getIcon, |
| 84 | persist: false"> |
| 85 | </div> |
| 86 | </div> |
| 87 | </div> |
| 88 | |
| 89 | <h3>Collection Count Summary</h3> |
| 90 | <p> |
| 91 | You can't drop items onto this tree, but you can reorder categories. The between threshold |
| 92 | is set to 5, so if you are near the top or bottom of a node the drop will be above or below it. |
| 93 | </p> |
| 94 | <div id="collectionsTree" |
| 95 | data-dojo-type="dijit/Tree" |
| 96 | data-dojo-props="'class': 'container', |
| 97 | model: catModel, |
| 98 | getLabel: catTreeCustomLabel, |
| 99 | betweenThreshold: 5, |
| 100 | checkItemAcceptance: collectionTreeCheckItemAcceptance, |
| 101 | getIconClass: getIcon, |
| 102 | persist: false"> |
| 103 | </div> |
| 104 | |
| 105 | <h3>Custom</h3> |
| 106 | <p>Should add this category to the store. The second parameter is the value for numberOfItems.</p> |
| 107 | <div class="container"> |
| 108 | <label for="newCat">New category:</label><input id="newCat" type="text" value="Pottedmeat" /> |
| 109 | <label for="numItems">numberOfItems:</label><input id="numItems" type="text" value="0" size="3"/> |
| 110 | <div id="addButton" data-dojo-type="dijit/form/Button">Add Category</div> |
| 111 | </div> |
| 112 | |
| 113 | <script type="text/javascript" src="../boilerplate.js"></script> |
| 114 | |
| 115 | <script type="text/javascript"> |
| 116 | /* dojo/store examples */ |
| 117 | require([ |
| 118 | "dojo/_base/array", |
| 119 | "dojo/_base/lang", |
| 120 | "dojo/dom", |
| 121 | "dojo/store/Memory", |
| 122 | "dojo/store/Observable", |
| 123 | "dijit/Tree", |
| 124 | /testController=selector/.test(window.location.search) ? "dijit/tree/_dndSelector" : "dijit/tree/dndSource", |
| 125 | "dijit/tree/ObjectStoreModel", |
| 126 | "dojo/text!../_data/categories.json" |
| 127 | ], function(arrayUtil, lang, dom, MemoryStore, Observable, Tree, testController, ObjectStoreModel, categoriesJson){ |
| 128 | Tree.prototype.dndController = testController; |
| 129 | |
| 130 | var TreeStore = MemoryStore.createSubclass([], { |
| 131 | getChildren: function(item){ |
| 132 | return this.query({parentId: item.id}); |
| 133 | }, |
| 134 | |
| 135 | mayHaveChildren: function(item){ |
| 136 | return Boolean(item.numberOfItems); |
| 137 | }, |
| 138 | |
| 139 | put: function(object, options){ |
| 140 | var id = this.inherited(arguments); |
| 141 | var parentId = options && options.parent && this.getIdentity(options.parent); |
| 142 | var oldParentId = options && options.oldParent && this.getIdentity(options.oldParent); |
| 143 | |
| 144 | if(parentId !== oldParentId){ |
| 145 | object.parentId = parentId; |
| 146 | if(isNaN(options.parent.numberOfItems)){ |
| 147 | options.parent.numberOfItems = 1; |
| 148 | } |
| 149 | else{ |
| 150 | options.parent.numberOfItems += 1; |
| 151 | } |
| 152 | this.put(options.parent); |
| 153 | |
| 154 | if(options.oldParent){ |
| 155 | options.oldParent.numberOfItems = Math.max(0, options.oldParent.numberOfItems - 1); |
| 156 | this.put(options.oldParent); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return id; |
| 161 | } |
| 162 | }); |
| 163 | |
| 164 | function transformData(data){ |
| 165 | arrayUtil.forEach(data, function(item){ |
| 166 | var children = item.children || item.items || []; |
| 167 | |
| 168 | arrayUtil.forEach(children, function(child){ |
| 169 | var childItem = arrayUtil.filter(data, function (o) { |
| 170 | return o.id === child._reference; |
| 171 | })[0]; |
| 172 | |
| 173 | if(childItem){ |
| 174 | childItem.parentId = item.id; |
| 175 | } |
| 176 | }); |
| 177 | |
| 178 | item.children = undefined; |
| 179 | item.items = undefined; |
| 180 | }); |
| 181 | |
| 182 | return data; |
| 183 | } |
| 184 | |
| 185 | memoryStore = new Observable(new TreeStore({ |
| 186 | data: transformData(JSON.parse(categoriesJson).items) |
| 187 | })); |
| 188 | |
| 189 | memoryStoreModel = new ObjectStoreModel({ |
| 190 | store: memoryStore, |
| 191 | query: { id: "0" }, |
| 192 | mayHaveChildren: function(item){ |
| 193 | return Boolean(item.numberOfItems); |
| 194 | } |
| 195 | }); |
| 196 | |
| 197 | function getLabel(item){ |
| 198 | return item.name + (item.numberOfItems ? (" (" + (item.numberOfItems || "0") + ")") : ""); |
| 199 | }; |
| 200 | |
| 201 | function getIcon(item){ |
| 202 | return ("numberOfItems" in item) ? "myFolder" : "myItem"; |
| 203 | }; |
| 204 | |
| 205 | memoryStoreTree = new Tree({ |
| 206 | className: "container", |
| 207 | model: memoryStoreModel, |
| 208 | betweenThreshold: 5, |
| 209 | persist: false, |
| 210 | getLabel: getLabel, |
| 211 | checkItemAcceptance: function(node, source){ |
| 212 | return source.tree && source.tree.id === "memoryStoreTree"; |
| 213 | }, |
| 214 | getIconClass: getIcon |
| 215 | }, dom.byId("memoryStoreTree")); |
| 216 | }); |
| 217 | </script> |
| 218 | |
| 219 | <script type="text/javascript"> |
| 220 | /* dojo/data examples */ |
| 221 | require([ |
| 222 | "dojo/aspect", |
| 223 | "dojo/data/ItemFileWriteStore", |
| 224 | "dojo/dom", |
| 225 | "dojo/parser", |
| 226 | "dojo/topic", |
| 227 | "dijit/registry", |
| 228 | "dijit/tree/TreeStoreModel", |
| 229 | |
| 230 | // for parser |
| 231 | "dojo/dnd/Source", |
| 232 | "dijit/Menu", |
| 233 | "dijit/form/Button", |
| 234 | "dijit/focus", // not used directly, but needed by robot test drivers, ex: Tree_Selector.html |
| 235 | "dijit/Tree" |
| 236 | ], function(aspect, ItemFileWriteStore, dom, parser, topic, registry, TreeStoreModel){ |
| 237 | |
| 238 | myStore = new ItemFileWriteStore({ |
| 239 | url: "../_data/categories.json" |
| 240 | }); |
| 241 | |
| 242 | catModel = new TreeStoreModel({ |
| 243 | store: myStore, |
| 244 | query: {id: "0"} |
| 245 | }); |
| 246 | |
| 247 | //create a custom label for tree one consisting of the label property pluss the value of the numberOfItems Column |
| 248 | catTreeCustomLabel = function(item){ |
| 249 | var label = myStore.getLabel(item); |
| 250 | var num = myStore.hasAttribute(item, "numberOfItems") ? myStore.getValues(item,"numberOfItems") : "?"; |
| 251 | return label + ' (' + num+ ')'; |
| 252 | }; |
| 253 | |
| 254 | // on collection tree, only accept itself as the source tree |
| 255 | collectionTreeCheckItemAcceptance = function(node, source, position){ |
| 256 | return source.tree && source.tree.id == "collectionsTree"; |
| 257 | } |
| 258 | |
| 259 | //on item tree , we want to drop on containers, the root node itself, or between items in the containers |
| 260 | itemTreeCheckItemAcceptance = function(node, source, position){ |
| 261 | /* |
| 262 | source.forInSelectedItems(function(item){ |
| 263 | console.log("testing to drop item of type " + item.type[0] + " and data " + item.data + ", position " + position); |
| 264 | }); |
| 265 | */ |
| 266 | var item = registry.getEnclosingWidget(node).item; |
| 267 | if(item && (item.root || myStore.hasAttribute(item,"numberOfItems"))){ |
| 268 | return true; |
| 269 | } |
| 270 | return position != "over"; |
| 271 | }; |
| 272 | |
| 273 | getIcon = function(item){ |
| 274 | if(!item || myStore.hasAttribute(item, "numberOfItems")){ |
| 275 | return "myFolder"; |
| 276 | } |
| 277 | return "myItem" |
| 278 | }; |
| 279 | |
| 280 | parser.parse(); |
| 281 | |
| 282 | selected = []; |
| 283 | |
| 284 | globalId = 1000; |
| 285 | lastSelected = null; |
| 286 | |
| 287 | // record the selection from tree 1 |
| 288 | topic.subscribe("myTree", function(message){ |
| 289 | if(message.event == "execute"){ |
| 290 | // console.log("Tree1 Select: ",dijit.byId("myTree").store.getLabel(message.item)); |
| 291 | lastSelected = selected["myTree"] = message.item; |
| 292 | } |
| 293 | }); |
| 294 | |
| 295 | // record the selection from tree 2 |
| 296 | topic.subscribe("myTree2", function(message){ |
| 297 | if(message.event == "execute"){ |
| 298 | // console.log("Tree2 Select: ",dijit.byId("myTree2").store.getLabel(message.item)); |
| 299 | lastSelected = selected["myTree2"] = message.item; |
| 300 | } |
| 301 | }); |
| 302 | |
| 303 | // connect to the add button and have it add a new container to the store as necessary |
| 304 | registry.byId("addButton").on("click", function(){ |
| 305 | var pInfo = { |
| 306 | parent: lastSelected, |
| 307 | attribute: "children" |
| 308 | }; |
| 309 | |
| 310 | myStore.newItem({ |
| 311 | name: dom.byId('newCat').value, |
| 312 | numberOfItems:0, |
| 313 | id:globalId++ |
| 314 | }, pInfo); |
| 315 | }); |
| 316 | |
| 317 | // since we don't have a server, we're going to connect to the store and |
| 318 | // do a few things the server/store combination would normal be taking care of for us. |
| 319 | // TODO: This isn't monitoring moves, deletes, etc. Instead of this code, why not monitor |
| 320 | // when an items children[] list changes? |
| 321 | aspect.after(myStore, "onNew", function(item, pInfo){ |
| 322 | var p = pInfo && pInfo.item; |
| 323 | if(p){ |
| 324 | var currentTotal = myStore.getValues(p, "numberOfItems")[0]; |
| 325 | myStore.setValue(p, "numberOfItems", ++currentTotal); |
| 326 | } |
| 327 | }, true); |
| 328 | }); |
| 329 | </script> |
| 330 | </body> |
| 331 | </html> |