Current File : //etc/zpanel/panel/etc/apps/filemanager/scripts/extjs3-ext/ux.locationbar/Ext.ux.LocationBar.js
/*
 * Copyright 2008, brainbits GmbH  All rights reserved.
 * Author: Stephan Wentz. swentz[at]brainbits.net
 * 
 * http://www.brainbits.net/
 */

/**
 * LocationBar class
 * Version:  0.1
 * @class Ext.ux.Locationbar
 * @extends Ext.Toolbar
 * Locationbar class.
 * @constructor
 * Creates a new LocationBar
 * @param {Object/Array} config A config object or an array of buttons to add
 */


var custom_location_bar ={
    
    /**
     * @cfg {Number} maxItem Maximum number of items the Locationbar takes before the first items are removed (defaults to 15).
     * Set to 0 for unlimited items.
     */
    maxItems: 15,
    
    /**
     * @cfg {String} emptyText The that is shown if no history is available (defaults to 'No node selected.').
     */
    emptyText: 'No node selected.',
    
    /**
     * @cfg {Boolean} noReload If set to true the reload button will not be visible (defaults to false).
     */
    noReload: false,
    
    /**
     * @cfg {Function} selectHandler  The function to
     * call when clicked. Arguments passed are:<ul>
     * <li><b>node</b> : Object<p style="margin-left:1em">The node associated with the clicked item.</p></li>
     * </ul>
     */
    selectHandler: null,
    
    /**
     * @cfg {Function} reloadHandler  The function to
     * call when clicked. Arguments passed are:<ul>
     * <li><b>node</b> : Object<p style="margin-left:1em">The node associated with the current item.</p></li>
     * </ul>
     */
    reloadHandler: null,
    
    /**
     * @cfg {String} locationItems Initial items (defaults to []).
     */
    locationItems: [],
    
    /**
     * @cfg {String} folderIconCls Iconclass for folder icons.
     */
    folderIconCls: 'x-locationbar-folder-icon',
    
    /**
     * @cfg {String} folderIconCls Iconclass for the backward icon.
     */
    backwardIconCls: 'x-locationbar-back-icon',
    
    /**
     * @cfg {String} folderIconCls Iconclass for the forward icon.
     */
    forwardIconCls: 'x-locationbar-forward-icon',
    
    /**
     * @cfg {String} folderIconCls Iconclass for the reload icon.
     */
    reloadIconCls: 'x-locationbar-reload-icon',
    
    /**
     * @cfg {Ext.tree.TreePanel} tree The treePanel this Locationbar is associated with.
     */
    tree: null,
    
    // private
    historyItemNodes: {},

    //private
    forwardHistoryNodes: {},
    
    // private
    historyItems: [],
 
    customHistory: [],
    
    // private
    currentItem: false,
    
    // private
    historyNext: true,

    //private
    forwardHistory: [],

    //private
    current_node: null,
	
    /*sss:function(node)
    {
	//node = node ? (node=="undefined" ? "": node) : "";
	if(node && node!="undefined")
	{
		if(!node.isExpanded())
		{
			//this.setNode(node);
			//var path = node.getPath();
			//alert("node:- "+path);
			//Ext.getCmp("dirTree").expandPath(path);
			//alert("adfafa"+node.isExpanded());
			//this.setNode(node);
			node.on('load', function() { expandNode( node, dir ) } );
			node.expand();
			alert(node.childNodes.length);
			//this.initComponent();
		}
		else
		{
			node.expand();
			alert("adfafa:- "+node.isExpanded());
		}
	}
	else
	{
 		alert("There is no node");
	}
    },*/
    //status_d:0,
//var x=0;
    // private
    initComponent: function() {
        if(this.tree) {
            this.tree.getLoader().addListener('load',function(tl,node,resp){
	//	console.log("1111111111111111111111111111:-  "+node);
                if(node){
	//	    console.log("Testttttt:-  "+node.id);
//	            node.loaded=true;
//          	    this.setNode(node);
//		    var path = node.getPath();
//                        console.log("sss111:- "+path);
//                        this.tree.expandPath(path);
//    		    if(node.id != "ext_root") 
//		    console.log("chDir( node.id.replace( /_RRR_/g, '/' ), true ):- "+ node.id.replace( /_RRR_/g, '/' ));
			chDir( node.id.replace( /_RRR_/g, '/' ), true );
			this.setNode(node);
/*			var child_nodes = node.childNodes;
			if(this.status_d<2) //Added for not repating the expandnode on initial.
			{
				for(var i=0; i<child_nodes.length;i++)
				{
					node.childNodes[i].expand();
				} 
			}
			this.status_d++;
*/                }
            },this)
            this.tree.getSelectionModel().addListener('selectionchange', function(sm, node) {
//		console.log(sm);
//		console.log("2222222222222SelectionChange Node:- "+this.tree+" node.childNodes.length  "+node.childNodes.length);
            	if( node && node.id ) {
//			node.expand(true);
//		 	node.loaded=true;
	                this.setNode(node);
//			node.leaf = true;
//console.log("selectionchange .addListener Node:- "+node.childNodes.length);
//console.log("selectionchange .addListener Node.id.replace _ggg:- "+node.id.replace(/_RRR_/g,'/'));
//          		chDir( node.id.replace( /_RRR_/g, '/' ), true );
			var path = node.getPath();
//			console.log("sss222:- "+path);
			this.tree.expandPath(path);
			this.onReload(node);
			//this.tree.getLoader().load(node);
            	}
                /*if (node.isLeaf()==false && node.childNodes.length==0){
                    //console.log(node.isLeaf(),node.childNodes.length)
                    this.nodeJustLoaded=node;
                    this.tree.getLoader().load(node);
                    //this.loadNode(node);
                }else{*/
                    this.setNode(node);
                //}
            }, this)
            
        } 
        
        //this.addListener('render', this.repaint, this);
        
        Ext.ux.LocationBar.superclass.initComponent.call(this);
    },
        
    // private
    autoCreate: {
        cls:'x-toolbar x-small-editor x-locationbar',
        html:'<table cellspacing="0"><tr></tr></table>'
    },

    // private
    onRender: function(ct, position) {
        Ext.ux.LocationBar.superclass.onRender.call(this, ct, position);
        
        this.repaint();
    },
    
    // private
    onClick: function(node) {
        if (this.selectHandler) {
            this.selectHandler(node);
        } else {
            if(node.parentNode) {
                node.parentNode.expand(false,true);
                node.ensureVisible();
            }
	    chDir( node.id.replace( /_RRR_/g, '/' ), true );
            node.select();
        }
    },
    
    // private
    onReload: function(node) {
        if (this.reloadHandler) {
            this.reloadHandler(node);
        } else if(node.reload) {
            node.reload();
        }
    },
    
    /**
     * Clears all items from the LocationBar.
     */
    clear: function() {
        this.locationItems = [];
        
        this.repaint;
    },
    
    /**
     * Sets the current Treenode
     * If a tree was provided as a config to this LocationBar, this should
     * be called automatically.     
     * @param {Ext.ux.TreeNode} node The currently selected TreeNode
     */
    setNode: function(node) {
        var path = [];
        var pNode = node;
        var i;
        do {
            var conf = {
                text: pNode.attributes.text,
                node: pNode,
                handler: this.onClick.createDelegate(this, [pNode], false)
            };
            if (pNode.childNodes.length) {
                var childs = [];
                for (i = 0; i < pNode.childNodes.length; i++) {
                    
                    childs[i] = {
                        text: pNode.childNodes[i].attributes.text,
                        node: pNode.childNodes[i],
                        iconCls: this.folderIconCls,
                        handler: this.onClick.createDelegate(this, [pNode.childNodes[i]], false)
                    };
                }
                conf.xtype = 'tbsplit';
                conf.menu = childs;
            }
            conf.fullPath = pNode.getPath('text').substr(1);
            path.unshift(conf);
        } while (pNode.parentNode && (pNode = pNode.parentNode) && pNode.id != 'root')
        
        this.locationItems = [];
        
        for(i=0; i<path.length; i++) {
            this.addPathItemRaw(path[i]);
        }
        this.currentItem = path[path.length - 1];
        
        this.addHistoryItemRaw(this.currentItem);
        
        this.repaint();
    },
    
    // private
    addHistoryItemRaw: function(item){
        if(this.historyItems.indexOf(item.text) != -1) {
            this.historyItems.remove(item.text);
            delete this.historyItemNodes[item.text];
        }
        this.historyItems.push(item.text);
        this.historyItemNodes[item.text] = item;
	custom_location_bar.current_node = this.historyItemNodes[item.text].node;

	if(this.forwardHistory.indexOf(item.text) == -1) {
            this.forwardHistory.push(item.text);
	    this.forwardHistoryNodes[item.text] = item;
        }
    },
    
    // private
    addPathItemRaw: function(item){
        // if number of items > maxItems, remove last
        if(this.maxItems && this.locationItems.length > this.maxItems) {
            this.locationItems.pop();
        }
        
        // put new item at the end
        this.locationItems.push(item);
    },
    
    // private
    repaint: function() {
    	if (this.items && this.items.length) {
    		var _doLayout = true;
    		this.items.each(function(item){            
    			this.items.remove(item);
    			item.destroy();
    		}, this.items);
    	} else {
    		var _doLayout = false;	
    	}
    	try {
	        this.items.each(function(item){            
	            this.items.remove(item);
	            item.destroy();
	        }, this.items); 
    	} catch(e) {}
        // back button
        this.add({
            cls: 'x-btn-icon',
            iconCls: this.backwardIconCls,
            handler: function() {
		console.log(this);
		//node.expand(false);
                this.historyNext = this.historyItems.pop();
		var currentItem = this.historyItemNodes[this.historyNext];				
                var itemKey = this.historyItems.pop();
                var item = this.historyItemNodes[itemKey];
		//this.onReload(currentItem.node.parentNode);
		//currentItem.node.collapse();
		if(item.node != "[Node ext_root]")
		item.node.collapse();
                this.onClick(item.node);
		//this.onReload(item.node);
            },
            scope: this,
            disabled: this.historyItems.length > 1 ? false : true
       	    //disabled:true
	 });
        
        // forward button
        // TODO: disabled, FUBAR
        this.add({
            cls: 'x-btn-icon',
            iconCls: this.forwardIconCls,
            handler: function() {
                var node = this.historyNext.node;
                this.historyNext = false;
	//	alert("forward button click");
                this.onClick(node);
            },
            scope: this,
	    //disabled: this.historyNext ? false : true
            disabled: true //this.historyNext ? false : true
        });
        
        this.add(' ','-',' ');
        
        if (this.locationItems.length) {
            // folder icon
            this.add({
                cls: 'x-btn-icon',
                iconCls: this.folderIconCls,
                ctCls: 'x-locationbar-location x-locationbar-location-first',
                disabled: true
            });
        
            var text;
            for (var i = 0; i < this.locationItems.length; i++) {
                var locationItem = this.locationItems[i];
                
                var item = {};
                
                if (typeof locationItem == 'object') {
                    item = locationItem;
                }
                else {
                    item.text = locationItem;
                }
                
                if(!item.text) {
                    item.text = 'n/a';
                }
                
                item.handler = this.onClick.createDelegate(this, [locationItem.node], false);
                
                item.ctCls = 'x-locationbar-location';
                
                this.add(item);
            }
            
            // spacer 
            this.addItem(
            {
                cls: 'x-locationbar-location x-locationbar-location-last',
                xtype: 'tbfill'
            });
            
            menu = [];
            for(var i=this.historyItems.length-2; i>=0; i--) {
                menu.push({
                   text: this.historyItemNodes[this.historyItems[i]].fullPath,
                   iconCls: this.folderIconCls,
                   node: this.historyItemNodes[this.historyItems[i]].node,
                   handler: function(item) {
                       this.onClick(item.node);
                   },
                   scope: this
                });
            }
            this.add({
                cls: 'x-btn-icon',
                ctCls: 'x-locationbar-location x-locationbar-location-last',
                menuAlign: 'tr-br?',
                menu: menu
            });
            if(!this.noReload) {
                this.add(' ');
                // reload button
                this.add({
                    cls: 'x-btn-icon',
                    iconCls: this.reloadIconCls,
                    handler: function() {
                        this.onReload(this.currentItem.node);
                    },
                    scope: this
                });
            }
            this.add(' ');
        } else {
            this.add({
                cls: 'x-btn-icon',
                iconCls: this.folderIconCls,
                ctCls: 'x-locationbar-location x-locationbar-location-first',
                disabled: true
            });
        
            if(this.emptyText) {
                this.add({
                    xtype: 'lbtext',
                    text: this.emptyText
                });
            }

            this.addItem(new Ext.ux.LocationBar.Fill());
            
            /*this.add({
                cls: 'x-btn-icon',
                ctCls: 'x-locationbar-location x-locationbar-location-last',
                menuAlign: 'tr-br?',
                disabled: true
            });
            this.add(' ');
            this.add({
                cls: 'x-btn-icon',
                iconCls: this.reloadIconCls,
                disabled: true
            });*/
            this.add(' ');
        }
    	if (_doLayout === true) {
    		this.doLayout();
    	}

    }
};


