source: trunk/phpgwapi/js/x_tools/xtools.js @ 5433

Revision 5433, 3.8 KB checked in by alexandrecorreia, 12 years ago (diff)

Ticket #673 - Alteração do arquivo xtools.

Line 
1(function()
2{
3        var _FILES = [];
4
5        function _convert(pString)
6        {
7                if ( typeof pString != 'string' )
8                        return false;
9
10                if ( window.ActiveXObject )
11                {
12                        var _xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
13                        _xmlDoc.async = "false";
14                        _xmlDoc.loadXML(pString);
15                }
16                else
17                {
18                        var parser = new DOMParser();
19                        var _xmlDoc = parser.parseFromString(pString, "text/xml");
20                }
21
22                return _xmlDoc;
23        }
24
25        function _load(pFile)
26        {
27                if( pFile.indexOf("/") == -1 )
28                        pFile = this._PATH + 'xsl/' + pFile;
29                /*else
30                        pFile = this._PATH + pFile;*/
31               
32                if ( !(_FILES[pFile]) )
33                {
34                        var _data = null;
35                       
36                        if ( document.implementation && document.implementation.createDocument )
37                        {
38                                XMLDocument.prototype.load = function(filePath)
39                                {
40                                        var xmlhttp = new XMLHttpRequest();
41                                        xmlhttp.open("GET", filePath, false);
42                                        xmlhttp.setRequestHeader("Content-Type","text/xml");
43                                        xmlhttp.send(null);
44                                        var newDOM = xmlhttp.responseXML;
45                                        if( newDOM )
46                                        {
47                                                var newElt = this.importNode(newDOM.documentElement, true);
48                                                this.appendChild(newElt);
49                                                return true;
50                                        }
51                                }
52
53                                _data = document.implementation.createDocument("", "", null);
54                        }
55                        else
56                                _data = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
57
58                        _data.async = false;
59                        _data.load( pFile + '?' + Date.parse(new Date));
60                        _FILES[pFile] = _data;
61                }
62                return _FILES[pFile];
63        }
64
65        function _parse()
66        {
67                if ( arguments.length == 1 )
68                {
69                        pXML = _xml('root');
70                        pXSL = arguments[0];
71                }
72                else
73                {
74                        pXML = arguments[0];
75                        pXSL = arguments[1];
76                }
77                switch ( typeof pXML )
78                {
79                        case 'object' :
80                        break;
81                        case 'string' :
82                                if ( pXML.indexOf('<') == 0 )
83                                        pXML = _convert(pXML);
84                                else
85                                        pXML = _load.call(this, pXML);
86                        break;
87                        default :
88                                return {'error':'invalid xml'}
89                }
90                switch ( typeof pXSL )
91                {
92                        case 'object' :
93                        break;
94                        case 'string' :
95                                pXSL = _load.call(this, pXSL);
96                        break;
97                        default :
98                                return {'error':'invalid xsl'}
99                }
100
101                var fragment = null;
102                if ( window.XSLTProcessor )
103                {
104                        var xslProc = new XSLTProcessor();
105                        xslProc.importStylesheet(pXSL);
106
107                        if ( (arguments.length == 3) && (typeof arguments[2] == 'object') )
108                        {
109                                var params = arguments[2];
110                                for (var i in params )
111                                        if ( params[ i ] && params[ i ].constructor != Function )
112                                                xslProc.setParameter(null, String( i ), String( params[i] ) );
113                        }
114
115                        fragment = xslProc.transformToFragment(pXML, document);
116
117                        var aux = document.createElement("div");
118                        aux.appendChild( fragment );
119                        fragment = aux.innerHTML;
120                }
121                else
122                {
123                        var xslTemplate = new ActiveXObject("MSXML2.XSLTemplate");
124                        xslTemplate.stylesheet = pXSL;
125       
126                        var xslProc = xslTemplate.createProcessor();
127                        xslProc.input = pXML;
128       
129                        if ( (arguments.length == 3) && (typeof arguments[2] == 'object') )
130                        {
131                                var params = arguments[2];
132                                for (var i in params )
133                                        if ( params[ i ] && params[ i ].constructor != Function )
134                                        {
135                                                xslProc.addParameter( String( i ), String( params[i] ), '');
136                                        }
137                        }
138 
139                        xslProc.transform();
140                        fragment = xslProc.output;
141                }
142                return fragment;
143        }
144
145        function _xml()
146        {
147                var a = false;
148                if ( document.implementation.createDocument )
149                        a = document.implementation.createDocument("", "", null);
150                else if ( ActiveXObject )
151                        a = new ActiveXObject("Msxml2.DOMDocument");
152
153                if ( arguments.length == 1 && typeof arguments[0] == 'string' )
154                        a.appendChild(a.createElement(arguments[0]));
155                //with ( a )
156                //      appendChild(createProcessingInstruction("xml", "version='1.0'"));
157
158                return a;
159        }
160
161        function xtools()
162        {
163                var _argv = arguments;
164                this._PATH = ( _argv.length > 0 ) ? _argv[0] : '';
165               
166                if ( this._PATH != '' && this._PATH.lastIndexOf('/') != (this._PATH.length - 1) )
167                        this._PATH += '/';
168        }
169
170        xtools.prototype.convert        = _convert;
171        xtools.prototype.load           = _load;
172        xtools.prototype.parse          = _parse;
173        xtools.prototype.xml            = _xml;
174       
175        window.xtools = xtools;
176}
177)();
Note: See TracBrowser for help on using the repository browser.