@fieldia/core 0.26.1 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (415) hide show
  1. package/README.md +307 -3
  2. package/package.json +1 -1
  3. package/schema/page.schema.json +227 -23
  4. package/src/index.d.mts +8 -2
  5. package/src/index.d.ts +8 -2
  6. package/src/index.js +22 -1
  7. package/src/index.js.map +1 -1
  8. package/src/index.mjs +9 -2
  9. package/src/index.mjs.map +1 -1
  10. package/src/lib/expression/app-functions.d.mts +47 -0
  11. package/src/lib/expression/app-functions.d.ts +47 -0
  12. package/src/lib/expression/app-functions.js +64 -0
  13. package/src/lib/expression/app-functions.js.map +1 -0
  14. package/src/lib/expression/app-functions.mjs +58 -0
  15. package/src/lib/expression/app-functions.mjs.map +1 -0
  16. package/src/lib/expression/dates.d.mts +21 -0
  17. package/src/lib/expression/dates.d.ts +21 -0
  18. package/src/lib/expression/dates.js +73 -0
  19. package/src/lib/expression/dates.js.map +1 -0
  20. package/src/lib/expression/dates.mjs +67 -0
  21. package/src/lib/expression/dates.mjs.map +1 -0
  22. package/src/lib/expression/evaluator.js +12 -6
  23. package/src/lib/expression/evaluator.js.map +1 -1
  24. package/src/lib/expression/evaluator.mjs +12 -6
  25. package/src/lib/expression/evaluator.mjs.map +1 -1
  26. package/src/lib/expression/expression.d.mts +7 -3
  27. package/src/lib/expression/expression.d.ts +7 -3
  28. package/src/lib/expression/expression.js +8 -2
  29. package/src/lib/expression/expression.js.map +1 -1
  30. package/src/lib/expression/expression.mjs +7 -2
  31. package/src/lib/expression/expression.mjs.map +1 -1
  32. package/src/lib/expression/functions-part.d.mts +7 -0
  33. package/src/lib/expression/functions-part.d.ts +7 -0
  34. package/src/lib/expression/functions-part.js +13 -0
  35. package/src/lib/expression/functions-part.js.map +1 -0
  36. package/src/lib/expression/functions-part.mjs +8 -0
  37. package/src/lib/expression/functions-part.mjs.map +1 -0
  38. package/src/lib/expression/functions.d.mts +60 -3
  39. package/src/lib/expression/functions.d.ts +60 -3
  40. package/src/lib/expression/functions.js +109 -15
  41. package/src/lib/expression/functions.js.map +1 -1
  42. package/src/lib/expression/functions.mjs +103 -16
  43. package/src/lib/expression/functions.mjs.map +1 -1
  44. package/src/lib/expression/kinds.d.mts +31 -0
  45. package/src/lib/expression/kinds.d.ts +31 -0
  46. package/src/lib/expression/kinds.js +159 -0
  47. package/src/lib/expression/kinds.js.map +1 -0
  48. package/src/lib/expression/kinds.mjs +151 -0
  49. package/src/lib/expression/kinds.mjs.map +1 -0
  50. package/src/lib/expression/modifier.d.mts +4 -3
  51. package/src/lib/expression/modifier.d.ts +4 -3
  52. package/src/lib/expression/modifier.js +4 -3
  53. package/src/lib/expression/modifier.js.map +1 -1
  54. package/src/lib/expression/modifier.mjs +4 -3
  55. package/src/lib/expression/modifier.mjs.map +1 -1
  56. package/src/lib/expression/more-functions.d.mts +8 -0
  57. package/src/lib/expression/more-functions.d.ts +8 -0
  58. package/src/lib/expression/more-functions.js +235 -0
  59. package/src/lib/expression/more-functions.js.map +1 -0
  60. package/src/lib/expression/more-functions.mjs +230 -0
  61. package/src/lib/expression/more-functions.mjs.map +1 -0
  62. package/src/lib/expression/parser.d.mts +4 -1
  63. package/src/lib/expression/parser.d.ts +4 -1
  64. package/src/lib/expression/parser.js +6 -3
  65. package/src/lib/expression/parser.js.map +1 -1
  66. package/src/lib/expression/parser.mjs +6 -3
  67. package/src/lib/expression/parser.mjs.map +1 -1
  68. package/src/lib/expression/reads.d.mts +2 -2
  69. package/src/lib/expression/reads.d.ts +2 -2
  70. package/src/lib/expression/reads.js +6 -4
  71. package/src/lib/expression/reads.js.map +1 -1
  72. package/src/lib/expression/reads.mjs +6 -4
  73. package/src/lib/expression/reads.mjs.map +1 -1
  74. package/src/lib/format/actions.d.mts +16 -16
  75. package/src/lib/format/actions.d.ts +16 -16
  76. package/src/lib/format/check-page.d.mts +7 -2
  77. package/src/lib/format/check-page.d.ts +7 -2
  78. package/src/lib/format/check-page.js +10 -4
  79. package/src/lib/format/check-page.js.map +1 -1
  80. package/src/lib/format/check-page.mjs +10 -4
  81. package/src/lib/format/check-page.mjs.map +1 -1
  82. package/src/lib/format/choice-options.d.mts +34 -0
  83. package/src/lib/format/choice-options.d.ts +34 -0
  84. package/src/lib/format/choice-options.js +60 -0
  85. package/src/lib/format/choice-options.js.map +1 -0
  86. package/src/lib/format/choice-options.mjs +51 -0
  87. package/src/lib/format/choice-options.mjs.map +1 -0
  88. package/src/lib/format/endings-checks.d.mts +8 -0
  89. package/src/lib/format/endings-checks.d.ts +8 -0
  90. package/src/lib/format/endings-checks.js +23 -0
  91. package/src/lib/format/endings-checks.js.map +1 -0
  92. package/src/lib/format/endings-checks.mjs +20 -0
  93. package/src/lib/format/endings-checks.mjs.map +1 -0
  94. package/src/lib/format/endings.d.mts +40 -0
  95. package/src/lib/format/endings.d.ts +40 -0
  96. package/src/lib/format/endings.js +15 -0
  97. package/src/lib/format/endings.js.map +1 -0
  98. package/src/lib/format/endings.mjs +12 -0
  99. package/src/lib/format/endings.mjs.map +1 -0
  100. package/src/lib/format/field.d.mts +151 -16
  101. package/src/lib/format/field.d.ts +151 -16
  102. package/src/lib/format/field.js +64 -11
  103. package/src/lib/format/field.js.map +1 -1
  104. package/src/lib/format/field.mjs +63 -10
  105. package/src/lib/format/field.mjs.map +1 -1
  106. package/src/lib/format/head-checks.d.mts +2 -0
  107. package/src/lib/format/head-checks.d.ts +2 -0
  108. package/src/lib/format/head-checks.js +2 -59
  109. package/src/lib/format/head-checks.js.map +1 -1
  110. package/src/lib/format/head-checks.mjs +1 -58
  111. package/src/lib/format/head-checks.mjs.map +1 -1
  112. package/src/lib/format/later-checks.d.mts +17 -0
  113. package/src/lib/format/later-checks.d.ts +17 -0
  114. package/src/lib/format/later-checks.js +20 -0
  115. package/src/lib/format/later-checks.js.map +1 -0
  116. package/src/lib/format/later-checks.mjs +17 -0
  117. package/src/lib/format/later-checks.mjs.map +1 -0
  118. package/src/lib/format/layout.d.mts +241 -188
  119. package/src/lib/format/layout.d.ts +241 -188
  120. package/src/lib/format/layout.js +33 -28
  121. package/src/lib/format/layout.js.map +1 -1
  122. package/src/lib/format/layout.mjs +13 -3
  123. package/src/lib/format/layout.mjs.map +1 -1
  124. package/src/lib/format/layout.types.d.mts +2 -1
  125. package/src/lib/format/layout.types.d.ts +2 -1
  126. package/src/lib/format/limits-checks.d.mts +14 -0
  127. package/src/lib/format/limits-checks.d.ts +14 -0
  128. package/src/lib/format/limits-checks.js +36 -0
  129. package/src/lib/format/limits-checks.js.map +1 -0
  130. package/src/lib/format/limits-checks.mjs +33 -0
  131. package/src/lib/format/limits-checks.mjs.map +1 -0
  132. package/src/lib/format/limits.d.mts +24 -0
  133. package/src/lib/format/limits.d.ts +24 -0
  134. package/src/lib/format/limits.js +35 -0
  135. package/src/lib/format/limits.js.map +1 -0
  136. package/src/lib/format/limits.mjs +32 -0
  137. package/src/lib/format/limits.mjs.map +1 -0
  138. package/src/lib/format/list-checks.d.mts +44 -0
  139. package/src/lib/format/list-checks.d.ts +44 -0
  140. package/src/lib/format/list-checks.js +340 -0
  141. package/src/lib/format/list-checks.js.map +1 -0
  142. package/src/lib/format/list-checks.mjs +336 -0
  143. package/src/lib/format/list-checks.mjs.map +1 -0
  144. package/src/lib/format/map-checks.d.mts +33 -0
  145. package/src/lib/format/map-checks.d.ts +33 -0
  146. package/src/lib/format/map-checks.js +180 -0
  147. package/src/lib/format/map-checks.js.map +1 -0
  148. package/src/lib/format/map-checks.mjs +175 -0
  149. package/src/lib/format/map-checks.mjs.map +1 -0
  150. package/src/lib/format/mapchecks-part.d.mts +5 -0
  151. package/src/lib/format/mapchecks-part.d.ts +5 -0
  152. package/src/lib/format/mapchecks-part.js +11 -0
  153. package/src/lib/format/mapchecks-part.js.map +1 -0
  154. package/src/lib/format/mapchecks-part.mjs +6 -0
  155. package/src/lib/format/mapchecks-part.mjs.map +1 -0
  156. package/src/lib/format/option-checks.d.mts +16 -0
  157. package/src/lib/format/option-checks.d.ts +16 -0
  158. package/src/lib/format/option-checks.js +39 -0
  159. package/src/lib/format/option-checks.js.map +1 -0
  160. package/src/lib/format/option-checks.mjs +36 -0
  161. package/src/lib/format/option-checks.mjs.map +1 -0
  162. package/src/lib/format/options-part.d.mts +6 -0
  163. package/src/lib/format/options-part.d.ts +6 -0
  164. package/src/lib/format/options-part.js +11 -0
  165. package/src/lib/format/options-part.js.map +1 -0
  166. package/src/lib/format/options-part.mjs +7 -0
  167. package/src/lib/format/options-part.mjs.map +1 -0
  168. package/src/lib/format/page.d.mts +147 -71
  169. package/src/lib/format/page.d.ts +147 -71
  170. package/src/lib/format/page.js +5 -0
  171. package/src/lib/format/page.js.map +1 -1
  172. package/src/lib/format/page.mjs +5 -0
  173. package/src/lib/format/page.mjs.map +1 -1
  174. package/src/lib/format/part-checks.d.mts +20 -0
  175. package/src/lib/format/part-checks.d.ts +20 -0
  176. package/src/lib/format/part-checks.js +3 -0
  177. package/src/lib/format/part-checks.js.map +1 -0
  178. package/src/lib/format/part-checks.mjs +2 -0
  179. package/src/lib/format/part-checks.mjs.map +1 -0
  180. package/src/lib/format/references.d.mts +71 -65
  181. package/src/lib/format/references.d.ts +71 -65
  182. package/src/lib/format/references.js +194 -631
  183. package/src/lib/format/references.js.map +1 -1
  184. package/src/lib/format/references.mjs +194 -631
  185. package/src/lib/format/references.mjs.map +1 -1
  186. package/src/lib/format/seats-checks.d.mts +3 -0
  187. package/src/lib/format/seats-checks.d.ts +3 -0
  188. package/src/lib/format/seats-checks.js +13 -0
  189. package/src/lib/format/seats-checks.js.map +1 -0
  190. package/src/lib/format/seats-checks.mjs +10 -0
  191. package/src/lib/format/seats-checks.mjs.map +1 -0
  192. package/src/lib/format/sheet-head-checks.d.mts +4 -0
  193. package/src/lib/format/sheet-head-checks.d.ts +4 -0
  194. package/src/lib/format/sheet-head-checks.js +68 -0
  195. package/src/lib/format/sheet-head-checks.js.map +1 -0
  196. package/src/lib/format/sheet-head-checks.mjs +65 -0
  197. package/src/lib/format/sheet-head-checks.mjs.map +1 -0
  198. package/src/lib/format/signature-checks.d.mts +7 -0
  199. package/src/lib/format/signature-checks.d.ts +7 -0
  200. package/src/lib/format/signature-checks.js +32 -0
  201. package/src/lib/format/signature-checks.js.map +1 -0
  202. package/src/lib/format/signature-checks.mjs +29 -0
  203. package/src/lib/format/signature-checks.mjs.map +1 -0
  204. package/src/lib/format/signature-options.d.mts +24 -0
  205. package/src/lib/format/signature-options.d.ts +24 -0
  206. package/src/lib/format/signature-options.js +30 -0
  207. package/src/lib/format/signature-options.js.map +1 -0
  208. package/src/lib/format/signature-options.mjs +26 -0
  209. package/src/lib/format/signature-options.mjs.map +1 -0
  210. package/src/lib/format/slots-checks.d.mts +4 -0
  211. package/src/lib/format/slots-checks.d.ts +4 -0
  212. package/src/lib/format/slots-checks.js +21 -0
  213. package/src/lib/format/slots-checks.js.map +1 -0
  214. package/src/lib/format/slots-checks.mjs +18 -0
  215. package/src/lib/format/slots-checks.mjs.map +1 -0
  216. package/src/lib/format/statusbar-checks.d.mts +8 -0
  217. package/src/lib/format/statusbar-checks.d.ts +8 -0
  218. package/src/lib/format/statusbar-checks.js +35 -0
  219. package/src/lib/format/statusbar-checks.js.map +1 -0
  220. package/src/lib/format/statusbar-checks.mjs +32 -0
  221. package/src/lib/format/statusbar-checks.mjs.map +1 -0
  222. package/src/lib/format/table-checks.d.mts +21 -0
  223. package/src/lib/format/table-checks.d.ts +21 -0
  224. package/src/lib/format/table-checks.js +86 -0
  225. package/src/lib/format/table-checks.js.map +1 -0
  226. package/src/lib/format/table-checks.mjs +82 -0
  227. package/src/lib/format/table-checks.mjs.map +1 -0
  228. package/src/lib/format/translate.js +2 -2
  229. package/src/lib/format/translate.js.map +1 -1
  230. package/src/lib/format/translate.mjs +2 -2
  231. package/src/lib/format/translate.mjs.map +1 -1
  232. package/src/lib/format/upgrade.d.mts +6 -3
  233. package/src/lib/format/upgrade.d.ts +6 -3
  234. package/src/lib/format/upgrade.js +6 -3
  235. package/src/lib/format/upgrade.js.map +1 -1
  236. package/src/lib/format/upgrade.mjs +6 -3
  237. package/src/lib/format/upgrade.mjs.map +1 -1
  238. package/src/lib/format/validate.d.mts +18 -2
  239. package/src/lib/format/validate.d.ts +18 -2
  240. package/src/lib/format/validate.js +36 -3
  241. package/src/lib/format/validate.js.map +1 -1
  242. package/src/lib/format/validate.mjs +35 -3
  243. package/src/lib/format/validate.mjs.map +1 -1
  244. package/src/lib/format/version.d.mts +2 -2
  245. package/src/lib/format/version.d.ts +2 -2
  246. package/src/lib/format/version.js +2 -2
  247. package/src/lib/format/version.js.map +1 -1
  248. package/src/lib/format/version.mjs +2 -2
  249. package/src/lib/format/version.mjs.map +1 -1
  250. package/src/lib/format/words.d.mts +6 -2
  251. package/src/lib/format/words.d.ts +6 -2
  252. package/src/lib/format/words.js +13 -7
  253. package/src/lib/format/words.js.map +1 -1
  254. package/src/lib/format/words.mjs +13 -7
  255. package/src/lib/format/words.mjs.map +1 -1
  256. package/src/lib/parts.d.mts +45 -0
  257. package/src/lib/parts.d.ts +45 -0
  258. package/src/lib/parts.js +60 -0
  259. package/src/lib/parts.js.map +1 -0
  260. package/src/lib/parts.mjs +53 -0
  261. package/src/lib/parts.mjs.map +1 -0
  262. package/src/lib/record/cells.d.mts +3 -3
  263. package/src/lib/record/cells.d.ts +3 -3
  264. package/src/lib/record/cells.js +16 -15
  265. package/src/lib/record/cells.js.map +1 -1
  266. package/src/lib/record/cells.mjs +16 -15
  267. package/src/lib/record/cells.mjs.map +1 -1
  268. package/src/lib/record/compute.d.mts +16 -5
  269. package/src/lib/record/compute.d.ts +16 -5
  270. package/src/lib/record/compute.js +57 -16
  271. package/src/lib/record/compute.js.map +1 -1
  272. package/src/lib/record/compute.mjs +57 -16
  273. package/src/lib/record/compute.mjs.map +1 -1
  274. package/src/lib/record/data-source.d.mts +140 -3
  275. package/src/lib/record/data-source.d.ts +140 -3
  276. package/src/lib/record/data-source.js.map +1 -1
  277. package/src/lib/record/data-source.mjs.map +1 -1
  278. package/src/lib/record/drafts.d.mts +19 -0
  279. package/src/lib/record/drafts.d.ts +19 -0
  280. package/src/lib/record/drafts.js +35 -0
  281. package/src/lib/record/drafts.js.map +1 -0
  282. package/src/lib/record/drafts.mjs +31 -0
  283. package/src/lib/record/drafts.mjs.map +1 -0
  284. package/src/lib/record/env.d.mts +5 -1
  285. package/src/lib/record/env.d.ts +5 -1
  286. package/src/lib/record/env.js +9 -1
  287. package/src/lib/record/env.js.map +1 -1
  288. package/src/lib/record/env.mjs +8 -1
  289. package/src/lib/record/env.mjs.map +1 -1
  290. package/src/lib/record/form-limits.d.mts +81 -0
  291. package/src/lib/record/form-limits.d.ts +81 -0
  292. package/src/lib/record/form-limits.js +231 -0
  293. package/src/lib/record/form-limits.js.map +1 -0
  294. package/src/lib/record/form-limits.mjs +224 -0
  295. package/src/lib/record/form-limits.mjs.map +1 -0
  296. package/src/lib/record/form.d.mts +173 -4
  297. package/src/lib/record/form.d.ts +173 -4
  298. package/src/lib/record/form.js +792 -88
  299. package/src/lib/record/form.js.map +1 -1
  300. package/src/lib/record/form.mjs +792 -88
  301. package/src/lib/record/form.mjs.map +1 -1
  302. package/src/lib/record/limits-part.d.mts +6 -0
  303. package/src/lib/record/limits-part.d.ts +6 -0
  304. package/src/lib/record/limits-part.js +12 -0
  305. package/src/lib/record/limits-part.js.map +1 -0
  306. package/src/lib/record/limits-part.mjs +7 -0
  307. package/src/lib/record/limits-part.mjs.map +1 -0
  308. package/src/lib/record/line-choices.d.mts +30 -0
  309. package/src/lib/record/line-choices.d.ts +30 -0
  310. package/src/lib/record/line-choices.js +42 -0
  311. package/src/lib/record/line-choices.js.map +1 -0
  312. package/src/lib/record/line-choices.mjs +36 -0
  313. package/src/lib/record/line-choices.mjs.map +1 -0
  314. package/src/lib/record/locales/ar.js +10 -0
  315. package/src/lib/record/locales/ar.js.map +1 -1
  316. package/src/lib/record/locales/ar.mjs +10 -0
  317. package/src/lib/record/locales/ar.mjs.map +1 -1
  318. package/src/lib/record/locales/de.js +10 -0
  319. package/src/lib/record/locales/de.js.map +1 -1
  320. package/src/lib/record/locales/de.mjs +10 -0
  321. package/src/lib/record/locales/de.mjs.map +1 -1
  322. package/src/lib/record/locales/fr.js +10 -0
  323. package/src/lib/record/locales/fr.js.map +1 -1
  324. package/src/lib/record/locales/fr.mjs +10 -0
  325. package/src/lib/record/locales/fr.mjs.map +1 -1
  326. package/src/lib/record/memory-data-source.d.mts +44 -3
  327. package/src/lib/record/memory-data-source.d.ts +44 -3
  328. package/src/lib/record/memory-data-source.js +102 -40
  329. package/src/lib/record/memory-data-source.js.map +1 -1
  330. package/src/lib/record/memory-data-source.mjs +101 -40
  331. package/src/lib/record/memory-data-source.mjs.map +1 -1
  332. package/src/lib/record/memory-kept.d.mts +20 -0
  333. package/src/lib/record/memory-kept.d.ts +20 -0
  334. package/src/lib/record/memory-kept.js +46 -0
  335. package/src/lib/record/memory-kept.js.map +1 -0
  336. package/src/lib/record/memory-kept.mjs +43 -0
  337. package/src/lib/record/memory-kept.mjs.map +1 -0
  338. package/src/lib/record/memory-later.d.mts +19 -0
  339. package/src/lib/record/memory-later.d.ts +19 -0
  340. package/src/lib/record/memory-later.js +39 -0
  341. package/src/lib/record/memory-later.js.map +1 -0
  342. package/src/lib/record/memory-later.mjs +35 -0
  343. package/src/lib/record/memory-later.mjs.map +1 -0
  344. package/src/lib/record/memory-part.d.mts +7 -0
  345. package/src/lib/record/memory-part.d.ts +7 -0
  346. package/src/lib/record/memory-part.js +13 -0
  347. package/src/lib/record/memory-part.js.map +1 -0
  348. package/src/lib/record/memory-part.mjs +8 -0
  349. package/src/lib/record/memory-part.mjs.map +1 -0
  350. package/src/lib/record/messages.d.mts +17 -0
  351. package/src/lib/record/messages.d.ts +17 -0
  352. package/src/lib/record/messages.js +10 -0
  353. package/src/lib/record/messages.js.map +1 -1
  354. package/src/lib/record/messages.mjs +10 -0
  355. package/src/lib/record/messages.mjs.map +1 -1
  356. package/src/lib/record/option-rules.d.mts +50 -0
  357. package/src/lib/record/option-rules.d.ts +50 -0
  358. package/src/lib/record/option-rules.js +137 -0
  359. package/src/lib/record/option-rules.js.map +1 -0
  360. package/src/lib/record/option-rules.mjs +134 -0
  361. package/src/lib/record/option-rules.mjs.map +1 -0
  362. package/src/lib/record/rules.d.mts +2 -2
  363. package/src/lib/record/rules.d.ts +2 -2
  364. package/src/lib/record/rules.js +3 -3
  365. package/src/lib/record/rules.js.map +1 -1
  366. package/src/lib/record/rules.mjs +3 -3
  367. package/src/lib/record/rules.mjs.map +1 -1
  368. package/src/lib/record/run.js +9 -7
  369. package/src/lib/record/run.js.map +1 -1
  370. package/src/lib/record/run.mjs +9 -7
  371. package/src/lib/record/run.mjs.map +1 -1
  372. package/src/lib/record/search.d.mts +12 -2
  373. package/src/lib/record/search.d.ts +12 -2
  374. package/src/lib/record/search.js +29 -4
  375. package/src/lib/record/search.js.map +1 -1
  376. package/src/lib/record/search.mjs +29 -4
  377. package/src/lib/record/search.mjs.map +1 -1
  378. package/src/lib/record/set-when.d.mts +3 -3
  379. package/src/lib/record/set-when.d.ts +3 -3
  380. package/src/lib/record/set-when.js +9 -9
  381. package/src/lib/record/set-when.js.map +1 -1
  382. package/src/lib/record/set-when.mjs +9 -9
  383. package/src/lib/record/set-when.mjs.map +1 -1
  384. package/src/lib/record/signed.d.mts +21 -0
  385. package/src/lib/record/signed.d.ts +21 -0
  386. package/src/lib/record/signed.js +62 -0
  387. package/src/lib/record/signed.js.map +1 -0
  388. package/src/lib/record/signed.mjs +57 -0
  389. package/src/lib/record/signed.mjs.map +1 -0
  390. package/src/lib/record/taking.d.mts +60 -0
  391. package/src/lib/record/taking.d.ts +60 -0
  392. package/src/lib/record/taking.js +58 -0
  393. package/src/lib/record/taking.js.map +1 -0
  394. package/src/lib/record/taking.mjs +50 -0
  395. package/src/lib/record/taking.mjs.map +1 -0
  396. package/src/lib/record/value-map.js +11 -4
  397. package/src/lib/record/value-map.js.map +1 -1
  398. package/src/lib/record/value-map.mjs +11 -4
  399. package/src/lib/record/value-map.mjs.map +1 -1
  400. package/src/lib/record/values.d.mts +20 -0
  401. package/src/lib/record/values.d.ts +20 -0
  402. package/src/lib/record/values.js.map +1 -1
  403. package/src/lib/record/values.mjs.map +1 -1
  404. package/src/lib/record/waits.d.mts +6 -1
  405. package/src/lib/record/waits.d.ts +6 -1
  406. package/src/lib/record/waits.js +2 -2
  407. package/src/lib/record/waits.js.map +1 -1
  408. package/src/lib/record/waits.mjs +2 -2
  409. package/src/lib/record/waits.mjs.map +1 -1
  410. package/src/runtime.d.mts +16 -5
  411. package/src/runtime.d.ts +16 -5
  412. package/src/runtime.js +41 -3
  413. package/src/runtime.js.map +1 -1
  414. package/src/runtime.mjs +13 -0
  415. package/src/runtime.mjs.map +1 -1
