CGI-ProgressBar

 view release on metacpan or  search on metacpan

lib/CGI/ProgressBar.pm  view on Meta::CPAN


	my $pb = bless {
		_updates=> 0,		debug	=> undef,
		mycss	=> '',		orientation	=> 'horizontal',
		from	=> 1,		to		=> 100,	width	=> '400',
		height	=> '20',	blocks	=> 10,
		label	=> 0,		colors	=> [100,'blue'],
	},__PACKAGE__;

	if (ref $_[0] eq 'HASH'){	%args = %{$_[0]} }
	elsif (not ref $_[0]){		%args = @_ }
	else {
		warn "Usage: \$class->new(  keys=>values,  )";
		return undef;
	}
	foreach my $k (keys %args){
		my $nk = $k;
		$nk =~ s/^-(.*)$/$1/;
		$pb->{$nk} = $args{$k};
	}
	$pb->{blocks} = $args{steps} if $args{steps};

	$pb->{orientation} = 'vertical' if $pb->{orientation} eq 'v';
	if ($pb->{orientation} eq 'vertical'){
		my $w = $pb->{width};
		$pb->{width} = $pb->{height};
		$pb->{height} = $w;
	}

	$pb->{colors}	= $pb->{colors}? {@{$pb->{colors}}} : {100=>'blue'};
	$pb->{_length}	= $pb->{to} - $pb->{from};	# Units in the bar
	$pb->{_interval} = 1;	# publicise?
#	$pb->{_interval} = $pb->{_length}>0? ($pb->{_length}/$pb->{blocks}) : 0;
 	$pb->{block_wi} = int( $pb->{width} / $pb->{blocks} ) -2;
	# IN A LATER VERSION....Store ourself in caller's progress_bar array
	# push @{ $self->{progress_bar} },$pb;
	$self->{progress_bar} = $pb;

	for my $k (qw[ from to blocks _interval ]){
		$pb->{$k} = int($pb->{$k});
	}

	if ($pb->{debug}){
		require Data::Dumper; import Data::Dumper;
		warn 'New CGI::ProgressBar '.Dumper($pb);
		warn 'Total blocks='.($pb->{blocks});
		warn 'Expected total calls='.($pb->{to}/$pb->{_interval});
	}

	return $self->_pb_init();
}



=head2 FUNCTION/METHOD update_progress_bar

Updates the progress bar.

=cut

sub update_progress_bar {
# 	my ($self, @crud) = CGI::self_or_default;
	return "<script type='text/javascript'>//<!--
	pblib_progress_update()\n//-->\n</script>\n";
}

=head2 FUNCTION/METHOD hide_progress_bar

Hides the progress bar.

=cut

sub hide_progress_bar {
	my ($self, @crud) = CGI::self_or_default;
	#my $pb = $self->{progress_bar}[$#{$self->{progress_bar}}];

	return $self->{progress_bar}?
	"<script type='text/javascript'>//<!--
	$self->{progress_bar}->{layer_id}->{container}.style.display='none';\n//-->\n</script>\n"
	: '' ;
}

=head1 CSS STYLE CLASS EMPLOYED

You can add CSS to be output into the page body (ugh) in the C<mycss> field.
Bear in mind that the width and height settings are programatically assigned.

=item pblib_bar

A C<DIV> containing the whole progress bar, including any
accessories (such as the label). The only attribute used
by this module is C<width>, which is set dynamically.
The rest is up to you. A good start is:

	padding:    2 px;
	border:     solid black 1px;
	text-align: center;

=item pblib_block_off, pblib_block_on

An individual block within the status bar. The following
attributes are set dynamically: C<width>, C<height>,
C<margin-right>.

=item pblib_number

Formatting for the C<label> text (part of which is actually
an C<input type='text'> element. C<border> and C<text-align>
are used here, and the whole appears centred within a C<table>.

=cut

sub CGI::_pb_init {
	my ($self, @crud) = CGI::self_or_default;
	my $html = "";
	# my $pb = $self->{progress_bar}[$#{$self->{progress_bar}}];
	$self->{progress_bar}->{block_wi} = 1 if not $self->{progress_bar}->{block_wi} or $self->{progress_bar}->{block_wi} < 1 ;

	$self->{progress_bar}->{layer_id} = {
		container	=> 'pb_cont'.time,
		form		=> 'pb_form'.time,



( run in 0.796 second using v1.01-cache-2.11-cpan-e1769b4cff6 )