4.x style
[freeside.git] / httemplate / elements / xmlhttp.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/xmlhttp.html',
6     # required
7     'url'  => $p.'misc/something.html',
8     'subs' => [ 'subroutine' ],
9
10     # optional
11     'method'     => 'GET',    # defaults to GET, could specify POST
12     'key'        => 'unique', # unique key
13     'skip_empty' => '1',      # When the given key value is null or = 0,
14                               #   skip making a useless http request
15
16   );
17
18 </%doc>
19 <%shared>
20 my %initialized = ();#won't work if component is "preloaded"... so don't do that
21 </%shared>
22 <& /elements/rs_init_object.html &>
23 <& /elements/init_overlib.html &>
24 <SCRIPT TYPE="text/javascript">
25
26 % foreach my $func ( @{$opt{'subs'}} ) { 
27 %
28 %   next if $initialized{$key.$func}++;
29 %
30 %   my $furl = $url;
31 %   $furl =~ s/\"/\\\\\"/; #javascript escape (fix gvim syntax: ")
32
33     function <%$key%><%$func%>() {
34         // count args; build URL
35         var url = "<%$furl%>";
36         var a = <%$key%><%$func%>.arguments;
37
38         var args;
39         var len;
40         var content = 'sub=<% uri_escape($func) %>';
41         if ( a && typeof a  == 'object'  && a[0].constructor == Array ) {
42             args = a[0];
43             len = args.length
44         } else {
45             args = a;
46             len = args.length - 1;
47         }
48 % if ( $opt{skip_empty} ) {
49         if ( args[0] == 0 || !args[0] ) {
50           return;
51         }
52 % }
53         for (var i = 0; i < len; i++) 
54             content = content + "&arg=" + encodeURIComponent(args[i]);
55         content = content.replace( /[+]/g, '%2B'); // fix unescaped plus signs 
56
57         if ( '<%$method%>' == 'GET' ) {
58           url = url + content;
59         }
60
61         //alert('<%$method%> ' + url);
62
63         var xmlhttp = rs_init_object();
64         xmlhttp.open("<%$method%>", url, true);
65
66         xmlhttp.onreadystatechange = function() {
67             if (xmlhttp.readyState != 4) 
68                 return;
69
70             if (xmlhttp.status != 200) {
71               if ( xmlhttp.status != 0 ) {
72                 //not warning on the 0 errors, they pop up when navagating away
73                 // from the page
74                 alert(xmlhttp.status + " status connecting to " + url);
75               }
76             } else {
77               var data = xmlhttp.responseText;
78               //alert('received response: ' + data);
79               if ( data.indexOf("<b>System error</b>") > -1 ) {
80                 // trim this a little
81                 var end = data.indexOf('<a href="#raw">') - 1;
82                 data = data.substring(0, end);
83
84                 overlib(data,
85                   WIDTH, 480, MIDX, 0, MIDY, 0,
86                   CAPTION, 'Error', STICKY, AUTOSTATUSCAP, DRAGGABLE,
87                   CLOSECLICK, BGCOLOR, '#f00', CGCOLOR, '#f00'
88                 );
89                 //var w;
90                 //if ( w = window.open("about:blank") ) {
91                 //  w.document.write(data);
92                 //} else {
93                 //  // popup blocking?  should use an overlib popup instead 
94                 //  alert("Error popup disabled; try disabling popup blocking to see");
95                 //}
96               } else {
97                 // invoke the callback
98                 a[a.length-1](data);
99               }
100             }
101         }
102
103         if ( '<%$method%>' == 'POST' ) {
104
105           xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
106           xmlhttp.send(content);
107
108         } else {
109
110           xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
111           xmlhttp.send(null);
112
113         }
114
115         //rs_debug("x_$func_name url = " + url);
116         //rs_debug("x_$func_name waiting..");
117     }
118 % } 
119
120
121 </SCRIPT>
122 <%init>
123 my ( %opt ) = @_;
124
125 my $url = $opt{'url'};
126 my $method = exists($opt{'method'}) ? $opt{'method'} : 'GET';
127 #my @subs = @{ $opt{'subs'};
128 my $key = exists($opt{'key'}) ? $opt{'key'} : '';
129
130 $url .= ( ($url =~ /\?/) ? '&' : '?' )
131   if $method eq 'GET';
132
133 </%init>