Files
visa/static/signature.js
T
grabowski d0e13dffd2
CI / secret scan (push) Successful in 6s
CI / fmt, clippy, tests (push) Successful in 1m0s
CI / release (push) Skipped
Agreements the client signs on the link (task 7.6)
Built into the app rather than a separate signing service: any number of
agreements (the processing of the client's data first, legal services
and consultations later), each in versions whose text never changes.

- Staff (Agreements in the sidebar, src/web/agreements.rs): paste the
  lawyers' text (empty line = paragraph, "# " heading, "- " list item),
  preview it as the client reads it, publish version 1; a changed text is
  the next version, the same text again is refused, and the database
  refuses any change to a published one (migrations/0007, trigger).
  Switch an agreement off and links stop asking for it.
- Client: every active agreement is the first step of every link. The
  client reads it, ticks that they accept, types their name and signs
  with a finger or the mouse (static/signature.js; a PNG of the pad). A
  text changed while they read it is refused. A new version is asked for
  again at the next link.
- The signed copy (src/agreements/pdf.rs, Sarabun so Thai prints, long
  Thai words cut without parting marks from their letter): the text, the
  name, the record, the time, the drawing, the link, the forwarded
  address, the browser and the text's SHA-256; kept as the person's
  encrypted document of kind "agreement", all in one transaction with the
  signature and the audit rows (actor link N).
- The person's page shows each agreement's state (signed, not signed, to
  sign again) and every signed copy.
- Phones: the section row now scrolls sideways instead of wrapping, so it
  keeps one height with five sections and Sign out.
- Demo data: an invented agreement with a Thai paragraph.

Checks: fmt, clippy; 105 unit and 56 integration tests (tests/agreements.rs:
versions that never change, signing with a drawing, address and browser
recorded, the PDF, signing again after a change, a stale version refused,
switched off); layout check light/dark desk/phone on the agreement pages,
the client's agreement page, the person page and the people list: no
problems; a drawn signature in Edge at 390x844 signed and printed.
2026-11-20 20:18:59 +07:00

93 lines
2.9 KiB
JavaScript

// The signature pad of an agreement on the client's link: drawn with a
// finger, a pen or the mouse, sent as a PNG in the hidden field `drawing`.
// Without JavaScript the pad stays hidden and the typed name signs alone.
(function () {
"use strict";
function setup(form) {
var wrap = form.querySelector("[data-sign-wrap]");
var pad = form.querySelector("[data-sign-pad]");
var out = form.querySelector("[data-sign-out]");
var msg = form.querySelector("[data-sign-msg]");
if (!wrap || !pad || !out || !pad.getContext) return;
wrap.hidden = false;
var ctx = pad.getContext("2d");
var drawn = false;
var last = null;
// the canvas in device pixels; a resize clears it, as the strokes no
// longer fit
function size() {
var r = pad.getBoundingClientRect();
var dpr = Math.min(window.devicePixelRatio || 1, 3);
pad.width = Math.max(1, Math.round(r.width * dpr));
pad.height = Math.max(1, Math.round(r.height * dpr));
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.lineCap = "round";
ctx.lineJoin = "round";
ctx.lineWidth = 2.4;
ctx.strokeStyle = "#071323";
drawn = false;
out.value = "";
}
function at(ev) {
var r = pad.getBoundingClientRect();
return { x: ev.clientX - r.left, y: ev.clientY - r.top };
}
pad.addEventListener("pointerdown", function (ev) {
ev.preventDefault();
pad.setPointerCapture(ev.pointerId);
last = at(ev);
ctx.beginPath();
ctx.arc(last.x, last.y, 1.1, 0, 2 * Math.PI);
ctx.fillStyle = "#071323";
ctx.fill();
drawn = true;
if (msg) msg.hidden = true;
});
pad.addEventListener("pointermove", function (ev) {
if (!last) return;
ev.preventDefault();
var p = at(ev);
ctx.beginPath();
ctx.moveTo(last.x, last.y);
ctx.lineTo(p.x, p.y);
ctx.stroke();
last = p;
});
function end() {
last = null;
}
pad.addEventListener("pointerup", end);
pad.addEventListener("pointercancel", end);
form.querySelector("[data-sign-clear]").addEventListener("click", function () {
ctx.clearRect(0, 0, pad.width, pad.height);
drawn = false;
out.value = "";
});
form.addEventListener("submit", function (ev) {
if (!drawn) {
ev.preventDefault();
if (msg) msg.hidden = false;
pad.scrollIntoView({ block: "center" });
return;
}
out.value = pad.toDataURL("image/png");
});
var width = 0;
window.addEventListener("resize", function () {
var w = pad.getBoundingClientRect().width;
if (Math.abs(w - width) > 2) {
width = w;
size();
}
});
width = pad.getBoundingClientRect().width;
size();
}
document.addEventListener("DOMContentLoaded", function () {
document.querySelectorAll("form[data-sign-form]").forEach(setup);
});
})();