-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathBarcodeGenerator.editorConfig.ts
More file actions
247 lines (218 loc) · 9.19 KB
/
BarcodeGenerator.editorConfig.ts
File metadata and controls
247 lines (218 loc) · 9.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { StructurePreviewProps } from "@mendix/widget-plugin-platform/preview/structure-preview-api";
import { hidePropertiesIn, hidePropertyIn, Properties } from "@mendix/pluggable-widgets-tools";
import { BarcodeGeneratorPreviewProps } from "../typings/BarcodeGeneratorProps";
import { validateAddonValue, validateBarcodeValue } from "./config/validation";
export type Problem = {
property?: string; // key of the property, at which the problem exists
severity?: "error" | "warning" | "deprecation"; // default = "error"
message: string; // description of the problem
studioMessage?: string; // studio-specific message, defaults to message
url?: string; // link with more information about the problem
studioUrl?: string; // studio-specific link
};
export function getProperties(values: BarcodeGeneratorPreviewProps, defaultProperties: Properties): Properties {
if (values.codeFormat === "QRCode") {
hidePropertiesIn(defaultProperties, values, ["codeWidth", "codeHeight", "displayValue", "codeMargin"]);
} else {
hidePropertiesIn(defaultProperties, values, ["qrOverlay", "qrSize", "qrMargin", "qrLevel", "qrTitle"]);
}
if (values.codeFormat !== "QRCode" || !values.qrOverlay) {
hidePropertiesIn(defaultProperties, values, [
"qrOverlaySrc",
"qrOverlayCenter",
"qrOverlayWidth",
"qrOverlayHeight",
"qrOverlayX",
"qrOverlayY",
"qrOverlayOpacity",
"qrOverlayExcavate"
]);
}
if (values.codeFormat === "QRCode" || (values.codeFormat !== "CODE128" && values.customCodeFormat !== "CODE128")) {
hidePropertyIn(defaultProperties, values, "enableEan128");
}
// enableFlat is only supported for EAN-13 and EAN-8, and NOT when addons are enabled
if (
!(
values.codeFormat === "Custom" &&
(values.customCodeFormat === "EAN13" || values.customCodeFormat === "EAN8") &&
values.addonFormat === "None"
)
) {
hidePropertyIn(defaultProperties, values, "enableFlat");
}
// lastChar is only supported for EAN-13, and NOT when flat is enabled or addons are present
if (
!(
values.codeFormat === "Custom" &&
values.customCodeFormat === "EAN13" &&
!values.enableFlat &&
values.addonFormat === "None"
)
) {
hidePropertyIn(defaultProperties, values, "lastChar");
}
// EAN addons are only supported for EAN-13, EAN-8, and UPC
if (
values.codeFormat === "QRCode" ||
values.codeFormat === "CODE128" ||
(values.codeFormat === "Custom" &&
values.customCodeFormat !== "EAN13" &&
values.customCodeFormat !== "EAN8" &&
values.customCodeFormat !== "UPC")
) {
hidePropertiesIn(defaultProperties, values, ["addonFormat", "addonValue", "addonSpacing"]);
}
if (
values.codeFormat === "QRCode" ||
values.codeFormat === "CODE128" ||
(values.codeFormat === "Custom" && values.addonFormat !== "EAN5" && values.addonFormat !== "EAN2")
) {
hidePropertiesIn(defaultProperties, values, ["addonValue", "addonSpacing"]);
}
if (
values.codeFormat === "QRCode" ||
values.codeFormat === "CODE128" ||
(values.codeFormat === "Custom" && values.customCodeFormat !== "CODE39")
) {
hidePropertyIn(defaultProperties, values, "enableMod43");
}
if (values.qrOverlayCenter) {
hidePropertiesIn(defaultProperties, values, ["qrOverlayX", "qrOverlayY"]);
}
if (values.codeFormat !== "Custom") {
hidePropertiesIn(defaultProperties, values, ["customCodeFormat"]);
}
if (!values.allowDownload) {
hidePropertiesIn(defaultProperties, values, [
"downloadButtonCaption",
"downloadButtonAriaLabel",
"downloadFileName",
"buttonPosition"
]);
}
return defaultProperties;
}
export function getPreview(_: StructurePreviewProps, _isDarkMode: boolean): StructurePreviewProps | null {
// Return null to use the widget icon (BarcodeGenerator.icon.png or BarcodeGenerator.icon.dark.png)
// based on the user's theme settings
return null;
}
export function check(_values: BarcodeGeneratorPreviewProps): Problem[] {
const errors: Problem[] = [];
if (!_values.codeWidth || _values.codeWidth < 1) {
errors.push({
property: `codeWidth`,
severity: "error",
message: `The value of 'Bar width' must be at least 1.`
});
}
if (!_values.codeHeight || _values.codeHeight < 20) {
errors.push({
property: `codeHeight`,
severity: "error",
message: `The value of 'Code height' must be at least 20.`
});
}
if (!_values.qrSize || _values.qrSize < 50) {
errors.push({
property: `codeHeight`,
severity: "error",
message: `The value of 'QR size' must be at least 50.`
});
}
// Design-time validation for static barcode value(s)
const valueProblems = validateCodeValues(_values);
return errors.concat(valueProblems);
}
function getActiveFormat(values: BarcodeGeneratorPreviewProps): string {
if (values.codeFormat === "Custom") {
return values.customCodeFormat || "CODE128";
}
return values.codeFormat;
}
function stripQuotes(value: string): string {
// Remove leading/trailing quotes and whitespace from expression values
let trimmed = value.trim();
// Match and remove surrounding quotes (single or double)
if ((trimmed.startsWith("'") && trimmed.endsWith("'")) || (trimmed.startsWith('"') && trimmed.endsWith('"'))) {
trimmed = trimmed.slice(1, -1);
}
return trimmed;
}
function isDynamicExpression(value: string): boolean {
// Check if the value is a dynamic expression (attribute binding, variable, etc.)
// Dynamic expressions start with $ or contain / paths or are empty
return !value || value.startsWith("$") || value.includes("/");
}
function getFormatHint(format: string): string {
const hints: Record<string, string> = {
EAN13: "EAN-13 requires 12 or 13 numeric digits",
EAN8: "EAN-8 requires 7 or 8 numeric digits",
UPC: "UPC requires 11 or 12 numeric digits",
ITF14: "ITF-14 requires exactly 14 numeric digits",
CODE39: "CODE39: uppercase A-Z, digits, space and - . $ / + % (max 43 chars)",
CODE128: "CODE128: alphanumeric, no control characters (max 80 chars)",
CODE93: "CODE93: alphanumeric, no control characters (max 47 chars)",
MSI: "MSI: numeric only (max 30 digits)",
pharmacode: "Pharmacode: numeric only (max 7 digits)",
codabar: "Codabar: digits, A-D start/stop, and - $ : / . + (max 20 chars)",
QRCode: "QR Code: any text (max 1200 chars recommended)"
};
return hints[format] || "";
}
function validateCodeValues(values: BarcodeGeneratorPreviewProps): Problem[] {
const problems: Problem[] = [];
const rawVal = values.codeValue ?? "";
const rawAddon = values.addonValue ?? "";
const format = getActiveFormat(values);
// Add informational hint for dynamic expressions
if (isDynamicExpression(rawVal) && rawVal) {
const hint = getFormatHint(format);
if (hint) {
problems.push({
property: "codeValue",
severity: "warning",
message: `Dynamic value provided. Ensure runtime value matches format: ${hint}`
});
}
}
// Only validate static literal values, skip dynamic expressions (attribute bindings, variables, etc.)
if (!isDynamicExpression(rawVal)) {
const val = stripQuotes(rawVal);
if (val) {
const result = validateBarcodeValue(format, val);
if (!result.valid) {
const msg = result.message || "Invalid barcode value for selected format.";
problems.push({ property: "codeValue", severity: "error", message: msg });
}
}
}
// Validate addon value if visible and format is selected
if (values.addonFormat !== "None") {
// Add informational hint for dynamic addon expressions
if (isDynamicExpression(rawAddon) && rawAddon) {
const addonHint =
values.addonFormat === "EAN5"
? "EAN-5 addon requires exactly 5 numeric digits"
: "EAN-2 addon requires exactly 2 numeric digits";
problems.push({
property: "addonValue",
severity: "warning",
message: `Dynamic addon value provided. Ensure runtime value matches format: ${addonHint}`
});
}
// Validate static addon values
if (!isDynamicExpression(rawAddon)) {
const addon = stripQuotes(rawAddon);
if (addon) {
const addonResult = validateAddonValue(values.addonFormat, addon);
if (!addonResult.valid) {
const msg = addonResult.message || "Invalid addon value.";
problems.push({ property: "addonValue", severity: "error", message: msg });
}
}
}
}
return problems;
}