Skip to content

Commit

Permalink
Item13331: Implement textarea and textfield
Browse files Browse the repository at this point in the history
 - Change calling convention to allow direct replacement of CGI:: with
   Foswiki::Render::HTML::
 - Replace all uses of textarea and textfield, with a couple of
   exceptions
  • Loading branch information
gac410 committed Apr 18, 2015
1 parent c3fa375 commit 203b59d
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 75 deletions.
14 changes: 7 additions & 7 deletions JQueryPlugin/lib/Foswiki/Form/Color.pm
Expand Up @@ -4,6 +4,7 @@ use Foswiki::Form::FieldDefinition;
our @ISA = qw( Foswiki::Form::FieldDefinition );

use Foswiki::Plugins::JQueryPlugin ();
use Foswiki::Render::HTML ();

use strict;
use warnings;
Expand Down Expand Up @@ -33,13 +34,12 @@ sub renderForEdit {
}
$value ||= '#000';

my $field = CGI::textfield(
-class => $this->cssClasses('foswikiInputField jqFarbtastic'),
-name => $this->{name},
-size => 11,
-override => 1,
-value => $value,
-id => $this->{name},
my $field = Foswiki::Render::HTML::textfield(
-class => $this->cssClasses('foswikiInputField jqFarbtastic'),
-name => $this->{name},
-size => 11,
-value => $value,
-id => $this->{name},
);

return ( '', $field );
Expand Down
3 changes: 2 additions & 1 deletion JQueryPlugin/lib/Foswiki/Form/Textboxlist.pm
Expand Up @@ -2,6 +2,7 @@
package Foswiki::Form::Textboxlist;

use Foswiki::Form::ListFieldDefinition;
use Foswiki::Render::HTML;
our @ISA = qw( Foswiki::Form::ListFieldDefinition );
use Foswiki::Plugins::JQueryPlugin ();

Expand Down Expand Up @@ -51,7 +52,7 @@ sub renderForEdit {
}
}

