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