summaryrefslogtreecommitdiff
path: root/httemplate/elements/xmlhttp.html
blob: b2f265afbb6bc03c9faddf10bc50f5f58eb0e737 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<%doc>

Example:

  include( '/elements/xmlhttp.html',
    # required
    'url'  => $p.'misc/something.html',
    'subs' => [ 'subroutine' ],

    # optional
    'method'     => 'GET',    # defaults to GET, could specify POST
    'key'        => 'unique', # unique key
    'skip_empty' => '1',      # When the given key value is null or = 0,
                              #   skip making a useless http request

  );

</%doc>
<%shared>
my %initialized = ();#won't work if component is "preloaded"... so don't do that
</%shared>
<& /elements/rs_init_object.html &>
<& /elements/init_overlib.html &>
<SCRIPT TYPE="text/javascript">

% foreach my $func ( @{$opt{'subs'}} ) { 
%
%   next if $initialized{$key.$func}++;
%
%   my $furl = $url;
%   $furl =~ s/\"/\\\\\"/; #javascript escape (fix gvim syntax: ")

    function <%$key%><%$func%>() {
        // count args; build URL
        var url = "<%$furl%>";
        var a = <%$key%><%$func%>.arguments;

        var args;
        var len;
        var content = 'sub=<% uri_escape($func) %>';
        if ( a && typeof a  == 'object'  && a[0].constructor == Array ) {
            args = a[0];
            len = args.length
        } else {
            args = a;
            len = args.length - 1;
        }
% if ( $opt{skip_empty} ) {
        if ( args[0] == 0 || !args[0] ) {
          return;
        }
% }
        for (var i = 0; i < len; i++) 
            content = content + "&arg=" + encodeURIComponent(args[i]);
        content = content.replace( /[+]/g, '%2B'); // fix unescaped plus signs 

        if ( '<%$method%>' == 'GET' ) {
          url = url + content;
        }

        //alert('<%$method%> ' + url);

        var xmlhttp = rs_init_object();
        xmlhttp.open("<%$method%>", url, true);

        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState != 4) 
        	return;

            if (xmlhttp.status != 200) {
              if ( xmlhttp.status != 0 ) {
                //not warning on the 0 errors, they pop up when navagating away
                // from the page
                alert(xmlhttp.status + " status connecting to " + url);
              }
            } else {
              var data = xmlhttp.responseText;
              //alert('received response: ' + data);
              if ( data.indexOf("<b>System error</b>") > -1 ) {
                // trim this a little
                var end = data.indexOf('<a href="#raw">') - 1;
                data = data.substring(0, end);

                overlib(data,
                  WIDTH, 480, MIDX, 0, MIDY, 0,
                  CAPTION, 'Error', STICKY, AUTOSTATUSCAP, DRAGGABLE,
                  CLOSECLICK, BGCOLOR, '#f00', CGCOLOR, '#f00'
                );
                //var w;
                //if ( w = window.open("about:blank") ) {
                //  w.document.write(data);
                //} else {
                //  // popup blocking?  should use an overlib popup instead 
                //  alert("Error popup disabled; try disabling popup blocking to see");
                //}
              } else {
                // invoke the callback
                a[a.length-1](data);
              }
            }
        }

        if ( '<%$method%>' == 'POST' ) {

          xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
          xmlhttp.send(content);

        } else {

          xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
          xmlhttp.send(null);

        }

        //rs_debug("x_$func_name url = " + url);
        //rs_debug("x_$func_name waiting..");
    }
% } 


</SCRIPT>
<%init>
my ( %opt ) = @_;

my $url = $opt{'url'};
my $method = exists($opt{'method'}) ? $opt{'method'} : 'GET';
#my @subs = @{ $opt{'subs'};
my $key = exists($opt{'key'}) ? $opt{'key'} : '';

$url .= ( ($url =~ /\?/) ? '&' : '?' )
  if $method eq 'GET';

</%init>