Skip to content

Commit

Permalink
r20761@GOP: gugod | 2008-02-07 04:58:15 +0800
Browse files Browse the repository at this point in the history
 self->{target} is removed. That's simply a duplicate
 of self->{object}. Since it is also make perfect sense to interpret
 js("3s")->foo() as: Invoking foo method on "3s" object.
 
 Also, remove if-else-elsif functions. Those are mind-less design.
 "do" method is the new way.
 


git-svn-id: http://code.handlino.com/svn/perl5/JavaScript-Writer@201 b19cb30a-9f2e-4084-8847-5e1a13269302
  • Loading branch information
gugod committed Feb 8, 2008
1 parent 2d587e9 commit 8bd18eb
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 70 deletions.
83 changes: 36 additions & 47 deletions lib/JavaScript/Writer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ sub _js {
sub js {
my $js = _js();

my ($target) = @_;
my ($obj) = @_;
if (defined $js) {
$js->{target} = $target if defined $target;
$js->{object} = $obj if defined $obj;
return $js;
}

$base = JavaScript::Writer->new() unless defined $base;
$base->{target} = $target if defined $target;
$base->{object} = $obj if defined $obj;
return $base;
}

Expand All @@ -78,7 +79,7 @@ sub call {
args => \@args,
end_of_call_chain => (!defined wantarray)
};
delete self->{target};

return self;
}

