source: branches/2.1/themes/default/js/scripts.js @ 7958

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

bug 2043: merge from trunk to branch 2.1
some Javascript errors in default theme (also makes the rating.js script async)

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