Ext.ux.LocationBar = Ext.extend(Ext.Toolbar,custom_location_bar);


Ext.reg('locationbar', Ext.ux.LocationBar);

Ext.ux.Fill = Ext.extend(Ext.Toolbar.Spacer, {
    // private
    render : function(td){

        td.style.width = '100%';
        Ext.fly(td).addClass('x-locationbar-location');
        Ext.ux.Fill.superclass.render.call(this, td);
    }
});
Ext.reg('tbfill', Ext.ux.Fill);

Ext.ux.LocationBar.Fill = Ext.extend(Ext.Toolbar.Fill, {
    // private
    render : function(td){
        td.className = 'x-locationbar-location';
        
        // insert a &nbsp;
        var data = document.createTextNode('\u00a0');
        this.el.appendChild(data);
        
        Ext.ux.LocationBar.Fill.superclass.render.call(this, td);
    }
});
Ext.reg('lbfill', Ext.ux.LocationBar.Fill);

Ext.ux.LocationBar.TextItem = Ext.extend(Ext.Toolbar.TextItem, {
    // private
    render : function(td){
        td.className = 'x-locationbar-location';
        
        Ext.ux.LocationBar.Fill.superclass.render.call(this, td);
    }
});
Ext.reg('lbtext', Ext.ux.LocationBar.TextItem);