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