3 In misc/something.html:
6 <INPUT TYPE="hidden" NAME="recordnum" VALUE="42">
7 <INPUT TYPE="hidden" NAME="what_to_do" VALUE="delete">
8 <% include( '/elements/progress-init.html',
10 [ 'recordnum', 'what_to_do' ],
11 $p.'misc/process_something.html',
12 { url => $p.'where_to_go_next.html' },
13 #or { message => 'Finished!' },
14 #or { url => $p.'where_to_go.html',
15 message => 'Finished' },
16 # which recirects to the URL and displays the message as a status
17 #or { popup_url => $p.'popup_contents.html' }
18 # which loads that URL into the popup after completion
19 #or { url => $p.'where_to_go.html',
20 error_url => $p.'where_we_just_were.html' }
21 # to redirect somewhere different on error
24 <SCRIPT TYPE="text/javascript>process();</SCRIPT>
26 In misc/process_something.html:
29 my $server = FS::UI::Web::JSRPC->new('FS::something::process_whatever', $cgi);
31 <% $server->process %>
35 sub process_whatever { #class method
37 my $param = thaw(base64_decode(shift));
38 # param = { 'recordnum' => 42, 'what_to_do' => delete }
39 # make use of this as you like
41 $job->update_statustext(20);
43 $job->update_statustext(40);
45 $job->update_statustext(60);
47 return 'this value will be ignored';
52 This component creates two JS functions: process(), which starts the
53 submission process, and start_job(), which starts the job on the
56 process() does the following:
57 - disable the form submit button
58 - for all form fields named in "fields", collect their values into an array
59 - declare a callback function "myCallback"
60 - pass the array and callback to start_job()
62 start_job() is an xmlhttp standard function that turns the array of values
63 into a JSON string, posts it to the location given in $action, receives a
64 job number back, and then invokes the callback with the job number as an
65 argument. Normally, the post target script will use FS::UI::Web::JSRPC to insert
66 a queue job (with the form field values as arguments), and return its
69 myCallback() opens an Overlib popup containing progress-popup.html, with
70 the job number, form name, and destination options (url, message, popup_url,
71 error_url) as URL parameters. This popup will poll the server for the status
72 of the job, display a progress bar, and on completion, either redirect to
73 'url' or 'popup_url', display 'message' in the popup window, or (on failure)
74 display the job statustext as an error message. If 'error_url' is specified,
75 the "Close" button in the popup will then redirect the user to that location.
78 <% include('/elements/xmlhttp.html',
81 'subs' => [ 'start_job' ],
86 <& /elements/init_overlib.html &>
88 <SCRIPT TYPE="text/javascript">
90 function <%$key%>process () {
92 //alert('<%$key%>process for form <%$formname%>');
94 if ( document.<%$formname%>.submit.disabled == false ) {
95 document.<%$formname%>.submit.disabled=true;
98 overlib( 'Submitting job to server...', WIDTH, 444, HEIGHT, 168, CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', CLOSECLICK, MIDX, 0, MIDY, 0, TEXTPADDING, 0, BASE, 0, BGCOLOR, '#333399', CGCOLOR, '#333399', FGCOLOR, '#f8f8f8' );
100 // jQuery .serializeArray() maybe?
101 var copy_fields = <% encode_json(\%copy_fields) %>;
102 var Hash = new Array();
105 for (var i = 0; i<document.<%$formname%>.elements.length; i++) {
106 field = document.<%$formname%>.elements[i];
107 if ( <% $all_fields %> || copy_fields[ field.name ] ) {
108 if ( field.type == 'select-multiple' ) {
109 //alert('select-multiple ' + field.name);
110 for (var j=0; j < field.options.length; j++) {
111 if ( field.options[j].selected ) {
112 //alert(field.name + ' => ' + field.options[j].value);
113 Hash[x++] = field.name;
114 Hash[x++] = field.options[j].value;
117 } else if ( ( field.type != 'radio' && field.type != 'checkbox' )
118 || ( ( field.type == 'radio' || field.type == 'checkbox' )
119 && document.<%$formname%>.elements[i].checked
123 Hash[x++] = field.name;
124 Hash[x++] = field.value;
129 Hash.push('key', '<%$key%>');
132 // jsrsExecute( '<% $action %>', <%$key%>myCallback, 'start_job', Hash );
134 //alert('start_job( ' + Hash + ', <%$key%>myCallback )' );
135 //alert('start_job()' );
136 <%$key%>start_job( Hash, <%$key%>myCallback );
140 function <%$key%>myCallback( jobnum ) {
142 var url = <% $progress_url->as_string |js_string %>;
143 url = url.replace('_JOBNUM_', jobnum);
144 overlib( OLiframeContent(url, 444, 168, '<% $popup_name %>', 0), CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', CLOSECLICK, MIDX, 0, MIDY, 0, TEXTPADDING, 0, BASE, 0, BGCOLOR, '#333399', CGCOLOR, '#333399', FGCOLOR, '#f8f8f8' );
152 my( $formname, $fields, $action, $url_or_message, $key ) = @_;
153 $key = '' unless defined $key;
156 if ( ref($url_or_message) ) { #its a message or something
157 %dest_info = map { $_ => $url_or_message->{$_} }
158 grep { $url_or_message->{$_} }
159 qw( message url popup_url error_url reload_with_error );
161 # it can also just be a url
162 %dest_info = ( 'url' => $url_or_message );
165 my $progress_url = URI->new($fsurl.'misc/progress-popup.html');
166 $progress_url->query_form(
167 'jobnum' => '_JOBNUM_',
168 'formname' => $formname,
174 if (grep '/^ALL$/', @$fields) {
177 %copy_fields = map { $_ => 1 } @$fields;
180 #stupid safari is caching the "location" of popup iframs, and submitting them
181 #instead of displaying them. this should prevent that.
182 my $popup_name = 'popup-'.random_id();