@@ -4,9 +4,13 @@ exports.unsavedWork = unsavedWork;
4
4
  exports.workAtRisk = workAtRisk;
5
5
  exports.createForm = createForm;
6
6
  exports.notANumber = notANumber;
7
+ const choice_options_1 = require("../format/choice-options");
8
+ const words_1 = require("../format/words");
7
9
  const upgrade_1 = require("../format/upgrade");
8
10
  const zoned_1 = require("../format/zoned");
9
11
  const modifier_1 = require("../expression/modifier");
12
+ const functions_1 = require("../expression/functions");
13
+ const app_functions_1 = require("../expression/app-functions");
10
14
  const zones_1 = require("./zones");
11
15
  const check_1 = require("./check");
12
16
  const inputs_1 = require("./inputs");
@@ -20,10 +24,14 @@ const roles_1 = require("./roles");
20
24
  const cells_1 = require("./cells");
21
25
  const messages_1 = require("./messages");
22
26
  const record_words_1 = require("./record-words");
27
+ const line_choices_1 = require("./line-choices");
23
28
  const data_source_1 = require("./data-source");
29
+ const taking_1 = require("./taking");
30
+ const drafts_1 = require("./drafts");
24
31
  const waits_1 = require("./waits");
25
32
  const uploads_1 = require("./uploads");
