source: trunk/themes/default/js/scripts.js @ 7852

Last change on this file since 7852 was 7852, checked in by rvelices, 13 years ago

small javascript and css simplification

  • Property svn:eol-style set to LF
File size: 3.4 KB
Line 
1function phpWGOpenWindow(theURL,winName,features)
2{
3        img = new Image();
4        img.src = theURL;
5        if (img.complete)
6        {
7                var width=img.width+40, height=img.height+40;
8        }
9        else
10        {
11                var width=640, height=480;
12                img.onload = function () { newWin.resizeTo( img.width+50, img.height+100); };
13        }
14        newWin = window.open(theURL,winName,features+',left=2,top=1,width=' + width + ',height=' + height);
15}
16
17function popuphelp(url)
18{
19        window.open( url, 'dc_popup',
20                'alwaysRaised=yes,dependent=yes,toolbar=no,height=420,width=500,menubar=no,resizable=yes,scrollbars=yes,status=no'
21        );
22}
23
24Function.prototype.pwgBind = function() {
25                var __method = this, object = arguments[0], args = Array.prototype.slice.call(arguments,1);
26                return function() {
27                                return __method.apply(object, args.concat(arguments) );
28                }
29}
30function PwgWS(urlRoot)
31{
32        this.urlRoot = urlRoot;
33        this.options = {
34                method: "GET",
35                async: true,
36                onFailure: null,
37                onSuccess: null
38        };
39};
40
41PwgWS.prototype = {
42
43        callService : function(method, parameters, options)
44        {
45                if (options)
46                {
47                        for (var property in options)
48                                this.options[property] = options[property];
49                }
50                try { this.transport = new XMLHttpRequest();}
51                catch(e) {
52                        try { this.transport = new ActiveXObject('Msxml2.XMLHTTP'); }
53                        catch(e) {
54                                try { this.transport = new ActiveXObject('Microsoft.XMLHTTP'); }
55                                catch (e){
56                                        dispatchError(0, "Cannot create request object");
57                                }
58                        }
59                }
60                this.transport.onreadystatechange = this.onStateChange.pwgBind(this);
61
62                var url = this.urlRoot+"ws.php?format=json";
63
64                var body = "method="+method;
65                if (parameters)
66                {
67                        for (var property in parameters)
68                        {
69                                if ( typeof parameters[property] == 'object' && parameters[property])
70                                {
71                                        for (var i=0; i<parameters[property].length; i++)
72                                                body += "&"+property+"[]="+encodeURIComponent(parameters[property][i]);
73                                }
74                                else
75                                        body += "&"+property+"="+encodeURIComponent(parameters[property]);
76                        }
77                }
78
79                if (this.options.method == "POST" )
80                {
81                        this.transport.open(this.options.method, url, this.options.async);
82                        this.transport.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
83                        this.transport.send(body);
84                }
85                else
86                {
87                        url += "&"+body;
88                        this.transport.open(this.options.method, url, this.options.async);
89                        this.transport.send(null);
90                }
91        },
92
93        onStateChange: function() {
94                var readyState = this.transport.readyState;
95                if (readyState==4)
96                        this.respondToReadyState(readyState);
97        },
98
99        dispatchError: function( httpCode, text )
100        {
101                !this.options.onFailure || this.options.onFailure( httpCode, text);
102        },
103
104        respondToReadyState: function(readyState)
105        {
106                var transport = this.transport;
107                if (readyState==4 && transport.status == 200)
108                {
109                        var resp;
110                        try {
111                                eval('resp = ' + transport.responseText);
112                        }
113                        catch (e) {
114                                this.dispatchError( 200, e.message + '\n' + transport.responseText.substr(0,512) );
115                        }
116                        if (resp!=null)
117                        {
118                                if (resp.stat==null)
119                                        this.dispatchError( 200, "Invalid response" );
120                                else if (resp.stat=='ok')
121                                {
122                                        if (this.options.onSuccess) this.options.onSuccess( resp.result );
123                                }
124                                else
125                                        this.dispatchError( 200, resp.err + " " + resp.message);
126                        }
127                }
128                if (readyState==4 && transport.status != 200)
129                        this.dispatchError( transport.status, transport.statusText );
130        },
131
132
133        transport: null,
134        urlRoot: null,
135        options: {}
136}
137
138function pwgAddEventListener(elem, evt, fn)
139{
140        if (window.attachEvent)
141                elem.attachEvent('on'+evt, fn);
142        else
143                elem.addEventListener(evt, fn, false);
144}
Note: See TracBrowser for help on using the repository browser.