head tag improvements

This commit is contained in:
JT Smith 2004-06-30 00:20:32 +00:00
parent f18f408efb
commit eab5fc8f15
5 changed files with 156 additions and 65 deletions

View file

@ -36,6 +36,15 @@
- Rmoved the httpHeader, httpRedirect, and setCookie subs from
WebGUI::Session and created a new class called WebGUI::HTTP for them. See
docs/migration.txt for details.
- The USS automatically adds an RSS feed link tag to the head of any page
it's on.
- Added a new API for dynamically adding javascript, css, xml, rss, and meta
information to the page. See WebGUI::Style for details.
- Fixed a long standing annoyance that javascripts used by the WebGUI::Form
would be loaded multiple times if there were multiple fields of the same
type in the same form.
- Sped up the page editing page by 50% by rearranging the javascript on the
page.
6.0.3

View file

@ -20,6 +20,7 @@ use WebGUI::DateTime;
use WebGUI::International;
use WebGUI::Session;
use WebGUI::SQL;
use WebGUI::Style;
use WebGUI::Template;
use WebGUI::URL;
@ -74,10 +75,6 @@ All of the functions in this package accept the input of a hash reference contai
=cut
#-------------------------------------------------------------------
sub _cssFile {
return '<link rel="stylesheet" type="text/css" media="all" href="'.$session{config}{extrasURL}.'/'.$_[0].'" />'."\n";
}
#-------------------------------------------------------------------
sub _fixMacros {
@ -108,10 +105,6 @@ sub _fixTags {
return $value;
}
#-------------------------------------------------------------------
sub _javascriptFile {
return '<script language="JavaScript" src="'.$session{config}{extrasURL}.'/'.$_[0].'"></script>'."\n";
}
#-------------------------------------------------------------------
@ -394,18 +387,17 @@ By default a date is placed in the "value" field. Set this to "1" to turn off th
sub date {
my $value = epochToSet($_[0]->{value}) unless ($_[0]->{noDate} && $_[0]->{value} eq '');
my $size = $_[0]->{size} || 10;
my $output = _cssFile("calendar/calendar-win2k-1.css")
._javascriptFile('calendar/calendar.js')
._javascriptFile('calendar/lang/calendar-en.js')
._javascriptFile('calendar/calendar-setup.js');
$output .= text({
WebGUI::Style::setScript($session{config}{extrasURL}.'/calendar/calendar.js',{ language=>'javascript' });
WebGUI::Style::setScript($session{config}{extrasURL}.'/calendar/lang/calendar-en.js',{ language=>'javascript' });
WebGUI::Style::setScript($session{config}{extrasURL}.'/calendar/calendar-setup.js',{ language=>'javascript' });
WebGUI::Style::setLink($session{config}{extrasURL}.'/calendar/calendar-win2k-1.css', { rel=>"stylesheet", type=>"text/css", media=>"all" });
return text({
name=>$_[0]->{name},
value=>$value,
size=>$size,
extras=>'id="'.$_[0]->{name}.'Id" '.$_[0]->{extras},
maxlength=>10
});
$output .= '<script type="text/javascript">
}) . '<script type="text/javascript">
Calendar.setup({
inputField : "'.$_[0]->{name}.'Id",
ifFormat : "%Y-%m-%d",
@ -414,7 +406,6 @@ sub date {
mondayFirst : false
});
</script>';
return $output;
}
@ -445,18 +436,17 @@ Extra parameters to add to the date/time form element such as javascript or styl
sub dateTime {
my $value = epochToSet($_[0]->{value},1);
my $output = _cssFile("calendar/calendar-win2k-1.css")
._javascriptFile('calendar/calendar.js')
._javascriptFile('calendar/lang/calendar-en.js')
._javascriptFile('calendar/calendar-setup.js');
$output .= text({
WebGUI::Style::setScript($session{config}{extrasURL}.'/calendar/calendar.js',{ language=>'javascript' });
WebGUI::Style::setScript($session{config}{extrasURL}.'/calendar/lang/calendar-en.js',{ language=>'javascript' });
WebGUI::Style::setScript($session{config}{extrasURL}.'/calendar/calendar-setup.js',{ language=>'javascript' });
WebGUI::Style::setLink($session{config}{extrasURL}.'/calendar/calendar-win2k-1.css', { rel=>"stylesheet", type=>"text/css", media=>"all" });
return text({
name=>$_[0]->{name},
value=>$value,
size=>19,
extras=>'id="'.$_[0]->{name}.'Id" '.$_[0]->{extras},
maxlength=>19
});
$output .= '<script type="text/javascript">
}) . '<script type="text/javascript">
Calendar.setup({
inputField : "'.$_[0]->{name}.'Id",
ifFormat : "%Y-%m-%d %H:%M:%S",
@ -465,7 +455,6 @@ sub dateTime {
mondayFirst : false
});
</script>';
return $output;
}
@ -505,9 +494,8 @@ The number of characters wide this form element should be. There should be no re
=cut
sub email {
my ($output);
$output = _javascriptFile('emailCheck.js');;
$output .= text({
WebGUI::Style::setScript($session{config}{extrasURL}.'/emailCheck.js',{ language=>'javascript' });
my $output .= text({
name=>$_[0]->{name},
value=>$_[0]->{value},
size=>$_[0]->{size},
@ -771,15 +759,14 @@ The number of characters wide this form element should be. There should be no re
sub float {
my $value = $_[0]->{value} || 0;
my $size = $_[0]->{size} || 11;
my $output = _javascriptFile('inputCheck.js');
$output .= text({
WebGUI::Style::setScript($session{config}{extrasURL}.'/inputCheck.js',{ language=>'javascript' });
return text({
name=>$_[0]->{name},
value=>$value,
size=>$size,
extras=>'onKeyUp="doInputCheck(this.form.'.$_[0]->{name}.',\'0123456789.\')" '.$_[0]->{extras},
maxlength=>$_[0]->{maxlength}
});
return $output;
}
@ -1030,18 +1017,16 @@ The number of characters wide this form element should be. There should be no re
=cut
sub integer {
my ($output, $size, $value);
$value = $_[0]->{value} || 0;
$size = $_[0]->{size} || 11;
$output = _javascriptFile('inputCheck.js');
$output .= text({
my $value = $_[0]->{value} || 0;
my $size = $_[0]->{size} || 11;
WebGUI::Style::setScript($session{config}{extrasURL}.'/inputCheck.js',{ language=>'javascript' });
return text({
name=>$_[0]->{name},
value=>$value,
size=>$size,
extras=>'onKeyUp="doInputCheck(this.form.'.$_[0]->{name}.',\'0123456789-\')" '.$_[0]->{extras},
maxlength=>$_[0]->{maxlength}
});
return $output;
}
#-------------------------------------------------------------------
@ -1178,16 +1163,15 @@ The number of characters wide this form element should be. There should be no re
=cut
sub phone {
my $output = _javascriptFile('inputCheck.js');
WebGUI::Style::setScript($session{config}{extrasURL}.'/inputCheck.js',{ language=>'javascript' });
my $maxLength = $_[0]->{maxLength} || 30;
$output .= text({
return text({
name=>$_[0]->{name},
maxlength=>$maxLength,
extras=>'onKeyUp="doInputCheck(this.form.'.$_[0]->{name}.',\'0123456789-()+ \')" '.$_[0]->{extras},
value=>$_[0]->{value},
size=>$_[0]->{size}
});
return $output;
}
#-------------------------------------------------------------------
@ -1540,8 +1524,8 @@ The number of characters wide this form element should be. There should be no re
sub timeField {
my $value = WebGUI::DateTime::secondsToTime($_[0]->{value});
my $output = _javascriptFile('inputCheck.js');
$output .= text({
WebGUI::Style::setScript($session{config}{extrasURL}.'/inputCheck.js',{ language=>'javascript' });
my $output = text({
name=>$_[0]->{name},
value=>$value,
size=>$_[0]->{size} || 8,
@ -1591,15 +1575,14 @@ The number of characters wide this form element should be. There should be no re
sub url {
my $maxLength = $_[0]->{maxlength} || 2048;
my $output = _javascriptFile('addHTTP.js');
$output .= text({
WebGUI::Style::setScript($session{config}{extrasURL}.'/addHTTP.js',{ language=>'javascript' });
return text({
name=>$_[0]->{name},
value=>$_[0]->{value},
extras=>$_[0]->{extras}.' onBlur="addHTTP(this.form.'.$_[0]->{name}.')"',
size=>$_[0]->{size},
maxlength=>$maxLength
});
return $output;
}
#-------------------------------------------------------------------
@ -1730,16 +1713,15 @@ The number of characters wide this form element should be. There should be no re
=cut
sub zipcode {
my $output = _javascriptFile('inputCheck.js');
WebGUI::Style::setScript($session{config}{extrasURL}.'/inputCheck.js',{ language=>'javascript' });
my $maxLength = $_[0]->{maxLength} || 10;
$output .= text({
return text({
name=>$_[0]->{name},
maxlength=>$maxLength,
extras=>'onKeyUp="doInputCheck(this.form.'.$_[0]->{name}.',\'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ- \')" '.$_[0]->{extras},
value=>$_[0]->{value},
size=>$_[0]->{size}
});
return $output;
}

View file

@ -18,6 +18,7 @@ use warnings;
use HTML::Template;
use strict;
use Tie::IxHash;
use WebGUI::DateTime;
use WebGUI::ErrorHandler;
use WebGUI::Grouping;
use WebGUI::HTMLForm;
@ -27,9 +28,9 @@ use WebGUI::Macro;
use WebGUI::Persistent::Tree;
use WebGUI::Session;
use WebGUI::SQL;
use WebGUI::Style;
use WebGUI::Template;
use WebGUI::Utility;
use WebGUI::DateTime;
our @ISA = qw(WebGUI::Persistent::Tree);
@ -388,6 +389,11 @@ Generates the content of the page.
sub generate {
return WebGUI::Privilege::noAccess() unless (canView());
my %var;
if ($session{page}{defaultMetaTags}) {
WebGUI::Style::setMeta({'http-equiv'=>"Keywords", name=>"Keywords", content=>join(",",$session{page}{title},$session{page}{menuTitle})});
WebGUI::Style::setMeta({'http-equiv'=>"Description", name=>"Description", content=>$session{page}{synopsis}}) if ($session{page}{synopsis});
}
WebGUI::Style::setRawHeadTags($session{page}{metaTags});
if ($session{page}{redirectURL} && !$session{var}{adminOn}) {
WebGUI::HTTP::setRedirect(WebGUI::Macro::process($session{page}{redirectURL}));
}

View file

@ -125,10 +125,10 @@ sub process {
$var{'head.tags'} .= ' />'."\n";
}
# generate additional javascript tags
foreach my $url (keys %{$session{page}{head}{javascript}}) {
$var{'head.tags'} .= '<script src="'.$url.'"';
foreach my $name (keys %{$session{page}{head}{javascript}{$url}}) {
$var{'head.tags'} .= ' '.$name.'="'.$session{page}{head}{javascript}{$url}{$name}.'"';
foreach my $tag (@{$session{page}{head}{javascript}}) {
$var{'head.tags'} .= '<script';
foreach my $name (keys %{$tag}) {
$var{'head.tags'} .= ' '.$name.'="'.$tag->{$name}.'"';
}
$var{'head.tags'} .= '></script>'."\n";
}
@ -140,7 +140,6 @@ sub process {
}
$var{'head.tags'} .= ' />'."\n";
}
$var{'head.tags'} .= $session{page}{metaTags};
if ($session{var}{adminOn}) {
# This "triple incantation" panders to the delicate tastes of various browsers for reliable cache suppression.
$var{'head.tags'} .= '
@ -149,20 +148,113 @@ sub process {
<meta http-equiv="Expires" content="0" />
';
}
if ($session{page}{defaultMetaTags}) {
$var{'head.tags'} .= '
<meta http-equiv="Keywords" name="Keywords" content="'.$session{page}{title}.', '.$session{setting}{companyName}.'" />
';
if ($session{page}{synopsis}) {
$var{'head.tags'} .= '
<meta http-equiv="Description" name="Description" content="'.$session{page}{synopsis}.'" />
';
}
}
return WebGUI::Template::process($templateId,"style",\%var);
}
#-------------------------------------------------------------------
=head2 setLink ( url, params )
Sets a <link> tag into the <head> of this rendered page for this page view. This is typically used for dynamically adding references to CSS and RSS documents.
=over
=item url
The URL to the document you are linking.
=item params
A hash reference containing the other parameters to be included in the link tag, such as "rel" and "type".
=back
=cut
sub setLink {
my $url = shift;
my $params = shift;
$session{page}{head}{link}{$url} = $params;
}
#-------------------------------------------------------------------
=head2 setMeta ( params )
Sets a <meta> tag into the <head> of this rendered page for this page view.
=over
=item params
A hash reference containing the parameters of the meta tag.
=back
=cut
sub setMeta {
my $params = shift;
push(@{$session{page}{head}{meta}},$params);
}
#-------------------------------------------------------------------
=head2 setRawHeadTags ( tags )
Sets data to be output into the <head> of the current rendered page for this page view.
=over
=item tags
A raw string containing tags. This is just a raw string so you must actually pass in the full tag to use this call.
=back
=cut
sub setRawHeadTags {
my $tags = shift;
$session{page}{head}{raw} .= $tags;
}
#-------------------------------------------------------------------
=head2 setScript ( url, params )
Sets a <script> tag into the <head> of this rendered page for this page view. This is typically used for dynamically adding references to Javascript or ECMA script.
=over
=item url
The URL to your script.
=item params
A hash reference containing the additional parameters to include in the script tag, such as "type" and "language".
=back
=cut
sub setScript {
my $url = shift;
my $params = shift;
$params->{src} = $url;
my $found = 0;
foreach my $script (@{$session{page}{head}{javascript}}) {
$found = 1 if ($script->{src} eq $url);
}
push(@{$session{page}{head}{javascript}},$params) unless ($found);
}
1;

View file

@ -27,8 +27,9 @@ use WebGUI::Operation;
use WebGUI::Paginator;
use WebGUI::Privilege;
use WebGUI::Session;
use WebGUI::Template;
use WebGUI::SQL;
use WebGUI::Style;
use WebGUI::Template;
use WebGUI::URL;
use WebGUI::User;
use WebGUI::Utility;
@ -657,6 +658,7 @@ sub www_view {
$var{"search.Form"} = WebGUI::Search::form({wid=>$_[0]->get("wobjectId"),func=>'view',search=>1});
$var{"search.url"} = WebGUI::Search::toggleURL("wid=".$_[0]->get("wobjectId")."&func=view");
$var{"rss.url"} = WebGUI::URL::page('func=viewRSS&wid='.$_[0]->get("wobjectId"));
WebGUI::Style::setLink($var{"rss.url"},{ rel=>'alternate', type=>'application/rss+xml', title=>'RSS' });
if ($session{scratch}{search}) {
$numResults = $session{scratch}{numResults};
$constraints = WebGUI::Search::buildConstraints([qw(username title content)]);