/// <reference path="../Extjs_Intellisense.js" />
/// <reference path="Base.js" />
/// <reference path="XmlTreeLoader.js" />


Ext.ns('Ext.ux.tree');
Ext.ux.tree.TreeFromXml = Ext.extend(Ext.tree.TreeLoader, {
    XML_NODE_ELEMENT : 1,
    XML_NODE_TEXT : 3,
    // private override
    LoadFromXml:function(xml,toNode)
    {
        var doc=CreateXml(xml);
        if(doc==null || doc==undefined)
        {
            return null;
        }
        this.xml=xml;
        return this.LoadFromDoc(doc,toNode);
    },
    read : function(xml)
    {
        this.xml=xml || this.xml;
        var doc=CreateXml(xml);
        return this.readRecords(doc);
    },
    LoadFromDoc:function(xmlDoc,toNode)
    {
        if(xmlDoc==null || xmlDoc==undefined)
        {
            return null;
        }
        var RootDEml = xmlDoc.documentElement || xmlDoc;
        this.dom=RootDEml;
        var allNodes=this.parseXml(RootDEml);
        if(toNode)
        {
            toNode.appendChild(allNodes);
        }
        if(toNode==undefined)
        {
            return allNodes;
        }else
        {
            return toNode;
        }
    },
    processResponse : function(response, node, callback)
    {    
        if(xmlData==null || xmlData==undefined)
        {
            return null;
        }
        var root = this.xmlData.documentElement || this.xmlData;
        try
        {
            node.beginUpdate();
            node.appendChild(this.parseXml(root));
            node.endUpdate();
            if(typeof callback == "function")
            {
                callback(this, node);
            }
        }catch(e)
        {
            this.handleFailure(response);
        }
    },
    // private
    parseXml : function(node)
    {
        var nodes = [];
        Ext.each(node.childNodes, function(n)
        {
            if(n.nodeType == this.XML_NODE_ELEMENT)
            {
                if(n.getAttribute("Type")!="5")
                {//不要解码器
                    var treeNode = this.createNode(n);
                    if(n.childNodes.length > 0)
                    {
                        var child = this.parseXml(n);
                        if(typeof child == 'string')
                        {
                            treeNode.attributes.innerText = child;
                        }else
                        {
                            treeNode.appendChild(child);
                        }
                    }
                    nodes.push(treeNode);
                }else
                {
                    var decodercount=1;
                }
            }
            else if(n.nodeType == this.XML_NODE_TEXT)
            {
                var text = n.nodeValue.trim();
                if(text.length > 0)
                {
                    return nodes = text;
                }
            }
        }, this);
        return nodes;
    },
    //生成一个treenode
    createNode : function(XmlNode)
    {
        var attr ={tagName: XmlNode.tagName};
        Ext.each(XmlNode.attributes, function(a){attr[a.nodeName] = a.nodeValue;});
        this.processAttributes(attr);
        if(this.baseAttrs)
        {
            Ext.applyIf(attr, this.baseAttrs);
        }
        if(Ext.isString(attr.uiProvider))
        {
           attr.uiProvider = this.uiProviders[attr.uiProvider] || eval(attr.uiProvider);
        }
        var newNode=new Ext.tree.TreeNode(attr);
        newNode.attributes=attr;
        return newNode;
    },
    processAttributes: Ext.emptyFn
});

//backwards compat
Ext.ux.TreeFromXml = Ext.ux.tree.TreeFromXml;

 
 
 Ext.data.StringProxy = function(cfg)
 {
    var api = {};
    cfg=cfg || {};
    this.reader = cfg.reader;
    this.xml = cfg.xml;
    api[Ext.data.Api.actions.read] = true;  
    Ext.data.StringProxy.superclass.constructor.call(this, {api: api});   
    this.appendData = false;
    if(cfg.appendData)
    {
        this.appendData=true;
    }   
};
   
Ext.extend(Ext.data.StringProxy, Ext.data.DataProxy,{
    doRequest : function(action, rs, params, reader, callback, scope, arg)
    {
        params = params || {};     
        var result;        
        try
        {
            result=this.LoadXml(this.xml);
        }catch(e)
        {
            this.fireEvent("loadexception", this, null, arg, e);   
            this.fireEvent('exception', this, 'response', action, arg, null, e);    
            callback.call(scope, null, arg, false);    
            return;   
        }       
        callback.call(scope, result, arg, true);
    },
    LoadXml:function(xml)
    {
        this.xml=this.xml || "";
        this.data = CreateXmlDom(this.xml);
        this.data=this.data;
        result = this.reader.readRecords(this.data,this.appendData);
        //result={records:this.data.childNodes[0].childNodes,success:true};
        return result;
    }
});




Ext.data.StringXmlStore=Ext.extend(Ext.data.XmlStore,{
     appendData:false,
     constructor:function(cfg)
     {  
        Ext.data.StringXmlStore.superclass.constructor.call(this,cfg);  
        cfg=cfg || {};
        this.proxy=cfg.proxy || this.proxy;
        this.reader = cfg.reader || this.reader; 
        this.xml = cfg.xml || this.xml;
        this.appendData = cfg.appendData;
    },
    read : function(xml)
    {
        this.xml=xml || this.xml;
        var doc=CreateXml(xml);
        return this.readRecords(doc);
    },
    LoadXml:function(xml,reader)
    {
        this.xml=xml || this.xml || "";
        this.data = CreateXml(this.xml);
        this.reader=reader || this.reader;
        result = this.reader.readRecords(this.data);
        this.loadData(result,this.appendData);
        //this.data=result.records;
    }
});








