package RapidApp::View::JSON;
use Moose;
use namespace::autoclean;
BEGIN { extends 'Catalyst::View'; }
use RapidApp::JSON::MixedEncoder;
use Scalar::Util 'blessed', 'reftype';
use HTML::Entities;
us
e RapidApp::Util qw(:all);
=head1 NAME
RapidApp::View::JSON
=head1 DESCRIPTION
This view displays content as a JSON packet that will be used by RapidApp's
client-side javascript.
It also handles
*
so that the browser doesn't screw it up, and then the rendered text of the
HTML should be valid JSON that the Javascript uses.
This is also where error/exceptions are processed. This view used to h
:JSON::ScriptWithData;
use strict;
use warnings;
use Scalar::Util;
use RapidApp::JSON::MixedEncoder 'encode_json';
=head1 NAME
RapidApp::JSON::ScriptWithData
=head1 SYNOPSIS
use RapidApp::JSON:
:ScriptWithData;
use RapidApp::JSON::MixedEncoder 'encode_json';
$swd= RapidApp::JSON::ScriptWithData->new(
'function () {',
'blah(12345);',
'var b= foo(bar(', $self->getSomethi
return encode_json($swd);
=cut
sub new {
my ($class, @args)= @_;
return bless \@args, $class;
}
sub TO_JSON_RAW {
my $self= shift;
return join '', (map { ref $_? encode_json($_) : $_ } @$sel
idApp::JSON::MixedEncoder;
use strict;
use warnings;
use Scalar::Util 'blessed';
use Data::Dumper;
use base 'JSON::PP';
our @EXPORT = qw{
encode_json decode_json encode_json_utf8 decode_json_utf8
encode_json_ascii decode_json_ascii
};
# ---
# These are values that we might encounter as ScalarRefs and how to
# translate them into safe values for the JSON encoder. There are
# only a few cases
fied this is where they should be put.
# (Note: \0 and \1 are already handled and expected by the JSON encoder)
# (Note: vals are lc before being tested, so \'NULL' is already seen as \'null')
our %SC
kage RapidApp::JSON::RawJavascript;
use strict;
use warnings;
use Moose;
use overload '""' => sub { (shift)->stringify(@_) };
sub stringify {
my $self = shift;
return $self->TO_JSON_RAW;
}
has '
js' => ( is => 'rw', isa => 'Str' );
# compat with RapidApp::JSONFunc API:
sub func { (shift)->js }
around BUILDARGS => sub {
my $orig= shift;
my $class= shift;
if (scalar(@_) == 1 && !ref $_[0])
js => $_[0] ); # interpret single-param as a javascript fragment
} else {
return $class->$orig(@_);
}
};
sub TO_JSON_RAW {
return (shift)->js;
}
no Moose;
__PACKAGE__->meta->make_immutable;
1;
# make sure config_params is a string of JSON
if (ref $c->stash->{config_params}) {
$c->stash->{config_params}= RapidApp::JSON::MixedEncoder::encode_json($c->stash->{config_params});
}
if (
ref $c->stash->{panel_cfg}) {
$c->stash->{panel_cfg}= RapidApp::JSON::MixedEncoder::encode_json($c->stash->{panel_cfg});
}
return $self->next::method($c);
}
sub _get_asset_controller_urls {
dule' controller does:
$content_type = 'text/javascript; charset=utf-8';
$output = encode_json_ascii($cnf);
}
else {
# This is a direct browser call:
my $html = $self->_render
plate . '</div>',
#'<div class="template-format">' . $format . '</div>',
encode_json_ascii($meta),
'</div>',
(
$exists ?
'<div title="Edit \''
="display:none;">',
#'<div class="template-name">', $template, '</div>',
encode_json_ascii({
name => $template,
format => $self->Access->get_template_format($t
ord_url or defined $self->add_record_class) {
$self->add_listener( beforerender => RapidApp::JSONFunc->new( raw => 1, func =>
'Ext.ux.RapidApp.AppTab.cnt_init_loadTarget'
));
}
2Def:
#if (defined $self->open_record_url) {
# $self->add_listener( rowdblclick => RapidApp::JSONFunc->new( raw => 1, func =>
# 'Ext.ux.RapidApp.AppTab.gridrow_nav'
# ));
#}
# --
extconfig( init_quick_search_mode => $qs_mode );
}
#$self->apply_config(store => $self->JsonStore);
$self->apply_extconfig(tbar => $self->tbar_items) if (defined $self->tbar_items);
}
s
# make sure config_params is a string of JSON
if (ref $c->stash->{config_params}) {
$c->stash->{config_params}= RapidApp::JSON::MixedEncoder::encode_json($c->stash->{config_params});
}
return
s::Standard qw(:all);
use Scalar::Util qw(blessed);
use RapidApp::Util::MetaKeys::FK;
use JSON qw( from_json -support_by_pp );
use Path::Class qw( file dir );
use Try::Tiny;
sub load {
my ($self,
from_string {
my ($self, $string) = @_;
my $data = scalar(
# Assume JSON as the first format
try{ from_json($string, {allow_singlequote => 1, allow_barekey => 1}) } ||
# free-f
="display:none;">',
#'<div class="template-name">', $template, '</div>',
encode_json_ascii({
name => $template,
format => $self->Access->get_template_format($t
per::Concise 'Dumper';
use RapidApp::RootModule;
use Clone qw(clone);
use RapidApp::JSON::MixedEncoder qw(encode_json);
use Try::Tiny;
use Time::HiRes qw(gettimeofday tv_interval);
sub scream {
loca
out => undef,
newline => 0,
list_args => 0,
list_out => 0,
dump_maxdepth => 3,
use_json => 0,
stack => 0,
instance => 0,
color => GREEN,
ret_color => RED.BOLD,
arg_igno
eturn Dumper(@_) unless ($opt{use_json});
#return RapidApp::JSON::MixedEncoder->new->allow_blessed->convert_blessed->allow_nonref->encode(\@_);
return encode_json(\@_);
} unless ($opt{dump_func})
rm::ANSIColor qw(:constants);
use RapidApp::JSON::MixedEncoder qw(
encode_json decode_json encode_json_utf8 decode_json_utf8 encode_json_ascii decode_json_ascii
);
use RapidApp::Util::Hash::Merge q
finally
gettimeofday tv_interval
Dumper
encode_json decode_json encode_json_utf8 decode_json_utf8 encode_json_ascii decode_json_ascii
merge
);
push @EXPORT_OK, @{$Term::ANSIColor
use RapidApp::Responder::InfoStatus;
use RapidApp::JSONFunc;
use RapidApp::JSON::MixedEncoder;
use RapidApp::JSON::RawJavascript;
use RapidApp::JSON::ScriptWithData;
use RapidApp::HTML::RawHtml;
use
',''
);
1;
__END__
use Exporter qw( import );
use Data::Dumper;
use RapidApp::JSON::MixedEncoder;
use RapidApp::JSON::RawJavascript;
use RapidApp::HTML::RawHtml;
use RapidApp::Responder::UserError;
ORT = qw(
asjson rawjs mixedjs ashtml rawhtml usererr userexception
jsfunc blessed merge infostatus clone
);
# Module shortcuts
#
# JSON shortcuts
#
# encode the object into JSON text, with a
b asjson {
scalar(@_) == 1 or die "Expected single argument";
return RapidApp::JSON::MixedEncoder::encode_json($_[0]);
}
# Bless a string as RawJavascript so that it doesn't get encoded as JSON dat
tv_interval);
use Catalyst::Utils;
use Scalar::Util qw(blessed weaken);
use RapidApp::JSONFunc;
use RapidApp::JSON::MixedEncoder;
use RapidApp::Util qw(:all);
has 'base_params' => ( is => 'r
ams->{'X-RapidApp-CustomPrompt-Data'};
return {} unless (defined $rawdata);
return $self->json->decode($rawdata);
}
);
##################################
#### Original Controller Role ####
ult => sub {{}} );
has 'default_action' => ( is => 'ro', default => undef );
has 'render_as_json' => ( is => 'rw', default => 1, traits => [ 'RapidApp::Role::PerRequestBuildDefReset' ] );
package RapidApp::JSONFunc;
use strict;
use warnings;
use Moose;
# This object allows returning functions within JSON. To prevent the function from being
# quoted (i.e. turned into a string), thi
be encoded with RapidApp::JSON::MixedEncoder
# which extends JSON::PP and modifies the behavior to return TO_JSON_RAW as-is
use RapidApp::Util qw(:all);
use RapidApp::JSON::MixedEncoder;
has 'func'
=> 'ro', default => 0 );
has 'json' => ( is => 'ro', lazy_build => 1 );
sub _build_json {
my $self = shift;
return RapidApp::JSON::MixedEncoder->new;
}
sub TO_JSON {
my $self = shift;
return $
mport::Into;
use Time::HiRes qw(gettimeofday tv_interval);
use HTTP::Request::Common;
use JSON qw(decode_json);
use Catalyst::Utils;
use RapidApp::Test::Client;
my $target;
my $app_class;
sub impor
s 'DataStore' => (
is => 'rw',
isa => 'RapidApp::Module::DatStor',
handles => {
JsonStore => 'JsonStore',
# store_read => 'store_read',
# store_read_raw => 'store_read_raw',
the 'store'
# key predeclared as the first key. Because the ExtJS client decodes and processes
# JSON in order, we want to make sure the store is processed before other parts
# which may need to refe
DataStore->getStore_func );
}
else {
$self->apply_extconfig( store => $self->Module('store')->JsonStore );
}
}
has 'add_edit_formpanel_defaults', is => 'ro', isa => 'HashRef', lazy => 1, defaul
r::InfoStatus';
calleruse 'RapidApp::JSONFunc';
calleruse 'RapidApp::JSON::MixedEncoder';
calleruse 'RapidApp::JSON::RawJavascript';
calleruse 'RapidApp::JSON::ScriptWithData';
calleruse 'RapidAp
resenting the event name, and the
# remaining elements representing 1 or more handlers (RapidApp::JSONFunc
# objects). Unlike "listeners" which is a built-in config param associated
# with all Ext.Obs
le::PerRequestBuildDefReset'
],
is => 'ro',
isa => 'HashRef[ArrayRef[RapidApp::JSONFunc]]',
default => sub { {} },
handles => {
apply_listener_callbacks => 'set',
$list, $func;
}
else {
# Auto convert strings into RapidApp::JSONFunc objects:
push @$list, RapidApp::JSONFunc->new( raw => 1, func => $func );
}
}
return $self->apply_l