26
33
  const scheduler_1 = require("./scheduler");
34
+ const signed_1 = require("./signed");
27
35
  const run_1 = require("./run");
28
36
  const values_1 = require("./values");
29
37
  /**
@@ -86,13 +94,48 @@ function innerForm(options, within, place, outer) {
86
94
  const messages = page.lowerLabels ? { ...(options.messages ?? messages_1.MESSAGES.en), lowerLabels: true } : (options.messages ?? messages_1.MESSAGES.en);
87
95
  /** One of the form's sentences in the words the page names its records by (`records`), else as the language says it plainly. */
88
96
  const named = (noun, plain) => (0, record_words_1.recordSentence)(page.records, noun, plain, messages.locale);
89
- const index = indexLayout(page.layout);
97
+ /** The app's functions the page's expressions call: a name none may call is refused now. */
98
+ const table = (0, app_functions_1.functionTable)(options.functions);
99
+ /**
100
+ * The answers of the app's functions that ask, for the record shown (`ExpressionEnv.answer`), by function and
101
+ * values: each asked once, `asking` until it comes. Another record shown, they start again: an answer for the last
102
+ * one is laid on none.
103
+ */
104
+ let answers = new Map();
105
+ /** Those on their way: a save waits for them. */
106
+ const asking = new Set();
107
+ /** Everything the form waits on (`track`): there before the form's first values are worked out, which may ask. */
108
+ const pending = new Set();
109
+ function answer(key, asked) {
110
+ const kept = answers.get(key);
111
+ if (kept !== undefined || !asked)
112
+ return kept;
113
+ const map = answers;
114
+ map.set(key, 'asking');
115
+ const land = (found) => {
116
+ asking.delete(going);
117
+ if (map !== answers || disposed)
118
+ return;
119
+ map.set(key, found);
120
+ answered();
121
+ };
122
+ const going = Promise.resolve(asked).then((value) => land({ value: value === undefined ? null : value }), (error) => land({ value: null, failed: error instanceof Error ? error.message : String(error) }));
123
+ asking.add(going);
124
+ track(going);
125
+ return 'asking';
126
+ }
127
+ /**
128
+ * What every expression of the form is handed of the app's — and, on a page calling Fieldia's functions whose bodies
129
+ * come in the functions part before it has come, the answers they wait for it with, as an app's function that asks.
130
+ */
131
+ const app = table ? { functions: table, answer } : !functions_1.functionsPart.loaded && (0, functions_1.callsPartFunctions)(page) ? { answer } : {};
132
+ const index = indexLayout(page.layout, table);
90
133
  /** Each field node's answer rules, read once. */
91
134
  const nodeRules = new Map();
92
135
  for (const node of index.values()) {
93
136
  const rules = node.source.validate;
94
137
  if (node.kind === 'field' && rules)
95
- nodeRules.set(node.id, (0, rules_1.compileRules)(rules));
138
+ nodeRules.set(node.id, (0, rules_1.compileRules)(rules, table));
96
139
  }
97
140
  /** Tables of lines with rules of their own, by their field node: each read once. */
98
141
  const tables = new Map();
@@ -101,14 +144,14 @@ function innerForm(options, within, place, outer) {
101
144
  if (node.kind !== 'field' || page.fields[source.field]?.type !== 'one2many')
102
145
  continue;
103
146
  if (source.cells || source.rowTones || source.rowBold !== undefined || source.rowButtons || source.lineDelete !== undefined)
104
- tables.set(node.id, { field: source.field, compiled: (0, cells_1.compileTable)(source) });
147
+ tables.set(node.id, { field: source.field, compiled: (0, cells_1.compileTable)(source, table) });
105
148
  }
106
149
  /** Field nodes whose value takes a tone or bold, by id: each read once. */
107
150
  const fieldTones = new Map();
108
151
  for (const node of index.values()) {
109
152
  const source = node.source;
110
153
  if (node.kind === 'field' && (source.tones || source.bold !== undefined))
111
- fieldTones.set(node.id, (0, cells_1.compileFieldTone)(source));
154
+ fieldTones.set(node.id, (0, cells_1.compileFieldTone)(source, table));
112
155
  }
113
156
  /** The tables whose lines load in an order of their own fields, by the field: the first node to give one. */
114
157
  const lineOrders = new Map();
