[Jifty-commit] r3768 - in jifty/trunk: . lib/Jifty/Plugin
lib/Jifty/Plugin/Chart/Renderer lib/Jifty/Plugin/Chart/Renderer/GD
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Fri Aug 3 12:40:37 EDT 2007
Author: sterling
Date: Fri Aug 3 12:40:37 2007
New Revision: 3768
Modified:
jifty/trunk/ (props changed)
jifty/trunk/lib/Jifty/Plugin/Chart.pm
jifty/trunk/lib/Jifty/Plugin/Chart/Renderer.pm
jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/Chart.pm
jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/GD/Graph.pm
jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/PlotKit.pm
jifty/trunk/lib/Jifty/Plugin/Chart/Web.pm
Log:
r8328 at riddle: andrew | 2007-08-03 11:40:04 -0500
Added a renderer parameter to the chart() method and add per-renderer initialization.
Modified: jifty/trunk/lib/Jifty/Plugin/Chart.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Chart.pm (original)
+++ jifty/trunk/lib/Jifty/Plugin/Chart.pm Fri Aug 3 12:40:37 2007
@@ -5,8 +5,9 @@
use base qw/ Jifty::Plugin Class::Accessor::Fast /;
use Jifty::Plugin::Chart::Web;
+use Scalar::Util qw/ blessed /;
-__PACKAGE__->mk_accessors(qw/ renderer /);
+__PACKAGE__->mk_accessors(qw/ renderer renderers /);
=head1 NAME
@@ -71,30 +72,52 @@
@_,
);
- if ( $args{renderer} !~ /::/ ) {
- $args{renderer} = __PACKAGE__.'::Renderer::'.$args{renderer};
- }
+ # Create the empty renderers list
+ $self->renderers({});
+
+ # Load the default renderer
+ $self->renderer( $self->init_renderer($args{renderer}) );
+
+ push @Jifty::Web::ISA, 'Jifty::Plugin::Chart::Web';
+}
+
+=head2 init_renderer
+
+ my $renderer = $chart_plugin->init_renderer($renderer_class)
+
+This is a helper method that is used by the API to initialize the renderer class. This is handled automatically so you probably shouldn't use this.
- eval "use $args{renderer}";
- warn $@ if $@;
- $self->renderer( $args{renderer} );
-
- if ( $self->renderer =~ 'PlotKit' ) {
- Jifty->web->add_external_javascript(qw(
- /static/js/mochikit.noexport.js
- /static/js/MochiKit/MochiKit.js
- ));
- Jifty->web->add_javascript(qw(
- PlotKit/excanvas.js
- PlotKit/PlotKit_Packed-20060807-custom.js
- ));
+=cut
+
+sub init_renderer {
+ my ($self, $renderer_class) = @_;
+
+ # If it's already an object, just return that
+ if ( blessed($renderer_class)
+ and $renderer_class->isa(__PACKAGE__.'::Renderer') ) {
+ return $renderer_class;
}
- else {
- Jifty->web->add_javascript('chart_img_behaviour.js');
+ # Prepend Jifty::Plugin::Chart::Renderer:: if we think we need to
+ if ( $renderer_class !~ /::/ ) {
+ $renderer_class = __PACKAGE__.'::Renderer::'.$renderer_class;
}
- push @Jifty::Web::ISA, 'Jifty::Plugin::Chart::Web';
+ # Check to see if we already loaded this one
+ my $renderer = $self->renderers->{ $renderer_class };
+ return $renderer if defined $renderer;
+
+ # Tell perl to load the class
+ $renderer_class->require;
+
+ # Initialize the renderer
+ $renderer = $renderer_class->new;
+
+ # Remember it
+ $self->renderers->{ $renderer_class } = $renderer;
+
+ # Return it!
+ return $renderer;
}
=head1 SEE ALSO
Modified: jifty/trunk/lib/Jifty/Plugin/Chart/Renderer.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Chart/Renderer.pm (original)
+++ jifty/trunk/lib/Jifty/Plugin/Chart/Renderer.pm Fri Aug 3 12:40:37 2007
@@ -20,15 +20,15 @@
package MyApp::Renderer;
use base qw/ Jifty::Plugin::Chart::Renderer /;
+ sub init {
+ my $self = shift;
+
+ # Handle any required initialization, like required CSS, JS, etc.
+ }
+
sub render {
my $self = shift;
- my %args = (
- type => 'points',
- width => 400,
- height => 300,
- data => [],
- @_,
- );
+ my %args = @_;
# Output your chart
Jifty->web->out( #{ Output your chart here... } );
@@ -41,6 +41,29 @@
Your renderer implementation must subclass this package and implement the following methods:
+=head2 new
+
+This is the constructor. Don't override this directly. Instead implement L</init>.
+
+=cut
+
+sub new {
+ my $class = shift;
+ my $self = bless {}, $class;
+ $self->init;
+ return $self;
+}
+
+=head2 init
+
+ $renderer->init();
+
+This is called by C<new> immediately after constructing the object. Subclasses should implement this method to do any required initialization such as letting Jifty know about required CSS files, JS files, etc.
+
+=cut
+
+sub init {}
+
=head2 render
Jifty->web->out($renderer->render(%args));
@@ -49,6 +72,10 @@
The C<render> method may either return it's output or print it out using L<Jifty::Web::out>.
+=cut
+
+sub render {}
+
=head1 SEE ALSO
L<Jifty::Plugin::Chart::Web>, L<Jifty::Plugin::Chart::Renderer::Chart>
Modified: jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/Chart.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/Chart.pm (original)
+++ jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/Chart.pm Fri Aug 3 12:40:37 2007
@@ -16,6 +16,16 @@
=head1 METHODS
+=head2 init
+
+Adds the F<chart_img_behaviour.js> script to those loaded.
+
+=cut
+
+sub init {
+ Jifty->web->add_javascript('chart_img_behaviour.js');
+}
+
=head2 render
Implemented the L<Jifty::Plugin::Chart::Renderer/render> method interface.
Modified: jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/GD/Graph.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/GD/Graph.pm (original)
+++ jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/GD/Graph.pm Fri Aug 3 12:40:37 2007
@@ -22,6 +22,16 @@
=head1 METHODS
+=head2 init
+
+Adds the F<chart_img_behaviour.js> script to those loaded.
+
+=cut
+
+sub init {
+ Jifty->web->add_javascript('chart_img_behaviour.js');
+}
+
=head2 render
Renders an IMG tag referring to the L<GD::Graph> image view.
Modified: jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/PlotKit.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/PlotKit.pm (original)
+++ jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/PlotKit.pm Fri Aug 3 12:40:37 2007
@@ -16,6 +16,23 @@
=head1 METHODS
+=head2 init
+
+Adds the various JavaScript files required to use PlotKit.
+
+=cut
+
+sub init {
+ Jifty->web->add_external_javascript(qw(
+ /static/js/mochikit.noexport.js
+ /static/js/MochiKit/MochiKit.js
+ ));
+ Jifty->web->add_javascript(qw(
+ PlotKit/excanvas.js
+ PlotKit/PlotKit_Packed-20060807-custom.js
+ ));
+}
+
=head2 render
Implemented the L<Jifty::Plugin::Chart::Renderer/render> method interface.
Modified: jifty/trunk/lib/Jifty/Plugin/Chart/Web.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Chart/Web.pm (original)
+++ jifty/trunk/lib/Jifty/Plugin/Chart/Web.pm Fri Aug 3 12:40:37 2007
@@ -77,6 +77,10 @@
This allows you to associated an additional class or classes to the element containing the chart. This can be a string containing on or more class names separated by spaces or an array of class names.
+=item renderer
+
+This allows you to use a different renderer than the one configured in F<config.yml>. Give the renderer as a class name, which will be initialized for you.
+
=back
Here's an example:
@@ -105,14 +109,18 @@
# TODO It might be a good idea to make this config.yml-able
# Setup the defaults
my %args = (
- type => 'points',
- width => undef,
- height => undef,
- data => [],
- class => [],
+ renderer => $plugin->renderer,
+ type => 'points',
+ width => undef,
+ height => undef,
+ data => [],
+ class => [],
@_,
);
+ # load the renderer
+ $args{renderer} = $plugin->init_renderer($args{renderer});
+
# canonicalize the width/height
$args{width} .= 'px' if looks_like_number($args{width});
$args{height} .= 'px' if looks_like_number($args{height});
@@ -130,8 +138,8 @@
$args{$key} = $args{$key}->(\%args) if ref $args{$key} eq 'CODE';
}
- # Call the rendering plugin's render method
- return $plugin->renderer->render(%args);
+ # Call the rendering class' render method
+ return $args{renderer}->render(%args);
}
=head1 CSS FOR CHARTS
More information about the Jifty-commit
mailing list