Subversion Repositories Applications.papyrus

Rev

Blame | Last modification | View Log | RSS feed

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
        <title>Dijit Tree Test</title>

        <style someProperty="text/css">
                @import "../../../dojo/resources/dojo.css";
                @import "../../themes/tundra/tundra.css";
                @import "../../themes/tundra/tundra_rtl.css";
                @import "../css/dijitTests.css";
                @import "../dndDefault.css"; 
                @import "../../../dojo/resources/dojo.css";
                @import "../../../dojo/resources/dnd.css";
                @import "../../../dojo/tests/dnd/dndDefault.css";
        </style>

        <script someProperty="text/javascript" src="testBidi.js"></script>

        <script someProperty="text/javascript" src="../../../dojo/dojo.js" 
                djConfig="parseOnLoad: true, isDebug: true"></script>

        <script language="JavaScript" someProperty="text/javascript">
                dojo.require("dojo.data.ItemFileWriteStore");
                dojo.require("dijit.Tree");
                dojo.require("dijit._tree.dndSource");
                dojo.require("dijit.Menu");
                dojo.require("dijit.form.Button");
                dojo.require("dojo.parser");    // scan page for widgets and instantiate them

                dojo.require("dojo.dnd.common");
                dojo.require("dojo.dnd.Source");

                selected=[];

                globalId=1000;
                lastSelected=null;

                dojo.addOnLoad(function(){
        
                        //record the selection from tree 1      
                        dojo.subscribe("myTree", null, function(message){
                                if(message.event=="execute"){
                                        console.log("Tree1 Select: ",dijit.byId("myTree").store.getLabel(message.item));
                                        lastSelected=selected["myTree"]=message.item;                                   
                                }
                        });

                        //record the selection from tree 2      
                        dojo.subscribe("myTree2", null, function(message){
                                if(message.event=="execute"){
                                        console.log("Tree2 Select: ",dijit.byId("myTree2").store.getLabel(message.item));
                                        lastSelected=selected["myTree2"]=message.item;                                  
                                }
                        });

                        //connect to the add button and have it add a new container to the store as necessary
                        dojo.connect(dijit.byId("addButton"), "onClick", function(){
                                var pInfo = {
                                        parent: lastSelected,           
                                        attribute: "children"   
                                };

                                //store.newItem({name: dojo.byId('newCat').value, id:globalId++, numberOfItems:dojo.byId('numItems').value}, pInfo);
                                catStore.newItem({name: dojo.byId('newCat').value, numberOfItems:0,id:globalId++}, pInfo);
                        });

                        //since we don't have a server, we're going to connect to the store and do a few things the server/store combination would normal be taking care of for us
                        dojo.connect(catStore, "onNew", function(item, pInfo){
                                var p = pInfo.item;
                                if (p) {        
                                        var currentTotal = catStore.getValues(p, "numberOfItems")[0];
                                        catStore.setValue(p, "numberOfItems", ++currentTotal);
                                }
                                
                        });


                        tree1 = dijit.byId('myTree');
                        tree2 = dijit.byId('myTree2');
                });


                //create a custom label for tree one consisting of the label property pluss the value of the numberOfItems Column
                function tree1CustomLabel(item){
                        var label = catStore.getLabel(item); 
                        var num = catStore.getValues(item,"numberOfItems");
                        //return the new label
                        return label + ' (' + num+ ')';
                }


                //custom function to handle drop for tree two.  Items dropped onto nodes here should be applied to the items attribute in this particular store.
                function tree2CustomDrop(source,nodes,copy){
                        if (this.containerState=="Over"){
                                this.isDragging=false;
                                var target= this.current;
                                var items = this.itemCreator(nodes, target);

                                if (!target || target==this.tree.domNode){
                                        for (var i=0; i<items.length;i++){
                                                this.tree.store.newItem(items[i],null);
                                        }
                                }else {
                                        for (var i=0; i<items.length;i++){
                                                pInfo={parent:dijit.getEnclosingWidget(target).item, attribute:"items"};
                                                this.tree.store.newItem(items[i],pInfo);
                                        }
                                }
                        }
                }

                //on tree two, we only want to drop on containers, not on items in the containers
                function tree2CheckItemAcceptance(node,source) {
                        var item = dijit.getEnclosingWidget(node).item;
                        if (item && this.tree.store.hasAttribute(item,"numberOfItems")){
                                var numItems=this.tree.store.getValues(item, "numberOfItems");
                                return true;
                        }
                        return false;
                }

                function tree2ItemCreator(nodes){
                        var items = []

                        for(var i=0;i<nodes.length;i++){
                                items.push({
                                        "name":nodes[i].textContent,
                                        "id": nodes[i].id
                                });
                        }
                        return items;
                }

                function dndAccept(source,nodes){
                        if (this.tree.id=="myTree"){
                                return false;
                        }
                        return true;
                }

                function getIcon(item) {
                        if (!item || catStore.hasAttribute(item, "numberOfItems")) {
                                return "myFolder";
                        } 
                        return "myItem"
                }
        </script>

        <style>
                .myFolder{
                        display: "block";
                        width: 16px;
                        height: 16px;
                        background: blue;
                }

                .myItem{
                        display: "block";
                        width: 16px;
                        height: 16px;
                        background: green;

                }
        </style>

