dependency_wev8.js 11.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
/*
 * Ext JS Library 2.0.2
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

Ext.onReady(function(){
    var xt = Ext.tree;
    // seeds for the new node suffix
    var cseed = 0, oseed = 0;

    // turn on quick tips
    Ext.QuickTips.init();

    var cview = Ext.DomHelper.append('main-ct',
        {cn:[{id:'main-tb'},{id:'cbody'}]}
    );


    // create the primary toolbar
    var tb = new Ext.Toolbar('main-tb');
    tb.add({
        id:'save',
        text:'Save',
        disabled:true,
        handler:save,
        cls:'x-btn-text-icon save',
        tooltip:'Saves all components to the server'
    },'-', {
        id:'add',
        text:'Component',
        handler:addComponent,
        cls:'x-btn-text-icon add-cmp',
        tooltip:'Add a new Component to the dependency builder'
    }, {
        id:'option',
        text:'Option',
        disabled:true,
        handler:addOption,
        cls:'x-btn-text-icon add-opt',
        tooltip:'Add a new optional dependency to the selected component'
    },'-',{
        id:'remove',
        text:'Remove',
        disabled:true,
        handler:removeNode,
        cls:'x-btn-text-icon remove',
        tooltip:'Remove the selected item'
    });
    // for enabling and disabling
    var btns = tb.items.map;



    // create our layout
    var layout = new Ext.BorderLayout('main-ct', {
        west: {
            split:true,
            initialSize: 200,
            minSize: 175,
            maxSize: 400,
            titlebar: true,
            margins:{left:5,right:0,bottom:5,top:5}
        },
        center: {
            title:'Components',
            margins:{left:0,right:5,bottom:5,top:5}
        }
    }, 'main-ct');

    layout.batchAdd({
       west: {
           id: 'source-files',
           autoCreate:true,
           title:'Ext Source Files',
           autoScroll:true,
           fitToFrame:true
       },
       center : {
           el: cview,
           autoScroll:true,
           fitToFrame:true,
           toolbar: tb,
           resizeEl:'cbody'
       }
    });



    // this is the source code tree
    var stree = new xt.TreePanel('source-files', {
        animate:true,
        loader: new xt.TreeLoader({dataUrl:'dependency.php'}),
        enableDrag:true,
        containerScroll: true
    });

    new xt.TreeSorter(stree, {folderSort:true});

    var sroot = new xt.AsyncTreeNode({
        text: 'Ext JS',
        draggable:false,
        id:'source'
    });
    stree.setRootNode(sroot);
    stree.render();
    sroot.expand(false, false);


    // the component tree
    var ctree = new xt.TreePanel('cbody', {
        animate:true,
        enableDD:true,
        containerScroll: true,
        lines:false,
        rootVisible:false,
        loader: new Ext.tree.TreeLoader()
    });

    ctree.el.addKeyListener(Ext.EventObject.DELETE, removeNode);

    var croot = new xt.AsyncTreeNode({
        allowDrag:false,
        allowDrop:true,
        id:'croot',
        text:'Packages and Components',
        cls:'croot',
        loader:new Ext.tree.TreeLoader({
            dataUrl:'dep-tree.json',
            createNode: readNode
        })
    });
    ctree.setRootNode(croot);
    ctree.render();
    croot.expand();

    // some functions to determine whether is not the drop is allowed
    function hasNode(t, n){
        return (t.attributes.type == 'fileCt' && t.findChild('id', n.id)) ||
            (t.leaf === true && t.parentNode.findChild('id', n.id));
    };

    function isSourceCopy(e, n){
        var a = e.target.attributes;
        return n.getOwnerTree() == stree && !hasNode(e.target, n) &&
           ((e.point == 'append' && a.type == 'fileCt') || a.leaf === true);
    };

    function isReorder(e, n){
        return n.parentNode == e.target.parentNode && e.point != 'append';
    };

    // handle drag over and drag drop
    ctree.on('nodedragover', function(e){
        var n = e.dropNode;
        return isSourceCopy(e, n) || isReorder(e, n);
    });

    ctree.on('beforenodedrop', function(e){
        var n = e.dropNode;

        // copy node from source tree
        if(isSourceCopy(e, n)){
            var copy = new xt.TreeNode(
                Ext.apply({allowDelete:true,expanded:true}, n.attributes)
            );
            copy.loader = undefined;
            if(e.target.attributes.options){
                e.target = createOption(e.target, copy.text);
                //return false;
            }
            e.dropNode = copy;
            return true;
        }

        return isReorder(e, n);
    });

    ctree.on('contextmenu', prepareCtx);

    // track whether save is allowed
    ctree.on('append', trackSave);
    ctree.on('remove', trackSave);
    ctree.el.swallowEvent('contextmenu', true);
    ctree.el.on('keypress', function(e){
        if(e.isNavKeyPress()){
            e.stopEvent();
        }
    });
    // when the tree selection changes, enable/disable the toolbar buttons
    var sm = ctree.getSelectionModel();
    sm.on('selectionchange', function(){
        var n = sm.getSelectedNode();
        if(!n){
            btns.remove.disable();
            btns.option.disable();
            return;
        }
        var a = n.attributes;
        btns.remove.setDisabled(!a.allowDelete);
        btns.option.setDisabled(!a.cmpId);
    });



    // create the editor for the component tree
    var ge = new xt.TreeEditor(ctree, {
        allowBlank:false,
        blankText:'A name is required',
        selectOnFocus:true
    });
    
    ge.on('beforestartedit', function(){
        if(!ge.editNode.attributes.allowEdit){
            return false;
        }
    });


    // add component handler
    function addComponent(){
        var id = guid('c-');
        var text = 'Component '+(++cseed);
        var node = createComponent(id, text);
        node.expand(false, false);
        node.select();
        node.lastChild.ensureVisible();
        ge.triggerEdit(node);
    }

    function createComponent(id, text, cfiles, cdep, coptions){
        var node = new xt.TreeNode({
            text: text,
            iconCls:'cmp',
            cls:'cmp',
            type:'cmp',
            id: id,
            cmpId:id,
            allowDelete:true,
            allowEdit:true
        });
        croot.appendChild(node);

        var files = new xt.AsyncTreeNode({
            text: 'Files',
            allowDrag:false,
            allowDrop:true,
            iconCls:'folder',
            type:'fileCt',
            cmpId:id,
            allowDelete:false,
            children:cfiles||[],
            expanded:true
        });

        var dep = new xt.AsyncTreeNode({
            text: 'Dependencies',
            allowDrag:false,
            allowDrop:true,
            iconCls:'folder',
            type:'fileCt',
            cmpId:id,
            allowDelete:false,
            children:cdep||[],
            expanded:true,
            allowCopy:true
        });

        var options = new xt.AsyncTreeNode({
            text: 'Optional Dependencies',
            allowDrag:false,
            allowDrop:true,
            iconCls:'folder',
            type:'fileCt',
            options:true,
            cmpId:id,
            allowDelete:false,
            children:coptions||[],
            expanded:true,
            allowCopy:true
        });

        node.appendChild(files);
        node.appendChild(dep);
        node.appendChild(options);

        return node;
    }

    // remove handler
    function removeNode(){
        var n = sm.getSelectedNode();
        if(n && n.attributes.allowDelete){
            ctree.getSelectionModel().selectPrevious();
            n.parentNode.removeChild(n);
        }
    }


    // add option handler
    function addOption(){
        var n = sm.getSelectedNode();
        if(n){
            var node = createOption(n, 'Option'+(++oseed));
            node.select();
            ge.triggerEdit(node);
        }
    }

    function createOption(n, text){
        var cnode = ctree.getNodeById(n.attributes.cmpId);

        var node = new xt.TreeNode({
            text: text,
            cmpId:cnode.id,
            iconCls:'folder',
            type:'fileCt',
            allowDelete:true,
            allowEdit:true,
            id:guid('o-')
        });
        cnode.childNodes[2].appendChild(node);
        cnode.childNodes[2].expand(false, false);

        return node;
    }

    // semi unique ids across edits
    function guid(prefix){
        return prefix+(new Date().getTime());
    }


    function trackSave(){
        btns.save.setDisabled(!croot.hasChildNodes());
    }

    function storeChildren(cmp, n, name){
        if(n.childrenRendered){
            cmp[name] = [];
            n.eachChild(function(f){
                cmp[name].push(f.attributes);
            });
        }else{
            cmp[name] = n.attributes.children || [];
        }
    }

    // save to the server in a format usable in PHP
    function save(){
        var ch = [];
        croot.eachChild(function(c){
            var cmp = {
                text:c.text,
                id: c.id,
                options:[]
            };

            storeChildren(cmp, c.childNodes[0], 'files');
            storeChildren(cmp, c.childNodes[1], 'dep');

            var onode = c.childNodes[2];
            if(!onode.childrenRendered){
                cmp.options = onode.attributes.children || [];
            }else{
                onode.eachChild(function(o){
                    var opt = Ext.apply({}, o.attributes);
                    storeChildren(opt, o, 'children');
                    cmp.options.push(opt);
                });
            }
            ch.push(cmp);
        });

        layout.el.mask('Sending data to server...', 'x-mask-loading');
        var hide = layout.el.unmask.createDelegate(layout.el);
        Ext.lib.Ajax.request(
            'POST',
            'save-dep.php',
            {success:hide,failure:hide},
            'data='+encodeURIComponent(Ext.encode(ch))
        );
    }

    function readNode(o){
        createComponent(o.id, o.text, o.files, o.dep, o.options);
    }

    // context menus

    var ctxMenu = new Ext.menu.Menu({
        id:'copyCtx',
        items: [{
                id:'expand',
                handler:expandAll,
                cls:'expand-all',
                text:'Expand All'
            },{
                id:'collapse',
                handler:collapseAll,
                cls:'collapse-all',
                text:'Collapse All'
            },'-',{
                id:'remove',
                handler:removeNode,
                cls:'remove-mi',
                text: 'Remove Item'
        }]
    });

    function prepareCtx(node, e){
        node.select();
        ctxMenu.items.get('remove')[node.attributes.allowDelete ? 'enable' : 'disable']();
        ctxMenu.showAt(e.getXY());
    }

    function collapseAll(){
        ctxMenu.hide();
        setTimeout(function(){
            croot.eachChild(function(n){
               n.collapse(false, false);
            });
        }, 10);
    }

    function expandAll(){
        ctxMenu.hide();
        setTimeout(function(){
            croot.eachChild(function(n){
               n.expand(false, false);
            });
        }, 10);
    }
});