source: branches/2.2.0.1/phpgwapi/js/x_tools/xtools.js @ 4482

Revision 4482, 3.6 KB checked in by rafaelraymundo, 13 years ago (diff)

Ticket #1726 - Add xtool. Criado template p info ao usuário os browsers compativeis. r4434 r4435

  • Property svn:executable set to *
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               
30                if ( !(_FILES[pFile]) )
31                {
32                        var _data = null;
33                       
34                        if ( document.implementation.createDocument )
35                        {
36                                var xmlHttp = new window.XMLHttpRequest();
37                                        xmlHttp.open("GET",pFile + '?' + Date.parse(new Date),false );
38                                        xmlHttp.send(null);
39                               
40                                _data = xmlHttp.responseXML.documentElement;
41                        }
42                        else
43                        {
44                                _data = new ActiveXObject( "Msxml2.FreeThreadedDOMDocument" );
45                                _data.async = false;
46                                _data.load( pFile + '?' + Date.parse(new Date) );
47                        }
48
49                        _FILES[pFile] = _data;
50                }
51                return _FILES[pFile];
52        }
53
54        function _parse()
55        {
56                if ( arguments.length == 1 )
57                {
58                        pXML = _xml('root');
59                        pXSL = arguments[0];
60                }
61                else
62                {
63                        pXML = arguments[0];
64                        pXSL = arguments[1];
65                }
66               
67                switch ( typeof pXML )
68                {
69                        case 'object' :
70                        break;
71                        case 'string' :
72                                if ( pXML.indexOf('<') == 0 )
73                                        pXML = _convert(pXML);
74                                else
75                                        pXML = _load.call(this, pXML);
76                        break;
77                        default :
78                                return { 'error' : 'invalid xml' }
79                }
80               
81                switch ( typeof pXSL )
82                {
83                        case 'object' :
84                        break;
85                        case 'string' :
86                                pXSL = _load.call(this, pXSL);
87                        break;
88                        default :
89                                return {'error':'invalid xsl'}
90                }
91
92                var fragment = null;
93               
94                if ( window.XSLTProcessor )
95                {
96                        var xslProc = new XSLTProcessor();
97                        xslProc.importStylesheet(pXSL);
98
99                        if ( (arguments.length == 3) && (typeof arguments[2] == 'object') )
100                        {
101                                var params = arguments[2];
102                                for (var i in params )
103                                        if ( params[ i ] && params[ i ].constructor != Function )
104                                                xslProc.setParameter(null, String( i ), String( params[i] ) );
105                        }
106
107                        fragment = xslProc.transformToFragment(pXML, document);
108
109                        // para retornar valor igual ao ie.
110                        var aux = document.createElement("div");
111                        aux.appendChild(fragment);
112                        fragment = aux.innerHTML;
113                }
114                else
115                {
116                        var xslTemplate = new ActiveXObject("MSXML2.XSLTemplate");
117                        xslTemplate.stylesheet = pXSL;
118
119                        var xslProc = xslTemplate.createProcessor();
120                        xslProc.input = pXML;
121
122                        if ( (arguments.length == 3) && (typeof arguments[2] == 'object') )
123                        {
124                                var params = arguments[2];
125                                for (var i in params )
126                                        if ( params[ i ] && params[ i ].constructor != Function )
127                                        {
128                                                xslProc.addParameter( String( i ), String( params[i] ), '');
129                                        }
130                        }
131
132                        xslProc.transform();
133                        fragment = xslProc.output;
134                }
135                return fragment;
136        }
137
138        function _xml()
139        {
140                var a = false;
141                if ( document.implementation.createDocument )
142                        a = document.implementation.createDocument("", "", null);
143                else if ( ActiveXObject )
144                        a = new ActiveXObject("Msxml2.DOMDocument");
145
146                if ( arguments.length == 1 && typeof arguments[0] == 'string' )
147                        a.appendChild(a.createElement(arguments[0]));
148                //with ( a )
149                //      appendChild(createProcessingInstruction("xml", "version='1.0'"));
150
151                return a;
152        }
153
154        function xtools()
155        {
156                var _argv = arguments;
157                this._PATH = ( _argv.length > 0 ) ? _argv[0] : '';
158
159                if ( this._PATH != '' && this._PATH.lastIndexOf('/') != (this._PATH.length - 1) )
160                        this._PATH += '/';
161        }
162
163        xtools.prototype.convert        = _convert;
164        xtools.prototype.load           = _load;
165        xtools.prototype.parse          = _parse;
166        xtools.prototype.xml            = _xml;
167       
168        window.xtools = xtools;
169}
170)();
Note: See TracBrowser for help on using the repository browser.