Expand All @@ -102,7 +103,7 @@ sub object {
sub latter {
my ($cb) = args;

my $timeout = delete self->{target};
my $timeout = delete self->{object};
$timeout =~ s/ms$//;
$timeout =~ s/s$/000/;

Expand Down Expand Up @@ -171,32 +172,19 @@ sub var {

use JavaScript::Writer::Block;

sub while {
my ($self, $condition, $block) = @_;
my $b = JavaScript::Writer::Block->new;
$b->body($block);
$self->append("while(${condition})${b}")
}

sub if {
my ($self, $condition, $block) = @_;
sub do {
my ($block) = args;
my $condition = delete self->{object};
my $b = JavaScript::Writer::Block->new;
$b->body($block);
$self->append("if(${condition})${b}", delimiter => "\n")
self->append("if($condition)${b}", delimiter => "" );
}

sub elsif {
sub while {
my ($self, $condition, $block) = @_;
my $b = JavaScript::Writer::Block->new;
$b->body($block);
$self->append("else if(${condition})${b}", delimiter => "\n");
}

sub else {
my ($self, $block) = @_;
my $b = JavaScript::Writer::Block->new;
$b->body($block);
$self->append("else${b}", delimiter => "\n");
$self->append("while(${condition})${b}", , delimiter => "" );
}

use JavaScript::Writer::Function;
Expand Down Expand Up @@ -240,9 +228,10 @@ sub as_string {
my $ret = "";

for (@{self->{statements}}) {
my $delimiter =
defined($_->{delimiter}) ? $_->{delimiter} : ($_->{end_of_call_chain} ? ";" : ".");
if (my $f = $_->{call}) {
my $delimiter = $_->{delimiter} ||
($_->{end_of_call_chain} ? ";" : ".");

my $args = $_->{args};
$ret .= ($_->{object} ? "$_->{object}." : "" ) .
"$f(" .
Expand All @@ -253,9 +242,8 @@ sub as_string {
) . ")" . $delimiter
}
elsif (my $c = $_->{code}) {
my $delimiter = $_->{delimiter} || ";";
$c .= $delimiter
unless $c =~ /$delimiter\s*$/s;
$delimiter = defined $_->{delimiter} ? $_->{delimiter} : ";";
$c .= $delimiter unless $c =~ /$delimiter\s*$/s;
$ret .= $c;
}
}
Expand All @@ -272,12 +260,6 @@ sub AUTOLOAD {
my $function = $AUTOLOAD;
$function =~ s/.*:://;

if (defined $self->{target}) {
if (!defined $self->{object}) {
$self->{object} = $self->{target}
}
}

return $self->call($function, @_);
}

Expand Down Expand Up @@ -315,7 +297,7 @@ use its C<call> method to call a certain functions from your library.
=over
=item js( [ $target ] )
=item js( [ $object ] )
This function is exported by default to your namespace. Is the spiffy
ultimate entry point for generating all kinds of javascripts.
Expand All @@ -324,14 +306,20 @@ C<js> represents a singleton object of all namespace. Unless used in a
subroutine passed to construct a JavaScript function, it always refers
to the same C<JavaScript::Writer> object.
It optionally takes a C<$target> parameter that represents something
It optionally takes a C<$object> parameter that represents something
on which you can perform some action. For example, here's the sweet
way to do C<setTimeout>:
js("3s")->latter(sub{
js->say('something');
});
Or usually it can just be a normal object:
js("Widget.Lightbox")->show( $content );
# Widget.Lightbox.show( ... )
=item new()
Object constructor. Takes nothing, gives you an javascript writer
Expand Down Expand Up @@ -441,20 +429,21 @@ The output of 'while' statement look like this:
$code
}
=item if ( $codnition => $code_ref )
=item do ( $code_ref )
=item elsif ( $codnition => $code_ref )
C<do> method acts like C<if>:
=item else ( $code_ref )
js("foo")->do(sub {
....
});
That code generate this javascript:
These 3 methods forms a trinity to construct the if..elsif..else form
of control structure. Of course, in JavaScript, it it's not called
"elsif", but "else if". But hey, we're Perl people.
if(foo) {
....
}
The arguements are pretty similar to C<while> method. But these
function use newline characters to deliminate them from others rather
then using ";". That's the major difference between them and all
other methods.
NOTICE: The old if-else-elsif methods are removed due to their stupid looking design.
=item function( $code_ref )
Expand Down
4 changes: 2 additions & 2 deletions t/09.while.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ plan tests => 2;

$js->while(1 => sub {});

is $js, "while(1){};", "an empty while loop";
is $js, "while(1){}", "an empty while loop";
}


{
my $js = JavaScript::Writer->new();
$js->while(1 => sub { $_[0]->alert("Nihao") });

is $js, 'while(1){alert("Nihao");};', "a simple while loop";
is $js, 'while(1){alert("Nihao");}', "a simple while loop";
}
21 changes: 20 additions & 1 deletion t/200.new_wave.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,26 @@ use warnings;
use JavaScript::Writer;
use Test::More;

plan tests => 9;
plan tests => 11;

{
js->new;
js("true")->do(sub {
js->alert(42);
});

is js->as_string, "if(true){alert(42);}";
}


{
js->new;
js->while("true", sub {
js->alert(42);
});

is js->as_string, qq{while(true){alert(42);}};
}

{
js->new;
Expand Down
23 changes: 3 additions & 20 deletions t/51.control.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,10 @@ use warnings;
use JavaScript::Writer;
use Test::More;

plan tests => 3;
plan tests => 1;

{
my $js = JavaScript::Writer->new;
$js->if( 1 => sub { $_[0]->alert(42) });
is $js, "if(1){alert(42);}\n"
}

{
my $js = JavaScript::Writer->new;
$js->if(1 => sub { $_[0]->alert(42) })
->else(sub {$_[0]->alert(43)});

is $js, "if(1){alert(42);}\nelse{alert(43);}\n"
}

{
my $js = JavaScript::Writer->new;
$js->if(1 => sub { $_[0]->alert(42) })
->elsif(2 => sub {$_[0]->alert(43)})
->else(sub {$_[0]->alert(44)});

is $js, "if(1){alert(42);}\nelse if(2){alert(43);}\nelse{alert(44);}\n"
$js->object(1)->do(sub { $_[0]->alert(42) });
is $js, "if(1){alert(42);}"
}

0 comments on commit 8bd18eb

Please sign in to comment.