From 8e6bec6e2e582e7d87661cd7b0d48bfd77ddcdb4 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Mon, 11 Jan 2021 17:19:24 +0100 Subject: [PATCH] JS -- Add few missing constants in global scope - these constants are available in pdfium implementation too - fix error code in aform.js --- src/scripting_api/aform.js | 53 ++++++++++++++------- src/scripting_api/constants.js | 71 +++++++++++++++++++++++++++++ src/scripting_api/initialization.js | 28 +++++++++++- test/unit/scripting_spec.js | 6 ++- 4 files changed, 138 insertions(+), 20 deletions(-) diff --git a/src/scripting_api/aform.js b/src/scripting_api/aform.js index 3686ed9b4..ee61a4f80 100644 --- a/src/scripting_api/aform.js +++ b/src/scripting_api/aform.js @@ -13,13 +13,14 @@ * limitations under the License. */ -/* global color */ +import { GlobalConstants } from "./constants.js"; class AForm { - constructor(document, app, util) { + constructor(document, app, util, color) { this._document = document; this._app = app; this._util = util; + this._color = color; this._dateFormats = [ "m/d", "m/d/yy", @@ -47,6 +48,10 @@ class AForm { ); } + _mkTargetName(event) { + return event.target ? `[ ${event.target.name} ]` : ""; + } + _parseDate(cFormat, cDate) { const ddate = Date.parse(cDate); if (isNaN(ddate)) { @@ -171,7 +176,7 @@ class AForm { } if (negStyle === 1 || negStyle === 3) { - event.target.textColor = sign === 1 ? color.black : color.red; + event.target.textColor = sign === 1 ? this._color.black : this._color.red; } if ((negStyle !== 0 || bCurrencyPrepend) && sign === -1) { @@ -212,14 +217,17 @@ class AForm { if (!pattern.test(value)) { if (event.willCommit) { - if (event.target) { - this._app.alert(`Invalid number in [ ${event.target.name} ]`); - } else { - this._app.alert(`Invalid number`); - } + const err = `${GlobalConstants.IDS_INVALID_VALUE} ${this._mkTargetName( + event + )}`; + this._app.alert(err); } event.rc = false; } + + if (event.willCommit && sepStyle > 1) { + event.value = parseFloat(value.replace(",", ".")); + } } AFPercent_Format(nDec, sepStyle, percentPrepend = false) { @@ -289,13 +297,18 @@ class AForm { return; } - const value = event.value; + const value = this.AFMergeChange(event); if (!value) { return; } if (this._parseDate(cFormat, value) === null) { - this._app.alert("Invalid date"); + const invalid = GlobalConstants.IDS_INVALID_DATE; + const invalid2 = GlobalConstants.IDS_INVALID_DATE2; + const err = `${invalid} ${this._mkTargetName( + event + )}${invalid2}${cFormat}`; + this._app.alert(err); event.rc = false; } } @@ -337,14 +350,18 @@ class AForm { let err = ""; if (bGreaterThan && bLessThan) { if (value < nGreaterThan || value > nLessThan) { - err = `${event.value} is not between ${nGreaterThan} and ${nLessThan}`; + err = this._util.printf( + GlobalConstants.IDS_GT_AND_LT, + nGreaterThan, + nLessThan + ); } } else if (bGreaterThan) { if (value < nGreaterThan) { - err = `${event.value} is not greater or equal than ${nGreaterThan}`; + err = this._util.printf(GlobalConstants.IDS_GREATER_THAN, nGreaterThan); } } else if (value > nLessThan) { - err = `${event.value} is not less or equal than ${nLessThan}`; + err = this._util.printf(GlobalConstants.IDS_LESS_THAN, nLessThan); } if (err) { this._app.alert(err); @@ -492,24 +509,27 @@ class AForm { return; } + const err = `${GlobalConstants.IDS_INVALID_VALUE} = "${cMask}"`; + if (value.length > cMask.length) { - this._app.alert("Value is too long"); + this._app.alert(err); event.rc = false; return; } if (event.willCommit) { if (value.length < cMask.length) { - this._app.alert("Value is too short"); + this._app.alert(err); event.rc = false; return; } if (!_checkValidity(value, cMask)) { - this._app.alert("Value doesn't fit the specified format"); + this._app.alert(err); event.rc = false; return; } + event.value += cMask.subString(value.length); return; } @@ -518,6 +538,7 @@ class AForm { } if (!_checkValidity(value, cMask)) { + this._app.alert(err); event.rc = false; } } diff --git a/src/scripting_api/constants.js b/src/scripting_api/constants.js index 6fb77808d..3bd441bfa 100644 --- a/src/scripting_api/constants.js +++ b/src/scripting_api/constants.js @@ -122,11 +122,82 @@ const ZoomType = Object.freeze({ refW: "ReflowWidth", }); +const GlobalConstants = Object.freeze({ + IDS_GREATER_THAN: "Invalid value: must be greater than or equal to % s.", + IDS_GT_AND_LT: + "Invalid value: must be greater than or equal to % s " + + "and less than or equal to % s.", + IDS_LESS_THAN: "Invalid value: must be less than or equal to % s.", + IDS_INVALID_MONTH: "** Invalid **", + IDS_INVALID_DATE: + "Invalid date / time: please ensure that the date / time exists.Field", + IDS_INVALID_DATE2: " should match format ", + IDS_INVALID_VALUE: "The value entered does not match the format of the field", + IDS_AM: "am", + IDS_PM: "pm", + IDS_MONTH_INFO: + "January[1] February[2] March[3] April[4] May[5] " + + "June[6] July[7] August[8] September[9] October[10] " + + "November[11] December[12] Sept[9] Jan[1] Feb[2] Mar[3] " + + "Apr[4] Jun[6] Jul[7] Aug[8] Sep[9] Oct[10] Nov[11] Dec[12]", + IDS_STARTUP_CONSOLE_MSG: "** ^ _ ^ **", + RE_NUMBER_ENTRY_DOT_SEP: ["[+-]?\\d*\\.?\\d*"], + RE_NUMBER_COMMIT_DOT_SEP: [ + // -1.0 or -1 + "[+-]?\\d+(\\.\\d+)?", + // -.1 + "[+-]?\\.\\d+", + // -1. + "[+-]?\\d+\\.", + ], + RE_NUMBER_ENTRY_COMMA_SEP: ["[+-]?\\d*,?\\d*"], + RE_NUMBER_COMMIT_COMMA_SEP: [ + // -1,0 or -1 + "[+-]?\\d+([.,]\\d+)?", + // -,1 + "[+-]?[.,]\\d+", + // -1, + "[+-]?\\d+[.,]", + ], + RE_ZIP_ENTRY: ["\\d{0,5}"], + RE_ZIP_COMMIT: ["\\d{5}"], + RE_ZIP4_ENTRY: ["\\d{0,5}(\\.|[- ])?\\d{0,4}"], + RE_ZIP4_COMMIT: ["\\d{5}(\\.|[- ])?\\d{4}"], + RE_PHONE_ENTRY: [ + // 555-1234 or 408 555-1234 + "\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", + // (408 + "\\(\\d{0,3}", + // (408) 555-1234 + // (allow the addition of parens as an afterthought) + "\\(\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", + // (408 555-1234 + "\\(\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", + // 408) 555-1234 + "\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", + // international + "011(\\.|[- \\d])*", + ], + RE_PHONE_COMMIT: [ + // 555-1234 + "\\d{3}(\\.|[- ])?\\d{4}", + // 408 555-1234 + "\\d{3}(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}", + // (408) 555-1234 + "\\(\\d{3}\\)(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}", + // international + "011(\\.|[- \\d])*", + ], + RE_SSN_ENTRY: ["\\d{0,3}(\\.|[- ])?\\d{0,2}(\\.|[- ])?\\d{0,4}"], + RE_SSN_COMMIT: ["\\d{3}(\\.|[- ])?\\d{2}(\\.|[- ])?\\d{4}"], +}); + export { Border, Cursor, Display, Font, + GlobalConstants, Highlight, Position, ScaleHow, diff --git a/src/scripting_api/initialization.js b/src/scripting_api/initialization.js index 96bba1553..fb1aa86de 100644 --- a/src/scripting_api/initialization.js +++ b/src/scripting_api/initialization.js @@ -18,6 +18,7 @@ import { Cursor, Display, Font, + GlobalConstants, Highlight, Position, ScaleHow, @@ -66,7 +67,6 @@ function initSandbox(params) { }); const util = new Util({ externalCall }); - const aform = new AForm(doc, app, util); if (data.objects) { for (const [name, objs] of Object.entries(data.objects)) { @@ -94,10 +94,12 @@ function initSandbox(params) { } } + const color = new Color(); + globalThis.event = null; globalThis.global = Object.create(null); globalThis.app = new Proxy(app, proxyHandler); - globalThis.color = new Proxy(new Color(), proxyHandler); + globalThis.color = new Proxy(color, proxyHandler); globalThis.console = new Proxy(new Console({ send }), proxyHandler); globalThis.util = new Proxy(util, proxyHandler); globalThis.border = Border; @@ -112,12 +114,34 @@ function initSandbox(params) { globalThis.trans = Trans; globalThis.zoomtype = ZoomType; + // AF... functions + const aform = new AForm(doc, app, util, color); for (const name of Object.getOwnPropertyNames(AForm.prototype)) { if (name !== "constructor" && !name.startsWith("_")) { globalThis[name] = aform[name].bind(aform); } } + // Add global constants such as IDS_GREATER_THAN or RE_NUMBER_ENTRY_DOT_SEP + for (const [name, value] of Object.entries(GlobalConstants)) { + Object.defineProperty(globalThis, name, { + value, + writable: false, + }); + } + + // Color functions + Object.defineProperties(globalThis, { + ColorConvert: { + value: color.convert.bind(color), + writable: true, + }, + ColorEqual: { + value: color.equal.bind(color), + writable: true, + }, + }); + // The doc properties must live in the global scope too const properties = Object.create(null); for (const name of Object.getOwnPropertyNames(Doc.prototype)) { diff --git a/test/unit/scripting_spec.js b/test/unit/scripting_spec.js index c4991e338..9448cb67b 100644 --- a/test/unit/scripting_spec.js +++ b/test/unit/scripting_spec.js @@ -858,7 +858,8 @@ describe("Scripting", function () { expect(send_queue.has("alert")).toEqual(true); expect(send_queue.get("alert")).toEqual({ command: "alert", - value: "Invalid number in [ MyField ]", + value: + "The value entered does not match the format of the field [ MyField ]", }); done(); } catch (ex) { @@ -1052,7 +1053,8 @@ describe("Scripting", function () { expect(send_queue.has("alert")).toEqual(true); expect(send_queue.get("alert")).toEqual({ command: "alert", - value: "12 is not between 123 and 456", + value: + "Invalid value: must be greater than or equal to 123 and less than or equal to 456.", }); done();