ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Hans Hagen <j.hagen@xs4all.nl>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>,
	Paul Mazaitis <pm39@andrew.cmu.edu>
Subject: Re: Widgets: Any good examples for developing JS-enabled PDFs that calculate?
Date: Mon, 18 Nov 2019 09:48:19 +0100	[thread overview]
Message-ID: <1b0452b5-4ae7-386b-1ee9-5149a62c32e5@xs4all.nl> (raw)
In-Reply-To: <ADE29B0D-B95B-4C22-8BE4-8B303C83F451@andrew.cmu.edu>

On 11/17/2019 9:13 PM, Paul Mazaitis wrote:
> 
> Hi everyone,
> 
> First: apologies for the long email; this got a little complicated.
> 
> Summary: there may be a bug in setting up calculate field for widgets?

it's more that javascript in pdf is kind of unreliable and can interfere 
in all kind of ways with settings in the viewer

so, what you need to do it

- play safe (only use actions that don't relate to something clever in 
the viewer, and calculat eis one of them)
- be redundant (hook into several actions)
- disable the spell checker
- enable javascript
- disable some security stuff
- use the debugger (which can be erratic but at least you can see 
something; in my older acrobat doesn't seem to work with field related 
scripts)
- put code in functions (easier to debug)

and

- cross your fingers

Here's something to play with ... note the Number conversion!

\usemodule[fields]

\starttext

\setupinteraction [state=start]

\startJSpreamble {whatever} used now
function recalculate() {
     var a_fld = this.getField("A");
     var b_fld = this.getField("B");
     var t_fld = this.getField("T");
     var a_val = Number(a_fld.value);
     var b_val = Number(b_fld.value);
     var t_val = a_val + b_val;
     t_fld.value = String(t_val);
     console.clear ;
     console.println("A: "+a_val) ;
     console.println("B: "+b_val) ;
     console.println("T: "+t_val) ;
     console.println("?: "+t_fld.value) ;
     this.dirty = true;
}
\stopJSpreamble

\setupfield
   [numField]
   [reset,horizontal]
   [height=2pc,
    rulethickness=1pt,
    afterkey=JS(recalculate{}),
    focusout=JS(recalculate{}),
    framecolor=lightgrey]

\setupfield
   [totalField]
   [reset,horizontal]
   [height=2pc,
    rulethickness=1pt,
    option=readonly,
    framecolor=lightgrey]

\definefield[A][line][numField]  [0]
\definefield[B][line][numField]  [0]
\definefield[T][line][totalField][0]

A: \field[A]\par
B: \field[B]\par
T: \field[T]\par

\stoptext



-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

  reply	other threads:[~2019-11-18  8:48 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-07 19:28 [Widgets] " Paul Mazaitis
2019-11-13 15:26 ` Pablo Rodriguez
2019-11-17 20:13   ` Widgets: " Paul Mazaitis
2019-11-18  8:48     ` Hans Hagen [this message]
2019-11-19 22:47       ` Paul Mazaitis
2019-11-18 15:15     ` Henning Hraban Ramm
2019-11-18 18:56       ` Hans Hagen
2019-11-19 13:59         ` Paul Mazaitis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1b0452b5-4ae7-386b-1ee9-5149a62c32e5@xs4all.nl \
    --to=j.hagen@xs4all.nl \
    --cc=ntg-context@ntg.nl \
    --cc=pm39@andrew.cmu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).