</head>
<body class="tundra"> 
        <h1 class="testTitle">Dijit Tree Test - Drag And Drop Support</h1>

        <div dojoType="dojo.data.ItemFileWriteStore" jsId="catStore"
                url="../_data/categories.json"></div>

        <table width="100%" style="margin:5px solid gray" >

        <tr style="width:100%">
                <td style="width: 50%">
                        <h2>Custom</h2> 
                        <p>Should add this category to the store.  The second parameter is the value for numberOfItems.</p>
                        <div class="container">
                                <input id="newCat" type="text" value="Pottedmeat" /><input id="numItems" type="text" value="0" size="3"/><div id="addButton" dojoType="dijit.form.Button">Add Category</div>
                        </div>
                </td>
                <td>
                        <h2>Items: </h2>
                        <p>List of Items to be categorized<p>
                        <div dojoType="dojo.dnd.Source" jsId="c2" class="container" style="height: 100px; overflow: auto">
                                <div class="dojoDndItem" id="1001">Apple</div>
                                <div class="dojoDndItem" id="1002">Orange</div>
                                <div class="dojoDndItem" id="1003">Banana</div>
                                <div class="dojoDndItem" id="1004">Tomato</div>
                                <div class="dojoDndItem" id="1005">Pepper</div>
                                <div class="dojoDndItem" id="1006">Wheat</div>
                                <div class="dojoDndItem" id="1007">Corn</div>
                                <div class="dojoDndItem" id="1008">Spinach</div>
                                <div class="dojoDndItem" id="1009">Cucumber</div>
                                <div class="dojoDndItem" id="1010">Carot</div>
                                <div class="dojoDndItem" id="1011">Potato</div>
                                <div class="dojoDndItem" id="1012">Grape</div>
                                <div class="dojoDndItem" id="1013">Lemon</div>
                                <div class="dojoDndItem" id="1010">Letuce</div>
                                <div class="dojoDndItem" id="1010">Peanut</div>
                        </div>
                </td>
        </tr>
        <tr>
                <td>
                        <h2>Collection Count Summary</h2>
                        <p>You can't drop items onto this tree.</p>
                        <div class="container" dojoType="dijit.Tree" id="myTree" store="catStore" label="Collections"
                                labelAttr="name" getLabel="tree1CustomLabel"  childrenAttr="children" dndController="dijit._tree.dndSource"
                                checkAcceptance="dndAccept" getIconClass="getIcon"></div>
                </td>
                <td>
                        <h2>Collection</h2>
                        <p>Drop items onto this tree, but only on categories, should fail to let you drop on other items.</p>
                        <div class="container" dojoType="dijit.Tree" id="myTree2" label="Collections" store="catStore"  labelAttr="name"  childrenAttr="children, items" dndController="dijit._tree.dndSource" onDndDrop="tree2CustomDrop" checkAcceptance="dndAccept" checkItemAcceptance="tree2CheckItemAcceptance" getIconClass="getIcon"></div> 
                </td>
        </tr>
        </table>

        </body>
</html>