1# XXX should support non-Javascript mode?
2package WebminUI::DynamicText;
3use WebminCore;
4
5=head2 new WebminUI::DynamicText(&start-function, &args)
6A page element for displaying text that takes time to generate, such as from
7a long-running script. Uses a non-editable text box, updated via Javascript.
8The function will be called when it is time to start producing output, with this
9object as a parameter. It must call the add_line function on the object for each
10new line to be added.
11=cut
12sub new
13{
14my ($self, $func, $args) = @_;
15$self = { 'func' => $func,
16	  'args' => $args,
17	  'name' => "dynamic".++$dynamic_count,
18	  'rows' => 20,
19	  'cols' => 80 };
20bless($self);
21return $self;
22}
23
24=head2 set_message(text)
25Sets the text describing what we are waiting for
26=cut
27sub set_message
28{
29my ($self, $message) = @_;
30$self->{'message'} = $message;
31}
32
33sub get_message
34{
35my ($self) = @_;
36return $self->{'message'};
37}
38
39=head2 html()
40Returns the HTML for the text box
41=cut
42sub html
43{
44my ($self) = @_;
45my $rv;
46if ($self->get_message()) {
47	$rv .= $self->get_message()."<p>\n";
48	}
49$rv .= "<form name=form_$self->{'name'}>";
50$rv .= "<textarea name=$self->{'name'} rows=$self->{'rows'} cols=$self->{'cols'} wrap=off disabled=true>";
51$rv .= "</textarea>\n";
52$rv .= "</form>";
53return $rv;
54}
55
56=head2 start()
57Called by the page to begin the dynamic output.
58=cut
59sub start
60{
61my ($self) = @_;
62&{$self->{'func'}}($self, @$args);
63}
64
65=head2 add_line(line)
66Called by the function to add a line of text to this output
67=cut
68sub add_line
69{
70my ($self, $line) = @_;
71$line =~ s/\r|\n//g;
72$line = &quote_escape($line);
73print "<script>window.document.forms[\"form_$self->{'name'}\"].$self->{'name'}.value += \"$line\"+\"\\n\";</script>\n";
74}
75
76=head2 set_wait(wait)
77If called with a non-zero arg, generation of the page should wait until this
78text box is complete. Otherwise, the page will be generated completely before the
79start function is called
80=cut
81sub set_wait
82{
83my ($self, $wait) = @_;
84$self->{'wait'} = $wait;
85}
86
87sub get_wait
88{
89my ($self) = @_;
90return $self->{'wait'};
91}
92
93=head2 set_page(WebminUI::Page)
94Called when this dynamic text box is added to a page
95=cut
96sub set_page
97{
98my ($self, $page) = @_;
99$self->{'page'} = $page;
100}
101
102sub set_rows
103{
104my ($self, $rows) = @_;
105$self->{'rows'} = $rows;
106}
107
108sub set_cols
109{
110my ($self, $cols) = @_;
111$self->{'cols'} = $cols;
112}
113
114=head2 needs_unbuffered()
115Must return 1 if the page needs to be in un-buffered and no-table mode
116=cut
117sub needs_unbuffered
118{
119return 0;
120}
121
1221;
123
124