my $field = CGI::textfield(
my $field = Foswiki::Render::HTML::textfield(
-class =>
$this->cssClasses("foswikiInputField jqTextboxList $metadata"),
-name => $this->{name},
Expand Down
3 changes: 2 additions & 1 deletion JSCalendarContrib/lib/Foswiki/Form/Date.pm
Expand Up @@ -9,6 +9,7 @@ use strict;
use warnings;

use Foswiki::Form::FieldDefinition ();
use Foswiki::Render::HTML ();
use Foswiki::Contrib::JSCalendarContrib ();
our @ISA = ('Foswiki::Form::FieldDefinition');

Expand All @@ -32,7 +33,7 @@ sub renderForEdit {
( $this, $web, $topic, $value ) = @_;
undef $topicObject;
}
$value = CGI::textfield(
$value = Foswiki::Render::HTML::textfield(
{
name => $this->{name},
id => 'id' . $this->{name},
Expand Down
9 changes: 5 additions & 4 deletions PreferencesPlugin/lib/Foswiki/Plugins/PreferencesPlugin.pm
Expand Up @@ -10,8 +10,9 @@ package Foswiki::Plugins::PreferencesPlugin;
use strict;
use warnings;

use Foswiki::Func (); # The plugins API
use Foswiki::Plugins (); # For the API version
use Foswiki::Func (); # The plugins API
use Foswiki::Plugins (); # For the API version
use Foswiki::Render::HTML (); # Rendering textarea and textfield

use vars qw( @shelter );

Expand Down Expand Up @@ -188,7 +189,7 @@ sub _generateEditField {
$rows++ while $value =~ m/\n/g;

# No form definition and there are newlines, default to textarea
$html = CGI::textarea(
$html = Foswiki::Render::HTML::textarea(
-class => 'foswikiAlert foswikiInputField',
-name => $name,
-cols => 80,
Expand All @@ -199,7 +200,7 @@ sub _generateEditField {
else {

# No form definition and no newlines, default to text field.
$html = CGI::textfield(
$html = Foswiki::Render::HTML::textfield(
-class => 'foswikiAlert foswikiInputField',
-name => $name,
-size => 80,
Expand Down
13 changes: 7 additions & 6 deletions core/lib/Foswiki/Form/FieldDefinition.pm
Expand Up @@ -20,6 +20,8 @@ use warnings;
use Assert;
use CGI ();

use Foswiki::Render::HTML;

BEGIN {
if ( $Foswiki::cfg{UseLocale} ) {
require locale;
Expand Down Expand Up @@ -138,12 +140,11 @@ sub renderForEdit {
'<br /><span class="foswikiAlert">MISSING TYPE '
. $this->{type}
. '</span>',
CGI::textfield(
-class => $this->cssClasses('foswikiAlert foswikiInputField'),
-name => $this->{name},
-size => 80,
-override => 1,
-value => $value,
Foswiki::Render::HTML::textfield(
-class => $this->cssClasses('foswikiAlert foswikiInputField'),
-name => $this->{name},
-size => 80,
-value => $value,
)
);
}
Expand Down
12 changes: 6 additions & 6 deletions core/lib/Foswiki/Form/Text.pm
Expand Up @@ -5,6 +5,7 @@ use strict;
use warnings;

use Foswiki::Form::FieldDefinition ();
use Foswiki::Render::HTML;
our @ISA = ('Foswiki::Form::FieldDefinition');

BEGIN {
Expand All @@ -29,12 +30,11 @@ sub renderForEdit {

return (
'',
CGI::textfield(
-class => $this->cssClasses('foswikiInputField'),
-name => $this->{name},
-size => $this->{size},
-override => 1,
-value => $value,
Foswiki::Render::HTML::textfield(
-class => $this->cssClasses('foswikiInputField'),
-name => $this->{name},
-size => $this->{size},
-value => $value,
)
);
}
Expand Down
12 changes: 5 additions & 7 deletions core/lib/Foswiki/Form/Textarea.pm
Expand Up @@ -52,13 +52,11 @@ sub renderForEdit {
return (
'',
Foswiki::Render::HTML::textarea(
{
class => $this->cssClasses('foswikiTextarea'),
cols => $this->{cols},
rows => $this->{rows},
name => $this->{name},
default => "\n" . $value,
}
-class => $this->cssClasses('foswikiTextarea'),
-cols => $this->{cols},
-rows => $this->{rows},
-name => $this->{name},
-default => "\n" . $value,
)
);
}
Expand Down
95 changes: 61 additions & 34 deletions core/lib/Foswiki/Render/HTML.pm
Expand Up @@ -23,46 +23,73 @@ Render parent meta-data. Support for %META%.
=cut

sub textarea {
my ($ah) = @_;

my $cols = $ah->{cols};
my $disabled = $ah->{disabled};
my $name = $ah->{name};
my $readonly = $ah->{readonly};
my $rows = $ah->{rows};
my $style = $ah->{style};
my $class = $ah->{class};
my $id = $ah->{id};
my $default = $ah->{default};

#$default =~ s/([<>%'"])/'&#'.ord($1).';'/ge;

$default =~ s/&/&amp;/g;
$default =~ s/</&lt;/g;
$default =~ s/>/&gt;/g;
$default =~ s/"/&quot;/g;

print STDERR Data::Dumper::Dumper( \$ah );

my $html = '<textarea ';
$html .= "class='$class' " if $class;
$html .= "cols='$cols' ";
$html .= "name='$name' " if $name;
$html .= "readonly='readonly' " if $readonly;
$html .= "rows='$rows' ";
$html .= "style='$style' " if $style;
$html .= "id='$id'" if $id;

$html .= ">$default</textarea>";

return $html;
my %ah = @_;

my $class = $ah{'-class'} || '';
my $cols = $ah{'-cols'} || 20;
my $text = $ah{'-default'};
my $id = $ah{'-id'} || '';
my $name = $ah{'-name'} || '';
my $rows = $ah{'-rows'} || 4;
my $style = $ah{'-style'} || '';

my $disabled = ( $ah{'-disabled'} ) ? 'disabled' : '';
my $readonly = ( $ah{'-readonly'} ) ? 'readonly' : '';

#load the templates (relying on the system-wide skin path.)
my $session = $Foswiki::Plugins::SESSION;
$session->templates->readTemplate('html');
my $tmpl = $session->templates->expandTemplate('textarea');

$tmpl =~ s/%CLASS%/$class/;
$tmpl =~ s/%COLS%/$cols/;
$tmpl =~ s/%DISABLED%/$disabled/;
$tmpl =~ s/%ID%/$id/;
$tmpl =~ s/%NAME%/$name/;
$tmpl =~ s/%READONLY%/$readonly/;
$tmpl =~ s/%ROWS%/$rows/;
$tmpl =~ s/%STYLE%/$style/;

$text = Foswiki::entityEncode($text);
$tmpl =~ s/%TEXT%/$text/g;
return $tmpl;
}

sub textfield {
my %ah = @_;

my $class = $ah{'-class'} || '';
my $id = $ah{'-id'} || '';
my $name = $ah{'-name'} || '';
my $size = $ah{'-size'} || 20;
my $style = $ah{'-style'} || '';
my $value = $ah{'-value'} || '';

my $disabled = ( $ah{'-disabled'} ) ? 'disabled' : '';
my $readonly = ( $ah{'-readonly'} ) ? 'readonly' : '';

#load the templates (relying on the system-wide skin path.)
my $session = $Foswiki::Plugins::SESSION;
$session->templates->readTemplate('html');
my $tmpl = $session->templates->expandTemplate('textfield');

$tmpl =~ s/%CLASS%/$class/;
$tmpl =~ s/%DISABLED%/$disabled/;
$tmpl =~ s/%ID%/$id/;
$tmpl =~ s/%NAME%/$name/;
$tmpl =~ s/%READONLY%/$readonly/;
$tmpl =~ s/%SIZE%/$size/;
$tmpl =~ s/%STYLE%/$style/;
$tmpl =~ s/%VALUE%/$value/;

return $tmpl;
}

1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2008-2012 Foswiki Contributors. Foswiki Contributors
Copyright (C) 2008-2015 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
Expand Down
16 changes: 7 additions & 9 deletions core/lib/Foswiki/UI/View.pm
Expand Up @@ -488,15 +488,13 @@ sub view {
my $p = $session->{prefs};
use Foswiki::Render::HTML;
$page .= Foswiki::Render::HTML::textarea(
{
readonly => 'readonly',
rows => $p->getPreference('EDITBOXHEIGHT'),
cols => $p->getPreference('EDITBOXWIDTH'),
style => $p->getPreference('EDITBOXSTYLE'),
class => 'foswikiTextarea foswikiTextareaRawView',
id => 'topic',
default => $text
}
-readonly => 1,
-rows => $p->getPreference('EDITBOXHEIGHT'),
-cols => $p->getPreference('EDITBOXWIDTH'),
-style => $p->getPreference('EDITBOXSTYLE'),
-class => 'foswikiTextarea foswikiTextareaRawView',
-id => 'topic',
-default => $text
);
}
}
Expand Down
8 changes: 8 additions & 0 deletions core/templates/html.tmpl
@@ -0,0 +1,8 @@
%{
This is the 'master template' file for Foswiki HTML generation. The definitions
are expanded in Foswiki::Render::HTML. The default template name is the name of
called function: HTML::textarea() will expand the 'textarea' template by default.
---------------------------------------------------
}%
%TMPL:DEF{"textarea"}%<textarea rows='%ROWS%' cols='%COLS%' class='%CLASS%' name='%NAME%' id='%ID%' %READONLY% %DISABLED% style='%STYLE%'>%TEXT%</textarea>%TMPL:END%
%TMPL:DEF{"textfield"}%<input type='text' name='%NAME%' value='%VALUE%' size='%SIZE%' class='%CLASS%' id='%ID%' %READONLY% %DISABLED% />%TMPL:END%

0 comments on commit 203b59d

Please sign in to comment.