\documentclass{article}
\usepackage{amsmath}
\usepackage
    [tight,
     dvipsone, % <- replace with: dvipsone, dvips, pdftex, dvipdfm
     designi,
     usetemplates,
     nodirectory
    ]{web}
\usepackage[execJS]{exerquiz}

\title{The \texorpdfstring{\texttt{insDLJS}}{insDLJS} Package: Demo the
\texorpdfstring{\texttt{execJS}}{execJS} and \texorpdfstring{\texttt{defineJS}}{defineJS} Environments}
\author{D. P. Story}
\subject{Sample file}
\keywords{LaTeX, PDF, derivative, calculus, JavaScript}

\university{THE UNIVERSITY OF AKRON\\
Theoretical and Applied Mathematics}
\email{dpstory@uakron.edu}
\version{3.0}
\copyrightyears{1999-\the\year}

\makeatletter
\renewcommand\titlepageTrailer
{%
    \copyright\ \webcopyrightyears\ \href{mailto:\webemail}{\webemail}\hfill Date: \@date
}

\makeatother

\hyphenation{Java-Script}

\textBgColor{cornsilk}

\newenvironment{sverbatim}
{\par\footnotesize\verbatim}{\endverbatim\noindent}
\newcommand{\cs}[1]{\texttt{\char`\\#1}}

\def\numPics{36}
\def\makeesc#1{\catcode`#1=0\relax}

\begin{execJS}{execjs}
function importMyIcons ()
{
    console.println("Importing Icons");
    for ( var i=0; i < \numPics; i++)
        this.importIcon("rotate"+i,"animation.pdf",i);
}
console.println("Creating Null Icon");
this.addIcon("nullIcon", this.createIcon("", 0, 0));
_MenuProc = importMyIcons;
app.execMenuItem("MenuProc");
_MenuProc = function() {;}
app.execMenuItem("Save");
\end{execJS}

\begin{defineJS}[\makeesc\@]{\animateAction}
var numSecs = 200;
aMyIcons = new Array();
for ( var i=0; i < @numPics; i++)
    aMyIcons[i] = this.getIcon( "rotate" + i);
var count = 0;
function ShowIt()
{
    f.delay = true;
    f.buttonSetIcon(aMyIcons[run.count], 0 );
    run.count++;
    run.count @%= @numPics;
    f.delay=false;
}
var f = this.getField("myAnimation");
var run = app.setInterval("ShowIt()",numSecs);
run.count = 0;
var timeout = app.setTimeOut( "app.clearInterval(run);", numSecs * (2 + 2*@numPics) );
\end{defineJS}

\begin{defineJS}{\clearJSAction}
try { app.clearInterval(run);} catch(e){}
try { app.clearTimeOut(timeout);} catch(e){}
var g = this.getField("myAnimation");
g.buttonSetIcon( this.getIcon("nullIcon") );
\end{defineJS}

\newcommand{\animateCtrl}
{%
    /S /JavaScript /JS(\animateAction)
}
\newcommand{\clearAction}
{%
    /S /JavaScript /JS(\clearJSAction)
}

\begin{document}

\maketitle


\section{Introduction}

This document demos two environments, \texttt{defineJS} and
\texttt{execJS}, defined in the \textsf{insDLJS Package}, a stand
alone package distributed with the \textsf{Acro\!\TeX{} eDucation
Bundle}.  The example presented here is an
\hyperref[animation]{\texttt{animation}} (of sorts)  created
completely from a {\LaTeX} source file that utilizes these two
environments.

\section{The \texttt{execJS} Environment}

The \texttt{execJS} environment can be used to create ``executable
and discardable'' JavaScript. The applications of this environment
are very exciting (to me), however, the \textit{full Acrobat
product is required} to make it work; more exactly, Acrobat~5.0 or
greater is required.  It should be emphasized, however, that the
document author can create the PDF document using the Acrobat
distiller, \textsf{pdftex} or \textsf{dvipdfm}, the Acrobat
Viewer---not the Acrobat Reader---is needed to import and execute
the JavaScript.

The \texttt{execJS} environment is used to write JavaScript. When
the document is \LaTeX ed, the script is written verbatim to an
FDF (Forms Data Format) file.  The environment also adds an open
action, so that when the newly created PDF document is opened for
the first time in Acrobat, the FDF file is imported and executed.
After the JavaScript has executed, the next thing to do is to
save the document. The FDF that is imported is not saved with the
document, and will not be imported again into the document,
thereafter. The document is then ready for distribution.

\medskip\noindent
\textcolor{red}{\textbf{Important:}} This environment, and the
technique on which it is based, described next, is used for
``post-creation'' document addendum; that is, after the document
is created, the JavaScript that appears within the \texttt{execJS}
environment, is executed. This script can perform a variety of
tasks such as importing sound or PDF icons.

\subsection{Security Restricted Methods}

If the JavaScript you want to execute has no security
restrictions, then no special preparations are needed to use this
environment.  Jump to the next section.

Many of the more interesting JavaScript methods have security
restrictions, they can only be executed during a menu event, a
console event, or a batch event. Using the method described below,
you can executed some or all of your JavaScript defined in the
\texttt{execJS} environment through a menu. To do this, you need a
custom menu.

Copy the following lines and paste them into a text file. Save the
file with a \texttt{.js} extension, and place it in the
\textsf{JavaScripts} folder of the Acrobat installation directory
tree.
\begin{sverbatim}
_MenuProc = function() {;}
app.addMenuItem( { cName: "MenuProc", cUser: "Menu Procedure",
    cParent: "Tools", cExec: "_MenuProc()", nPos: 0 } );
\end{sverbatim}
This code will create a custom menu item under the \texttt{Tools}
menu. It is through this menu that we shall execute our
JavaScript.

\medskip\noindent
\textcolor{red}{\textbf{Note:}} For security reasons, rename the
variable \texttt{\_MenuProc} and the menu name \texttt{"MenuProc"}
to something ``secret''.  Arbitrary JavaScript can be executed
through this menu, so no one should be able to use this technique
on your computer, but you!

\subsection{The Environment}

See \texttt{insDLJS.dtx} for more details of the
\texttt{execJS} environment.  An example of usage follows:
\begin{sverbatim}
\begin{execJS}{execjs}
function importMyIcons ()                                       (1)
{
    for ( var i=0; i < \numPics; i++)
        this.importIcon("rotate"+i,"animation.pdf",i);
}
this.addIcon("nullIcon", this.createIcon("", 0, 0));            (2)
_MenuProc = importMyIcons;                                      (3)
app.execMenuItem("MenuProc");                                   (4)
_MenuProc = function() {;}                                      (5)
\end{execJS}
\end{sverbatim}
This is taken from the preamble of this document, and is used in
the \hyperref[animation]{animation} example that follows. In line
(1), we define a JavaScript function \texttt{importMyIcons}; the
body of which imports and names a series of PDF images from the
\texttt{animation.pdf} document. (The document
\texttt{animation.pdf} was produced using PSTricks.)  We also
create a null icon, (2), using the undocumented method,
\texttt{this.addIcon}. This icon is used to clear the button face
of the \hyperref[animation]{animation}.

The Acrobat JavaScript method \texttt{importIcon} has security
restrictions, it can only be executed through a menu, the console,
or in a batch sequence.

The trick to this technique is to then assign, in line (3), the
variable \texttt{\_MenuProc}, defined in the \texttt{.js} file
described above, a value of \texttt{importMyIcons}, then execute
the function \texttt{importMyIcons} through the menu item with the
\texttt{app.execMenuItem("MenuProc")}.  We finish off by resetting
the value of \texttt{\_MenuProc} to its default in line (5).

\section{The \texttt{defineJS} Environment}
The \texttt{defineJS} environment can be used to write
JavaScript for buttons and other field fields.

Use \texttt{defineJS} to define a text macro whose expansion is the
lines of code laid out within the environment.
The \texttt{defineJS} environment takes one required argument and one optional one.
The required parameter is the name of the text macro to be defined.
The JavaScript code lines entered inside the \texttt{defineJS} environment
are read verbatim and are added to the definition of a macro.
The optional parameter allows you to modify the verbatim read.

From the preamble, we have
\begin{sverbatim}
\begin{defineJS}[\makecomment\%\makeesc\@]{\animateAction}
aMyIcons = new Array();
for ( var i=0; i < @numPics; i++)
    aMyIcons[i] = this.getIcon( "rotate" + i);
var count = 0;
function ShowIt()
{
    f.delay = true;
    f.buttonSetIcon( aMyIcons[run.count], 0 );
    run.count++;
    run.count @%= @numPics;
    f.delay=false;
}
var f = this.getField("myAnimation");
var run = app.setInterval("ShowIt()",150);
run.count = 0;
var timeout = app.setTimeOut( "app.clearInterval(run);", 3*36*150);
\end{defineJS}
\end{sverbatim}
This environment defines a command \cs{animateAction}, whose
expansion is the JavaScript code you see above.  The optional
argument was used to change catcodes of \texttt{\%} and
\texttt{@}, to comment and escape, respectively. The command
\cs{animateAction} to control the animation, see the next section.

Now we define a new command
\begin{sverbatim}
\newcommand{\animateCtrl}
{%
    /S /JavaScript /JS(\animateAction)
}
\end{sverbatim}
an optional definition. Finally, we create the button with the
following code:
\begin{sverbatim}
\pushButton[\CA{Push}\BC{1 .973 .863}
    \A{\animateCtrl}]{aniCtrl}{36bp}{10bp}
\end{sverbatim}
The creation of the \cs{animateCtrl} is just a convenience. We
could have defined the button this way as well:
\begin{sverbatim}
\pushButton
    [
        \CA{Push}\BC{1 .973 .863}
        \A{/S /JavaScript /JS(\animateAction)}
    ]{aniCtrl}{36bp}{10bp}%
\end{sverbatim}


\section{An Animation}\label{animation}

Below is a simple animation--- or picture show---which uses the
techniques described above.  The animation is produced by defining
a sequence of PDF images as the appearance of the button below.
The images are shuffled in and out at regular intervals.

\begin{center}
    \eqIcon[\BC{0 0 0}]{myAnimation}{72bp}{72bp}\\[1ex]
    \pushButton[\CA{Push}\BC{1 .973 .863}\A{\animateCtrl}]{aniCtrl}{36bp}{10bp}%
    \pushButton[\CA{Clear}\BC{1 .973 .863}\A{\clearAction}]{Clear}{36bp}{10bp}
\end{center}

\end{document}
