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?
Parts:
- calculator.tex
- A M(N)WE
- The MWE Only Sort of Works
# calculator.tex
On 13 Nov 2019, at 10:26, Pablo Rodriguez wrote:
Hi Paul,
I wonder whether
tex/texmf-context/doc/context/examples/calculator/calculator.tex from
the ConTeXt Suite might be what you look for.
Just in case it helps,
It does (and is quite something!); it's not quite what I need, though.
I'd like to take advantage of the internal _calculate_ event when a
field updates - the calculator.tex PDF appears to be entirely
programmatically event driven?
(Which could certainly work for my application, but I want to try to
keep this as simple for the user as possible.)
Pablo
# A M(N)WE
Here's a Minimal Working Example, with some caveats:
--- begin MWE ---
\usemodule[fields]
\starttext
\setupinteraction [state=start]
\startJScode{tallyUp}
var a_fld = this.getField("A");
var b_fld = this.getField("B");
event.value = a_fld.value + b_fld.value;
\stopJScode
\setupfield[numField][reset,horizontal]
[height=2pc,rulethickness=1pt,framecolor=lightgrey]
\setupfield[totalField][reset,horizontal]
[
height=2pc,
rulethickness=1pt,
framecolor=lightgrey,
option=readonly,
calculate=JS(tallyUp),
]
\definefield[A][line][numField][]
\definefield[B][line][numField][]
\definefield[Total][line][totalField][]
A: \field[A]
B: \field[B]
\blank[big]
Total: \field[Total]
\stoptext
--- end MWE ---
The idea here is that the resulting PDF will have three fields: A, B,
and Total. Whenever a value is placed in A and/or B, on blur the value
in Total will get updated automatically.
# The MWE Only Sort of Works
Things that are working:
- I can put values in A and B
- I can't directly manipulate Total (read-only seems to be working
fine)
Thing that doesn't work:
- Total does not get automatically updated (for _any_ of the
JS-enabled/tolerant PDF viewers I have.)
Upon closer inspection of the resulting PDF with PDF Studio 2019 (I
don't have Acrobat Pro at the moment), I can confirm some things:
- The appropriate JavaScript is placed in the form, as a custom
calculation script for the Total field.
(This leads me to believe that the calculate= key in \setupfield is
working.)
- If I try to check the calculation order of the PDF, PDF Studio 2019
gives me the warning: "There are no fields with calculations."
(So maybe there's a magic bit that needs to get flipped somewhere?)
- If I use PDF Studio 2019 to change the calculate attribute on the
field Total to something else, save, and then revert the calculate
attribute _back_ to the custom script as supplied from the .tex file and
save, the Total field starts working as expected, and I can see the
field in the calculation ordering.
If I can get the PDF to work in PDF Studio 2019, I can confirm that it
also works in all of the other JS-enabled/tolerant PDF viewers I have.
# Summary
It looks like the appropriate JavaScript script is getting attached to
the field for a calculate event, but is not being run? Or something?
Anyway, that's as far as I've gotten. Any insight or assistance is
appreciated; I'll see if I can make more progress.
Thanks!
-Paul
___________________________________________________________________________________
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
___________________________________________________________________________________