@@ -136,7 +179,7 @@ function innerForm(options, within, place, outer) {
136
179
  const placeholders = new Map();
137
180
  const placeholderOf = (id, own) => {
138
181
  if (own.placeholderWhen?.length)
139
- placeholders.set(id, { fixed: own.placeholder, when: own.placeholderWhen.map((item) => ({ text: item.text, holds: (0, modifier_1.compileModifier)(item.when) })) });
182
+ placeholders.set(id, { fixed: own.placeholder, when: own.placeholderWhen.map((item) => ({ text: item.text, holds: (0, modifier_1.compileModifier)(item.when, table) })) });
140
183
  };
141
184
  for (const node of index.values())
142
185
  if (node.kind === 'field' && 'type' in node.source)
@@ -146,9 +189,11 @@ function innerForm(options, within, place, outer) {
146
189
  /** Whether any rule only warns: without one, there are never warnings to look for. */
147
190
  const warns = [...nodeRules.values()].some((rules) => rules.some((compiled) => compiled.rule.level === 'warning'));
148
191
  const steps = page.layout.type === 'wizard' ? page.layout.children.map((step) => step.id) : [];
149
- const draftKey = () => `fieldia:draft:${page.id}:${state.recordId ?? 'new'}`;
192
+ const storeKey = () => `fieldia:draft:${page.id}:${state.recordId ?? 'new'}`;
150
193
  /** Today, on the clock of the page's time zone (the browser's own when it names none). */
151
194
  const today = () => (0, zones_1.dayIn)(options.now?.() ?? new Date(), page.timeZone);
195
+ /** Whether a field's own words — a label, a choice's — show a value: found once, so a page with none checks as before. */
196
+ const worded = /\{[A-Za-z_]\w*(\|[^{}]*)?\}/.test(JSON.stringify(page.fields));
152
197
  const clock = () => (options.now?.() ?? new Date()).getTime();
153
198
  /** What a page's bot check saw — its hidden box filled, the captcha's token — and when the page was shown or started again. */
154
199
  let bot = { honeypot: false, captcha: null, since: clock() };
@@ -168,10 +213,10 @@ function innerForm(options, within, place, outer) {
168
213
  /** Whether the record is being edited now: not while a view locks it, nor when its source lets the person only read it. */
169
214
  const editingNow = () => editing && !(started && state.can?.edit === false);
170
215
  const about = () => ({ recordId: started ? state.recordId : (options.recordId ?? null), user: options.user, editing: editingNow(), context: options.context, parent: parentContext });
171
- const computed = (0, compute_1.compileComputed)(page, today, about);
216
+ const computed = (0, compute_1.compileComputed)(page, today, about, app);
172
217
  /** Each value worked out from others — by the page, or by the app as the lines change — and what it waits for. */
173
- const workedOut = (0, waits_1.workedOutFrom)(page, !!options.dataSource?.onchange && page.data.kind === 'record', !!options.parent);
174
- const setWhen = (0, set_when_1.compileSetWhen)(page, today, about);
218
+ const workedOut = (0, waits_1.workedOutFrom)(page, !!options.dataSource?.onchange && page.data.kind === 'record', !!options.parent, table);
219
+ const setWhen = (0, set_when_1.compileSetWhen)(page, today, about, app);
175
220
  /** The saved forms placed on the page, by the part's id: each one's answers sit under its name, as JSON. */
176
221
  const parts = new Map();
177
222
  for (const node of index.values())
@@ -223,7 +268,7 @@ function innerForm(options, within, place, outer) {
223
268
  const values = (0, values_1.initialValues)(fields, today());
224
269
  if (isNew) {
225
270
  const about = { recordId: null, user: options.user, context: options.context, parent: parentContext };
226
- Object.assign(values, (0, value_map_1.firstValues)(page.fields, mapScope(values, page.fields, (0, env_1.recordContext)(values, page.fields, about), (0, env_1.expressionEnv)(values, page.fields, today))));
271
+ Object.assign(values, (0, value_map_1.firstValues)(page.fields, mapScope(values, page.fields, (0, env_1.recordContext)(values, page.fields, about), (0, env_1.expressionEnv)(values, page.fields, today, app))));
227
272
  }
228
273
  return given ? ordered({ ...values, ...(0, values_1.structuredCopy)(options.values ?? {}) }) : values;
229
274
  }
@@ -252,7 +297,7 @@ function innerForm(options, within, place, outer) {
252
297
  function lineScope(field, values) {
253
298
  const def = lineField(field);
254
299
  const lineFields = def.fields;
255
- return mapScope(values, lineFields, (0, env_1.lineContext)(values, lineFields, reading().context), { today }, { values: state.values, fields: page.fields });
300
+ return mapScope(values, lineFields, (0, env_1.lineContext)(values, lineFields, reading().context), { ...app, today }, { values: state.values, fields: page.fields });
256
301
  }
257
302
  function tableOf(nodeId) {
258
303
  const table = tables.get(nodeId);
@@ -272,7 +317,7 @@ function innerForm(options, within, place, outer) {
272
317
  if (!line)
273
318
  throw new Error(`"${table.field}" has no line "${key}"`);
274
319
  const def = lineField(table.field);
275
- return table.compiled.line((0, env_1.lineContext)(line.values, def.fields, reading().context, line.id ?? null), { today }, userRoles);
320
+ return table.compiled.line((0, env_1.lineContext)(line.values, def.fields, reading().context, line.id ?? null), { ...app, today }, userRoles);
276
321
  }
277
322
  function createValuesOf(field, line) {
278
323
  if (!line) {
@@ -285,7 +330,9 @@ function innerForm(options, within, place, outer) {
285
330
  return {};
286
331
  return (0, value_map_1.readLooseMap)(sub.createValues, lineScope(line.lines, found.values));
287
332
  }
288
- let baseline = computed.apply(startingValues());
333
+ /** The values worked out from an app's function that asks with no answer yet, as the values shown last settled. */
334
+ let askedNow = new Map();
335
+ let baseline = computed.apply(startingValues(), askedNow);
289
336
  /** The setWhen conditions holding for the values as they last settled: one starts to hold only against these. */
290
337
  let holding = setWhen.holding(baseline);
291
338
  let state = {
@@ -312,6 +359,12 @@ function innerForm(options, within, place, outer) {
312
359
  unsent: [],
313
360
  leaving: false,
314
361
  unread: {},
362
+ sent: null,
363
+ asked: Object.fromEntries(askedNow),
364
+ takenOut: {},
365
+ // Worked out once the form has its state, as the limits part comes (`limitsCode`).
366
+ limits: null,
367
+ resume: null,
315
368
  };
316
369
  started = true;
317
370
  /**
@@ -484,7 +537,6 @@ function innerForm(options, within, place, outer) {
484
537
  let onchangeSeq = 0;
485
538
  let draftTimer = null;
486
539
  let autosaveTimer = null;
487
- const pending = new Set();
488
540
  const listeners = new Set();
489
541
  /** The app's listeners, by event. */
490
542
  const events = new Map();
@@ -498,10 +550,23 @@ function innerForm(options, within, place, outer) {
498
550
  const userRoles = [...(options.user?.roles ?? [])];
499
551
  /** The latest load of each field's choices: an answer to an older one is let go. */
500
552
  const choiceLoads = {};
553
+ /** The tables' columns whose choices are the app's: none on most pages, and then nothing below is followed. */
554
+ const listed = (0, line_choices_1.listedColumns)(page.fields);
555
+ /** Whether the values set now are a change someone made (`writeValues`), after which a line's choice no longer offered is cleared. */
556
+ let edit = false;
501
557
  function set(patch) {
502
558
  const was = state.values;
503
559
  const shown = state.errors;
560
+ const edited = edit;
561
+ edit = false;
562
+ const stepWas = state.step;
504
563
  state = { ...state, ...patch };
564
+ // A line's choices go with their line.
565
+ if (state.values !== was)
566
+ state = { ...state, choices: choicesNow(state.choices) };
567
+ // What waits for an app's answer, as the values shown last settled.
568
+ if (patch.values !== undefined && patch.asked === undefined && !sameAsked(state.asked, askedNow))
569
+ state = { ...state, asked: Object.fromEntries(askedNow) };
505
570
  // The person's changes the last save did not send, worked out again from the values as they are now, and from the
506
571
  // text they left that does not read.
507
572
  if (patch.unsent === undefined && (patch.values !== undefined || patch.unread !== undefined)) {
@@ -517,15 +582,21 @@ function innerForm(options, within, place, outer) {
517
582
  listener(state);
518
583
  // Choices that change with a value that just changed are asked for again.
519
584
  if (state.values !== was) {
585
+ // After someone's change, a choice its new choices do not offer is taken out and said, as a line's is.
520
586
  for (const name in state.choices)
521
- if (fromList(name).dependsOn?.some((other) => was[other] !== state.values[other]))
522
- loadChoices(name);
587
+ if (!name.includes('.') && fromList(name)?.dependsOn?.some((other) => was[other] !== state.values[other]))
588
+ loadChoices(name, edited);
523
589
  for (const name in state.definitions)
524
590
  if (fromRecord(name).dependsOn?.some((other) => was[other] !== state.values[other]))
525
591
  loadDefinitions(name);
592
+ if (listed.length)
593
+ followLines(was, edited);
526
594
  }
527
595
  if (showing && state.step !== told)
528
596
  tellStep(entering);
597
+ // A wizard's step entered with answers typed is kept in the draft, so it opens there again.
598
+ if (state.step !== stepWas && showing && state.dirty.length)
599
+ scheduleDraft();
529
600
  }
530
601
  function emit(event, payload) {
531
602
  const heard = events.get(event);
@@ -548,6 +619,9 @@ function innerForm(options, within, place, outer) {
548
619
  /** Another record is shown: nothing of the last one's is the person's work any more, and its files on their way stop. */
549
620
  function newGeneration() {
550
621
  generation++;
622
+ // Another record's lines ask the source anew.
623
+ lineAsks = new Map();
624
+ answers = new Map();
551
625
  personal.clear();
552
626
  personalLines.clear();
553
627
  filed.clear();
@@ -672,16 +746,127 @@ function innerForm(options, within, place, outer) {
672
746
  entering = was;
673
747
  }
674
748
  }
675
- const fromList = (name) => page.fields[name].optionsFrom;
676
- function loadChoices(name) {
677
- const { list } = fromList(name);
749
+ /** The app's list a choice's options come from; none for one whose options are its own or carried. */
750
+ const fromList = (name) => (0, choice_options_1.optionsList)(page.fields[name]?.optionsFrom);
751
+ // ---- a table line's choices from the app's lists (line-choices.ts) ----------------------------------------------
752
+ /** A line's choices asked of the source, by what they are asked by (`askKey`), for the record shown: lines alike share one. */
753
+ let lineAsks = new Map();
754
+ /** What each line cell's choices were last asked by. */
755
+ const askedBy = new Map();
756
+ const listedAt = (table, column) => listed.find((one) => one.table === table && one.column === column);
757
+ const lineAt = (table, key) => (state.values[table] ?? []).find((line) => line.key === key);
758
+ /** A line's choices from the source: asked once for every line whose columns they change with hold the same values. */
759
+ function askLine(one, values) {
760
+ const key = (0, line_choices_1.askKey)(one, values, state.values);
761
+ const asks = lineAsks;
762
+ let ask = asks.get(key);
763
+ if (ask)
764
+ return ask;
765
+ const source = options.dataSource;
766
+ const request = { list: one.list, values: (0, values_1.structuredCopy)(values), parent: (0, values_1.structuredCopy)(state.values) };
767
+ ask = source?.options ? source.options(request) : Promise.reject(new Error(`No list "${one.list}"`));
768
+ asks.set(key, ask);
769
+ // One that failed is asked again next time.
770
+ ask.catch(() => asks.get(key) === ask && asks.delete(key));
771
+ return ask;
772
+ }
773
+ /** The choices as the values are now: a line's go with their line. */
774
+ function choicesNow(choices) {
775
+ let kept = null;
776
+ for (const path of Object.keys(choices)) {
777
+ if (valueAt(state.values, path) !== gone)
778
+ continue;
779
+ kept ??= { ...choices };
780
+ delete kept[path];
781
+ askedBy.delete(path);
782
+ }
783
+ return kept ?? choices;
784
+ }
785
+ /**
786
+ * Values changed: each line's choices loaded are asked again where what they change with changed on their line (a
787
+ * record shown with the same line, Discard, a draft); after a change someone made (`edited`), judged too — and so is a
788
+ * line holding a choice whose choices nobody asked for yet, so none is kept that its columns no longer offer.
789
+ */
790
+ function followLines(was, edited) {
791
+ // Choices that change with nothing are asked once.
792
+ for (const one of listed.filter((column) => column.own.length || column.parent.length)) {
793
+ for (const line of state.values[one.table] ?? []) {
794
+ const path = `${one.table}.${line.key}.${one.column}`;
795
+ const by = (0, line_choices_1.askKey)(one, line.values, state.values);
796
+ const old = !(path in state.choices) && edited && !(0, values_1.isEmpty)(one.def, line.values[one.column]) && (was[one.table] ?? []).find((l) => l.key === line.key);
797
+ if (path in state.choices ? by !== askedBy.get(path) : old && by !== (0, line_choices_1.askKey)(one, old.values, was))
798
+ loadChoices(path, edited);
799
+ }
800
+ }
801
+ }
802
+ /**
803
+ * Load a field's choices, or a line cell's by its path (`table.key.column`). `judge`, after a change someone made of
804
+ * what they change with: a choice they no longer offer is taken out and said, on the record they were asked for,
805
+ * while a line still asks by the same.
806
+ */
807
+ function loadChoices(name, judge = false) {
808
+ const cell = (0, line_choices_1.cellOf)(name);
809
+ const one = cell && listedAt(cell.table, cell.column);
810
+ const line = cell && lineAt(cell.table, cell.key);
811
+ if (cell && (!one || !line))
812
+ throw new Error(`"${name}" is no line's choice from the app's lists`);
813
+ const from = one ?? fromList(name);
814
+ if (!from)
815
+ throw new Error(`"${name}" takes no choices from the app's lists`);
816
+ const { list } = from;
678
817
  const load = (choiceLoads[name] = (choiceLoads[name] ?? 0) + 1);
679
- const put = (choices) => load === choiceLoads[name] && set({ choices: { ...state.choices, [name]: choices } });
818
+ const by = one && line ? (0, line_choices_1.askKey)(one, line.values, state.values) : '';
819
+ if (cell)
820
+ askedBy.set(name, by);
821
+ // A line's answer lands while its line is there.
822
+ const put = (choices) => load === choiceLoads[name] && (!cell || name in state.choices) && set({ choices: { ...state.choices, [name]: choices } });
680
823
  // What was shown stays while the next ones load, so the list does not flash empty.
681
824
  const shown = state.choices[name]?.options ?? null;
682
- put({ options: shown, loading: true });
825
+ const loading = { options: shown, loading: true };
826
+ // A line's cell asks first as it is drawn, inside its table's drawing: told with the next change, never drawn again
827
+ // inside it. Asked again, as a column they change with changes, it says it is loading at once.
828
+ if (cell && !(name in state.choices))
829
+ state = { ...state, choices: { ...state.choices, [name]: loading } };
830
+ else
831
+ set({ choices: { ...state.choices, [name]: loading } });
683
832
  const source = options.dataSource;
684
- void track((source?.options ? source.options({ list, values: (0, values_1.structuredCopy)(state.values) }) : Promise.reject(new Error(`No list "${list}"`))).then((loaded) => put({ options: loaded }), (error) => put({ options: shown, error: error.message ?? String(error) })));
833
+ const onRecord = shownNow();
834
+ const ask = one && line
835
+ ? askLine(one, line.values)
836
+ : source?.options
837
+ ? // A line's own form (a dialog) asks as its line does: the record it is on as parent.
838
+ source.options({ list, values: (0, values_1.structuredCopy)(state.values), ...(options.parent ? { parent: (0, values_1.structuredCopy)(options.parent.values) } : {}) })
839
+ : Promise.reject(new Error(`No list "${list}"`));
840
+ void track(ask.then((loaded) => {
841
+ if (load !== choiceLoads[name])
842
+ return;
843
+ const now = cell && lineAt(cell.table, cell.key);
844
+ const held = !cell ? state.values[name] : now && (0, line_choices_1.askKey)(one, now.values, state.values) === by ? now.values[cell.column] : null;
845
+ const lost = judge && onRecord() ? (0, line_choices_1.notOffered)(one?.def ?? page.fields[name], held, loaded) : [];
846
+ // Taken out and said: the words stay until its value changes again.
847
+ if (lost.length)
848
+ clearChoices(cell ? cell.table : name, cell, lost, namedChoices(lost, shown, loaded));
849
+ put({ options: loaded });
850
+ // Options of the app's list with places, never counted yet: how many took each is asked.
851
+ if (!cell && loaded.some((option) => option.seats !== undefined) && (!limitsKit || limitsKit.uncounted(name)))
852
+ void track(limitsCode().then((kit) => kit?.ask()));
853
+ }, (error) => put({ options: shown, error: error.message ?? String(error) })));
854
+ }
855
+ /** Choices by their labels, as the choices shown before or now name them. */
856
+ const namedChoices = (lost, shown, loaded) => lost.map((value) => [...(shown ?? []), ...loaded].find((option) => option.value === value)?.label ?? String(value));
857
+ /**
858
+ * A choice — a field's, or a line's (`key`) column — less what its choices no longer offer, worked out from someone's
859
+ * change, and said as every choice taken out is (`FormState.takenOut`): their claim stays.
860
+ */
861
+ function clearChoices(field, line, lost, labels) {
862
+ const without = (value) => (Array.isArray(value) ? value.filter((v) => !lost.includes(v)) : null);
863
+ const values = state.values;
864
+ const written = line
865
+ ? (values[field] ?? []).map((l) => (l.key === line.key ? { ...l, values: { ...l.values, [line.column]: without(l.values[line.column]) } } : l))
866
+ : without(values[field]);
867
+ writeValues({ ...values, [field]: written }, {}, false, { [line ? `${field}.${line.key}.${line.column}` : field]: takenOutWords(labels) });
868
+ afterEdit(field);
869
+ changed(field, 'step', { keep: true });
685
870
  }
686
871
  const fromRecord = (name) => page.fields[name].definitionsFrom;
687
872
  const definitionLoads = {};
@@ -701,8 +886,23 @@ function innerForm(options, within, place, outer) {
701
886
  set({ definitions: { ...state.definitions, [name]: { definitions } } });
702
887
  await track(options.dataSource?.saveDefinitions?.({ list, values: (0, values_1.structuredCopy)(state.values), definitions }) ?? Promise.resolve());
703
888
  }
704
- /** A field as its value is checked: a list's choices are those loaded, and until they are, any choice may be one. */
889
+ /**
890
+ * A field as its value is checked: a list's choices are those loaded, and until they are, any choice may be one; its
891
+ * label and its choices' words with their `{field}`s filled in, as the page shows them.
892
+ */
705
893
  function checked(name) {
894
+ const def = checkedAsLoaded(name);
895
+ return worded ? shownWords(def) : def;
896
+ }
897
+ /** A field whose words show values — its label, its choices' — with them filled in now, else as it is. */
898
+ function shownWords(def) {
899
+ const options = def.type === 'selection' && def.options.some((option) => option.label.includes('{')) ? def.options.map((option) => ({ ...option, label: wordsNow(option.label) })) : null;
900
+ if (!def.label.includes('{') && !options)
901
+ return def;
902
+ return { ...def, label: wordsNow(def.label), ...(options ? { options } : {}) };
903
+ }
904
+ /** A field as its value is checked, its choices those loaded from the app's list. */
905
+ function checkedAsLoaded(name) {
706
906
  const def = page.fields[name];
707
907
  const loaded = state.choices[name]?.options;
708
908
  if (def.type === 'properties' && def.definitionsFrom) {
@@ -710,7 +910,73 @@ function innerForm(options, within, place, outer) {
710
910
  const definitions = state.definitions[name]?.definitions;
711
911
  return definitions ? { ...def, definitions } : def;
712
912
  }
713
- return def.type === 'selection' && def.optionsFrom && loaded ? { ...def, options: loaded, optionsFrom: undefined } : def;
913
+ // Carried from an earlier question: any of its options, and its own after them, is one of this one's.
914
+ if (def.type === 'selection' && (0, choice_options_1.optionsField)(def.optionsFrom))
915
+ return { ...def, options: everyOption(name), optionsFrom: undefined };
916
+ return def.type === 'selection' && (0, choice_options_1.optionsList)(def.optionsFrom) && loaded ? { ...def, options: loaded, optionsFrom: undefined } : def;
917
+ }
918
+ /** Every option a choice may hold, whatever its conditions: its own, its list's once loaded, or those of the question it carries them from and its own. */
919
+ function everyOption(name, seen = []) {
920
+ const def = page.fields[name];
921
+ if (def?.type !== 'selection')
922
+ return [];
923
+ const carried = (0, choice_options_1.optionsField)(def.optionsFrom);
924
+ if (carried && !seen.includes(carried.field))
925
+ return [...everyOption(carried.field, [...seen, name]).filter((o) => !o.exclusive), ...def.options];
926
+ return [...((fromList(name) && state.choices[name]?.options) || def.options)];
927
+ }
928
+ // ---- options by condition, and carried from an earlier question (the options part: option-rules.ts) -----------
929
+ /** The options' rules made for this form from the options part, once it came: none before, nor on a page with none. */
930
+ let optionKit = null;
931
+ /**
932
+ * The options' rules, made from their part as it comes — at once where it has come — or null on a page with none, or
933
+ * where the part could not come. Values written before they came are judged as they come: a chosen option the values
934
+ * no longer offer, since the form was made, is taken out and said.
935
+ */
936
+ function optionsCode() {
937
+ if (optionKit || !(0, choice_options_1.hasOptionRules)(page))
938
+ return Promise.resolve(optionKit);
939
+ if (choice_options_1.optionRulesPart.loaded)
940
+ return Promise.resolve(optionsMade(choice_options_1.optionRulesPart.loaded));
941
+ const from = state.values;
942
+ return choice_options_1.optionRulesPart.load().then((part) => {
943
+ const kit = optionsMade(part);
944
+ if (kit && !disposed && state.values !== from)
945
+ writeValues(state.values, {}, false, {}, from);
946
+ return kit;
947
+ }, () => null);
948
+ }
949
+ function optionsMade(part) {
950
+ optionKit ??= part.optionRules({ page, table, app, state: () => state, today, about, reading, settle, words: (labels) => takenOutWords(labels) });
951
+ return optionKit;
952
+ }
953
+ if ((0, choice_options_1.hasOptionRules)(page))
954
+ void track(optionsCode());
955
+ /** A table line's choice's options as they are offered on that line: its list's once loaded, or its own, by their conditions once they came. */
956
+ function lineOffered(lines, line, column, record) {
957
+ if (optionKit)
958
+ return optionKit.lineOffered(lines, line, column, record);
959
+ const sub = page.fields[lines].fields[column];
960
+ return [...(((0, choice_options_1.optionsList)(sub.optionsFrom) && state.choices[`${lines}.${line.key}.${column}`]?.options) || sub.options)];
961
+ }
962
+ /** Options in words, as a sentence joins them: "Garden and Pool". */
963
+ function listedLabels(labels) {
964
+ try {
965
+ return new Intl.ListFormat(messages.locale, { type: 'conjunction' }).format(labels);
966
+ }
967
+ catch {
968
+ return labels.join(', ');
969
+ }
970
+ }
971
+ /**
972
+ * What is said of options taken out of an answer, by their labels — an answer they show (`{branch}`) as it reads now:
973
+ * "Garden is no longer offered, so it was taken out".
974
+ */
975
+ const takenOutWords = (labels) => (0, messages_1.fillMessage)(messages, labels.length > 1 ? messages.notOfferedNowMany : messages.notOfferedNow, { name: listedLabels(labels.map((label) => (label.includes('{') ? wordsNow(label) : label))) });
976
+ /** A line's field as its value is checked: its choices from the app's list, those loaded for its line. */
977
+ function checkedLine(path, def) {
978
+ const loaded = def.type === 'selection' && (0, choice_options_1.optionsList)(def.optionsFrom) ? state.choices[path]?.options : null;
979
+ return loaded ? { ...def, options: loaded, optionsFrom: undefined } : def;
714
980
  }
715
981
  /** A properties field's problems, each at its property's path (`field.property`), as its row shows them. */
716
982
  function propertyProblems(name) {
@@ -729,12 +995,57 @@ function innerForm(options, within, place, outer) {
729
995
  promise.then(done, done);
730
996
  return promise;
731
997
  }
998
+ /**
999
+ * Words with each `{field}` showing the value `values` hold (the form's now, unless given), as the app's viewer
1000
+ * writes it (`valueWords`), else plainly; `other` answers a name first (an ending's `{id}`). A name that is no field
1001
+ * shows nothing.
1002
+ */
1003
+ function wordsNow(text, values = state.values, other) {
1004
+ if (!text.includes('{'))
1005
+ return text;
1006
+ return (0, words_1.fillValues)(text, (name) => {
1007
+ const own = other?.(name);
1008
+ if (own !== undefined)
1009
+ return own;
1010
+ const def = page.fields[name];
1011
+ if (!def)
1012
+ return '';
1013
+ return options.valueWords ? options.valueWords(def, values[name], values) : wordsOfValue(def, values[name]);
1014
+ });
1015
+ }
1016
+ /**
1017
+ * Each ending's condition, read once — with Fieldia's functions and the app's own, as every condition of the page —
1018
+ * null for the default (no `when`), and one that does not read never holds.
1019
+ */
1020
+ const endingConditions = (page.endings ?? []).map((ending) => {
1021
+ if (ending.when === undefined)
1022
+ return null;
1023
+ try {
1024
+ return (0, modifier_1.compileModifier)(ending.when, table);
1025
+ }
1026
+ catch {
1027
+ return (0, modifier_1.compileModifier)(false);
1028
+ }
1029
+ });
1030
+ /** The ending for the response sent: the first whose condition holds on it — its answers, what the source added, its id — else the default. */
1031
+ function endingNow() {
1032
+ const sent = state.sent;
1033
+ const endings = page.endings;
1034
+ if (!sent || !endings?.length)
1035
+ return null;
1036
+ const values = sent.values;
1037
+ const context = (0, env_1.recordContext)(values, page.fields, { ...about(), recordId: sent.id });
1038
+ // An app's function that asks its server is asked as any condition asks it: it holds once answered.
1039
+ const env = (0, env_1.expressionEnv)(values, page.fields, today, app);
1040
+ const at = endingConditions.findIndex((condition) => !condition || condition.evaluate(context, env));
1041
+ return at === -1 ? null : endings[at];
1042
+ }
732
1043
  /** The values as expressions read them, and what else they read: the record's id, the person, the lines' rows, today. */
733
1044
  function reading() {
734
1045
  if (contextCache?.values !== state.values || contextCache.recordId !== state.recordId || contextCache.editing !== editingNow()) {
735
1046
  const values = state.values;
736
1047
  const context = (0, env_1.recordContext)(values, page.fields, about());
737
- contextCache = { values, recordId: state.recordId, editing: editingNow(), context, env: (0, env_1.expressionEnv)(values, page.fields, today) };
1048
+ contextCache = { values, recordId: state.recordId, editing: editingNow(), context, env: (0, env_1.expressionEnv)(values, page.fields, today, app) };
738
1049
  }
739
1050
  return contextCache;
740
1051
  }
@@ -772,6 +1083,10 @@ function innerForm(options, within, place, outer) {
772
1083
  if ((node.kind === 'field' || node.kind === 'form') && (can.edit === false || (node.field !== undefined && can.readonlyFields?.includes(node.field) === true)))
773
1084
  readonly = true;
774
1085
  }
1086
+ // An answer a signature kept signed: locked until the signature is taken away and signed again.
1087
+ const answer = answerOf(node);
1088
+ if (answer !== undefined && signedNow().has(answer))
1089
+ readonly = true;
775
1090
  return {
776
1091
  invisible,
777
1092
  readonly: readonly || (def !== undefined && lockedField(def)),
@@ -779,6 +1094,17 @@ function innerForm(options, within, place, outer) {
779
1094
  ...(node.pressed ? { pressed: node.pressed.evaluate(ctx, env) } : {}),
780
1095
  };
781
1096
  }
1097
+ /** The answer a field node shows, by its field — or a saved form placed on the page, by the name its answers are kept under. */
1098
+ function answerOf(node) {
1099
+ return node.kind === 'field' ? node.field : node.kind === 'form' ? node.source.name : undefined;
1100
+ }
1101
+ /** The answers signatures signed, as the values hold them now: worked out once for each change of the values. */
1102
+ let signedCache = null;
1103
+ function signedNow() {
1104
+ if (signedCache?.values !== state.values)
1105
+ signedCache = { values: state.values, answers: (0, signed_1.signedBy)(page, state.values) };
1106
+ return signedCache.answers;
1107
+ }
782
1108
  /** Field nodes — or saved forms placed on the page — inside `root` (or the whole page), visible right now; `field`'s alone, when named. */
783
1109
  function visibleFieldNodes(root, kind = 'field', field) {
784
1110
  const inside = (node) => {
@@ -807,7 +1133,7 @@ function innerForm(options, within, place, outer) {
807
1133
  return {};
808
1134
  const name = node.field;
809
1135
  const def = page.fields[name];
810
- const label = node.source.label ?? def.label;
1136
+ const label = wordsNow(node.source.label ?? def.label);
811
1137
  const { context: ctx, env } = reading();
812
1138
  return (0, rules_1.checkRules)(def, label, state.values[name], rules, { context: ctx, env, now: options.now?.() ?? new Date(), messages });
813
1139
  }
@@ -820,6 +1146,14 @@ function innerForm(options, within, place, outer) {
820
1146
  /** The widget a field is shown with where it keeps its value its own way (a per cent): the first of its nodes'. */
821
1147
  const shownWidget = (shown) => shown.map((node) => node.source.widget).find((widget) => widget === 'percentage');
822
1148
  /** A field node's error now: its field's own rules first, then its answer rules. */
1149
+ /** A path's field's label: a field's, or a line's cell's (`field.lineKey.subfield`). */
1150
+ function labelOfPath(path) {
1151
+ const parts = path.split('.');
1152
+ const def = page.fields[parts[0]];
1153
+ if (parts.length >= 3 && def?.type === 'one2many')
1154
+ return def.fields[parts[parts.length - 1]]?.label ?? path;
1155
+ return def?.label ?? path;
1156
+ }
823
1157
  function nodeError(node) {
824
1158
  const name = node.field;
825
1159
  return unticked(node) ?? (0, inputs_1.checkInput)(page.fields[name], node.source, state.values[name], messages) ?? (0, check_1.checkValue)(checked(name), state.values[name], nodeState(node.id).required, messages, today(), node.source.widget) ?? ruleCheck(node).error;
@@ -868,7 +1202,7 @@ function innerForm(options, within, place, outer) {
868
1202
  return undefined;
869
1203
  const at = (v) => (def.type === 'datetime' ? Date.parse(v) : Date.parse(v.slice(0, 10)));
870
1204
  if (at(value) < at(start))
871
- return (0, messages_1.fillMessage)(messages, messages.endBeforeStart, { end: def.label, start: page.fields[node.field].label });
1205
+ return (0, messages_1.fillMessage)(messages, messages.endBeforeStart, { end: wordsNow(def.label), start: wordsNow(page.fields[node.field].label) });
872
1206
  return undefined;
873
1207
  }
874
1208
  /** Errors for the visible fields under `root`, without publishing them. */
@@ -906,7 +1240,7 @@ function innerForm(options, within, place, outer) {
906
1240
  const cell = ruled[sub];
907
1241
  if (cell?.invisible || (tables.has(node.id) && tables.get(node.id).compiled.hidden(sub, reading().context, reading().env, userRoles)))
908
1242
  continue;
909
- const lineMessage = (0, check_1.checkValue)(subDef, line.values[sub], subDef.required === true || cell?.required === true, messages, today(), node.source.cells?.[sub]?.widget);
1243
+ const lineMessage = (0, check_1.checkValue)(checkedLine(`${name}.${line.key}.${sub}`, subDef), line.values[sub], subDef.required === true || cell?.required === true, messages, today(), node.source.cells?.[sub]?.widget);
910
1244
  if (lineMessage)
911
1245
  errors[`${name}.${line.key}.${sub}`] = lineMessage;
912
1246
  }
@@ -917,6 +1251,16 @@ function innerForm(options, within, place, outer) {
917
1251
  for (const [path, unread] of Object.entries(state.unread))
918
1252
  if (seen.has(path.split('.')[0]))
919
1253
  errors[path] = unread.message;
1254
+ // A value an app's function is asked for answers for its field: still asked, or why it did not come.
1255
+ for (const [path, found] of Object.entries(state.asked)) {
1256
+ if (!seen.has(path.split('.')[0]) || errors[path])
1257
+ continue;
1258
+ const label = labelOfPath(path);
1259
+ errors[path] = found.failed === undefined ? (0, messages_1.fillMessage)(messages, messages.stillWorkedOut, { label }) : (0, messages_1.fillMessage)(messages, messages.notWorkedOut, { label, problem: found.failed });
1260
+ }
1261
+ // A choice held whose places filled up since it was picked: never sent, said under it.
1262
+ if (limitsKit)
1263
+ Object.assign(errors, limitsKit.fullErrors(seen, errors));
920
1264
  // A saved form placed here answers for its fields by its own page's rules.
921
1265
  for (const node of visibleFieldNodes(root, 'form')) {
922
1266
  const part = attached.get(node.id);
@@ -941,6 +1285,13 @@ function innerForm(options, within, place, outer) {
941
1285
  const line = (values[field] ?? []).find((l) => l.key === key);
942
1286
  return line ? line.values[sub] : gone;
943
1287
  }
1288
+ /** A value — a field's, or a table line's by its cell's path — the same in both: a line that is gone, none. */
1289
+ function sameAt(a, b, path) {
1290
+ if (!path.includes('.'))
1291
+ return a[path] === b[path];
1292
+ const now = valueAt(a, path);
1293
+ return now !== gone && JSON.stringify(now ?? null) === JSON.stringify(valueAt(b, path) ?? null);
1294
+ }
944
1295
  /** Unread text whose field has since been written another way — a value typed that reads, the app's — is let go. */
945
1296
  function unreadKept(values) {
946
1297
  let kept = null;
@@ -981,7 +1332,7 @@ function innerForm(options, within, place, outer) {
981
1332
  const value = valueAt(state.values, path);
982
1333
  if (!sub || value === gone || (0, values_1.isEmpty)(sub, value))
983
1334
  return undefined;
984
- return (0, check_1.checkValue)(sub, value, false, messages, today());
1335
+ return (0, check_1.checkValue)(checkedLine(path, sub), value, false, messages, today());
985
1336
  }
986
1337
  /**
987
1338
  * Answers changed from outside a saved form — loaded, put back, set — handed
@@ -1010,25 +1361,75 @@ function innerForm(options, within, place, outer) {
1010
1361
  * few rounds at most, so rules that set each other cannot run forever.
1011
1362
  */
1012
1363
  function settle(written) {
1013
- let values = computed.apply(written);
1364
+ let asked = new Map();
1365
+ let values = computed.apply(written, asked);
1014
1366
  for (let round = 0; round < 8; round++) {
1015
1367
  const result = setWhen.apply(values, holding);
1016
1368
  holding = result.holding;
1017
1369
  if (result.values === values)
1018
1370
  break;
1019
- values = computed.apply(result.values);
1371
+ asked = new Map();
1372
+ values = computed.apply(result.values, asked);
1020
1373
  }
1374
+ askedNow = asked;
1021
1375
  return values;
1022
1376
  }
1023
1377
  /** Values to start again from, as loaded or restored: conditions holding in them hold already, and set nothing. */
1024
1378
  function startFrom(values) {
1025
- const worked = computed.apply(values);
1379
+ const asked = new Map();
1380
+ const worked = computed.apply(values, asked);
1381
+ askedNow = asked;
1026
1382
  holding = setWhen.holding(worked);
1027
1383
  return worked;
1028
1384
  }
1029
- /** Write values someone or something changed. `fresh` values are a starting point instead (a restored draft). */
1030
- function writeValues(written, extra = {}, fresh = false) {
1031
- const values = syncParts(fresh ? startFrom(written) : settle(written), fresh ? 'fresh' : 'edit');
1385
+ /**
1386
+ * An app's function answered: the values worked out again with its answer, and the record as loaded with them, so
1387
+ * an answer is never a change of the person's. Values no one has changed start again from it — no rule starts to
1388
+ * hold on what only came late — and changed ones settle as any change does.
1389
+ */
1390
+ function answered() {
1391
+ baseline = computed.apply(baseline);
1392
+ if (state.dirty.length)
1393
+ return writeValues(state.values);
1394
+ const values = startFrom(state.values);
1395
+ set({ values, dirty: dirtyFields(values) });
1396
+ }
1397
+ /** Every answer on its way, and those that come of them, waited for: what a save does before it checks. */
1398
+ async function asksSettled() {
1399
+ while (asking.size)
1400
+ await Promise.all([...asking]);
1401
+ }
1402
+ /** Answers that failed asked again — a save's try — and waited for. */
1403
+ async function askAgain() {
1404
+ let failed = false;
1405
+ for (const [key, found] of answers) {
1406
+ if (found === 'asking' || found.failed === undefined)
1407
+ continue;
1408
+ answers.delete(key);
1409
+ failed = true;
1410
+ }
1411
+ if (failed)
1412
+ writeValues(state.values);
1413
+ await asksSettled();
1414
+ }
1415
+ /**
1416
+ * Write values someone or something changed. `fresh` values are a starting point instead (a restored draft). `said`:
1417
+ * options taken out of an answer by the write, by path, with their words (`FormState.takenOut`); `from`, the values
1418
+ * those the write no longer offers were offered in (the form's now, unless said).
1419
+ */
1420
+ function writeValues(written, extra = {}, fresh = false, said = {}, from = state.values) {
1421
+ let settled = fresh ? startFrom(written) : settle(written);
1422
+ // A choice written again says no more what was taken out of it — a line's, nor once its line is gone; options a
1423
+ // change no longer offers are taken out now.
1424
+ let takenOut = fresh ? {} : { ...Object.fromEntries(Object.entries(state.takenOut).filter(([path]) => sameAt(written, state.values, path))), ...said };
1425
+ const out = fresh || !optionKit ? null : optionKit.takeOut(from, settled);
1426
+ if (out) {
1427
+ settled = out.values;
1428
+ takenOut = { ...takenOut, ...out.said };
1429
+ }
1430
+ if (Object.keys(takenOut).length || Object.keys(state.takenOut).length)
1431
+ extra = { takenOut, ...extra };
1432
+ const values = syncParts(settled, fresh ? 'fresh' : 'edit');
1032
1433
  // Text left unread in a box goes once its field is written another way, and its words with it.
1033
1434
  const unread = unreadKept(values);
1034
1435
  const before = state;
@@ -1054,6 +1455,8 @@ function innerForm(options, within, place, outer) {
1054
1455
  }
1055
1456
  if (Object.keys(words).length)
1056
1457
  errors = { ...errors, ...words };
1458
+ // A change someone made: a line's choice its columns no longer offer is cleared (followLines); a draft restored is none.
1459
+ edit = !fresh;
1057
1460
  set({ values, dirty: dirtyFields(values), errors, warnings: withValues(values, collectWarnings), unread, ...extra });
1058
1461
  }
1059
1462
  /** Once errors are on screen, keep them current as the person fixes things. */
@@ -1076,24 +1479,84 @@ function innerForm(options, within, place, outer) {
1076
1479
  kept[key] ??= unstoredNow[key];
1077
1480
  return kept;
1078
1481
  }
1482
+ /**
1483
+ * Whether drafts are kept on the app's server: its source keeps them, and the page offers "Continue later" (or the
1484
+ * form continues one by its key). A page without either keeps its drafts where it always did.
1485
+ */
1486
+ const onServer = () => !!options.dataSource?.saveDraft && !!options.dataSource.loadDraft && (page.continueLater === true || !!options.resume);
1487
+ /** Whether drafts are kept: the app gave a store, or they are kept on the server. */
1488
+ const draftsOn = () => !!options.drafts || onServer();
1489
+ /** The key of the draft kept on the server for the record shown; null until the source gave one. */
1490
+ let serverKey = null;
1491
+ /** Counted each time the draft is let go (sent, discarded, another record shown): a keeping answered after is dropped. */
1492
+ let draftEpoch = 0;
1493
+ /** The draft kept on the server last asked to be kept: the next waits for it, so it carries the key it gave. */
1494
+ let draftGoing = Promise.resolve();
1495
+ /** How many drafts are on their way to the server. */
1496
+ let keeping = 0;
1497
+ /** The draft found as the form opened, offered to restore (`state.draft`). */
1498
+ let found = null;
1499
+ /** The key the form was given to continue from (`resume`), until it is opened. */
1500
+ let resumeKey = outer ? undefined : options.resume;
1079
1501
  function scheduleDraft() {
1080
- const drafts = options.drafts;
1081
- if (!drafts)
1502
+ if (!draftsOn())
1082
1503
  return;
1083
1504
  if (draftTimer !== null)
1084
1505
  scheduler.clearTimeout(draftTimer);
1085
1506
  draftTimer = scheduler.setTimeout(() => {
1086
1507
  draftTimer = null;
1087
- // Where files are stored apart, never a file's bytes: they would fill the browser's store (uploads.ts). Without, kept as always.
1088
- // A store that is full anyway keeps no draft.
1089
- const values = options.dataSource?.upload ? draftOf(state.values) : state.values;
1090
- try {
1091
- drafts.store.setItem(draftKey(), JSON.stringify({ savedAt: new Date().toISOString(), values }));
1092
- }
1093
- catch {
1094
- // The draft is a convenience: the form goes on without it.
1095
- }
1096
- }, drafts.delayMs ?? 400);
1508
+ // On the server, a failure is kept in the browser's store instead, where there is one: the form goes on.
1509
+ void track(keepDraft(false).catch(() => keepHere(draftNow())));
1510
+ }, options.drafts?.delayMs ?? (onServer() ? 1500 : 400));
1511
+ }
1512
+ /** The draft of what is typed now: the answers, the wizard's step and the steps passed over, text that does not read. */
1513
+ function draftNow() {
1514
+ // Where files are stored apart, never a file's bytes: they would fill the browser's store (uploads.ts). Without, kept as always.
1515
+ const values = options.dataSource?.upload ? draftOf(state.values) : (0, values_1.structuredCopy)(state.values);
1516
+ return {
1517
+ values,
1518
+ ...(steps.length ? { step: state.step, skipped: [...state.skipped] } : {}),
1519
+ ...(Object.keys(state.unread).length ? { unread: (0, values_1.structuredCopy)(state.unread) } : {}),
1520
+ answered: Object.keys(fields).filter((name) => state.dirty.includes(name)),
1521
+ savedAt: new Date(clock()).toISOString(),
1522
+ };
1523
+ }
1524
+ /** A draft in the browser's store, where the app gave one: a store that is full keeps none. */
1525
+ function keepHere(kept) {
1526
+ try {
1527
+ options.drafts?.store.setItem(storeKey(), JSON.stringify(kept));
1528
+ }
1529
+ catch {
1530
+ // The draft is a convenience: the form goes on without it.
1531
+ }
1532
+ }
1533
+ /**
1534
+ * Keep what is typed now: on the server where the source keeps drafts — one at a time, each carrying the key the last
1535
+ * was given; its key in the browser's store — else in the browser's store. The server's answer, or null.
1536
+ */
1537
+ function keepDraft(later) {
1538
+ const draft = draftNow();
1539
+ const source = options.dataSource;
1540
+ if (!onServer() || !source?.saveDraft) {
1541
+ keepHere(draft);
1542
+ return Promise.resolve(null);
1543
+ }
1544
+ const shown = shownNow();
1545
+ const epoch = draftEpoch;
1546
+ keeping++;
1547
+ const going = draftGoing.then(() => source.saveDraft({ pageId: page.id, model: page.data.kind === 'record' ? page.data.model : '', recordId: state.recordId, key: epoch === draftEpoch ? serverKey : null, draft, ...(later ? { later } : {}) }));
1548
+ draftGoing = going.then(() => void keeping--, () => void keeping--);
1549
+ return going.then((saved) => {
1550
+ // Kept for a record no longer shown, or for answers sent or let go meanwhile: the source has it; nothing is laid here.
1551
+ if (!shown() || epoch !== draftEpoch)
1552
+ return saved;
1553
+ serverKey = saved.key;
1554
+ if (state.resume?.key !== saved.key || state.resume.missing)
1555
+ set({ resume: { key: saved.key } });
1556
+ keepHere({ savedAt: draft.savedAt, key: saved.key });
1557
+ emit('draft', { key: saved.key, savedAt: draft.savedAt, later, saved });
1558
+ return saved;
1559
+ });
1097
1560
  }
1098
1561
  /** The values with a draft's over them: a field it left out — a file's bytes — as the record holds it, in a saved form placed here too. */
1099
1562
  function withDraft(kept) {
@@ -1105,32 +1568,144 @@ function innerForm(options, within, place, outer) {
1105
1568
  }
1106
1569
  return values;
1107
1570
  }
1571
+ /** A draft laid on the form: its answers as a starting point, its wizard step (or the nearest before it that applies), its text that does not read. */
1572
+ function applyDraft(draft) {
1573
+ found = null;
1574
+ writeValues(withDraft(draft.values ?? {}), { draft: null }, true);
1575
+ const known = (draft.skipped ?? []).filter((id) => steps.includes(id));
1576
+ // Text typed that did not read stays as typed, said as it was, where its field is still on the page.
1577
+ const unread = Object.fromEntries(Object.entries(draft.unread ?? {}).filter(([path]) => !!page.fields[path.split('.')[0]] && typeof draft.unread?.[path]?.text === 'string'));
1578
+ const patch = {
1579
+ ...(steps.length ? { skipped: known, step: (0, drafts_1.resumeStep)(steps, form.steps(), draft.step) } : {}),
1580
+ ...(Object.keys(unread).length ? { unread: { ...state.unread, ...unread } } : {}),
1581
+ // Its boxes were left with it: why each does not read is said at once, as it was where it was typed.
1582
+ ...(Object.keys(unread).length ? { errors: { ...state.errors, ...Object.fromEntries(Object.entries(unread).map(([path, kept]) => [path, kept.message])) } } : {}),
1583
+ };
1584
+ if (Object.keys(patch).length)
1585
+ set(patch);
1586
+ }
1108
1587
  function forgetDraft() {
1109
1588
  if (draftTimer !== null)
1110
1589
  scheduler.clearTimeout(draftTimer);
1111
1590
  draftTimer = null;
1112
- options.drafts?.store.removeItem(draftKey());
1591
+ found = null;
1592
+ draftEpoch++;
1593
+ serverKey = null;
1594
+ if (state.resume)
1595
+ set({ resume: null });
1596
+ options.drafts?.store.removeItem(storeKey());
1597
+ }
1598
+ /** A draft found: laid at once (`restore: "auto"`, or a draft continued from its key over nothing typed), else offered. */
1599
+ function offer(draft, auto = options.drafts?.restore === 'auto') {
1600
+ // What the person typed while it was asked for is theirs: the draft is offered, never laid over it.
1601
+ if (auto && !state.dirty.length && !Object.keys(state.unread).length)
1602
+ return applyDraft(draft);
1603
+ found = draft;
1604
+ set({ draft: { savedAt: draft.savedAt } });
1113
1605
  }
1606
+ /**
1607
+ * The draft kept for the record shown, offered as it opens: the one the form was given to continue from (`resume`),
1608
+ * else the browser's store's — on the server by its key, or in the store. A draft asked of the server is laid only
1609
+ * on the record it was asked for.
1610
+ */
1114
1611
  function offerDraft() {
1115
- const drafts = options.drafts;
1116
- if (!drafts)
1612
+ const source = options.dataSource;
1613
+ const given = resumeKey;
1614
+ resumeKey = undefined;
1615
+ const kept = given ? null : (0, drafts_1.draftRead)(options.drafts?.store.getItem(storeKey()) ?? null);
1616
+ if (!given && !kept)
1117
1617
  return;
1118
- const raw = drafts.store.getItem(draftKey());
1119
- if (!raw)
1618
+ if (kept && !('key' in kept))
1619
+ return offer(kept);
1620
+ const key = given ?? kept.key;
1621
+ if (!source?.loadDraft || !onServer())
1120
1622
  return;
1121
- let draft;
1122
- try {
1123
- draft = JSON.parse(raw);
1124
- }
1125
- catch {
1126
- drafts.store.removeItem(draftKey());
1127
- return;
1128
- }
1129
- if (drafts.restore === 'auto')
1130
- writeValues(withDraft(draft.values), { draft: null }, true);
1131
- else
1132
- set({ draft: { savedAt: draft.savedAt } });
1623
+ const shown = shownNow();
1624
+ if (given)
1625
+ set({ status: 'loading' });
1626
+ return track(source.loadDraft({ pageId: page.id, key }).then((draft) => {
1627
+ if (!shown())
1628
+ return;
1629
+ if (given)
1630
+ set({ status: 'ready' });
1631
+ if (!draft) {
1632
+ // Sent, or gone: the form starts empty, and says so where it was asked to continue it.
1633
+ options.drafts?.store.removeItem(storeKey());
1634
+ if (given)
1635
+ set({ resume: { key, missing: true } });
1636
+ return;
1637
+ }
1638
+ serverKey = key;
1639
+ set({ resume: { key } });
1640
+ offer(draft, given ? true : undefined);
1641
+ }, (error) => {
1642
+ if (shown() && given)
1643
+ set({ status: 'error', error: error?.message ?? String(error) });
1644
+ }));
1645
+ }
1646
+ // ---- when the page takes answers, and each choice's places (the limits part: form-limits.ts) --------------------
1647
+ /** The limits made for this form from the limits part, once it came: none before, nor on a page with neither limits nor places. */
1648
+ let limitsKit = null;
1649
+ /** Whether the record shown was loaded once: its counts are asked then, its own choices not counted against it. */
1650
+ let loadedOnce = false;
1651
+ /** Whether the form keeps limits: a page with them or with places of its own, or a list's choices that came with places. */
1652
+ const keepsLimits = () => !outer && ((0, taking_1.hasLimits)(page) || Object.values(state.choices).some((choices) => choices.options?.some((option) => option.seats !== undefined)));
1653
+ /**
1654
+ * The limits, made from their part as it comes — at once where it has come — shown, and asked of the source as the
1655
+ * form opens (or once its record is loaded); null where the form keeps none, or the part could not come: the source's
1656
+ * submit decides then.
1657
+ */
1658
+ function limitsCode() {
1659
+ if (limitsKit || !keepsLimits())
1660
+ return Promise.resolve(limitsKit);
1661
+ if (taking_1.formLimitsPart.loaded)
1662
+ return Promise.resolve(limitsMade(taking_1.formLimitsPart.loaded));
1663
+ return taking_1.formLimitsPart.load().then(limitsMade, () => null);
1664
+ }
1665
+ function limitsMade(part) {
1666
+ if (limitsKit || disposed)
1667
+ return limitsKit;
1668
+ const kit = part.formLimits({
1669
+ page,
1670
+ dataSource: options.dataSource,
1671
+ user: options.user,
1672
+ messages,
1673
+ scheduler,
1674
+ state: () => state,
1675
+ set,
1676
+ clock,
1677
+ started: () => started,
1678
+ shownNow,
1679
+ generation: () => generation,
1680
+ replaced: () => named(messages.recordReplacedNoun, messages.recordReplaced),
1681
+ listened: () => listeners.size > 0,
1682
+ disposed: () => disposed,
1683
+ refuse(name, message) {
1684
+ // Said until the field changes from what it holds, as a refused save's words are.
1685
+ serverErrors[name] = { message, value: JSON.stringify(state.values[name] ?? null) };
1686
+ set({ errors: { ...state.errors, [name]: message } });
1687
+ },
1688
+ });
1689
+ limitsKit = kit;
1690
+ kit.show();
1691
+ if (kit.asks() && (state.recordId == null || loadedOnce))
1692
+ void track(kit.ask());
1693
+ return kit;
1694
+ }
1695
+ /** Ask the source's `limits` again and show what it tells; without the limits part, nothing to ask. */
1696
+ const askLimits = () => (limitsKit ? limitsKit.ask() : Promise.resolve());
1697
+ /** Whether the form asks the source for counts. */
1698
+ const asksLimits = () => !!limitsKit?.asks();
1699
+ /**
1700
+ * Whether the page takes these answers now, asked of the source again just before they go — the limits part waited
1701
+ * for where the form keeps limits: why not, as a save's problem, or null.
1702
+ */
1703
+ async function stillTaking(asked) {
1704
+ const kit = await limitsCode();
1705
+ return kit ? kit.stillTaking(asked) : null;
1133
1706
  }
1707
+ /** A choice of a full option, picked by the person, refused under its field: the field keeps what it held. */
1708
+ const refusedFull = (name, value) => !!limitsKit?.refusedFull(name, value);
1134
1709
  function scheduleAutosave() {
1135
1710
  const autosave = options.autosave;
1136
1711
  // A record the person may not change is never saved unasked: it would only be refused.
@@ -1271,6 +1846,20 @@ function innerForm(options, within, place, outer) {
1271
1846
  await waiting;
1272
1847
  if (asked !== generation)
1273
1848
  return elsewhere();
1849
+ // Options by condition waited for where they have not come: what is saved never holds one they would take out.
1850
+ if (!optionKit && (0, choice_options_1.hasOptionRules)(page)) {
1851
+ await optionsCode();
1852
+ if (asked !== generation)
1853
+ return elsewhere();
1854
+ }
1855
+ // Values the app's functions are asked for are waited for — those that failed asked again — so what is saved is worked out.
1856
+ if (asking.size || Object.values(state.asked).some((found) => found.failed !== undefined)) {
1857
+ set({ status: 'saving', error: null, saveProblem: null });
1858
+ await askAgain();
1859
+ if (asked !== generation)
1860
+ return elsewhere();
1861
+ set({ status: 'ready' });
1862
+ }
1274
1863
  // Files still on their way are waited for first — a saved form's placed here too: the state says how many, so the page can say why it waits.
1275
1864
  if (filesGoing()) {
1276
1865
  set({ status: 'saving', error: null, saveProblem: null });
@@ -1292,6 +1881,10 @@ function innerForm(options, within, place, outer) {
1292
1881
  return { reason: 'save', ...(before.message !== undefined ? { message: before.message } : {}) };
1293
1882
  }
1294
1883
  }
1884
+ // Whether the page still takes answers — its dates, its counts asked again — and each choice held still has room.
1885
+ const refused = await stillTaking(asked);
1886
+ if (refused)
1887
+ return refused;
1295
1888
  // A file that did not reach the store stops it, under its field, with the form's own checks.
1296
1889
  const errors = { ...collectErrors(), ...fileProblems() };
1297
1890
  if (Object.keys(errors).length) {
@@ -1301,6 +1894,13 @@ function innerForm(options, within, place, outer) {
1301
1894
  const source = options.dataSource;
1302
1895
  serverErrors = {};
1303
1896
  set({ status: 'saving', errors: {}, error: null, saveProblem: null, conflict: null });
1897
+ // A draft being kept on the server goes first, so the send names the draft it ends; none is kept after it.
1898
+ if (draftTimer !== null)
1899
+ scheduler.clearTimeout(draftTimer);
1900
+ draftTimer = null;
1901
+ if (keeping)
1902
+ await draftGoing;
1903
+ const draft = serverKey ? { draft: serverKey } : {};
1304
1904
  /** The generation the record was in when this save went to the source. */
1305
1905
  let at = generation;
1306
1906
  try {
@@ -1319,8 +1919,10 @@ function innerForm(options, within, place, outer) {
1319
1919
  const sent = shownValues();
1320
1920
  const before = state.values;
1321
1921
  at = generation;
1922
+ const resetAt = resets;
1322
1923
  const seen = botReport();
1323
- await source.submit({ pageId: page.id, values: sent, ...(seen ? { bot: seen } : {}) });
1924
+ // What the source answered: the id it gave the response, and the values it added — what an ending reads.
1925
+ const answer = (await source.submit({ pageId: page.id, values: sent, ...(seen ? { bot: seen } : {}), ...draft })) ?? {};
1324
1926
  // The captcha's token is spent once sent: the next send waits for a new one.
1325
1927
  if (seen?.captcha)
1326
1928
  bot.captcha = null;
@@ -1330,8 +1932,13 @@ function innerForm(options, within, place, outer) {
1330
1932
  forgetDraft();
1331
1933
  // What it sent is what an answer changed since is told against: still to send.
1332
1934
  sentAs = before;
1333
- set({ status: 'saved', unsent: unsentNow() });
1935
+ const added = answer.values && typeof answer.values === 'object' ? (0, values_1.structuredCopy)(answer.values) : {};
1936
+ // Started again meanwhile (`reset`): a new response is on screen, and no ending is the old one's.
1937
+ set({ status: 'saved', unsent: unsentNow(), ...(resets === resetAt ? { sent: { id: answer.id ?? null, values: { ...(0, values_1.structuredCopy)(sent), ...added } } } : {}) });
1334
1938
  emit('send', { values: sent, ...(seen ? { bot: seen } : {}) });
1939
+ // Counted again: one response fewer to take, this person's sent.
1940
+ if (asksLimits())
1941
+ void track(askLimits());
1335
1942
  void track(moment('on.afterSave', page.on?.afterSave, chain, false));
1336
1943
  return null;
1337
1944
  }
@@ -1349,6 +1956,7 @@ function innerForm(options, within, place, outer) {
1349
1956
  values: sentValues(),
1350
1957
  // The version it was opened at, for a source that refuses a save over someone else's.
1351
1958
  ...(state.recordId != null && state.version ? { version: state.version } : {}),
1959
+ ...draft,
1352
1960
  });
1353
1961
  // Answered for a record no longer shown — another opened, or this one loaded again: the source has it, and the
1354
1962
  // form lays nothing of it over the record it shows now.
@@ -1624,7 +2232,11 @@ function innerForm(options, within, place, outer) {
1624
2232
  return;
1625
2233
  // Lines load in their tables' orders; where a person moves them after, they stay.
1626
2234
  baseline = syncParts(startFrom(ordered({ ...(0, values_1.initialValues)(fields, today()), ...loaded })), 'start');
1627
- set({ status: 'ready', values: (0, values_1.structuredCopy)(baseline), dirty: [], errors: {}, warnings: withValues(baseline, collectWarnings), version: version ?? null, can: can ?? null, unread: {} });
2235
+ set({ status: 'ready', values: (0, values_1.structuredCopy)(baseline), dirty: [], errors: {}, warnings: withValues(baseline, collectWarnings), version: version ?? null, can: can ?? null, unread: {}, takenOut: {} });
2236
+ // Each choice's places, for this record: its own choices are its own.
2237
+ loadedOnce = true;
2238
+ if (asksLimits())
2239
+ void track(askLimits());
1628
2240
  if (again)
1629
2241
  return;
1630
2242
  offerDraft();
@@ -1653,7 +2265,11 @@ function innerForm(options, within, place, outer) {
1653
2265
  told = null;
1654
2266
  showing = false;
1655
2267
  clearFiles();
1656
- const fresh = { dirty: [], errors: {}, warning: null, warningField: null, error: null, draft: null, step: steps[0] ?? null, skipped: [], saveProblem: null, version: null, can: null, conflict: null, setAside: null, unsent: [], unread: {} };
2268
+ // The draft kept for the record shown before is that one's.
2269
+ found = null;
2270
+ serverKey = null;
2271
+ draftEpoch++;
2272
+ const fresh = { dirty: [], errors: {}, warning: null, warningField: null, error: null, draft: null, step: steps[0] ?? null, skipped: [], saveProblem: null, version: null, can: null, conflict: null, setAside: null, unsent: [], unread: {}, takenOut: {}, resume: null };
1657
2273
  if (id != null) {
1658
2274
  set({ ...fresh, recordId: id, status: 'idle' });
1659
2275
  await load();
@@ -1663,6 +2279,8 @@ function innerForm(options, within, place, outer) {
1663
2279
  baseline = startFrom(startingValues(true, false));
1664
2280
  const values = syncParts((0, values_1.structuredCopy)(baseline), 'start');
1665
2281
  set({ ...fresh, recordId: null, status: 'ready', values, warnings: withValues(values, collectWarnings) });
2282
+ if (asksLimits())
2283
+ void track(askLimits());
1666
2284
  offerDraft();
1667
2285
  await opening();
1668
2286
  return true;
@@ -2023,10 +2641,18 @@ function innerForm(options, within, place, outer) {
2023
2641
  }
2024
2642
  const form = {
2025
2643
  page,
2644
+ ...(table ? { functions: table } : {}),
2026
2645
  getState: () => state,
2027
2646
  subscribe(listener) {
2028
2647
  listeners.add(listener);
2029
- return () => listeners.delete(listener);
2648
+ // Listened to: the page opening or closing is said as it comes.
2649
+ if (listeners.size === 1)
2650
+ limitsKit?.heard(true);
2651
+ return () => {
2652
+ listeners.delete(listener);
2653
+ if (!listeners.size)
2654
+ limitsKit?.heard(false);
2655
+ };
2030
2656
  },
2031
2657
  load() {
2032
2658
  const go = letGo();
@@ -2034,6 +2660,9 @@ function innerForm(options, within, place, outer) {
2034
2660
  },
2035
2661
  setValue(name, value, how) {
2036
2662
  fieldDef(name);
2663
+ // A choice whose places are all taken is shown, and refused when the person picks it: said under its field.
2664
+ if ((how?.by ?? 'person') === 'person' && refusedFull(name, value))
2665
+ return;
2037
2666
  writeValues({ ...state.values, [name]: (0, values_1.structuredCopy)(value) });
2038
2667
  afterEdit(name);
2039
2668
  changed(name, how?.by ?? 'person');
@@ -2059,11 +2688,34 @@ function innerForm(options, within, place, outer) {
2059
2688
  // Nothing of the record changed; what shows of it may have.
2060
2689
  set({});
2061
2690
  },
2062
- fieldReadonly: (name) => lockedField(fieldDef(name)) || state.can?.edit === false || state.can?.readonlyFields?.includes(name) === true,
2063
- loadChoices,
2691
+ fieldReadonly: (name) => lockedField(fieldDef(name)) || state.can?.edit === false || state.can?.readonlyFields?.includes(name) === true || signedNow().has(name),
2692
+ loadChoices: (field) => loadChoices(field),
2064
2693
  loadDefinitions,
2065
2694
  addDefinition,
2695
+ lineChoices(field, key, subfield, values = {}) {
2696
+ const one = listedAt(field, subfield);
2697
+ const line = key === null ? one && { values: newLineValues(field) } : lineAt(field, key);
2698
+ if (!one || !line)
2699
+ throw new Error(`"${field}.${key}.${subfield}" is no line's choice from the app's lists`);
2700
+ return track(askLine(one, { ...line.values, ...values }));
2701
+ },
2702
+ offered(name) {
2703
+ const cell = (0, line_choices_1.cellOf)(name);
2704
+ if (!cell)
2705
+ return page.fields[name]?.type === 'selection' ? (optionKit ? optionKit.offered(name, state.values, reading()) : everyOption(name)) : null;
2706
+ const lines = page.fields[cell.table];
2707
+ const line = lineAt(cell.table, cell.key);
2708
+ return lines?.type === 'one2many' && lines.fields[cell.column]?.type === 'selection' && line ? lineOffered(cell.table, line, cell.column, reading().context) : null;
2709
+ },
2710
+ offersChange(name) {
2711
+ const cell = (0, line_choices_1.cellOf)(name);
2712
+ const lines = cell ? page.fields[cell.table] : undefined;
2713
+ return (0, choice_options_1.changesOptions)(cell ? (lines?.type === 'one2many' ? lines.fields[cell.column] : undefined) : page.fields[name]);
2714
+ },
2066
2715
  waitsFor(name) {
2716
+ const found = state.asked[name];
2717
+ if (found)
2718
+ return { field: name, asking: true, ...(found.failed !== undefined ? { failed: found.failed } : {}) };
2067
2719
  const from = workedOut.get(name);
2068
2720
  return from ? (0, waits_1.waitingOn)(page, from, state.values, state.unread) : null;
2069
2721
  },
@@ -2145,6 +2797,9 @@ function innerForm(options, within, place, outer) {
2145
2797
  save: () => track(save()),
2146
2798
  canUpload: () => typeof options.dataSource?.upload === 'function',
2147
2799
  today,
2800
+ now: () => options.now?.() ?? new Date(),
2801
+ user: () => options.user ?? null,
2802
+ shownFields: () => [...new Set([...visibleFieldNodes(), ...visibleFieldNodes(undefined, 'form')].map((node) => answerOf(node)).filter((name) => !!name))],
2148
2803
  recordReads: (id, values, fields) => (0, env_1.recordContext)(values, fields ?? page.fields, { recordId: id, user: options.user, editing: false, context: options.context }),
2149
2804
  canFindSlots: () => typeof options.dataSource?.slots === 'function',
2150
2805
  findSlots(field, ask) {
@@ -2179,6 +2834,22 @@ function innerForm(options, within, place, outer) {
2179
2834
  const others = (matches ?? []).filter((match) => match && String(match.id) !== String(recordId));
2180
2835
  return others.every((match) => typeof match.score === 'number') ? others.sort((a, b) => b.score - a.score) : others;
2181
2836
  },
2837
+ keepsNotDuplicates: () => !!(page.data.kind === 'record' && options.dataSource?.notDuplicates && options.dataSource.markNotDuplicate),
2838
+ async notDuplicates() {
2839
+ const [source, model] = recordSource('notDuplicates');
2840
+ const recordId = state.recordId;
2841
+ if (recordId === null)
2842
+ return [];
2843
+ const ids = await source.notDuplicates({ model, recordId });
2844
+ return Array.isArray(ids) ? ids.filter((id) => (typeof id === 'string' || typeof id === 'number') && String(id) !== String(recordId)) : [];
2845
+ },
2846
+ async markNotDuplicate(matchId) {
2847
+ const [source, model] = recordSource('markNotDuplicate');
2848
+ const recordId = state.recordId;
2849
+ if (recordId === null)
2850
+ throw new Error('A new record has no id to keep it by');
2851
+ await source.markNotDuplicate({ model, recordId, matchId });
2852
+ },
2182
2853
  canMerge: () => !!(page.data.kind === 'record' && options.dataSource?.merge && options.dataSource.load),
2183
2854
  async readRecord(id) {
2184
2855
  const [source, model] = recordSource('load');
@@ -2219,7 +2890,19 @@ function innerForm(options, within, place, outer) {
2219
2890
  // Started again: a new response, timed from now.
2220
2891
  bot = { ...bot, honeypot: false, since: clock() };
2221
2892
  const values = syncParts((0, values_1.structuredCopy)(baseline), 'start');
2222
- set({ values, dirty: [], errors: {}, warnings: withValues(values, collectWarnings), warning: null, warningField: null, draft: null, step: steps[0] ?? null, skipped: [], saveProblem: null, conflict: null, unsent: [], unread: {} });
2893
+ set({ values, dirty: [], errors: {}, warnings: withValues(values, collectWarnings), warning: null, warningField: null, draft: null, step: steps[0] ?? null, skipped: [], saveProblem: null, conflict: null, unsent: [], unread: {}, takenOut: {}, sent: null });
2894
+ // Started again: whether the page takes another response, and each choice's places, asked again.
2895
+ if (asksLimits())
2896
+ void track(askLimits());
2897
+ },
2898
+ words: (text) => wordsNow(text),
2899
+ ending: endingNow,
2900
+ endingWords(text) {
2901
+ const sent = state.sent;
2902
+ if (!sent)
2903
+ return wordsNow(text);
2904
+ // The response's id where the page has no field of that name: "Your booking is {id}".
2905
+ return wordsNow(text, sent.values, (name) => (name === 'id' && !page.fields['id'] ? String(sent.id ?? '') : undefined));
2223
2906
  },
2224
2907
  changes,
2225
2908
  addLine: (field, values = {}) => addLine(field, values, 'person'),
@@ -2480,16 +3163,27 @@ function innerForm(options, within, place, outer) {
2480
3163
  return () => void heard.delete(listener);
2481
3164
  },
2482
3165
  restoreDraft() {
2483
- const raw = options.drafts?.store.getItem(draftKey());
2484
- if (!raw)
2485
- return;
2486
- const draft = JSON.parse(raw);
2487
- writeValues(withDraft(draft.values), { draft: null }, true);
3166
+ const kept = found ?? (0, drafts_1.draftRead)(options.drafts?.store.getItem(storeKey()) ?? null);
3167
+ if (kept && 'values' in kept)
3168
+ applyDraft(kept);
2488
3169
  },
2489
3170
  discardDraft() {
2490
3171
  forgetDraft();
2491
3172
  set({ draft: null });
2492
3173
  },
3174
+ canContinueLater: () => page.continueLater === true && onServer() && page.layout.type !== 'list',
3175
+ async continueLater() {
3176
+ if (!form.canContinueLater())
3177
+ throw new Error('No continueLater, or no saveDraft and loadDraft');
3178
+ // What is typed is read first, as a save reads it: text that does not read is kept as typed, said again as it opens.
3179
+ await (readTyped() ?? Promise.resolve());
3180
+ if (draftTimer !== null)
3181
+ scheduler.clearTimeout(draftTimer);
3182
+ draftTimer = null;
3183
+ const saved = await track(keepDraft(true));
3184
+ return saved;
3185
+ },
3186
+ refreshLimits: () => askLimits(),
2493
3187
  async reloadTheirs() {
2494
3188
  // A save on its way first: its answer is not dropped by the load.
2495
3189
  const asked = shownNow();
@@ -2616,6 +3310,8 @@ function innerForm(options, within, place, outer) {
2616
3310
  scheduler,
2617
3311
  messages,
2618
3312
  now: options.now,
3313
+ // Its words that show its answers read them as this form's do.
3314
+ ...(options.valueWords ? { valueWords: options.valueWords } : {}),
2619
3315
  }, chain,
2620
3316
  // Its files are the record's: stored for it, under the part's name.
2621
3317
  { model: where.model, recordId: where.recordId, under: `${where.under}${part.name}.` },
@@ -2659,6 +3355,7 @@ function innerForm(options, within, place, outer) {
2659
3355
  scheduler.clearTimeout(draftTimer);
2660
3356
  if (autosaveTimer !== null)
2661
3357
  scheduler.clearTimeout(autosaveTimer);
3358
+ limitsKit?.dispose();
2662
3359
  listeners.clear();
2663
3360
  events.clear();
2664
3361
  for (const part of attached.values())
@@ -2667,11 +3364,16 @@ function innerForm(options, within, place, outer) {
2667
3364
  };
2668
3365
  // A record's starting values may already break a warning rule; nothing listens yet.
2669
3366
  state = { ...state, warnings: collectWarnings() };
2670
- if (state.recordId == null)
2671
- offerDraft();
2672
- // A new form has its values now: it opens once whoever made it has had the chance to listen.
2673
- if (state.recordId == null)
2674
- void track(Promise.resolve().then(opening));
3367
+ // When the page takes answers — at once where the limits part has come, else as soon as it does — its counts and each
3368
+ // choice's places as the source tells them: a record's own once it is loaded, its own choices not counted against it.
3369
+ if (keepsLimits())
3370
+ void track(limitsCode());
3371
+ // A new form has its values now — a draft continued from its key once it is laid — and opens once whoever made it
3372
+ // has had the chance to listen.
3373
+ if (state.recordId == null) {
3374
+ const continued = offerDraft();
3375
+ void track(Promise.resolve(continued).then(opening));
3376
+ }
2675
3377
  return {
2676
3378
  form,
2677
3379
  take(values, how) {
@@ -2681,7 +3383,7 @@ function innerForm(options, within, place, outer) {
2681
3383
  filed.clear();
2682
3384
  baseline = startFrom({ ...(0, values_1.initialValues)(page.fields, today()), ...(0, values_1.structuredCopy)(values) });
2683
3385
  const start = syncParts((0, values_1.structuredCopy)(baseline), 'start');
2684
- set({ values: start, dirty: [], errors: {}, warnings: withValues(start, collectWarnings), warning: null, warningField: null, unread: {} });
3386
+ set({ values: start, dirty: [], errors: {}, warnings: withValues(start, collectWarnings), warning: null, warningField: null, unread: {}, takenOut: {} });
2685
3387
  },
2686
3388
  errorsNow: () => collectErrors(),
2687
3389
  typing: typingNow,
@@ -2705,6 +3407,8 @@ function innerForm(options, within, place, outer) {
2705
3407
  const sameHow = (a, b) => !!a.changes === !!b.changes && !!a.discard === !!b.discard;
2706
3408
  /** Two lists of names the same, in the same order. */
2707
3409
  const sameList = (a, b) => a.length === b.length && a.every((name, i) => name === b[i]);
3410
+ /** Whether what waits for an app's answer is the same as before: the same paths, each waiting or failed for the same reason. */
3411
+ const sameAsked = (shown, now) => Object.keys(shown).length === now.size && [...now].every(([path, found]) => shown[path] !== undefined && shown[path].failed === found.failed);
2708
3412
  /** A file on its way in a saved form placed in another, as that one names it: its id and its field under the part's name. */
2709
3413
  function underPart(name, upload) {
2710
3414
  return { ...upload, id: `${name}/${upload.id}`, ...(upload.line ? { line: { ...upload.line, field: `${name}.${upload.line.field}` } } : { field: `${name}.${upload.field}` }) };
@@ -2815,7 +3519,7 @@ function linkOps(before, now) {
2815
3519
  ...before.filter((r) => !has.has(r.id)).map((r) => ({ op: 'unlink', id: r.id })),
2816
3520
  ];
2817
3521
  }
2818
- function indexLayout(root) {
3522
+ function indexLayout(root, functions) {
2819
3523
  const index = new Map();
2820
3524
  const add = (source, parent, kind, modifiers) => {
2821
3525
  const roles = source.roles;
@@ -2827,10 +3531,10 @@ function indexLayout(root) {
2827
3531
  kind,
2828
3532
  field: modifiers.field,
2829
3533
  ...(roles?.length ? { roles } : {}),
2830
- invisible: (0, modifier_1.compileModifier)(modifiers.invisible),
2831
- readonly: (0, modifier_1.compileModifier)(modifiers.readonly),
2832
- required: (0, modifier_1.compileModifier)(modifiers.required),
2833
- ...(pressed !== undefined ? { pressed: (0, modifier_1.compileModifier)(pressed) } : {}),
3534
+ invisible: (0, modifier_1.compileModifier)(modifiers.invisible, functions),
3535
+ readonly: (0, modifier_1.compileModifier)(modifiers.readonly, functions),
3536
+ required: (0, modifier_1.compileModifier)(modifiers.required, functions),
3537
+ ...(pressed !== undefined ? { pressed: (0, modifier_1.compileModifier)(pressed, functions) } : {}),
2834
3538
  ...(does.length ? { does } : {}),
2835
3539
  source,
2836
3540
  });