package CGI::Lazy::Javascript; use strict; use CGI::Lazy::Globals; use CGI::Lazy::Javascript::JSONParser; use JavaScript::Minifier qw(minify); #javascript for ajax requests our $AJAXJS = q[ function ajaxSend(request, outgoing, returnHandler, returnTarget) { try { request = new XMLHttpRequest(); browser = "standards-compliant"; } catch (err) { try { request = new ActiveXObject("Msxml12.XMLHTTP"); browser = "bogus"; } catch (err) { try { request = new ActiveXObject("Microsoft.XMLHTTP"); browser = "bogus"; } catch (err) { alert("your browser doesn't support AJAX, try upgrading to Firefox"); request = null; } } } try { request.open('POST',parent.location,true); } catch (err) { alert("AJAX call failed: "+ err); } request.setRequestHeader('Content-Type', 'application/json'); request.send(JSON.stringify(outgoing)); request.onreadystatechange = function() { if (request.readyState == 4) { returnHandler(request.status, request.responseText, returnTarget); } } } ]; #javascript for sjax requests our $SJAXJS = q[ function sjaxSend(request, outgoing, returnHandler) { try { request = new XMLHttpRequest(); browser = "standards-compliant"; } catch (err) { try { request = new ActiveXObject("Msxml12.XMLHTTP"); browser = "bogus"; } catch (err) { try { request = new ActiveXObject("Microsoft.XMLHTTP"); browser = "bogus"; } catch (err) { alert("your browser doesn't support AJAX, try upgrading to Firefox"); request = null; } } } try { request.open('POST',parent.location,false); } catch (err) { alert("AJAX call failed: "+ err); } request.setRequestHeader('Content-Type', 'application/json'); request.send(JSON.stringify(outgoing)); returnHandler(request.status, request.responseText); } ]; #javascript for Widget::Dataset our $DatasetJS = < $DatasetJS, 'CGI::Lazy::Widget::DomLoader' => $DOMLOADJS, 'CGI::Lazy::Widget::Composite' => $COMPJS, 'CGI::Lazy::Widget::Controller' => $CONTROLLERJS, ); #------------------------------------------------------------------------------------------------- sub dir { my $self = shift; return $self->{_dir}; } #------------------------------------------------------------------------------------------------- sub file { my $self = shift; my $file = shift; my $dir = $self->dir; return "$dir/$file"; } #------------------------------------------------------------------------------------------------- sub q { my $self = shift; return $self->{_q}; } #---------------------------------------------------------------------------------------- sub load { my $self = shift; my $file = shift; my $jsdir = $self->dir; $jsdir =~ s/^\///; #strip a leading slash so we don't double it my $docroot = $ENV{DOCUMENT_ROOT}; $docroot =~ s/\/$//; #strip the trailing slash so we don't double it open IF, "< $docroot/$jsdir/$file" or $self->q->errorHandler->couldntOpenJsFile($docroot, $jsdir, $file, $!); my $script = minify(input => *IF); close IF; return $self->q->jswrap($script); } #------------------------------------------------------------------------------------------------- sub modules { my $self = shift; my @args = @_; my $output = $JSONPARSER . minify(input => $AJAXJS). minify(input => $SJAXJS); if (@args) { my $inc = {}; $self->parsewidget($inc, $_) foreach @args; $output .= minify(input => $component{$_}) foreach keys %$inc; } return $self->q->jswrap($output); } #------------------------------------------------------------------------------------------------- sub parsewidget { my $self = shift; my $list = shift; my $widget = shift; if (ref $widget eq 'CGI::Lazy::Widget::Composite') { $list->{ref $widget} = 1; $self->parsewidget($list, $_) for @{$widget->memberarray}; } else { $list->{ref $widget} = 1; } return; } #------------------------------------------------------------------------------------------------- sub new { my $class = shift; my $q = shift; return bless { _q => $q, _dir => $q->config->jsDir, }, $class; } 1 __END__ =head1 LEGAL #=========================================================================== Copyright (C) 2008 by Nik Ogura. All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Bug reports and comments to nik.ogura@gmail.com. #=========================================================================== =head1 NAME CGI::Lazy::Javascript =head1 SYNOPSIS use CGI::Lazy; my $q = CGI::Lazy->new(); my $widget1 = $q->ajax->activeDataSet({...}); my $widget2 = $q->ajax->activeDataSet({...}); print $q->header, $q->javascript->modules($widget1, $widget2); print $q->javascript->load('somefile.js'); =head2 DESCRIPTION CGI::Lazy::Javascript is predominately a javascript container. It holds the js modules necessary for widgets to function, and outputs them for printing to the browser. It also has some convenience methods for loading javascript files =head1 METHODS =head2 dir () Returns directory path where javascript files can be found as specified on Lazy object creation. =head2 q ( ) Returns CGI::Lazy object. =head2 modules ( components ) Returns javascript for parsing JSON and making ajax calls, as well as the clientside goodness for the ajax widgets. This method needs to be printed on any page that is going to use JSON or the Widget objects.. Its included as a separate method as it should be sent only once per page, and would be included in the header except this would be an irritation for cases where CGI::Lazy is not using Widget objects. If called without components, it will send out only the defaults listed below. =head3 components List of widgets whose javascript needs to be loaded. JSON parser, ajaxSend, and sjaxSend are exported by default, the rest is on a per widget basis. The modules method is smart enough to only output the necessary code for a given type of module once. Multiple widgets of the same type will not result in the same code being printed over and over. For composite widgets, it loads each constituent widget in turn. =head2 load (file) Reads file from jss directory , wraps in script tags for output to browser =head3 file filename of js file =head2 file (js) Returns absolute path to file css parsed with document root and css directory =head3 jss Javascript file name =head2 new ( q ) constructor. =head3 q CGI::Lazy object =cut