From ad971289282b76f43d88405ab9281d645659dece Mon Sep 17 00:00:00 2001 From: Michael Wiebusch Date: Mon, 2 Dec 2013 18:58:15 +0100 Subject: [PATCH] removed ugly code for configFile and specFile selectors and replaced it with a modular solution, the fileSelector object, which can easily be reused --- tools/Widgets.pm | 98 +++++++++++++++++++++++++++++++++++++++++++++ tools/jtageditor.pl | 84 +++++++++----------------------------- tools/testgui.pl | 11 ----- 3 files changed, 116 insertions(+), 77 deletions(-) create mode 100644 tools/Widgets.pm diff --git a/tools/Widgets.pm b/tools/Widgets.pm new file mode 100644 index 0000000..9efe195 --- /dev/null +++ b/tools/Widgets.pm @@ -0,0 +1,98 @@ +package fileSelector; + +use warnings; +use strict; + + + +sub new { + my $class=shift; + my %options = @_; + my $self = { + id => '', + name => 'fileSelectionDropdown', + class => '', + onchange => '', + items => [], + selected => '', + %options + }; + bless($self, $class); + return $self; +} + +sub add_item { + my $self = shift; + my %entry = @_; + + push(@{$self->{items}},{%entry}); +} + +sub print_items { + my $self = shift; + for my $href (@{$self->{items}}) { + for my $key (keys %$href) { + print "$key:\t".$href->{$key}."\n"; + } + } +} + +sub add_items_from_dir{ + my $self = shift; + my %options = @_; + my $ext = $options{ext}; + my @files; + + opendir( DIR, $options{dir}) or die $!; + + while ( my $file = readdir(DIR) ) { + + # Use a regular expression to ignore files beginning with a period + next if ( $file =~ m/^\./ ); + push(@files, $file); + + } + + for my $file (sort @files) { + if (defined($ext)) { + if ( $file =~ m/\.$ext$/ ) { +# push( @xmlfiles, $file ); + $self->add_item(value=>$file); + } + } else { + $self->add_item(value=>$file); + } + } + + closedir(DIR); + +} + +sub print_html { + + my $self = shift; + + print q%%; + + print "\n"; +} + +1; \ No newline at end of file diff --git a/tools/jtageditor.pl b/tools/jtageditor.pl index 02576e9..7e4345a 100755 --- a/tools/jtageditor.pl +++ b/tools/jtageditor.pl @@ -69,6 +69,7 @@ require xmlRendering; use FindBin; use lib "$FindBin::Bin/.."; use Environment; +use Widgets; my %cgiHash = &read_input; @@ -145,70 +146,6 @@ if ( defined $cgiHash{'debuginput'} ) { ## subs generating html output ############################### -sub print_fileSelector { - - my $configFile = $_[0]; - my $elementId = $_[1]; - my $action = $_[2]; - opendir( DIR, $confDir ) or die $!; - - print ''; -} - -sub print_specSelector { - - my $configFile = $_[0]; - opendir( DIR, $specDir ) or die $!; - - print ''; -} - sub print_fileSelection { my $configFile = $_[0]; @@ -216,7 +153,16 @@ sub print_fileSelection { print "

selected config file

"; print "

"; - print_fileSelector( $configFile, "fileSelector", "loadFile()" ); +# print_fileSelector( $configFile, "fileSelector", "loadFile()" ); + my $config_selector = fileSelector->new( + selected=>$configFile, + id=>"fileSelector", + name=>"fileSelectionDropdown", + onchange=>"loadFile()" + ); + $config_selector->add_item(value=>'...'); + $config_selector->add_items_from_dir(dir=>$confDir,ext=>"xml"); + $config_selector->print_html(); print ""; print @@ -233,7 +179,13 @@ sub print_fileSelection { print ""; print ""; print ""; - print_specSelector(); + my $spec_selector = fileSelector->new( + id=>"specSelector", + name=>"specSelectonDropdown" + ); + $spec_selector->add_items_from_dir(dir=>$specDir,ext=>"xml"); + $spec_selector->print_html(); +# print_specSelector(); print ""; print ""; diff --git a/tools/testgui.pl b/tools/testgui.pl index 8bdf96f..eee8e85 100755 --- a/tools/testgui.pl +++ b/tools/testgui.pl @@ -113,17 +113,6 @@ sub parse_setupFile { } $setupTree = $parser->parse_file($setupFile); } - - -# sub parse_quickEditMaskFile { -# my $parser = XML::LibXML->new(); -# unless( -e $quickEditMaskFile) { -# die "quickEditMaskFile $quickEditMaskFile does not exist!\n"; -# } -# $quickEditMaskTree = $parser->parse_file($quickEditMaskFile); -# } - - sub init_html{ -- 2.43.0