@olenbetong/appframe-core 2.11.10 → 2.13.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.
- package/dist/esm/af.common.js +1988 -626
- package/dist/esm/af.common.legacy.js +1942 -626
- package/dist/esm/af.common.legacy.min.js +4 -3
- package/dist/esm/af.common.legacy.min.js.map +1 -7
- package/dist/esm/af.common.min.js +4 -3
- package/dist/esm/af.common.min.js.map +1 -7
- package/dist/iife/af.common.js +2146 -708
- package/dist/iife/af.common.legacy.js +2100 -708
- package/dist/iife/af.common.legacy.min.js +4 -3
- package/dist/iife/af.common.legacy.min.js.map +1 -7
- package/dist/iife/af.common.min.js +4 -3
- package/dist/iife/af.common.min.js.map +1 -7
- package/es/alert.js +16 -1
- package/es/filter/convertFilter.d.ts +23 -0
- package/es/filter/convertFilter.js +26 -0
- package/es/filter/describeFilter.d.ts +13 -0
- package/es/filter/describeFilter.js +60 -0
- package/es/filter/describeFilter.test.d.ts +1 -0
- package/es/filter/describeFilter.test.js +68 -0
- package/es/filter/fields.d.ts +28 -0
- package/es/filter/fields.js +37 -0
- package/es/filter/fields.test.d.ts +1 -0
- package/es/filter/fields.test.js +49 -0
- package/es/filter/index.d.ts +8 -0
- package/es/filter/index.js +7 -0
- package/es/filter/operators.d.ts +37 -0
- package/es/filter/operators.js +235 -0
- package/es/filter/operators.test.d.ts +1 -0
- package/es/filter/operators.test.js +107 -0
- package/es/filter/savedFilters.d.ts +111 -0
- package/es/filter/savedFilters.js +118 -0
- package/es/filter/savedFilters.test.d.ts +1 -0
- package/es/filter/savedFilters.test.js +108 -0
- package/es/filter/standardLayout.d.ts +18 -0
- package/es/filter/standardLayout.js +55 -0
- package/es/filter/standardLayout.test.d.ts +1 -0
- package/es/filter/standardLayout.test.js +66 -0
- package/es/filter/tree.d.ts +70 -0
- package/es/filter/tree.js +185 -0
- package/es/filter/tree.test.d.ts +1 -0
- package/es/filter/tree.test.js +184 -0
- package/es/filter/types.d.ts +73 -0
- package/es/filter/types.js +1 -0
- package/es/index.d.ts +2 -0
- package/es/index.js +4 -2
- package/es/parseFunc.test.js +5 -6
- package/es/report/index.d.ts +6 -0
- package/es/report/index.js +5 -0
- package/es/report/parameterDefaults.d.ts +26 -0
- package/es/report/parameterDefaults.js +77 -0
- package/es/report/parameters.d.ts +46 -0
- package/es/report/parameters.js +188 -0
- package/es/report/parameters.test.d.ts +1 -0
- package/es/report/parameters.test.js +161 -0
- package/es/report/recordSource.d.ts +30 -0
- package/es/report/recordSource.js +105 -0
- package/es/report/render.d.ts +81 -0
- package/es/report/render.js +219 -0
- package/es/report/render.test.d.ts +1 -0
- package/es/report/render.test.js +144 -0
- package/es/report/report.d.ts +16 -0
- package/es/report/report.js +50 -0
- package/es/report/report.test.d.ts +1 -0
- package/es/report/report.test.js +86 -0
- package/es/report/types.d.ts +124 -0
- package/es/report/types.js +8 -0
- package/package.json +7 -7
- package/src/alert.ts +16 -1
- package/src/filter/convertFilter.ts +47 -0
- package/src/filter/describeFilter.test.ts +103 -0
- package/src/filter/describeFilter.ts +79 -0
- package/src/filter/fields.test.ts +58 -0
- package/src/filter/fields.ts +56 -0
- package/src/filter/index.ts +62 -0
- package/src/filter/operators.test.ts +137 -0
- package/src/filter/operators.ts +258 -0
- package/src/filter/savedFilters.test.ts +145 -0
- package/src/filter/savedFilters.ts +192 -0
- package/src/filter/standardLayout.test.ts +88 -0
- package/src/filter/standardLayout.ts +65 -0
- package/src/filter/tree.test.ts +258 -0
- package/src/filter/tree.ts +238 -0
- package/src/filter/types.ts +146 -0
- package/src/index.ts +3 -1
- package/src/parseFunc.test.ts +5 -6
- package/src/report/index.ts +37 -0
- package/src/report/parameterDefaults.ts +101 -0
- package/src/report/parameters.test.ts +218 -0
- package/src/report/parameters.ts +230 -0
- package/src/report/recordSource.ts +123 -0
- package/src/report/render.test.ts +230 -0
- package/src/report/render.ts +269 -0
- package/src/report/report.test.ts +104 -0
- package/src/report/report.ts +51 -0
- package/src/report/types.ts +135 -0
package/dist/esm/af.common.js
CHANGED
|
@@ -1,529 +1,1921 @@
|
|
|
1
|
-
|
|
1
|
+
//#region ../../node_modules/.pnpm/mitt@3.0.1/node_modules/mitt/dist/mitt.mjs
|
|
2
2
|
function mitt_default(n) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
3
|
+
return {
|
|
4
|
+
all: n = n || /* @__PURE__ */ new Map(),
|
|
5
|
+
on: function(t, e) {
|
|
6
|
+
var i = n.get(t);
|
|
7
|
+
i ? i.push(e) : n.set(t, [e]);
|
|
8
|
+
},
|
|
9
|
+
off: function(t, e) {
|
|
10
|
+
var i = n.get(t);
|
|
11
|
+
i && (e ? i.splice(i.indexOf(e) >>> 0, 1) : n.set(t, []));
|
|
12
|
+
},
|
|
13
|
+
emit: function(t, e) {
|
|
14
|
+
var i = n.get(t);
|
|
15
|
+
i && i.slice().map(function(n) {
|
|
16
|
+
n(e);
|
|
17
|
+
}), (i = n.get("*")) && i.slice().map(function(n) {
|
|
18
|
+
n(t, e);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region \0@oxc-project+runtime@0.143.0/helpers/esm/typeof.js
|
|
26
|
+
function _typeof(o) {
|
|
27
|
+
"@babel/helpers - typeof";
|
|
28
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
29
|
+
return typeof o;
|
|
30
|
+
} : function(o) {
|
|
31
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
32
|
+
}, _typeof(o);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region \0@oxc-project+runtime@0.143.0/helpers/esm/toPrimitive.js
|
|
37
|
+
function toPrimitive(t, r) {
|
|
38
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
39
|
+
var e = t[Symbol.toPrimitive];
|
|
40
|
+
if (void 0 !== e) {
|
|
41
|
+
var i = e.call(t, r || "default");
|
|
42
|
+
if ("object" != _typeof(i)) return i;
|
|
43
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
44
|
+
}
|
|
45
|
+
return ("string" === r ? String : Number)(t);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region \0@oxc-project+runtime@0.143.0/helpers/esm/toPropertyKey.js
|
|
50
|
+
function toPropertyKey(t) {
|
|
51
|
+
var i = toPrimitive(t, "string");
|
|
52
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region \0@oxc-project+runtime@0.143.0/helpers/esm/defineProperty.js
|
|
57
|
+
function _defineProperty(e, r, t) {
|
|
58
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
59
|
+
value: t,
|
|
60
|
+
enumerable: !0,
|
|
61
|
+
configurable: !0,
|
|
62
|
+
writable: !0
|
|
63
|
+
}) : e[r] = t, e;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/SessionHandler.ts
|
|
20
68
|
var SessionHandler = class {
|
|
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
|
-
return this.eventHandler.on(eventName, handler);
|
|
65
|
-
}
|
|
66
|
-
detachEvent(eventName, handler) {
|
|
67
|
-
return this.eventHandler.off(eventName, handler);
|
|
68
|
-
}
|
|
69
|
+
constructor() {
|
|
70
|
+
_defineProperty(this, "eventHandler", mitt_default());
|
|
71
|
+
}
|
|
72
|
+
authenticated() {
|
|
73
|
+
this.eventHandler.emit("onAuthenticated");
|
|
74
|
+
}
|
|
75
|
+
async expired(callback, type) {
|
|
76
|
+
try {
|
|
77
|
+
let authPromise = new Promise((resolve) => {
|
|
78
|
+
let eventHandler = () => {
|
|
79
|
+
this.eventHandler.off("onAuthenticated", eventHandler);
|
|
80
|
+
callback?.();
|
|
81
|
+
resolve();
|
|
82
|
+
};
|
|
83
|
+
this.eventHandler.on("onAuthenticated", eventHandler);
|
|
84
|
+
});
|
|
85
|
+
let dialog = document.querySelector("dialog.session-expired-dialog");
|
|
86
|
+
if (!dialog) {
|
|
87
|
+
dialog = document.createElement("dialog");
|
|
88
|
+
dialog.className = "session-expired-dialog";
|
|
89
|
+
dialog.style.display = "flex";
|
|
90
|
+
dialog.style.padding = "0";
|
|
91
|
+
dialog.style.left = "50%";
|
|
92
|
+
dialog.style.transform = "translateX(-50%)";
|
|
93
|
+
let link = "/login?ReturnUrl=%2fapi/sessionexpired/remove";
|
|
94
|
+
if (type !== "RequireTwoFactor") await fetch("/logout", { method: "POST" });
|
|
95
|
+
else link = "/login?RequireTwoFactor=1&ReturnUrl=%2fapi/sessionexpired/remove";
|
|
96
|
+
dialog.innerHTML = `<iframe sandbox="allow-same-origin allow-scripts allow-modals allow-top-navigation-by-user-activation allow-forms" src="${link}" style="border:0;width:min(500px,100vw);height:min(700px, 100vh);"></iframe>`;
|
|
97
|
+
document.body.append(dialog);
|
|
98
|
+
dialog.showModal();
|
|
99
|
+
await authPromise;
|
|
100
|
+
dialog.remove();
|
|
101
|
+
} else await authPromise;
|
|
102
|
+
} catch (_) {
|
|
103
|
+
window.location.href = `/login?ReturnUrl=${encodeURIComponent(window.location.pathname)}${encodeURIComponent(window.location.search)}`;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
attachEvent(eventName, handler) {
|
|
107
|
+
return this.eventHandler.on(eventName, handler);
|
|
108
|
+
}
|
|
109
|
+
detachEvent(eventName, handler) {
|
|
110
|
+
return this.eventHandler.off(eventName, handler);
|
|
111
|
+
}
|
|
69
112
|
};
|
|
70
113
|
|
|
71
|
-
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region src/UserSession.ts
|
|
72
116
|
var UserSession = class extends SessionHandler {
|
|
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
|
-
|
|
117
|
+
constructor(userData) {
|
|
118
|
+
super();
|
|
119
|
+
_defineProperty(this, "amDesignator", "a.m.");
|
|
120
|
+
_defineProperty(this, "dayNames", [
|
|
121
|
+
"sunday",
|
|
122
|
+
"monday",
|
|
123
|
+
"tuesday",
|
|
124
|
+
"wednesday",
|
|
125
|
+
"thursday",
|
|
126
|
+
"friday",
|
|
127
|
+
"saturday"
|
|
128
|
+
]);
|
|
129
|
+
_defineProperty(this, "dayNamesShort", [
|
|
130
|
+
"sun",
|
|
131
|
+
"mon",
|
|
132
|
+
"tue",
|
|
133
|
+
"wed",
|
|
134
|
+
"thu",
|
|
135
|
+
"fri",
|
|
136
|
+
"sat"
|
|
137
|
+
]);
|
|
138
|
+
_defineProperty(this, "decimalSeparator", ".");
|
|
139
|
+
_defineProperty(this, "domain", "OB");
|
|
140
|
+
_defineProperty(this, "email", void 0);
|
|
141
|
+
_defineProperty(this, "formats", void 0);
|
|
142
|
+
_defineProperty(this, "groupSeparator", null);
|
|
143
|
+
_defineProperty(this, "integratedSecurity", false);
|
|
144
|
+
_defineProperty(this, "isDeveloper", false);
|
|
145
|
+
_defineProperty(this, "login", "");
|
|
146
|
+
_defineProperty(this, "loginType", void 0);
|
|
147
|
+
_defineProperty(this, "monthNames", [
|
|
148
|
+
"january",
|
|
149
|
+
"february",
|
|
150
|
+
"march",
|
|
151
|
+
"april",
|
|
152
|
+
"may",
|
|
153
|
+
"june",
|
|
154
|
+
"july",
|
|
155
|
+
"august",
|
|
156
|
+
"september",
|
|
157
|
+
"october",
|
|
158
|
+
"november",
|
|
159
|
+
"december",
|
|
160
|
+
""
|
|
161
|
+
]);
|
|
162
|
+
_defineProperty(this, "monthNamesShort", [
|
|
163
|
+
"jan",
|
|
164
|
+
"feb",
|
|
165
|
+
"mar",
|
|
166
|
+
"apr",
|
|
167
|
+
"may",
|
|
168
|
+
"jun",
|
|
169
|
+
"jul",
|
|
170
|
+
"augt",
|
|
171
|
+
"sep",
|
|
172
|
+
"oct",
|
|
173
|
+
"nov",
|
|
174
|
+
"dec",
|
|
175
|
+
""
|
|
176
|
+
]);
|
|
177
|
+
_defineProperty(this, "name", void 0);
|
|
178
|
+
_defineProperty(this, "pmDesignator", "p.m.");
|
|
179
|
+
_defineProperty(this, "shortDateFormat", "dd.MM.yyyy");
|
|
180
|
+
_defineProperty(this, "culture", "en-GB");
|
|
181
|
+
_defineProperty(this, "uiculture", "en-GB");
|
|
182
|
+
this.formats = {};
|
|
183
|
+
Object.assign(this, userData);
|
|
184
|
+
}
|
|
111
185
|
};
|
|
112
186
|
|
|
113
|
-
|
|
114
|
-
|
|
187
|
+
//#endregion
|
|
188
|
+
//#region src/log.ts
|
|
189
|
+
const article = window.af?.article?.id ?? window.window?.location.pathname.substring(1).split("/")[0];
|
|
190
|
+
/**
|
|
191
|
+
* Logs an activity to the website activity log
|
|
192
|
+
*
|
|
193
|
+
* @param activity name of the activity we are logging
|
|
194
|
+
* @param parameters A dictionary of parameters used when doing the activity
|
|
195
|
+
* @param timeElapsed Number of milliseconds elapsed while executing
|
|
196
|
+
*/
|
|
115
197
|
async function logActivity(activity, parameters = {}, timeElapsed = 0) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
198
|
+
try {
|
|
199
|
+
let response = await fetch("/api/logging/log-activity", {
|
|
200
|
+
method: "POST",
|
|
201
|
+
body: JSON.stringify({
|
|
202
|
+
article,
|
|
203
|
+
timeElapsed,
|
|
204
|
+
action: activity,
|
|
205
|
+
parameters: {
|
|
206
|
+
...parameters,
|
|
207
|
+
_session: getSessionData()
|
|
208
|
+
}
|
|
209
|
+
}),
|
|
210
|
+
headers: {
|
|
211
|
+
"Content-Type": "application/json",
|
|
212
|
+
Accept: "application/json"
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
if (!response.ok) {
|
|
216
|
+
if (response.headers.get("Content-Type")?.includes("application/json")) {
|
|
217
|
+
let data = await response.json();
|
|
218
|
+
console.error(data.error);
|
|
219
|
+
} else console.error(`${response.status} ${response.statusText}`);
|
|
220
|
+
}
|
|
221
|
+
} catch {}
|
|
140
222
|
}
|
|
223
|
+
/**
|
|
224
|
+
* Records the current time, and returns a function that logs the activity with the
|
|
225
|
+
* time elapsed when it is called.
|
|
226
|
+
*
|
|
227
|
+
* @param activity Name of the activity we are logging
|
|
228
|
+
* @param parameters A dictionary of parameters used when doing the activity
|
|
229
|
+
* @returns A function that should be called when the activity is completed to log it to the website log
|
|
230
|
+
*/
|
|
141
231
|
function startActivity(activity, startParameters = {}) {
|
|
142
|
-
|
|
143
|
-
|
|
232
|
+
let start = performance.now();
|
|
233
|
+
return (endParameters = {}) => logActivity(activity, {
|
|
234
|
+
...startParameters,
|
|
235
|
+
...endParameters
|
|
236
|
+
}, performance.now() - start);
|
|
144
237
|
}
|
|
238
|
+
/**
|
|
239
|
+
* Records the time used to execute an operation and returns the value of the operation and the time
|
|
240
|
+
* in milliseconds
|
|
241
|
+
*/
|
|
145
242
|
async function time(callback) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
return [returnValue, performance.now() - start];
|
|
243
|
+
let start = performance.now();
|
|
244
|
+
return [await callback(), performance.now() - start];
|
|
149
245
|
}
|
|
150
246
|
async function logError(error) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return await result.json();
|
|
167
|
-
}
|
|
168
|
-
console.error(Error("Failed to log error"));
|
|
169
|
-
} catch {
|
|
170
|
-
}
|
|
247
|
+
try {
|
|
248
|
+
if (!error) return;
|
|
249
|
+
let data = typeof error === "string" ? { error } : typeof error === "object" ? "message" in error ? {
|
|
250
|
+
error: error.message,
|
|
251
|
+
stack: error.stack ?? null
|
|
252
|
+
} : { error: error.toString() } : { error: "Unspecified error" };
|
|
253
|
+
console.error(error);
|
|
254
|
+
let result = await fetch("/data/Logger/LogError", {
|
|
255
|
+
method: "POST",
|
|
256
|
+
body: JSON.stringify(data),
|
|
257
|
+
headers: { "Content-Type": "application/json; charset=utf-8" }
|
|
258
|
+
});
|
|
259
|
+
if (result.ok) return await result.json();
|
|
260
|
+
console.error(Error("Failed to log error"));
|
|
261
|
+
} catch {}
|
|
171
262
|
}
|
|
172
263
|
function getSessionId() {
|
|
173
|
-
|
|
264
|
+
return crypto.randomUUID?.();
|
|
174
265
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
266
|
+
let sessionStart = Date.now();
|
|
267
|
+
let sessionId = getSessionId();
|
|
268
|
+
let deviceId = "";
|
|
269
|
+
let browserSessionId = "";
|
|
179
270
|
if ("document" in window) {
|
|
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
|
-
_session: getSessionData()
|
|
207
|
-
}
|
|
208
|
-
})
|
|
209
|
-
);
|
|
210
|
-
} else {
|
|
211
|
-
startSession();
|
|
212
|
-
}
|
|
213
|
-
});
|
|
271
|
+
let startSession = () => logActivity("AfSessionInit", {
|
|
272
|
+
"appframe-core": window.af?.common?.version,
|
|
273
|
+
"appframe-data": window.af?.data?.version,
|
|
274
|
+
"app-version": window.af?.article?.version
|
|
275
|
+
});
|
|
276
|
+
document.addEventListener("DOMContentLoaded", () => {
|
|
277
|
+
deviceId = localStorage.getItem("_afDeviceId");
|
|
278
|
+
browserSessionId = sessionStorage.getItem("_afSessionId");
|
|
279
|
+
if (!deviceId) {
|
|
280
|
+
deviceId = getSessionId();
|
|
281
|
+
localStorage.setItem("_afDeviceId", deviceId);
|
|
282
|
+
}
|
|
283
|
+
if (!browserSessionId) {
|
|
284
|
+
browserSessionId = sessionId;
|
|
285
|
+
sessionStorage.setItem("_afSessionId", browserSessionId);
|
|
286
|
+
}
|
|
287
|
+
startSession();
|
|
288
|
+
});
|
|
289
|
+
document.addEventListener("visibilitychange", function logData() {
|
|
290
|
+
if (document.visibilityState === "hidden") navigator.sendBeacon("/api/logging/log-activity", JSON.stringify({
|
|
291
|
+
article,
|
|
292
|
+
action: "AfSessionEnd",
|
|
293
|
+
parameters: { _session: getSessionData() }
|
|
294
|
+
}));
|
|
295
|
+
else startSession();
|
|
296
|
+
});
|
|
214
297
|
}
|
|
215
298
|
function getSessionData() {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
// src/localStorage.ts
|
|
299
|
+
return {
|
|
300
|
+
browserSessionId,
|
|
301
|
+
sessionStart,
|
|
302
|
+
sessionLength: Date.now() - sessionStart,
|
|
303
|
+
sessionId,
|
|
304
|
+
deviceId
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
//#endregion
|
|
309
|
+
//#region src/errorHandler.ts
|
|
310
|
+
window.window?.addEventListener("error", (event) => {
|
|
311
|
+
if (typeof event === "object" && "error" in event) logError(event.error);
|
|
312
|
+
else if (typeof event === "object" && "message" in event) logError(event);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
//#endregion
|
|
316
|
+
//#region src/localStorage.ts
|
|
237
317
|
function getLocalKey(key) {
|
|
238
|
-
|
|
318
|
+
return [
|
|
319
|
+
af?.article?.id || "",
|
|
320
|
+
"common",
|
|
321
|
+
key
|
|
322
|
+
].join(".");
|
|
239
323
|
}
|
|
240
324
|
var LocalStorage = class {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
localStorage.removeItem(key);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
}
|
|
325
|
+
constructor() {
|
|
326
|
+
_defineProperty(this, "isPersisted", () => true);
|
|
327
|
+
}
|
|
328
|
+
getLocalStorageKey(key, global) {
|
|
329
|
+
return global ? ["common", key].join(".") : getLocalKey(key);
|
|
330
|
+
}
|
|
331
|
+
get(key, defaultValue, global) {
|
|
332
|
+
const lsKey = this.getLocalStorageKey(key, global);
|
|
333
|
+
const value = localStorage.getItem(lsKey);
|
|
334
|
+
return value !== null ? JSON.parse(value) : defaultValue;
|
|
335
|
+
}
|
|
336
|
+
set(key, value, global) {
|
|
337
|
+
localStorage.setItem(this.getLocalStorageKey(key, global), JSON.stringify(value));
|
|
338
|
+
}
|
|
339
|
+
remove(key, global) {
|
|
340
|
+
const lsKey = this.getLocalStorageKey(key, global);
|
|
341
|
+
if (localStorage.getItem(lsKey) !== null) {
|
|
342
|
+
localStorage.removeItem(lsKey);
|
|
343
|
+
return true;
|
|
344
|
+
} else return false;
|
|
345
|
+
}
|
|
346
|
+
clear() {
|
|
347
|
+
const baseKey = getLocalKey("");
|
|
348
|
+
for (let key in localStorage) if (key.slice(0, baseKey.length) === baseKey) localStorage.removeItem(key);
|
|
349
|
+
}
|
|
270
350
|
};
|
|
271
351
|
var localStorage_default = new LocalStorage();
|
|
272
352
|
|
|
273
|
-
|
|
353
|
+
//#endregion
|
|
354
|
+
//#region src/date.ts
|
|
274
355
|
function toLocalISODate(date) {
|
|
275
|
-
|
|
356
|
+
return date ? toLocalISODateTime(date).substring(0, 10) : null;
|
|
276
357
|
}
|
|
277
358
|
function pad(num, digits = 2) {
|
|
278
|
-
|
|
359
|
+
return String(num).padStart(digits, "0");
|
|
279
360
|
}
|
|
280
361
|
function toLocalISODateTime(date) {
|
|
281
|
-
|
|
282
|
-
date.getMinutes()
|
|
283
|
-
)}:${pad(date.getSeconds())}.${pad(date.getMilliseconds(), 3)}` : null;
|
|
362
|
+
return date ? `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}T${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}.${pad(date.getMilliseconds(), 3)}` : null;
|
|
284
363
|
}
|
|
364
|
+
/**
|
|
365
|
+
* Converts a date object to a string compatible with input type datetime-local.
|
|
366
|
+
* Will handle the UTC value of the date as if it was local, to be compatible with
|
|
367
|
+
* data objects.
|
|
368
|
+
*
|
|
369
|
+
* If null or undefined is passed, returns an empty string.
|
|
370
|
+
*
|
|
371
|
+
* @param date
|
|
372
|
+
*/
|
|
285
373
|
function toLocalDateTime(date) {
|
|
286
|
-
|
|
374
|
+
return date ? date.toISOString().substring(0, 16) : "";
|
|
287
375
|
}
|
|
288
376
|
|
|
289
|
-
|
|
377
|
+
//#endregion
|
|
378
|
+
//#region src/downloadBlob.ts
|
|
290
379
|
function downloadBlob(blob, fileName) {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
|
|
380
|
+
return new Promise((resolve, reject) => {
|
|
381
|
+
try {
|
|
382
|
+
const url = URL.createObjectURL(blob);
|
|
383
|
+
const parent = document.body;
|
|
384
|
+
const anchor = document.createElement("a");
|
|
385
|
+
anchor.setAttribute("download", fileName);
|
|
386
|
+
anchor.href = url;
|
|
387
|
+
parent.append(anchor);
|
|
388
|
+
anchor.click();
|
|
389
|
+
parent.removeChild(anchor);
|
|
390
|
+
setTimeout(() => {
|
|
391
|
+
URL.revokeObjectURL(url);
|
|
392
|
+
resolve();
|
|
393
|
+
}, 250);
|
|
394
|
+
} catch (ex) {
|
|
395
|
+
reject(ex);
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
//#endregion
|
|
401
|
+
//#region src/invariant.ts
|
|
402
|
+
/**
|
|
403
|
+
* Throws an error if the condition is falsy. The provided message is used as the error message.
|
|
404
|
+
* A function can be provided to lazily generate the message.
|
|
405
|
+
*
|
|
406
|
+
* @param {any} condition
|
|
407
|
+
* @param {string | (() => string)} [message]
|
|
408
|
+
* @returns {void}
|
|
409
|
+
*/
|
|
312
410
|
function invariant(condition, message) {
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
const provided = typeof message === "function" ? message() : message;
|
|
317
|
-
throw new Error(provided ?? "Invariant failed");
|
|
411
|
+
if (condition) return;
|
|
412
|
+
const provided = typeof message === "function" ? message() : message;
|
|
413
|
+
throw new Error(provided ?? "Invariant failed");
|
|
318
414
|
}
|
|
319
415
|
|
|
320
|
-
|
|
416
|
+
//#endregion
|
|
417
|
+
//#region src/downloadRequest.ts
|
|
321
418
|
async function downloadRequest(request, { fileName, signal, onProgress }) {
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
419
|
+
let bytesReceived = 0;
|
|
420
|
+
onProgress?.({ status: "requested" });
|
|
421
|
+
try {
|
|
422
|
+
let response = await fetch(request);
|
|
423
|
+
if (!response.ok || !response.body) {
|
|
424
|
+
if (response.headers.get("content-type")?.includes("application/json")) {
|
|
425
|
+
let data = await response.json();
|
|
426
|
+
invariant(!data.error, data.error);
|
|
427
|
+
}
|
|
428
|
+
throw new Error(`${response.status} ${response.statusText}`);
|
|
429
|
+
}
|
|
430
|
+
try {
|
|
431
|
+
const reader = response.body.getReader();
|
|
432
|
+
const stream = new ReadableStream({ start: (controller) => {
|
|
433
|
+
function pump() {
|
|
434
|
+
return reader.read().then((read) => {
|
|
435
|
+
if (read.done) {
|
|
436
|
+
controller.close();
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
bytesReceived += read.value.length;
|
|
440
|
+
onProgress?.({
|
|
441
|
+
bytesReceived,
|
|
442
|
+
status: "downloading"
|
|
443
|
+
});
|
|
444
|
+
controller.enqueue(read.value);
|
|
445
|
+
return pump();
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
return pump();
|
|
449
|
+
} });
|
|
450
|
+
response = new Response(stream);
|
|
451
|
+
} catch {}
|
|
452
|
+
const blob = await response.blob();
|
|
453
|
+
onProgress?.({
|
|
454
|
+
bytesReceived,
|
|
455
|
+
status: "downloaded"
|
|
456
|
+
});
|
|
457
|
+
await downloadBlob(blob, fileName);
|
|
458
|
+
onProgress?.({
|
|
459
|
+
bytesReceived,
|
|
460
|
+
status: "completed"
|
|
461
|
+
});
|
|
462
|
+
} catch (err) {
|
|
463
|
+
if (signal?.aborted) onProgress?.({ status: "cancelled" });
|
|
464
|
+
else onProgress?.({
|
|
465
|
+
error: err.message,
|
|
466
|
+
status: "error"
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
//#endregion
|
|
472
|
+
//#region src/parseFunc.ts
|
|
473
|
+
const funcRegex = /^function(?:\s+([a-zA-Z$_][a-zA-Z0-9$_]*))?\s*\(((?:(?:\s*[a-zA-Z$_][a-zA-Z0-9$_]*)(?:\s*,\s*[a-zA-Z$_][a-zA-Z0-9$_]*)*)?)\s*\)\s*\{((?:.|\n|\r)*)\}$/;
|
|
474
|
+
const arrowBlockRegex = /^(?:\(([^)]*)\)|([a-zA-Z$_][a-zA-Z0-9$_]*))\s*=>\s*\{([\s\S]*)\}$/;
|
|
475
|
+
const arrowExpressionRegex = /^(?:\(([^)]*)\)|([a-zA-Z$_][a-zA-Z0-9$_]*))\s*=>\s*([\s\S]+)$/;
|
|
476
|
+
const parseArguments = (args) => args.split(",").map((a) => a.trim()).filter(Boolean);
|
|
373
477
|
function parseFunc(func) {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
478
|
+
const funcString = func.toString();
|
|
479
|
+
const arrowBlockMatch = funcString.match(arrowBlockRegex);
|
|
480
|
+
if (arrowBlockMatch) {
|
|
481
|
+
const args = parseArguments(arrowBlockMatch[1] ?? arrowBlockMatch[2] ?? "");
|
|
482
|
+
return {
|
|
483
|
+
name: func.name,
|
|
484
|
+
args,
|
|
485
|
+
body: arrowBlockMatch[3]
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
const arrowExpressionMatch = funcString.match(arrowExpressionRegex);
|
|
489
|
+
if (arrowExpressionMatch) {
|
|
490
|
+
const args = parseArguments(arrowExpressionMatch[1] ?? arrowExpressionMatch[2] ?? "");
|
|
491
|
+
return {
|
|
492
|
+
name: func.name,
|
|
493
|
+
args,
|
|
494
|
+
body: arrowExpressionMatch[3]
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
const match = funcString.match(funcRegex);
|
|
498
|
+
invariant(match, "Unable to parse function!");
|
|
499
|
+
return {
|
|
500
|
+
name: func.name ?? match[1],
|
|
501
|
+
args: parseArguments(match[2]),
|
|
502
|
+
body: match[3]
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
//#endregion
|
|
507
|
+
//#region src/Namespace.ts
|
|
508
|
+
function Namespace() {}
|
|
509
|
+
Namespace.prototype = Object.create(null);
|
|
510
|
+
|
|
511
|
+
//#endregion
|
|
512
|
+
//#region src/requireNamespace.ts
|
|
397
513
|
function requireNamespace(root, namespace) {
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
514
|
+
if (typeof root === "object") {
|
|
515
|
+
if (typeof namespace === "string") return requireNamespace(root, namespace.split("."));
|
|
516
|
+
else if (Array.isArray(namespace)) {
|
|
517
|
+
let current = root;
|
|
518
|
+
for (let i = 0; i < namespace.length; i++) current = current[namespace[i]] = current[namespace[i]] || new Namespace();
|
|
519
|
+
return current;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
//#endregion
|
|
525
|
+
//#region src/expose.ts
|
|
526
|
+
/**
|
|
527
|
+
* Expose a value to a potentially deeply nested global variable
|
|
528
|
+
* @param name {string | object} Either a dot-separated path to the global variable, or an object to expose the value on
|
|
529
|
+
* @param value {object | Function | Function[]} The value(s) to expose
|
|
530
|
+
*/
|
|
413
531
|
function expose(name, value, options = {}) {
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
invariant(false, "Invalid typeof parameter 'name', or unable to parse pValue!");
|
|
440
|
-
}
|
|
441
|
-
if (Object.hasOwn(namespace, keyName) && options?.overwrite !== true) {
|
|
442
|
-
console.warn(`${[name, keyName].join(".")} is already defined!`);
|
|
443
|
-
} else {
|
|
444
|
-
namespace[keyName] = value;
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
// src/getLocalizedString.ts
|
|
532
|
+
if (Array.isArray(value)) for (let func of value) {
|
|
533
|
+
const { name: funcName } = parseFunc(func);
|
|
534
|
+
invariant(funcName, "Unable to detect name of function! Name it, or only supply one function and include name in first parameter.");
|
|
535
|
+
expose(name, func);
|
|
536
|
+
}
|
|
537
|
+
else {
|
|
538
|
+
let keyName;
|
|
539
|
+
let namespace;
|
|
540
|
+
if (typeof value === "function") keyName = parseFunc(value).name;
|
|
541
|
+
if (typeof name === "string") {
|
|
542
|
+
let pathParts = name.split(".");
|
|
543
|
+
if (!keyName) {
|
|
544
|
+
keyName = pathParts[pathParts.length - 1];
|
|
545
|
+
pathParts = pathParts.slice(0, pathParts.length - 1);
|
|
546
|
+
}
|
|
547
|
+
namespace = requireNamespace(window, pathParts);
|
|
548
|
+
} else if (keyName && (typeof name === "object" || typeof name === "function")) namespace = name;
|
|
549
|
+
else invariant(false, "Invalid typeof parameter 'name', or unable to parse pValue!");
|
|
550
|
+
if (Object.hasOwn(namespace, keyName) && options?.overwrite !== true) console.warn(`${[name, keyName].join(".")} is already defined!`);
|
|
551
|
+
else namespace[keyName] = value;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
//#endregion
|
|
556
|
+
//#region src/getLocalizedString.ts
|
|
450
557
|
async function fetchLocalizedString(text) {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
} catch {
|
|
465
|
-
}
|
|
466
|
-
return text;
|
|
558
|
+
try {
|
|
559
|
+
let response = await fetch(`/api/user/localize/new/${af.article.id}`, {
|
|
560
|
+
body: JSON.stringify({ text }),
|
|
561
|
+
headers: {
|
|
562
|
+
Accept: "application/json; */*",
|
|
563
|
+
"Content-Type": "application/json",
|
|
564
|
+
"X-Requested-With": "XMLHttpRequest"
|
|
565
|
+
},
|
|
566
|
+
method: "POST"
|
|
567
|
+
});
|
|
568
|
+
if (response.ok) return await response.json();
|
|
569
|
+
} catch {}
|
|
570
|
+
return text;
|
|
467
571
|
}
|
|
468
572
|
function getLocalizedString(string, wait) {
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
573
|
+
if (typeof string === "string" && string.trim().length === 0) return string;
|
|
574
|
+
if (string === void 0 || string === "" || string === null) return;
|
|
575
|
+
if (window.af?.article?.i18n !== void 0) {
|
|
576
|
+
if (!Object.hasOwn(af.article.i18n, string)) {
|
|
577
|
+
af.article.i18n[string] = string;
|
|
578
|
+
let promise = fetchLocalizedString(string).then((text) => {
|
|
579
|
+
af.article.i18n[string] = text;
|
|
580
|
+
return text;
|
|
581
|
+
});
|
|
582
|
+
if (wait) return promise;
|
|
583
|
+
}
|
|
584
|
+
return af.article.i18n[string];
|
|
585
|
+
} else return string;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
//#endregion
|
|
589
|
+
//#region src/filter/operators.ts
|
|
590
|
+
/**
|
|
591
|
+
* Operators that take no value at all.
|
|
592
|
+
*
|
|
593
|
+
* `eOperators.ParameterLessChecks` plus the blank checks.
|
|
594
|
+
*/
|
|
595
|
+
const PARAMETERLESS_OPERATORS = [
|
|
596
|
+
"isnull",
|
|
597
|
+
"isnotnull",
|
|
598
|
+
"istrue",
|
|
599
|
+
"isfalse",
|
|
600
|
+
"isblank",
|
|
601
|
+
"isnotblank"
|
|
602
|
+
];
|
|
603
|
+
/** Operators whose value is a two element `[from, to]` array. */
|
|
604
|
+
const PAIR_OPERATORS = [
|
|
605
|
+
"between",
|
|
606
|
+
"notbetween",
|
|
607
|
+
"datebetween",
|
|
608
|
+
"datenotbetween"
|
|
609
|
+
];
|
|
610
|
+
/** Operators whose value is an array of values. */
|
|
611
|
+
const LIST_OPERATORS = ["inlist", "notinlist"];
|
|
612
|
+
function getOperatorArity(operator) {
|
|
613
|
+
if (PARAMETERLESS_OPERATORS.includes(operator)) return "none";
|
|
614
|
+
if (PAIR_OPERATORS.includes(operator)) return "pair";
|
|
615
|
+
if (LIST_OPERATORS.includes(operator)) return "list";
|
|
616
|
+
return "single";
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Operator labels, keyed by operator. Comparison operators have a different
|
|
620
|
+
* label for dates than for numbers, matching the Windows filter editor.
|
|
621
|
+
*
|
|
622
|
+
* @see `appframe/Appframe3.Win/Controls/afFilterEditor/Utils/FilterLineControlUtils.vb`
|
|
623
|
+
*/
|
|
624
|
+
const OPERATOR_LABELS = {
|
|
625
|
+
equals: "Equal to",
|
|
626
|
+
notequals: "Not equal to",
|
|
627
|
+
greaterthan: "Greater than",
|
|
628
|
+
greaterthanorequal: "Greater than or equal to",
|
|
629
|
+
lessthan: "Less than",
|
|
630
|
+
lessthanorequal: "Less than or equal to",
|
|
631
|
+
beginswith: "Begins with",
|
|
632
|
+
notbeginswith: "Does not begin with",
|
|
633
|
+
endswith: "Ends with",
|
|
634
|
+
notendswith: "Does not end with",
|
|
635
|
+
contains: "Contains",
|
|
636
|
+
notcontains: "Does not contain",
|
|
637
|
+
like: "Like",
|
|
638
|
+
notlike: "Not like",
|
|
639
|
+
isnull: "Is null",
|
|
640
|
+
isnotnull: "Is not null",
|
|
641
|
+
isblank: "Is empty",
|
|
642
|
+
isnotblank: "Is not empty",
|
|
643
|
+
istrue: "Is true",
|
|
644
|
+
isfalse: "Is false",
|
|
645
|
+
inlist: "In list",
|
|
646
|
+
notinlist: "Not in list",
|
|
647
|
+
between: "Between",
|
|
648
|
+
notbetween: "Not between",
|
|
649
|
+
dateequals: "Equal to",
|
|
650
|
+
notdatequals: "Not equal to",
|
|
651
|
+
dategreaterthan: "After",
|
|
652
|
+
dategreaterthanorequal: "On or after",
|
|
653
|
+
datelessthan: "Before",
|
|
654
|
+
datelessthanorequal: "Up to",
|
|
655
|
+
datebetween: "Between",
|
|
656
|
+
datenotbetween: "Not between"
|
|
657
|
+
};
|
|
658
|
+
/** Comparison operator labels used for date and time fields. */
|
|
659
|
+
const DATE_OPERATOR_LABELS = {
|
|
660
|
+
greaterthan: "After",
|
|
661
|
+
greaterthanorequal: "On or after",
|
|
662
|
+
lessthan: "Before",
|
|
663
|
+
lessthanorequal: "Up to"
|
|
664
|
+
};
|
|
665
|
+
/**
|
|
666
|
+
* Returns the localized label for an operator. Date fields get the
|
|
667
|
+
* "After"/"Before" wording instead of "Greater than"/"Less than".
|
|
668
|
+
*/
|
|
669
|
+
function getOperatorLabel(operator, fieldType) {
|
|
670
|
+
let label = (fieldType === "date" || fieldType === "datetime" ? DATE_OPERATOR_LABELS[operator] : void 0) ?? OPERATOR_LABELS[operator];
|
|
671
|
+
return getLocalizedString(label ?? operator);
|
|
672
|
+
}
|
|
673
|
+
const EQUALITY = ["equals", "notequals"];
|
|
674
|
+
const LIST_CHECKS = ["inlist", "notinlist"];
|
|
675
|
+
const RANGE = ["between", "notbetween"];
|
|
676
|
+
const COMPARISONS = [
|
|
677
|
+
"greaterthan",
|
|
678
|
+
"greaterthanorequal",
|
|
679
|
+
"lessthan",
|
|
680
|
+
"lessthanorequal"
|
|
681
|
+
];
|
|
682
|
+
const STRING_CHECKS = [
|
|
683
|
+
"contains",
|
|
684
|
+
"notcontains",
|
|
685
|
+
"beginswith",
|
|
686
|
+
"notbeginswith",
|
|
687
|
+
"endswith",
|
|
688
|
+
"notendswith",
|
|
689
|
+
"isblank",
|
|
690
|
+
"isnotblank"
|
|
691
|
+
];
|
|
692
|
+
/**
|
|
693
|
+
* Operators offered per field type, in the order the Windows filter editor
|
|
694
|
+
* presents them.
|
|
695
|
+
*/
|
|
696
|
+
const OPERATORS_BY_TYPE = {
|
|
697
|
+
date: [
|
|
698
|
+
...EQUALITY,
|
|
699
|
+
...LIST_CHECKS,
|
|
700
|
+
...RANGE,
|
|
701
|
+
...COMPARISONS
|
|
702
|
+
],
|
|
703
|
+
datetime: [
|
|
704
|
+
...EQUALITY,
|
|
705
|
+
...LIST_CHECKS,
|
|
706
|
+
...RANGE,
|
|
707
|
+
...COMPARISONS
|
|
708
|
+
],
|
|
709
|
+
number: [
|
|
710
|
+
...EQUALITY,
|
|
711
|
+
...LIST_CHECKS,
|
|
712
|
+
...RANGE,
|
|
713
|
+
...COMPARISONS
|
|
714
|
+
],
|
|
715
|
+
boolean: ["istrue", "isfalse"],
|
|
716
|
+
string: [
|
|
717
|
+
...EQUALITY,
|
|
718
|
+
...STRING_CHECKS,
|
|
719
|
+
...LIST_CHECKS,
|
|
720
|
+
...RANGE,
|
|
721
|
+
...COMPARISONS
|
|
722
|
+
],
|
|
723
|
+
uniqueidentifier: [
|
|
724
|
+
...EQUALITY,
|
|
725
|
+
...STRING_CHECKS,
|
|
726
|
+
...LIST_CHECKS,
|
|
727
|
+
...RANGE,
|
|
728
|
+
...COMPARISONS
|
|
729
|
+
]
|
|
730
|
+
};
|
|
731
|
+
/**
|
|
732
|
+
* Default operator per field type.
|
|
733
|
+
*
|
|
734
|
+
* Text defaults to `contains` because a substring search is what people
|
|
735
|
+
* reach for when they type into a filter box without picking an operator —
|
|
736
|
+
* `beginswith` silently misses matches in the middle of the value.
|
|
737
|
+
*/
|
|
738
|
+
const DEFAULT_OPERATOR_BY_TYPE = {
|
|
739
|
+
date: "equals",
|
|
740
|
+
datetime: "equals",
|
|
741
|
+
number: "equals",
|
|
742
|
+
boolean: "istrue",
|
|
743
|
+
string: "contains",
|
|
744
|
+
uniqueidentifier: "beginswith"
|
|
745
|
+
};
|
|
746
|
+
/**
|
|
747
|
+
* Returns the operators available for a field. Nullable fields additionally get
|
|
748
|
+
* the null checks. Boolean fields never get null checks, since `istrue` and
|
|
749
|
+
* `isfalse` already cover the parameterless cases.
|
|
750
|
+
*/
|
|
751
|
+
function getOperatorsForField(field) {
|
|
752
|
+
let operators = [...OPERATORS_BY_TYPE[field.type] ?? OPERATORS_BY_TYPE.string];
|
|
753
|
+
if (field.nullable && field.type !== "boolean") operators.push("isnull", "isnotnull");
|
|
754
|
+
return operators;
|
|
755
|
+
}
|
|
756
|
+
function getDefaultOperator(field) {
|
|
757
|
+
return DEFAULT_OPERATOR_BY_TYPE[field.type] ?? "equals";
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* The `valueType` to send for a field/operator combination.
|
|
761
|
+
*
|
|
762
|
+
* Parameterless operators send `"null"`, list and range operators send
|
|
763
|
+
* `"array"`, everything else sends the field's own value type.
|
|
764
|
+
*/
|
|
765
|
+
function getValueType(field, operator) {
|
|
766
|
+
if (getOperatorArity(operator) === "none") return "null";
|
|
767
|
+
switch (field.type) {
|
|
768
|
+
case "number": return "number";
|
|
769
|
+
case "boolean": return "boolean";
|
|
770
|
+
case "date":
|
|
771
|
+
case "datetime": return "datetime";
|
|
772
|
+
case "uniqueidentifier": return "uniqueidentifier";
|
|
773
|
+
default: return "string";
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
/** Relative date values offered in the date value editor, in menu order. */
|
|
777
|
+
const RELATIVE_DATE_VALUES = [
|
|
778
|
+
"Yesterday",
|
|
779
|
+
"Today",
|
|
780
|
+
"Tomorrow",
|
|
781
|
+
"ThisWeek",
|
|
782
|
+
"ThisMonth",
|
|
783
|
+
"ThisYear",
|
|
784
|
+
"LastWeek",
|
|
785
|
+
"LastMonth",
|
|
786
|
+
"LastYear",
|
|
787
|
+
"NextWeek",
|
|
788
|
+
"NextMonth",
|
|
789
|
+
"NextYear"
|
|
790
|
+
];
|
|
791
|
+
const DYNAMIC_VALUE_LABELS = {
|
|
792
|
+
CurrentUser: "Current user",
|
|
793
|
+
Yesterday: "Yesterday",
|
|
794
|
+
Today: "Today",
|
|
795
|
+
Tomorrow: "Tomorrow",
|
|
796
|
+
ThisWeek: "This week",
|
|
797
|
+
ThisMonth: "This month",
|
|
798
|
+
ThisYear: "This year",
|
|
799
|
+
LastWeek: "Last week",
|
|
800
|
+
LastMonth: "Last month",
|
|
801
|
+
LastYear: "Last year",
|
|
802
|
+
NextWeek: "Next week",
|
|
803
|
+
NextMonth: "Next month",
|
|
804
|
+
NextYear: "Next year",
|
|
805
|
+
UserYesterday: "Yesterday",
|
|
806
|
+
UserToday: "Today",
|
|
807
|
+
UserTomorrow: "Tomorrow",
|
|
808
|
+
UserThisWeek: "This week",
|
|
809
|
+
UserThisMonth: "This month",
|
|
810
|
+
UserThisYear: "This year",
|
|
811
|
+
UserLastWeek: "Last week",
|
|
812
|
+
UserLastMonth: "Last month",
|
|
813
|
+
UserLastYear: "Last year",
|
|
814
|
+
UserNextWeek: "Next week",
|
|
815
|
+
UserNextMonth: "Next month",
|
|
816
|
+
UserNextYear: "Next year"
|
|
817
|
+
};
|
|
818
|
+
function getDynamicValueLabel(value) {
|
|
819
|
+
return getLocalizedString(DYNAMIC_VALUE_LABELS[value] ?? value);
|
|
820
|
+
}
|
|
821
|
+
/**
|
|
822
|
+
* Returns the dynamic ("special") values that can be used with a field. Date
|
|
823
|
+
* fields get the relative dates; string fields can be compared to the current
|
|
824
|
+
* user name.
|
|
825
|
+
*/
|
|
826
|
+
function getDynamicValuesForField(field) {
|
|
827
|
+
if (field.type === "date" || field.type === "datetime") return [...RELATIVE_DATE_VALUES];
|
|
828
|
+
if (field.type === "string") return ["CurrentUser"];
|
|
829
|
+
return [];
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Aliases for names `eOperators.ToString()` can produce that are not operators
|
|
833
|
+
* in their own right. `Not` is the bare negation flag (i.e. "not equal to"),
|
|
834
|
+
* `Tweens` shares its numeric value with `NotBetween`, and combined flags are
|
|
835
|
+
* rendered as a comma separated list.
|
|
836
|
+
*
|
|
837
|
+
* @see `FilterElementConverter.FilterExpressionToDictionary`
|
|
838
|
+
*/
|
|
839
|
+
const OPERATOR_ALIASES = {
|
|
840
|
+
not: "notequals",
|
|
841
|
+
tweens: "notbetween",
|
|
842
|
+
notdate: "notdatequals",
|
|
843
|
+
notdateequals: "notdatequals"
|
|
844
|
+
};
|
|
845
|
+
/**
|
|
846
|
+
* Normalizes an operator string coming from the server or from another filter
|
|
847
|
+
* producer (`AfGrid` emits PascalCase names) to the canonical lower-case form.
|
|
848
|
+
*/
|
|
849
|
+
function normalizeOperator(operator) {
|
|
850
|
+
let normalized = operator.toLowerCase().replace(/[\s,]/g, "");
|
|
851
|
+
return OPERATOR_ALIASES[normalized] ?? normalized;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
//#endregion
|
|
855
|
+
//#region src/filter/tree.ts
|
|
856
|
+
/**
|
|
857
|
+
* Creates the default empty filter: a single `and` group with no conditions.
|
|
858
|
+
*/
|
|
859
|
+
function createEmptyFilter() {
|
|
860
|
+
return createEmptyGroup("and");
|
|
861
|
+
}
|
|
862
|
+
function createEmptyGroup(mode = "and") {
|
|
863
|
+
return {
|
|
864
|
+
type: "group",
|
|
865
|
+
mode,
|
|
866
|
+
items: []
|
|
867
|
+
};
|
|
868
|
+
}
|
|
869
|
+
function isGroup(item) {
|
|
870
|
+
return item.type === "group";
|
|
871
|
+
}
|
|
872
|
+
function isExpression(item) {
|
|
873
|
+
return item.type === "expression";
|
|
874
|
+
}
|
|
875
|
+
/**
|
|
876
|
+
* Normalizes any filter value into a tree the editor can render, without
|
|
877
|
+
* changing its structure. An expression is wrapped in a group; a missing filter
|
|
878
|
+
* becomes the default layout.
|
|
879
|
+
*/
|
|
880
|
+
function toFilterTree(filter) {
|
|
881
|
+
if (!filter) return createEmptyFilter();
|
|
882
|
+
if (isExpression(filter)) return {
|
|
883
|
+
type: "group",
|
|
884
|
+
mode: "and",
|
|
885
|
+
items: [filter]
|
|
886
|
+
};
|
|
887
|
+
return filter;
|
|
888
|
+
}
|
|
889
|
+
/**
|
|
890
|
+
* True when the filter contains no expressions. An empty filter must not be
|
|
891
|
+
* saved or applied.
|
|
892
|
+
*/
|
|
893
|
+
function isEmptyFilter(filter) {
|
|
894
|
+
return countExpressions(filter) === 0;
|
|
895
|
+
}
|
|
896
|
+
function countExpressions(filter) {
|
|
897
|
+
if (!filter) return 0;
|
|
898
|
+
if (isExpression(filter)) return 1;
|
|
899
|
+
return filter.items.reduce((total, item) => total + countExpressions(item), 0);
|
|
900
|
+
}
|
|
901
|
+
/**
|
|
902
|
+
* Removes incomplete expressions and empty groups from the whole tree. Returns
|
|
903
|
+
* null when nothing is left. Use before applying a filter to a data object so
|
|
904
|
+
* incomplete rows are not sent.
|
|
905
|
+
*/
|
|
906
|
+
function pruneFilter(filter) {
|
|
907
|
+
if (!filter) return null;
|
|
908
|
+
if (isExpression(filter)) return isCompleteExpression(filter) ? {
|
|
909
|
+
type: "group",
|
|
910
|
+
mode: "and",
|
|
911
|
+
items: [filter]
|
|
912
|
+
} : null;
|
|
913
|
+
let items = filter.items.map((item) => isGroup(item) ? pruneFilter(item) : isCompleteExpression(item) ? item : null).filter((item) => item !== null);
|
|
914
|
+
if (items.length === 0) return null;
|
|
915
|
+
return {
|
|
916
|
+
...filter,
|
|
917
|
+
items
|
|
918
|
+
};
|
|
919
|
+
}
|
|
920
|
+
/**
|
|
921
|
+
* True when an expression has everything it needs to be sent to the server.
|
|
922
|
+
*/
|
|
923
|
+
function isCompleteExpression(item) {
|
|
924
|
+
if (isGroup(item)) return item.items.some(isCompleteExpression);
|
|
925
|
+
if (!item.column || !item.operator) return false;
|
|
926
|
+
let arity = getOperatorArity(normalizeOperator(item.operator));
|
|
927
|
+
if (arity === "none") return true;
|
|
928
|
+
if (item.value === null || item.value === void 0 || item.value === "") return false;
|
|
929
|
+
if (arity === "pair") return Array.isArray(item.value) && item.value.length === 2 && item.value.every((v) => v !== "" && v != null);
|
|
930
|
+
if (arity === "list") return Array.isArray(item.value) && item.value.length > 0;
|
|
931
|
+
return true;
|
|
932
|
+
}
|
|
933
|
+
/**
|
|
934
|
+
* Creates an expression for a field, using the field's default operator and an
|
|
935
|
+
* empty value of the right shape.
|
|
936
|
+
*/
|
|
937
|
+
function createExpression(field, operator) {
|
|
938
|
+
let op = operator ?? getDefaultOperator(field);
|
|
939
|
+
return {
|
|
940
|
+
type: "expression",
|
|
941
|
+
column: field.name,
|
|
942
|
+
operator: op,
|
|
943
|
+
value: createEmptyValue(op),
|
|
944
|
+
valueType: getValueType(field, op)
|
|
945
|
+
};
|
|
946
|
+
}
|
|
947
|
+
function createEmptyValue(operator) {
|
|
948
|
+
switch (getOperatorArity(operator)) {
|
|
949
|
+
case "none": return null;
|
|
950
|
+
case "pair": return ["", ""];
|
|
951
|
+
case "list": return [];
|
|
952
|
+
default: return "";
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
/**
|
|
956
|
+
* Returns a copy of the expression with a new operator, adjusting the value to
|
|
957
|
+
* the new operator's arity and the value type accordingly.
|
|
958
|
+
*/
|
|
959
|
+
function changeOperator(expression, operator, field) {
|
|
960
|
+
let previousArity = getOperatorArity(normalizeOperator(expression.operator));
|
|
961
|
+
let arity = getOperatorArity(operator);
|
|
962
|
+
let value = expression.value;
|
|
963
|
+
if (arity !== previousArity) value = createEmptyValue(operator);
|
|
964
|
+
let valueType = expression.valueType === "special" && arity === previousArity ? "special" : getValueType(field, operator);
|
|
965
|
+
return {
|
|
966
|
+
...expression,
|
|
967
|
+
operator,
|
|
968
|
+
value,
|
|
969
|
+
valueType
|
|
970
|
+
};
|
|
971
|
+
}
|
|
972
|
+
/** Returns the node at `path`, or null when the path does not exist. */
|
|
973
|
+
function getNodeAtPath(filter, path) {
|
|
974
|
+
let node = filter;
|
|
975
|
+
for (let index of path) {
|
|
976
|
+
if (!node || !isGroup(node)) return null;
|
|
977
|
+
node = node.items[index];
|
|
978
|
+
}
|
|
979
|
+
return node ?? null;
|
|
980
|
+
}
|
|
981
|
+
/**
|
|
982
|
+
* Returns a copy of the tree with `updater` applied to the node at `path`.
|
|
983
|
+
* Returning null removes the node. An unknown path leaves the tree untouched.
|
|
984
|
+
*/
|
|
985
|
+
function updateNodeAtPath(filter, path, updater) {
|
|
986
|
+
if (path.length === 0) return updater(filter) ?? filter;
|
|
987
|
+
if (!isGroup(filter)) return filter;
|
|
988
|
+
let [index, ...rest] = path;
|
|
989
|
+
let child = filter.items[index];
|
|
990
|
+
if (!child) return filter;
|
|
991
|
+
let items = [...filter.items];
|
|
992
|
+
if (rest.length === 0) {
|
|
993
|
+
let updated = updater(child);
|
|
994
|
+
if (updated === null) items.splice(index, 1);
|
|
995
|
+
else items[index] = updated;
|
|
996
|
+
} else items[index] = updateNodeAtPath(child, rest, updater);
|
|
997
|
+
return {
|
|
998
|
+
...filter,
|
|
999
|
+
items
|
|
1000
|
+
};
|
|
1001
|
+
}
|
|
1002
|
+
/** Returns a copy of the tree with `node` appended to the group at `path`. */
|
|
1003
|
+
function insertNodeAtPath(filter, path, node) {
|
|
1004
|
+
return updateNodeAtPath(filter, path, (target) => isGroup(target) ? {
|
|
1005
|
+
...target,
|
|
1006
|
+
items: [...target.items, node]
|
|
1007
|
+
} : target);
|
|
1008
|
+
}
|
|
1009
|
+
/**
|
|
1010
|
+
* Returns a copy of the tree with the node at `path` removed. The root is never
|
|
1011
|
+
* removed; it is reset to the default layout instead.
|
|
1012
|
+
*/
|
|
1013
|
+
function removeNodeAtPath(filter, path) {
|
|
1014
|
+
if (path.length === 0) return createEmptyFilter();
|
|
1015
|
+
return updateNodeAtPath(filter, path, () => null);
|
|
1016
|
+
}
|
|
1017
|
+
/** Returns a copy of the tree with the mode of the group at `path` changed. */
|
|
1018
|
+
function setGroupMode(filter, path, mode) {
|
|
1019
|
+
return updateNodeAtPath(filter, path, (node) => isGroup(node) ? {
|
|
1020
|
+
...node,
|
|
1021
|
+
mode
|
|
1022
|
+
} : node);
|
|
1023
|
+
}
|
|
1024
|
+
/** Returns a copy of the tree with an empty group appended to the group at `path`. */
|
|
1025
|
+
function addGroupAtPath(filter, path = []) {
|
|
1026
|
+
let parent = getNodeAtPath(filter, path);
|
|
1027
|
+
return insertNodeAtPath(filter, path, createEmptyGroup(parent && isGroup(parent) && parent.mode === "and" ? "or" : "and"));
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
//#endregion
|
|
1031
|
+
//#region src/filter/convertFilter.ts
|
|
1032
|
+
const CONVERT_URL = "/api/filter/convert";
|
|
1033
|
+
/**
|
|
1034
|
+
* Converts between a structured filter and a DevExpress filter string.
|
|
1035
|
+
*
|
|
1036
|
+
* The conversion is done server-side by `api/filter/convert`, because the
|
|
1037
|
+
* criteria string grammar is implemented by the licensed DevExpress parser that
|
|
1038
|
+
* the Windows client and the report engine use. Doing it in the browser would
|
|
1039
|
+
* risk producing strings the server cannot parse back.
|
|
1040
|
+
*/
|
|
1041
|
+
async function convertFilter(request, signal) {
|
|
1042
|
+
let response = await fetch(CONVERT_URL, {
|
|
1043
|
+
method: "POST",
|
|
1044
|
+
headers: { "Content-Type": "application/json" },
|
|
1045
|
+
body: JSON.stringify(request),
|
|
1046
|
+
signal
|
|
1047
|
+
});
|
|
1048
|
+
if (!response.ok) throw new Error(`Failed to convert filter: ${response.status} ${response.statusText}`);
|
|
1049
|
+
let result = await response.json();
|
|
1050
|
+
return {
|
|
1051
|
+
...result,
|
|
1052
|
+
filterObject: result.filterObject ? toFilterTree(result.filterObject) : null
|
|
1053
|
+
};
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
//#endregion
|
|
1057
|
+
//#region src/filter/describeFilter.ts
|
|
1058
|
+
/**
|
|
1059
|
+
* Builds a human-readable description of a filter, without contacting the
|
|
1060
|
+
* server. Use this for read-only summaries; the authoritative filter string is
|
|
1061
|
+
* produced by `api/filter/convert`, since only the server can generate the
|
|
1062
|
+
* DevExpress criteria syntax.
|
|
1063
|
+
*/
|
|
1064
|
+
function describeFilter(filter, options = {}) {
|
|
1065
|
+
if (!filter) return "";
|
|
1066
|
+
return describe(filter, new Map((options.fields ?? []).map((field) => [field.name.toLowerCase(), field])), 0);
|
|
1067
|
+
}
|
|
1068
|
+
function describe(item, fields, depth) {
|
|
1069
|
+
if (isExpression(item)) return describeExpression(item, fields);
|
|
1070
|
+
let parts = item.items.map((child) => describe(child, fields, depth + 1)).filter((part) => part !== "");
|
|
1071
|
+
if (parts.length === 0) return "";
|
|
1072
|
+
if (parts.length === 1) return parts[0];
|
|
1073
|
+
let separator = item.mode === "or" ? " OR " : " AND ";
|
|
1074
|
+
let joined = parts.join(separator);
|
|
1075
|
+
return depth === 0 ? joined : `(${joined})`;
|
|
1076
|
+
}
|
|
1077
|
+
function describeExpression(expression, fields) {
|
|
1078
|
+
if (!expression.column || !expression.operator) return "";
|
|
1079
|
+
let field = fields.get(expression.column.toLowerCase());
|
|
1080
|
+
let caption = field?.caption ?? expression.column;
|
|
1081
|
+
let operator = normalizeOperator(expression.operator);
|
|
1082
|
+
let label = getOperatorLabel(operator, field?.type);
|
|
1083
|
+
let arity = getOperatorArity(operator);
|
|
1084
|
+
if (arity === "none") return `[${caption}] ${label}`;
|
|
1085
|
+
return `[${caption}] ${label} ${formatValue(expression, arity)}`;
|
|
1086
|
+
}
|
|
1087
|
+
function formatValue(expression, arity) {
|
|
1088
|
+
let { value, valueType } = expression;
|
|
1089
|
+
if (valueType === "special" && typeof value === "string") return getDynamicValueLabel(value);
|
|
1090
|
+
if (Array.isArray(value)) {
|
|
1091
|
+
let formatted = value.map((item) => formatScalar(item, valueType));
|
|
1092
|
+
return arity === "pair" ? formatted.join(" - ") : `(${formatted.join(", ")})`;
|
|
1093
|
+
}
|
|
1094
|
+
return formatScalar(value, valueType);
|
|
1095
|
+
}
|
|
1096
|
+
function formatScalar(value, valueType) {
|
|
1097
|
+
if (value === null || value === void 0) return "";
|
|
1098
|
+
if (value instanceof Date) return value.toISOString().slice(0, 10);
|
|
1099
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
1100
|
+
if (valueType === "number" || valueType === "boolean") return String(value);
|
|
1101
|
+
return `'${String(value)}'`;
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
//#endregion
|
|
1105
|
+
//#region src/filter/fields.ts
|
|
1106
|
+
const FIELD_TYPES = [
|
|
1107
|
+
"string",
|
|
1108
|
+
"number",
|
|
1109
|
+
"boolean",
|
|
1110
|
+
"date",
|
|
1111
|
+
"datetime",
|
|
1112
|
+
"uniqueidentifier"
|
|
1113
|
+
];
|
|
1114
|
+
/** The builder type for a normalized `DataType`. Unknown types are filtered as text. */
|
|
1115
|
+
function toFilterFieldType(dataType) {
|
|
1116
|
+
let normalized = dataType?.trim().toLowerCase();
|
|
1117
|
+
return FIELD_TYPES.find((type) => type === normalized) ?? "string";
|
|
1118
|
+
}
|
|
1119
|
+
function toBoolean$1(value) {
|
|
1120
|
+
return value === true || value === 1;
|
|
1121
|
+
}
|
|
1122
|
+
/** Maps a view field record onto the metadata the builder renders. */
|
|
1123
|
+
function toFieldMetadata(record) {
|
|
1124
|
+
return {
|
|
1125
|
+
name: record.FieldName ?? "",
|
|
1126
|
+
type: toFilterFieldType(record.DataType),
|
|
1127
|
+
nullable: toBoolean$1(record.Nullable),
|
|
1128
|
+
excludeFromFilter: toBoolean$1(record.ExcludeFromFilter),
|
|
1129
|
+
valueListRecordSource: record.ValueListRecordSource || void 0
|
|
1130
|
+
};
|
|
1131
|
+
}
|
|
1132
|
+
/**
|
|
1133
|
+
* Maps and sorts view field records. Fields the view marks as excluded are
|
|
1134
|
+
* dropped rather than hidden, since nothing else can use them.
|
|
1135
|
+
*/
|
|
1136
|
+
function toFieldMetadataList(records) {
|
|
1137
|
+
return records.map(toFieldMetadata).filter((field) => field.name !== "" && !field.excludeFromFilter).sort((a, b) => a.name.localeCompare(b.name));
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
//#endregion
|
|
1141
|
+
//#region src/filter/standardLayout.ts
|
|
1142
|
+
/**
|
|
1143
|
+
* The "standard layout" is the simplified shape the Windows filter editor can
|
|
1144
|
+
* render in its flow view: an outer `or` group whose items are `and` groups of
|
|
1145
|
+
* expressions. Anything else falls back to the advanced (DevExpress) editor
|
|
1146
|
+
* there, and to the nested tree editor on the web.
|
|
1147
|
+
*
|
|
1148
|
+
* @see `afFilterEditor.CanRenderFilter`
|
|
1149
|
+
*/
|
|
1150
|
+
function isStandardLayout(filter) {
|
|
1151
|
+
if (!filter || isExpression(filter) || filter.mode !== "or") return false;
|
|
1152
|
+
return filter.items.every((item) => isGroup(item) && item.mode === "and" && item.items.every(isExpression));
|
|
1153
|
+
}
|
|
1154
|
+
/**
|
|
1155
|
+
* Coerces an arbitrary filter tree into the standard layout.
|
|
1156
|
+
*
|
|
1157
|
+
* An `or` group's items each become their own `and` group. Anything else is
|
|
1158
|
+
* flattened into a single `and` group. This loses nesting, so only use it when
|
|
1159
|
+
* the consumer cannot handle a nested tree.
|
|
1160
|
+
*/
|
|
1161
|
+
function coerceToStandardLayout(filter) {
|
|
1162
|
+
if (!filter) return {
|
|
1163
|
+
type: "group",
|
|
1164
|
+
mode: "or",
|
|
1165
|
+
items: [{
|
|
1166
|
+
type: "group",
|
|
1167
|
+
mode: "and",
|
|
1168
|
+
items: []
|
|
1169
|
+
}]
|
|
1170
|
+
};
|
|
1171
|
+
if (isExpression(filter)) return {
|
|
1172
|
+
type: "group",
|
|
1173
|
+
mode: "or",
|
|
1174
|
+
items: [{
|
|
1175
|
+
type: "group",
|
|
1176
|
+
mode: "and",
|
|
1177
|
+
items: [filter]
|
|
1178
|
+
}]
|
|
1179
|
+
};
|
|
1180
|
+
if (filter.mode === "or") {
|
|
1181
|
+
let groups = filter.items.map((item) => {
|
|
1182
|
+
let expressions = [];
|
|
1183
|
+
collectExpressions(item, expressions);
|
|
1184
|
+
return {
|
|
1185
|
+
type: "group",
|
|
1186
|
+
mode: "and",
|
|
1187
|
+
items: expressions
|
|
1188
|
+
};
|
|
1189
|
+
});
|
|
1190
|
+
return {
|
|
1191
|
+
type: "group",
|
|
1192
|
+
mode: "or",
|
|
1193
|
+
items: groups.length > 0 ? groups : [{
|
|
1194
|
+
type: "group",
|
|
1195
|
+
mode: "and",
|
|
1196
|
+
items: []
|
|
1197
|
+
}]
|
|
1198
|
+
};
|
|
1199
|
+
}
|
|
1200
|
+
let expressions = [];
|
|
1201
|
+
collectExpressions(filter, expressions);
|
|
1202
|
+
return {
|
|
1203
|
+
type: "group",
|
|
1204
|
+
mode: "or",
|
|
1205
|
+
items: [{
|
|
1206
|
+
type: "group",
|
|
1207
|
+
mode: "and",
|
|
1208
|
+
items: expressions
|
|
1209
|
+
}]
|
|
1210
|
+
};
|
|
1211
|
+
}
|
|
1212
|
+
/**
|
|
1213
|
+
* Collects every expression in a (possibly nested) filter, in document order.
|
|
1214
|
+
*/
|
|
1215
|
+
function collectExpressions(item, into) {
|
|
1216
|
+
if (isExpression(item)) {
|
|
1217
|
+
into.push(item);
|
|
1218
|
+
return;
|
|
1219
|
+
}
|
|
1220
|
+
for (let child of item.items) collectExpressions(child, into);
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
//#endregion
|
|
1224
|
+
//#region src/filter/savedFilters.ts
|
|
1225
|
+
/**
|
|
1226
|
+
* `sviw_Database_MyFilters` unions in a synthetic "No Filter" row so the Windows
|
|
1227
|
+
* tree can render a "no filter" entry. It is not a real filter and must be
|
|
1228
|
+
* dropped before the list is shown.
|
|
1229
|
+
*/
|
|
1230
|
+
const NO_FILTER_ID = -1;
|
|
1231
|
+
/**
|
|
1232
|
+
* Maps a database record onto a `SavedFilter`.
|
|
1233
|
+
*
|
|
1234
|
+
* `CreatedByUser` needs care: the view computes it as the *string* `'true'` or
|
|
1235
|
+
* `'false'` (and `'False'` on the synthetic row), not as a bit.
|
|
1236
|
+
*/
|
|
1237
|
+
function toSavedFilter(record) {
|
|
1238
|
+
return {
|
|
1239
|
+
filterId: record.FilterID ?? -1,
|
|
1240
|
+
primKey: record.PrimKey ?? "",
|
|
1241
|
+
name: record.FilterName ?? "",
|
|
1242
|
+
criteria: record.FilterCriteria ?? "",
|
|
1243
|
+
dbObjectId: record.DBObjectID ?? "",
|
|
1244
|
+
accessLevel: toAccessLevel(record.AccessLevel),
|
|
1245
|
+
createdByUser: toBoolean(record.CreatedByUser),
|
|
1246
|
+
createdBy: record.CreatedBy ?? "",
|
|
1247
|
+
domain: record.Domain ?? null,
|
|
1248
|
+
crossDomain: toBoolean(record.CrossDomain),
|
|
1249
|
+
isPersonallyShared: toBoolean(record.IsPersonallyShared),
|
|
1250
|
+
hideCriteria: toBoolean(record.HideFilterCriteria),
|
|
1251
|
+
headerText: record.OptionalHeaderText || null,
|
|
1252
|
+
comment: record.OptionalComment || null,
|
|
1253
|
+
parameters: record.Parameters || null,
|
|
1254
|
+
reportTitle: record.ReportTitle || null,
|
|
1255
|
+
created: toDate(record.Created),
|
|
1256
|
+
updated: toDate(record.Updated)
|
|
1257
|
+
};
|
|
1258
|
+
}
|
|
1259
|
+
function toAccessLevel(value) {
|
|
1260
|
+
if (value === "Manager" || value === "Editor") return value;
|
|
1261
|
+
return "Reader";
|
|
1262
|
+
}
|
|
1263
|
+
function toBoolean(value) {
|
|
1264
|
+
if (typeof value === "string") return value.toLowerCase() === "true";
|
|
1265
|
+
return Boolean(value);
|
|
1266
|
+
}
|
|
1267
|
+
function toDate(value) {
|
|
1268
|
+
if (!value) return null;
|
|
1269
|
+
if (value instanceof Date) return value;
|
|
1270
|
+
let date = new Date(value);
|
|
1271
|
+
return Number.isNaN(date.getTime()) ? null : date;
|
|
1272
|
+
}
|
|
1273
|
+
/** Drops the synthetic "No Filter" row and sorts by name. */
|
|
1274
|
+
function toSavedFilters(records) {
|
|
1275
|
+
return records.filter((record) => (record.FilterID ?? -1) !== -1).map(toSavedFilter).sort((a, b) => a.name.localeCompare(b.name));
|
|
1276
|
+
}
|
|
1277
|
+
/**
|
|
1278
|
+
* Splits filters into the two branches the Windows tree shows: the ones the user
|
|
1279
|
+
* created, and the ones that have been shared with them.
|
|
1280
|
+
*/
|
|
1281
|
+
function partitionSavedFilters(filters) {
|
|
1282
|
+
let mine = [];
|
|
1283
|
+
let shared = [];
|
|
1284
|
+
for (let filter of filters) if (filter.createdByUser) mine.push(filter);
|
|
1285
|
+
else shared.push(filter);
|
|
1286
|
+
return {
|
|
1287
|
+
mine,
|
|
1288
|
+
shared
|
|
1289
|
+
};
|
|
1290
|
+
}
|
|
1291
|
+
function findSavedFilter(filters, filterId) {
|
|
1292
|
+
if (filterId == null) return void 0;
|
|
1293
|
+
return filters.find((filter) => filter.filterId === filterId);
|
|
1294
|
+
}
|
|
1295
|
+
/** True when the user may overwrite the filter's criteria. */
|
|
1296
|
+
function canEditSavedFilter(filter) {
|
|
1297
|
+
return filter.accessLevel === "Editor" || filter.accessLevel === "Manager";
|
|
1298
|
+
}
|
|
1299
|
+
/** True when the user may change who the filter is shared with. */
|
|
1300
|
+
function canShareSavedFilter(filter) {
|
|
1301
|
+
return filter.accessLevel === "Manager";
|
|
1302
|
+
}
|
|
1303
|
+
/**
|
|
1304
|
+
* True when the user may rename or delete the filter.
|
|
1305
|
+
*
|
|
1306
|
+
* `sstp_WebSiteCMS_FilterBuilder_RenameFilter` and `…_DeleteFilter` both require
|
|
1307
|
+
* `CreatedBy = SUSER_SNAME()` — the manager-access branch is commented out in
|
|
1308
|
+
* both procedures, deliberately, because the UI only ever offers these for a
|
|
1309
|
+
* user's own filters.
|
|
1310
|
+
*/
|
|
1311
|
+
function canRenameSavedFilter(filter) {
|
|
1312
|
+
return filter.createdByUser;
|
|
1313
|
+
}
|
|
1314
|
+
const canDeleteSavedFilter = canRenameSavedFilter;
|
|
1315
|
+
/**
|
|
1316
|
+
* True when the user may toggle the cross-domain flag.
|
|
1317
|
+
*
|
|
1318
|
+
* `sstp_Database_Filters_SetCrossDomain` also filters on
|
|
1319
|
+
* `CreatedBy = SUSER_SNAME()`.
|
|
1320
|
+
*/
|
|
1321
|
+
const canSetCrossDomain = canRenameSavedFilter;
|
|
1322
|
+
|
|
1323
|
+
//#endregion
|
|
1324
|
+
//#region src/getUserImage.ts
|
|
1325
|
+
const sizeMap = {
|
|
1326
|
+
xs: 64,
|
|
1327
|
+
sm: 128,
|
|
1328
|
+
md: 256,
|
|
1329
|
+
lg: 512,
|
|
1330
|
+
xl: void 0
|
|
499
1331
|
};
|
|
500
1332
|
function getUserImage(primKeyOrOptions, login, hash, size = "xs") {
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
return "";
|
|
506
|
-
}
|
|
507
|
-
let sizePx = sizeMap[size];
|
|
508
|
-
return `/api/data/blob/EmployeesLookup/${primKeyOrOptions}/Image/${encodeURIComponent(login ?? "")}${login ? "." : ""}${encodeURIComponent(hash)}.jpg${sizePx ? `?width=${sizePx}` : ""}`;
|
|
1333
|
+
if (primKeyOrOptions && typeof primKeyOrOptions === "object") return getUserImage(primKeyOrOptions.primKey, primKeyOrOptions.login, primKeyOrOptions.hash, primKeyOrOptions.size);
|
|
1334
|
+
if (!primKeyOrOptions || !hash) return "";
|
|
1335
|
+
let sizePx = sizeMap[size];
|
|
1336
|
+
return `/api/data/blob/EmployeesLookup/${primKeyOrOptions}/Image/${encodeURIComponent(login ?? "")}${login ? "." : ""}${encodeURIComponent(hash)}.jpg${sizePx ? `?width=${sizePx}` : ""}`;
|
|
509
1337
|
}
|
|
510
1338
|
|
|
511
|
-
|
|
1339
|
+
//#endregion
|
|
1340
|
+
//#region src/localize.ts
|
|
1341
|
+
/**
|
|
1342
|
+
* Localization function for tagged templates. Replaces the arguments
|
|
1343
|
+
* with $n, where n is the index, before asking Appframe to translate
|
|
1344
|
+
* the string.
|
|
1345
|
+
*
|
|
1346
|
+
* For example, if you call localize`Page ${2} or ${5}`, the string
|
|
1347
|
+
* "Page $0 of $1" will be sent to Appframe, and for norwegian,
|
|
1348
|
+
* "Side 2 av 5" will be returned.
|
|
1349
|
+
*
|
|
1350
|
+
* @param stringParts
|
|
1351
|
+
* @param args
|
|
1352
|
+
* @returns Translated string
|
|
1353
|
+
*/
|
|
512
1354
|
function localize(stringParts, ...args) {
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
1355
|
+
invariant(Array.isArray(stringParts) && stringParts.length === args.length + 1, "Method 'localize' must be used as a tag function");
|
|
1356
|
+
let stringToLocalize = stringParts.map((s, i) => i === stringParts.length - 1 ? s : `${s}$${i}`).join("");
|
|
1357
|
+
let localized = getLocalizedString(stringToLocalize);
|
|
1358
|
+
args.forEach((arg, index) => {
|
|
1359
|
+
localized = localized.replace(`$${index}`, arg);
|
|
1360
|
+
});
|
|
1361
|
+
return localized;
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1364
|
+
//#endregion
|
|
1365
|
+
//#region src/report/recordSource.ts
|
|
1366
|
+
const XML_ENTITIES = {
|
|
1367
|
+
amp: "&",
|
|
1368
|
+
lt: "<",
|
|
1369
|
+
gt: ">",
|
|
1370
|
+
quot: "\"",
|
|
1371
|
+
apos: "'"
|
|
1372
|
+
};
|
|
1373
|
+
/** Resolves the XML entities the .NET serializer emits. */
|
|
1374
|
+
function decodeXml(value) {
|
|
1375
|
+
return value.replace(/&(#x?[0-9a-f]+|[a-z]+);/gi, (match, entity) => {
|
|
1376
|
+
if (entity.startsWith("#x") || entity.startsWith("#X")) return String.fromCodePoint(Number.parseInt(entity.slice(2), 16));
|
|
1377
|
+
if (entity.startsWith("#")) return String.fromCodePoint(Number.parseInt(entity.slice(1), 10));
|
|
1378
|
+
return XML_ENTITIES[entity.toLowerCase()] ?? match;
|
|
1379
|
+
});
|
|
1380
|
+
}
|
|
1381
|
+
/** Escapes a value for use as XML text or as an attribute value. */
|
|
1382
|
+
function encodeXml(value) {
|
|
1383
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
1384
|
+
}
|
|
1385
|
+
/**
|
|
1386
|
+
* The text content of the first `<name>` element, or `null` when the element is
|
|
1387
|
+
* missing or self-closing (`<FilterString />`, which the serializer writes for
|
|
1388
|
+
* an empty value).
|
|
1389
|
+
*/
|
|
1390
|
+
function readElement(xml, name) {
|
|
1391
|
+
let match = new RegExp(`<${name}(?:\\s[^>]*)?>([\\s\\S]*?)</${name}>`).exec(xml);
|
|
1392
|
+
if (!match) return null;
|
|
1393
|
+
let value = decodeXml(match[1]).trim();
|
|
1394
|
+
return value === "" ? null : value;
|
|
1395
|
+
}
|
|
1396
|
+
/** The text content of every `<name>` element, in document order. */
|
|
1397
|
+
function readElements(xml, name) {
|
|
1398
|
+
return [...xml.matchAll(new RegExp(`<${name}(?:\\s[^>]*)?>([\\s\\S]*?)</${name}>`, "g"))].map((match) => decodeXml(match[1]).trim());
|
|
1399
|
+
}
|
|
1400
|
+
/**
|
|
1401
|
+
* The CLR types the designer writes on a select column. Anything else — and a
|
|
1402
|
+
* column with no `DataType` at all, which is what it writes for strings — is
|
|
1403
|
+
* treated as text.
|
|
1404
|
+
*/
|
|
1405
|
+
const COLUMN_TYPES = {
|
|
1406
|
+
Byte: "number",
|
|
1407
|
+
Decimal: "number",
|
|
1408
|
+
Double: "number",
|
|
1409
|
+
Int16: "number",
|
|
1410
|
+
Int32: "number",
|
|
1411
|
+
Int64: "number",
|
|
1412
|
+
Single: "number",
|
|
1413
|
+
Boolean: "boolean",
|
|
1414
|
+
DateTime: "date"
|
|
1415
|
+
};
|
|
1416
|
+
/** The columns of a `<SelectColumns>` block, in document order. */
|
|
1417
|
+
function readSelectColumns(xml) {
|
|
1418
|
+
let block = /<SelectColumns>([\s\S]*?)<\/SelectColumns>/.exec(xml)?.[1] ?? "";
|
|
1419
|
+
let columns = [];
|
|
1420
|
+
for (let match of block.matchAll(/<afSelectColumn>([\s\S]*?)<\/afSelectColumn>/g)) {
|
|
1421
|
+
let name = readElement(match[1], "Name");
|
|
1422
|
+
if (!name) continue;
|
|
1423
|
+
columns.push({
|
|
1424
|
+
name,
|
|
1425
|
+
type: COLUMN_TYPES[readElement(match[1], "DataType") ?? ""] ?? "string"
|
|
1426
|
+
});
|
|
1427
|
+
}
|
|
1428
|
+
return columns;
|
|
1429
|
+
}
|
|
1430
|
+
/**
|
|
1431
|
+
* Reads the parts of an `afRecordSource` a web client can act on: the view, the
|
|
1432
|
+
* selected columns and the design-time filter.
|
|
1433
|
+
*
|
|
1434
|
+
* Returns `null` for anything that is not a record source, so a missing or
|
|
1435
|
+
* empty `RecordSource` column simply means "no lookup".
|
|
1436
|
+
*/
|
|
1437
|
+
function parseRecordSource(xml) {
|
|
1438
|
+
if (!xml || !xml.includes("<afRecordSource")) return null;
|
|
1439
|
+
let viewName = readElement(xml, "ViewName");
|
|
1440
|
+
if (!viewName) return null;
|
|
1441
|
+
let maxRecords = readElement(xml, "MaxRecords");
|
|
1442
|
+
return {
|
|
1443
|
+
viewName,
|
|
1444
|
+
columns: readSelectColumns(xml),
|
|
1445
|
+
whereClause: readElement(xml, "WhereClause"),
|
|
1446
|
+
filterString: readElement(xml, "FilterString"),
|
|
1447
|
+
maxRecords: maxRecords === null || maxRecords === "0" ? null : Number(maxRecords)
|
|
1448
|
+
};
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
//#endregion
|
|
1452
|
+
//#region src/report/parameters.ts
|
|
1453
|
+
/**
|
|
1454
|
+
* Report parameters: mapping the metadata records onto something renderable,
|
|
1455
|
+
* and the XML the parameter values are stored as on a saved filter.
|
|
1456
|
+
*/
|
|
1457
|
+
const PARAMETER_TYPES = [
|
|
1458
|
+
"String",
|
|
1459
|
+
"Integer",
|
|
1460
|
+
"Double",
|
|
1461
|
+
"Date",
|
|
1462
|
+
"Boolean"
|
|
1463
|
+
];
|
|
1464
|
+
/**
|
|
1465
|
+
* A default value naming a stored procedure, e.g. `astp_Reports_CurrentPeriod`.
|
|
1466
|
+
*
|
|
1467
|
+
* The Windows launcher executes the procedure and uses the first column of the
|
|
1468
|
+
* first row. The web client cannot execute an arbitrary procedure, so these are
|
|
1469
|
+
* resolved through the report endpoint instead.
|
|
1470
|
+
*/
|
|
1471
|
+
const PROCEDURE_DEFAULT = /^[a-z]stp_/i;
|
|
1472
|
+
/** True when a `DefaultValue` names a stored procedure rather than a literal. */
|
|
1473
|
+
function isProcedureDefault(defaultValue) {
|
|
1474
|
+
return typeof defaultValue === "string" && PROCEDURE_DEFAULT.test(defaultValue.trim());
|
|
1475
|
+
}
|
|
1476
|
+
/** The choices of a parameter, one per line of its `Items` column. */
|
|
1477
|
+
function parseParameterItems(items) {
|
|
1478
|
+
if (!items) return [];
|
|
1479
|
+
return items.split(/\r\n|\r|\n/).map((item) => item.trim()).filter((item) => item !== "");
|
|
1480
|
+
}
|
|
1481
|
+
function toParameterType(dataType) {
|
|
1482
|
+
return PARAMETER_TYPES.find((type) => type.toLowerCase() === dataType?.trim().toLowerCase()) ?? "String";
|
|
1483
|
+
}
|
|
1484
|
+
/**
|
|
1485
|
+
* The editor a parameter should use. A lookup or a fixed item list wins over the
|
|
1486
|
+
* data type, which is what the Windows launcher does as well.
|
|
1487
|
+
*/
|
|
1488
|
+
function toParameterEditor(type, hasLookup, hasItems) {
|
|
1489
|
+
if (hasLookup) return "lookup";
|
|
1490
|
+
if (hasItems) return "select";
|
|
1491
|
+
switch (type) {
|
|
1492
|
+
case "Integer":
|
|
1493
|
+
case "Double": return "number";
|
|
1494
|
+
case "Date": return "date";
|
|
1495
|
+
case "Boolean": return "boolean";
|
|
1496
|
+
default: return "text";
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
/** Maps a `sviw_WinClient_ReportsParameters` record onto a `ReportParameter`. */
|
|
1500
|
+
function toReportParameter(record) {
|
|
1501
|
+
let name = record.Name ?? "";
|
|
1502
|
+
let type = toParameterType(record.DataType);
|
|
1503
|
+
let items = parseParameterItems(record.Items);
|
|
1504
|
+
let recordSource = parseRecordSource(record.RecordSource);
|
|
1505
|
+
let valueMember = record.ValueMember ?? null;
|
|
1506
|
+
if (recordSource && !valueMember) valueMember = recordSource.columns[0]?.name ?? null;
|
|
1507
|
+
let displayMember = recordSource?.columns.find((column) => column.name !== valueMember)?.name ?? null;
|
|
1508
|
+
let hasLookup = Boolean(recordSource && valueMember);
|
|
1509
|
+
return {
|
|
1510
|
+
name,
|
|
1511
|
+
label: record.Label?.trim() || name,
|
|
1512
|
+
description: record.Description?.trim() || null,
|
|
1513
|
+
type,
|
|
1514
|
+
editor: toParameterEditor(type, hasLookup, items.length > 0),
|
|
1515
|
+
defaultValue: record.DefaultValue ?? null,
|
|
1516
|
+
items,
|
|
1517
|
+
recordSource: hasLookup ? recordSource : null,
|
|
1518
|
+
valueMember: hasLookup ? valueMember : null,
|
|
1519
|
+
displayMember: hasLookup ? displayMember : null,
|
|
1520
|
+
sortOrder: record.SortOrder ?? 0,
|
|
1521
|
+
lookupWidth: record.LookupWidth ?? null,
|
|
1522
|
+
lookupHeight: record.LookupHeight ?? null
|
|
1523
|
+
};
|
|
1524
|
+
}
|
|
1525
|
+
/** Maps and sorts a report's parameter records. */
|
|
1526
|
+
function toReportParameters(records) {
|
|
1527
|
+
return records.filter((record) => record.Name).map(toReportParameter).sort((a, b) => a.sortOrder - b.sortOrder || a.label.localeCompare(b.label));
|
|
1528
|
+
}
|
|
1529
|
+
/** Converts a raw value to the type a parameter expects. */
|
|
1530
|
+
function coerceParameterValue(parameter, value) {
|
|
1531
|
+
if (value === null || value === void 0 || value === "") return parameter.type === "Boolean" ? false : null;
|
|
1532
|
+
switch (parameter.type) {
|
|
1533
|
+
case "Boolean": return typeof value === "boolean" ? value : /^(true|1|ja|yes)$/i.test(String(value));
|
|
1534
|
+
case "Integer": {
|
|
1535
|
+
let parsed = Number.parseInt(String(value), 10);
|
|
1536
|
+
return Number.isNaN(parsed) ? null : parsed;
|
|
1537
|
+
}
|
|
1538
|
+
case "Double": {
|
|
1539
|
+
let parsed = Number(String(value).replace(",", "."));
|
|
1540
|
+
return Number.isNaN(parsed) ? null : parsed;
|
|
1541
|
+
}
|
|
1542
|
+
default: return String(value);
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
/**
|
|
1546
|
+
* The initial values for a set of parameters.
|
|
1547
|
+
*
|
|
1548
|
+
* Defaults naming a stored procedure are left unset: they have to be resolved
|
|
1549
|
+
* server-side, and the caller fills them in once they are.
|
|
1550
|
+
*/
|
|
1551
|
+
function getDefaultParameterValues(parameters) {
|
|
1552
|
+
let values = {};
|
|
1553
|
+
for (let parameter of parameters) values[parameter.name] = isProcedureDefault(parameter.defaultValue) ? coerceParameterValue(parameter, null) : coerceParameterValue(parameter, parameter.defaultValue);
|
|
1554
|
+
return values;
|
|
1555
|
+
}
|
|
1556
|
+
function toXmlValue(parameter, value) {
|
|
1557
|
+
if (value === null || value === void 0) return "";
|
|
1558
|
+
if (typeof value === "boolean") return value ? "True" : "False";
|
|
1559
|
+
return String(value);
|
|
1560
|
+
}
|
|
1561
|
+
/**
|
|
1562
|
+
* Serializes parameter values the way the Windows filter builder does, so that
|
|
1563
|
+
* a filter saved on the web is readable in the Windows client and the other way
|
|
1564
|
+
* around.
|
|
1565
|
+
*
|
|
1566
|
+
* ```xml
|
|
1567
|
+
* <Parameters Version="1">
|
|
1568
|
+
* <Parameter Name="Period" Type="Integer">202601</Parameter>
|
|
1569
|
+
* </Parameters>
|
|
1570
|
+
* ```
|
|
1571
|
+
*/
|
|
1572
|
+
function serializeParameters(parameters, values) {
|
|
1573
|
+
return `<Parameters Version="1">\n${parameters.map((parameter) => {
|
|
1574
|
+
let value = toXmlValue(parameter, values[parameter.name] ?? null);
|
|
1575
|
+
return ` <Parameter Name="${encodeXml(parameter.name)}" Type="${parameter.type}">${encodeXml(value)}</Parameter>`;
|
|
1576
|
+
}).join("\n")}\n</Parameters>`;
|
|
1577
|
+
}
|
|
1578
|
+
/**
|
|
1579
|
+
* Reads the values back out of the XML stored on a saved filter. Values are
|
|
1580
|
+
* returned as strings and coerced against the report's own parameters by
|
|
1581
|
+
* `restoreParameterValues`, since the XML may name parameters the report no
|
|
1582
|
+
* longer has.
|
|
1583
|
+
*/
|
|
1584
|
+
function parseParameters(xml) {
|
|
1585
|
+
if (!xml) return {};
|
|
1586
|
+
let values = {};
|
|
1587
|
+
for (let match of xml.matchAll(/<Parameter\s+([^>]*?)\/?>(?:([\s\S]*?)<\/Parameter>)?/g)) {
|
|
1588
|
+
let name = /Name\s*=\s*"([^"]*)"/.exec(match[1])?.[1];
|
|
1589
|
+
if (!name) continue;
|
|
1590
|
+
values[name] = readElement(match[0], "Parameter") ?? "";
|
|
1591
|
+
}
|
|
1592
|
+
return values;
|
|
1593
|
+
}
|
|
1594
|
+
/**
|
|
1595
|
+
* Applies the values stored on a saved filter to a report's parameters, keeping
|
|
1596
|
+
* the defaults for anything the filter does not mention.
|
|
1597
|
+
*/
|
|
1598
|
+
function restoreParameterValues(parameters, xml) {
|
|
1599
|
+
let stored = parseParameters(xml);
|
|
1600
|
+
let values = getDefaultParameterValues(parameters);
|
|
1601
|
+
for (let parameter of parameters) if (parameter.name in stored) values[parameter.name] = coerceParameterValue(parameter, stored[parameter.name]);
|
|
1602
|
+
return values;
|
|
1603
|
+
}
|
|
1604
|
+
|
|
1605
|
+
//#endregion
|
|
1606
|
+
//#region src/report/parameterDefaults.ts
|
|
1607
|
+
/**
|
|
1608
|
+
* Resolving report parameter defaults that name a stored procedure.
|
|
1609
|
+
*
|
|
1610
|
+
* A `DefaultValue` matching `^[a-z]stp_` names a procedure that produces the
|
|
1611
|
+
* default rather than being the default itself. Executing it is a server
|
|
1612
|
+
* concern, and the framework already exposes it through the `WebsiteCMS`
|
|
1613
|
+
* `api/reporting/filters` route.
|
|
1614
|
+
*/
|
|
1615
|
+
/**
|
|
1616
|
+
* Runs the stored procedure a parameter's default names and returns the value
|
|
1617
|
+
* it produces — the first column of the first row, the way the Windows launcher
|
|
1618
|
+
* reads it.
|
|
1619
|
+
*
|
|
1620
|
+
* Returns `null` when the parameter has no procedure default or the procedure
|
|
1621
|
+
* produced no rows.
|
|
1622
|
+
*/
|
|
1623
|
+
async function fetchParameterDefault(reportId, parameter, signal) {
|
|
1624
|
+
if (!parameter.defaultValue || !isProcedureDefault(parameter.defaultValue)) return null;
|
|
1625
|
+
let response = await fetch(`/api/reporting/filters/${encodeURIComponent(reportId)}/GetParameterDefaultValue`, {
|
|
1626
|
+
body: JSON.stringify({ parameterName: parameter.name }),
|
|
1627
|
+
credentials: "same-origin",
|
|
1628
|
+
headers: {
|
|
1629
|
+
"Content-Type": "application/json",
|
|
1630
|
+
"X-Requested-With": "XMLHttpRequest"
|
|
1631
|
+
},
|
|
1632
|
+
method: "POST",
|
|
1633
|
+
signal
|
|
1634
|
+
});
|
|
1635
|
+
if (!response.ok) throw new Error(`${response.status} ${response.statusText}`);
|
|
1636
|
+
return firstValue(await response.json());
|
|
1637
|
+
}
|
|
1638
|
+
/**
|
|
1639
|
+
* Resolves every procedure default of a report in parallel and merges the
|
|
1640
|
+
* results over the literal defaults.
|
|
1641
|
+
*
|
|
1642
|
+
* A parameter whose procedure fails is left at its literal default rather than
|
|
1643
|
+
* failing the whole form — the user can still type a value.
|
|
1644
|
+
*/
|
|
1645
|
+
async function resolveParameterDefaults(reportId, parameters, literalDefaults, signal) {
|
|
1646
|
+
let dynamic = parameters.filter((parameter) => isProcedureDefault(parameter.defaultValue ?? ""));
|
|
1647
|
+
if (dynamic.length === 0) return literalDefaults;
|
|
1648
|
+
let resolved = await Promise.all(dynamic.map(async (parameter) => {
|
|
1649
|
+
try {
|
|
1650
|
+
return [parameter.name, await fetchParameterDefault(reportId, parameter, signal)];
|
|
1651
|
+
} catch {
|
|
1652
|
+
return [parameter.name, null];
|
|
1653
|
+
}
|
|
1654
|
+
}));
|
|
1655
|
+
let values = { ...literalDefaults };
|
|
1656
|
+
for (let [name, value] of resolved) if (value !== null) values[name] = value;
|
|
1657
|
+
return values;
|
|
1658
|
+
}
|
|
1659
|
+
/** The first column of the first row of whichever result set came back. */
|
|
1660
|
+
function firstValue(result) {
|
|
1661
|
+
let row = (Array.isArray(result) ? result : Object.values(result).find((value) => Array.isArray(value)))?.[0];
|
|
1662
|
+
if (row === void 0 || row === null) return null;
|
|
1663
|
+
if (Array.isArray(row)) return row[0] ?? null;
|
|
1664
|
+
if (typeof row === "object") return Object.values(row)[0] ?? null;
|
|
1665
|
+
return row;
|
|
1666
|
+
}
|
|
1667
|
+
|
|
1668
|
+
//#endregion
|
|
1669
|
+
//#region src/report/render.ts
|
|
1670
|
+
/**
|
|
1671
|
+
* Rendering a report through the framework's report route.
|
|
1672
|
+
*
|
|
1673
|
+
* The route is a CMS code module owned by the Appframe framework
|
|
1674
|
+
* (`WebsiteCMS.Reports`), registered on
|
|
1675
|
+
* `api/report/{ReportID}/{FileName}.{Extension}`. Everything here builds
|
|
1676
|
+
* requests for it — there is no report endpoint of our own.
|
|
1677
|
+
*/
|
|
1678
|
+
/**
|
|
1679
|
+
* The file extension each format is requested with. The route only accepts
|
|
1680
|
+
* `(pdf|csv|xlsx?|html|xbm|mht|rtf|txt)`, which is why `TEXT` is `txt` and
|
|
1681
|
+
* `IMAGE` is `xbm` rather than the format name.
|
|
1682
|
+
*/
|
|
1683
|
+
const REPORT_EXTENSIONS = {
|
|
1684
|
+
CSV: "csv",
|
|
1685
|
+
HTML: "html",
|
|
1686
|
+
IMAGE: "xbm",
|
|
1687
|
+
MHT: "mht",
|
|
1688
|
+
PDF: "pdf",
|
|
1689
|
+
RTF: "rtf",
|
|
1690
|
+
TEXT: "txt",
|
|
1691
|
+
XLS: "xls",
|
|
1692
|
+
XLSX: "xlsx"
|
|
1693
|
+
};
|
|
1694
|
+
/** The media type each format is served as, for previewing a rendered report. */
|
|
1695
|
+
const REPORT_MEDIA_TYPES = {
|
|
1696
|
+
CSV: "text/csv",
|
|
1697
|
+
HTML: "text/html",
|
|
1698
|
+
IMAGE: "image/x-xbitmap",
|
|
1699
|
+
MHT: "message/rfc822",
|
|
1700
|
+
PDF: "application/pdf",
|
|
1701
|
+
RTF: "text/enriched",
|
|
1702
|
+
TEXT: "text/plain",
|
|
1703
|
+
XLS: "application/vnd.ms-excel",
|
|
1704
|
+
XLSX: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
1705
|
+
};
|
|
1706
|
+
/** The extension a format is requested with. */
|
|
1707
|
+
function getReportExtension(format) {
|
|
1708
|
+
return REPORT_EXTENSIONS[format];
|
|
1709
|
+
}
|
|
1710
|
+
/** The media type a format is served as. */
|
|
1711
|
+
function getReportMediaType(format) {
|
|
1712
|
+
return REPORT_MEDIA_TYPES[format];
|
|
1713
|
+
}
|
|
1714
|
+
/**
|
|
1715
|
+
* Characters that must not reach the file name, because it is a path segment of
|
|
1716
|
+
* the route: separators, the extension delimiter, the query and fragment
|
|
1717
|
+
* delimiters, and control characters.
|
|
1718
|
+
*/
|
|
1719
|
+
const UNSAFE_FILE_NAME_CHARACTERS = /[\\/:*?"<>|.#&+%\u0000-\u001f]/g;
|
|
1720
|
+
/**
|
|
1721
|
+
* The name a rendered report is downloaded as, including its extension.
|
|
1722
|
+
*
|
|
1723
|
+
* The file name is part of the route rather than a query parameter, so anything
|
|
1724
|
+
* that would change the path is replaced.
|
|
1725
|
+
*/
|
|
1726
|
+
function getReportFileName(options) {
|
|
1727
|
+
let safe = (options.fileName ?? options.reportId).trim().replace(UNSAFE_FILE_NAME_CHARACTERS, "-").replace(/-{2,}/g, "-").replace(/^-|-$/g, "").trim();
|
|
1728
|
+
return `${safe === "" ? options.reportId : safe}.${getReportExtension(options.format)}`;
|
|
1729
|
+
}
|
|
1730
|
+
/**
|
|
1731
|
+
* The route is always on the same origin, but a `Request` needs an absolute
|
|
1732
|
+
* URL, so the path is resolved against the document when there is one.
|
|
1733
|
+
*/
|
|
1734
|
+
function toRequestUrl(path) {
|
|
1735
|
+
let base = window.location?.href;
|
|
1736
|
+
return base ? new URL(path, base).toString() : path;
|
|
1737
|
+
}
|
|
1738
|
+
/**
|
|
1739
|
+
* The request that renders a report.
|
|
1740
|
+
*
|
|
1741
|
+
* A report without parameters is a plain `GET`, so it can be opened directly in
|
|
1742
|
+
* a new tab. As soon as there are parameters or a header the options go in a
|
|
1743
|
+
* JSON body, which the route only reads from a `POST`.
|
|
1744
|
+
*
|
|
1745
|
+
* `filter` is always sent alongside `parameters`: the route reads the filter out
|
|
1746
|
+
* of the body *guarded by the presence of `parameters`*, and reads it with an
|
|
1747
|
+
* indexer that throws when the key is missing.
|
|
1748
|
+
*/
|
|
1749
|
+
function getReportRequest(options) {
|
|
1750
|
+
let { domain, filter, headerText, hideFilterString, parameters, reportId, signal, timezone } = options;
|
|
1751
|
+
let fileName = getReportFileName(options);
|
|
1752
|
+
let url = `/api/report/${encodeURIComponent(reportId)}/${encodeURIComponent(fileName)}`;
|
|
1753
|
+
let query = new URLSearchParams();
|
|
1754
|
+
let entries = toParameterEntries(parameters);
|
|
1755
|
+
let needsBody = entries.length > 0 || Boolean(headerText) || hideFilterString === true;
|
|
1756
|
+
if (domain) query.set("Domain", domain);
|
|
1757
|
+
query.set("timezone", String(timezone ?? -(/* @__PURE__ */ new Date()).getTimezoneOffset() / 60));
|
|
1758
|
+
if (!needsBody) {
|
|
1759
|
+
if (filter) query.set("Filter", filter);
|
|
1760
|
+
return new Request(toRequestUrl(`${url}?${query}`), {
|
|
1761
|
+
credentials: "same-origin",
|
|
1762
|
+
signal
|
|
1763
|
+
});
|
|
1764
|
+
}
|
|
1765
|
+
let body = {
|
|
1766
|
+
filter: filter ?? "",
|
|
1767
|
+
parameters: entries
|
|
1768
|
+
};
|
|
1769
|
+
if (headerText) body.optionalheader = headerText;
|
|
1770
|
+
if (hideFilterString === true) body.showfiltercriteria = "true";
|
|
1771
|
+
return new Request(toRequestUrl(`${url}?${query}`), {
|
|
1772
|
+
body: JSON.stringify(body),
|
|
1773
|
+
credentials: "same-origin",
|
|
1774
|
+
headers: {
|
|
1775
|
+
"Content-Type": "application/json",
|
|
1776
|
+
"X-Requested-With": "XMLHttpRequest"
|
|
1777
|
+
},
|
|
1778
|
+
method: "POST",
|
|
1779
|
+
signal
|
|
1780
|
+
});
|
|
1781
|
+
}
|
|
1782
|
+
/**
|
|
1783
|
+
* Renders a report and resolves with the rendered file.
|
|
1784
|
+
*
|
|
1785
|
+
* The route answers with a JSON body carrying an `error` when rendering fails,
|
|
1786
|
+
* rather than with a failing status, so a JSON response is turned into an error
|
|
1787
|
+
* here.
|
|
1788
|
+
*/
|
|
1789
|
+
async function renderReport(options) {
|
|
1790
|
+
let response = await fetch(getReportRequest(options));
|
|
1791
|
+
if ((response.headers.get("content-type") ?? "").includes("application/json")) {
|
|
1792
|
+
let data = await response.json();
|
|
1793
|
+
throw new Error(data.error ?? "Failed to render report");
|
|
1794
|
+
}
|
|
1795
|
+
if (!response.ok) throw new Error(`${response.status} ${response.statusText}`);
|
|
1796
|
+
return response.blob();
|
|
1797
|
+
}
|
|
1798
|
+
/** Renders a report and saves it, reporting progress as it downloads. */
|
|
1799
|
+
function downloadReport(options) {
|
|
1800
|
+
return downloadRequest(getReportRequest(options), {
|
|
1801
|
+
fileName: getReportFileName(options),
|
|
1802
|
+
onProgress: options.onProgress,
|
|
1803
|
+
signal: options.signal
|
|
1804
|
+
});
|
|
1805
|
+
}
|
|
1806
|
+
/**
|
|
1807
|
+
* Renders a report and opens it in a new tab.
|
|
1808
|
+
*
|
|
1809
|
+
* The blob is rendered first rather than pointing a tab at the route, so a
|
|
1810
|
+
* failure surfaces as an error here instead of as a tab showing JSON, and so
|
|
1811
|
+
* reports with parameters — which have to be posted — can be previewed too.
|
|
1812
|
+
*/
|
|
1813
|
+
async function openReport(options) {
|
|
1814
|
+
let blob = await renderReport(options);
|
|
1815
|
+
let url = URL.createObjectURL(blob.type ? blob : new Blob([blob], { type: getReportMediaType(options.format) }));
|
|
1816
|
+
if (!window.open(url, "_blank", "noopener")) {
|
|
1817
|
+
URL.revokeObjectURL(url);
|
|
1818
|
+
throw new Error("The report could not be opened. Allow pop-ups for this site and try again.");
|
|
1819
|
+
}
|
|
1820
|
+
window.setTimeout(() => URL.revokeObjectURL(url), 6e4);
|
|
1821
|
+
}
|
|
1822
|
+
/**
|
|
1823
|
+
* Renders a report and opens the browser's print dialog for it.
|
|
1824
|
+
*
|
|
1825
|
+
* The report is loaded into an offscreen iframe rather than a new tab, so the
|
|
1826
|
+
* print dialog opens over the launcher and the user is not left with a stray
|
|
1827
|
+
* tab afterwards. Browsers that will not print an embedded document fall back
|
|
1828
|
+
* to opening it, which leaves the user one keystroke from printing.
|
|
1829
|
+
*/
|
|
1830
|
+
async function printReport(options) {
|
|
1831
|
+
let document_ = window.document;
|
|
1832
|
+
if (!document_?.body) return openReport(options);
|
|
1833
|
+
let blob = await renderReport(options);
|
|
1834
|
+
let url = URL.createObjectURL(blob.type ? blob : new Blob([blob], { type: getReportMediaType(options.format) }));
|
|
1835
|
+
let frame = document_.createElement("iframe");
|
|
1836
|
+
frame.setAttribute("aria-hidden", "true");
|
|
1837
|
+
frame.style.cssText = "position:fixed;inset-block-start:0;inset-inline-start:0;inline-size:1px;block-size:1px;border:0;visibility:hidden";
|
|
1838
|
+
let cleanUp = () => {
|
|
1839
|
+
frame.remove();
|
|
1840
|
+
URL.revokeObjectURL(url);
|
|
1841
|
+
};
|
|
1842
|
+
try {
|
|
1843
|
+
await new Promise((resolve, reject) => {
|
|
1844
|
+
frame.addEventListener("load", () => resolve(), { once: true });
|
|
1845
|
+
frame.addEventListener("error", () => reject(/* @__PURE__ */ new Error("The report could not be loaded for printing")), { once: true });
|
|
1846
|
+
frame.src = url;
|
|
1847
|
+
document_.body.append(frame);
|
|
1848
|
+
});
|
|
1849
|
+
frame.contentWindow?.focus();
|
|
1850
|
+
frame.contentWindow?.print();
|
|
1851
|
+
} catch {
|
|
1852
|
+
cleanUp();
|
|
1853
|
+
return openReport(options);
|
|
1854
|
+
}
|
|
1855
|
+
window.setTimeout(cleanUp, 6e4);
|
|
1856
|
+
}
|
|
1857
|
+
/**
|
|
1858
|
+
* Parameters in the shape the route expects — `{ name, value }` objects.
|
|
1859
|
+
*
|
|
1860
|
+
* Empty values are dropped: the route skips them anyway, and a report falls back
|
|
1861
|
+
* to its own default when a parameter is not sent.
|
|
1862
|
+
*/
|
|
1863
|
+
function toParameterEntries(parameters) {
|
|
1864
|
+
if (!parameters) return [];
|
|
1865
|
+
return Object.entries(parameters).filter(([, value]) => value !== null && value !== void 0 && value !== "").map(([name, value]) => ({
|
|
1866
|
+
name,
|
|
1867
|
+
value: typeof value === "boolean" ? String(value) : value
|
|
1868
|
+
}));
|
|
1869
|
+
}
|
|
1870
|
+
|
|
1871
|
+
//#endregion
|
|
1872
|
+
//#region src/report/report.ts
|
|
1873
|
+
/**
|
|
1874
|
+
* Mapping a `sviw_WinClient_MyReports` record onto the report model, including
|
|
1875
|
+
* which output formats the report offers.
|
|
1876
|
+
*/
|
|
1877
|
+
function isSet(value) {
|
|
1878
|
+
return value === true || value === 1;
|
|
1879
|
+
}
|
|
1880
|
+
/**
|
|
1881
|
+
* The formats a report may be rendered to, in the order the launcher offers
|
|
1882
|
+
* them.
|
|
1883
|
+
*
|
|
1884
|
+
* `FormatCSV` covers the whole spreadsheet family: the Windows launcher enables
|
|
1885
|
+
* "save as XLS" and "save as XLSX" from the same flag, because they all come out
|
|
1886
|
+
* of the same DevExpress export.
|
|
1887
|
+
*/
|
|
1888
|
+
function getReportFormats(record) {
|
|
1889
|
+
let formats = [];
|
|
1890
|
+
if (isSet(record.FormatPDF)) formats.push("PDF");
|
|
1891
|
+
if (isSet(record.FormatCSV)) formats.push("XLSX", "XLS", "CSV");
|
|
1892
|
+
if (isSet(record.FormatHTML)) formats.push("HTML");
|
|
1893
|
+
if (isSet(record.FormatRTF)) formats.push("RTF");
|
|
1894
|
+
if (isSet(record.FormatMHT)) formats.push("MHT");
|
|
1895
|
+
if (isSet(record.FormatTEXT)) formats.push("TEXT");
|
|
1896
|
+
if (isSet(record.FormatIMAGE)) formats.push("IMAGE");
|
|
1897
|
+
return formats;
|
|
1898
|
+
}
|
|
1899
|
+
/** Maps a report record onto the model the launcher renders. */
|
|
1900
|
+
function toReport(record) {
|
|
1901
|
+
let recordSource = parseRecordSource(record.RecordSource);
|
|
1902
|
+
return {
|
|
1903
|
+
reportId: record.ReportID ?? "",
|
|
1904
|
+
shortId: record.ShortID ?? null,
|
|
1905
|
+
title: record.Title?.trim() || null,
|
|
1906
|
+
description: record.Description?.trim() || null,
|
|
1907
|
+
viewName: recordSource?.viewName ?? null,
|
|
1908
|
+
initialFilter: recordSource?.filterString ?? null,
|
|
1909
|
+
hasTitle: isSet(record.HasTitle),
|
|
1910
|
+
hasHeader: isSet(record.HasHeader),
|
|
1911
|
+
formats: getReportFormats(record),
|
|
1912
|
+
timeout: record.Timeout ?? null
|
|
1913
|
+
};
|
|
1914
|
+
}
|
|
1915
|
+
|
|
1916
|
+
//#endregion
|
|
1917
|
+
//#region src/alert.ts
|
|
1918
|
+
const template = `
|
|
527
1919
|
<dialog class="alert-dialog">
|
|
528
1920
|
<form method="dialog">
|
|
529
1921
|
<article></article>
|
|
@@ -533,160 +1925,130 @@ var template = `
|
|
|
533
1925
|
</form>
|
|
534
1926
|
</dialog>
|
|
535
1927
|
`;
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
1928
|
+
function addDialogTemplate() {
|
|
1929
|
+
if (document.querySelector("#AlertDialogTemplate")) return;
|
|
1930
|
+
let $template = document.createElement("template");
|
|
1931
|
+
$template.id = "AlertDialogTemplate";
|
|
1932
|
+
$template.innerHTML = template;
|
|
1933
|
+
document.body.append($template);
|
|
1934
|
+
}
|
|
1935
|
+
if (window.document) {
|
|
1936
|
+
if (document.body) addDialogTemplate();
|
|
1937
|
+
else document.addEventListener("DOMContentLoaded", addDialogTemplate, { once: true });
|
|
541
1938
|
}
|
|
542
1939
|
function waitForDialog(dialog, cancelValue = "No") {
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
resolve(cancelValue);
|
|
557
|
-
controller.abort();
|
|
558
|
-
},
|
|
559
|
-
{ signal: controller.signal }
|
|
560
|
-
);
|
|
561
|
-
});
|
|
562
|
-
}
|
|
563
|
-
var alertDialogPromiseMap = /* @__PURE__ */ new Map();
|
|
1940
|
+
return new Promise((resolve) => {
|
|
1941
|
+
let controller = new AbortController();
|
|
1942
|
+
dialog.addEventListener("close", () => {
|
|
1943
|
+
resolve(dialog.returnValue);
|
|
1944
|
+
controller.abort();
|
|
1945
|
+
}, { signal: controller.signal });
|
|
1946
|
+
dialog.addEventListener("cancel", () => {
|
|
1947
|
+
resolve(cancelValue);
|
|
1948
|
+
controller.abort();
|
|
1949
|
+
}, { signal: controller.signal });
|
|
1950
|
+
});
|
|
1951
|
+
}
|
|
1952
|
+
const alertDialogPromiseMap = /* @__PURE__ */ new Map();
|
|
564
1953
|
async function openAlertDialog({ buttons, content, className, id, title }) {
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
return returnValue;
|
|
604
|
-
});
|
|
605
|
-
if (id) {
|
|
606
|
-
alertDialogPromiseMap.set(id, dialogPromise);
|
|
607
|
-
}
|
|
608
|
-
return await dialogPromise;
|
|
609
|
-
}
|
|
610
|
-
var ConfirmButtons = /* @__PURE__ */ ((ConfirmButtons2) => {
|
|
611
|
-
ConfirmButtons2[ConfirmButtons2["YesNo"] = 0] = "YesNo";
|
|
612
|
-
ConfirmButtons2[ConfirmButtons2["OkCancel"] = 1] = "OkCancel";
|
|
613
|
-
return ConfirmButtons2;
|
|
614
|
-
})(ConfirmButtons || {});
|
|
1954
|
+
if (id && alertDialogPromiseMap.has(id)) return await alertDialogPromiseMap.get(id);
|
|
1955
|
+
let $template = document.querySelector("#AlertDialogTemplate");
|
|
1956
|
+
if (!$template) throw Error("No alert dialog template found");
|
|
1957
|
+
let $dialog = $template.content.firstElementChild?.cloneNode(true);
|
|
1958
|
+
let $content = $dialog?.querySelector("article");
|
|
1959
|
+
let $menu = $dialog?.querySelector("footer menu");
|
|
1960
|
+
if (!$dialog || !$content || !$menu) throw Error("Invalid alert dialog template");
|
|
1961
|
+
if (className) for (const className_ of className.split(/\s+/)) $dialog?.classList.add(className_);
|
|
1962
|
+
if (title) {
|
|
1963
|
+
let $header = document.createElement("header");
|
|
1964
|
+
$header.append(title);
|
|
1965
|
+
$content.parentNode?.insertBefore($header, $content);
|
|
1966
|
+
}
|
|
1967
|
+
$content.appendChild(typeof content === "string" ? document.createTextNode(content) : content);
|
|
1968
|
+
for (let { label, value, primary } of buttons) {
|
|
1969
|
+
let listItem = document.createElement("li");
|
|
1970
|
+
let buttonElem = document.createElement("button");
|
|
1971
|
+
buttonElem.value = value;
|
|
1972
|
+
if (primary) buttonElem.classList.add("primary");
|
|
1973
|
+
buttonElem.textContent = label;
|
|
1974
|
+
listItem.appendChild(buttonElem);
|
|
1975
|
+
$menu.appendChild(buttonElem);
|
|
1976
|
+
}
|
|
1977
|
+
document.body.appendChild($dialog);
|
|
1978
|
+
$dialog.showModal();
|
|
1979
|
+
let dialogPromise = waitForDialog($dialog).then((returnValue) => {
|
|
1980
|
+
if (id && alertDialogPromiseMap.has(id)) alertDialogPromiseMap.delete(id);
|
|
1981
|
+
if ($dialog) document.body.removeChild($dialog);
|
|
1982
|
+
return returnValue;
|
|
1983
|
+
});
|
|
1984
|
+
if (id) alertDialogPromiseMap.set(id, dialogPromise);
|
|
1985
|
+
return await dialogPromise;
|
|
1986
|
+
}
|
|
1987
|
+
let ConfirmButtons = /* @__PURE__ */ function(ConfirmButtons) {
|
|
1988
|
+
ConfirmButtons[ConfirmButtons["YesNo"] = 0] = "YesNo";
|
|
1989
|
+
ConfirmButtons[ConfirmButtons["OkCancel"] = 1] = "OkCancel";
|
|
1990
|
+
return ConfirmButtons;
|
|
1991
|
+
}({});
|
|
615
1992
|
async function confirm(prompt, options = {}) {
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
1993
|
+
let { buttons: optionButtons = 0, title } = options;
|
|
1994
|
+
return await openAlertDialog({
|
|
1995
|
+
content: prompt,
|
|
1996
|
+
buttons: optionButtons === 1 ? [{
|
|
1997
|
+
label: getLocalizedString("OK"),
|
|
1998
|
+
value: "yes",
|
|
1999
|
+
primary: true
|
|
2000
|
+
}, {
|
|
2001
|
+
label: getLocalizedString("Cancel"),
|
|
2002
|
+
value: "no"
|
|
2003
|
+
}] : [{
|
|
2004
|
+
label: getLocalizedString("Yes"),
|
|
2005
|
+
value: "yes",
|
|
2006
|
+
primary: true
|
|
2007
|
+
}, {
|
|
2008
|
+
label: getLocalizedString("No"),
|
|
2009
|
+
value: "no"
|
|
2010
|
+
}],
|
|
2011
|
+
className: "alert-dialog-confirm",
|
|
2012
|
+
title
|
|
2013
|
+
}) === "yes";
|
|
626
2014
|
}
|
|
627
2015
|
async function alert(prompt, options = {}) {
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
UserSession,
|
|
668
|
-
alert,
|
|
669
|
-
confirm,
|
|
670
|
-
downloadBlob,
|
|
671
|
-
downloadRequest,
|
|
672
|
-
expose,
|
|
673
|
-
getLocalizedString,
|
|
674
|
-
getSessionData,
|
|
675
|
-
getSettings,
|
|
676
|
-
getUserImage,
|
|
677
|
-
invariant,
|
|
678
|
-
localStorage_default as localStorage,
|
|
679
|
-
localize,
|
|
680
|
-
logActivity,
|
|
681
|
-
logError,
|
|
682
|
-
mitt_default as mitt,
|
|
683
|
-
openAlertDialog,
|
|
684
|
-
saveSettings,
|
|
685
|
-
startActivity,
|
|
686
|
-
time,
|
|
687
|
-
toLocalDateTime,
|
|
688
|
-
toLocalISODate,
|
|
689
|
-
toLocalISODateTime,
|
|
690
|
-
version,
|
|
691
|
-
waitForDialog
|
|
692
|
-
};
|
|
2016
|
+
await openAlertDialog({
|
|
2017
|
+
content: prompt,
|
|
2018
|
+
className: "alert-dialog-alert",
|
|
2019
|
+
buttons: [{
|
|
2020
|
+
label: getLocalizedString("OK"),
|
|
2021
|
+
value: "ok",
|
|
2022
|
+
primary: true
|
|
2023
|
+
}],
|
|
2024
|
+
...options
|
|
2025
|
+
});
|
|
2026
|
+
}
|
|
2027
|
+
|
|
2028
|
+
//#endregion
|
|
2029
|
+
//#region src/settings.ts
|
|
2030
|
+
function getKey(article) {
|
|
2031
|
+
return `${article ?? window.af?.article?.id ?? window.window?.location.pathname.substring(1).split("/")[0]}.${window.af?.userSession.login ?? "anonymous"}`;
|
|
2032
|
+
}
|
|
2033
|
+
function getSettings(setting, article) {
|
|
2034
|
+
if (!window.af?.userSession) return null;
|
|
2035
|
+
const key = getKey(article);
|
|
2036
|
+
const settings = JSON.parse(localStorage.getItem(key) || "{}");
|
|
2037
|
+
if (setting) return settings[setting] || null;
|
|
2038
|
+
return settings;
|
|
2039
|
+
}
|
|
2040
|
+
function saveSettings(setting, value, article) {
|
|
2041
|
+
if (!window.af?.userSession) return;
|
|
2042
|
+
const key = getKey(article);
|
|
2043
|
+
let userSettings = JSON.parse(localStorage.getItem(key) || "{}");
|
|
2044
|
+
if (typeof value !== "undefined") userSettings[setting] = value;
|
|
2045
|
+
else userSettings = setting;
|
|
2046
|
+
localStorage.setItem(key, JSON.stringify(userSettings));
|
|
2047
|
+
}
|
|
2048
|
+
|
|
2049
|
+
//#endregion
|
|
2050
|
+
//#region src/index.ts
|
|
2051
|
+
const version = "2.12.0";
|
|
2052
|
+
|
|
2053
|
+
//#endregion
|
|
2054
|
+
export { ConfirmButtons, NO_FILTER_ID, RELATIVE_DATE_VALUES, SessionHandler, UserSession, addGroupAtPath, alert, canDeleteSavedFilter, canEditSavedFilter, canRenameSavedFilter, canSetCrossDomain, canShareSavedFilter, changeOperator, coerceParameterValue, coerceToStandardLayout, confirm, convertFilter, countExpressions, createEmptyFilter, createEmptyGroup, createExpression, decodeXml, describeFilter, downloadBlob, downloadReport, downloadRequest, encodeXml, expose, fetchParameterDefault, findSavedFilter, getDefaultOperator, getDefaultParameterValues, getDynamicValueLabel, getDynamicValuesForField, getLocalizedString, getNodeAtPath, getOperatorArity, getOperatorLabel, getOperatorsForField, getReportExtension, getReportFileName, getReportFormats, getReportMediaType, getReportRequest, getSessionData, getSettings, getUserImage, getValueType, insertNodeAtPath, invariant, isCompleteExpression, isEmptyFilter, isExpression, isGroup, isProcedureDefault, isStandardLayout, localStorage_default as localStorage, localize, logActivity, logError, mitt_default as mitt, normalizeOperator, openAlertDialog, openReport, parseParameterItems, parseParameters, parseRecordSource, partitionSavedFilters, printReport, pruneFilter, readElement, readElements, removeNodeAtPath, renderReport, resolveParameterDefaults, restoreParameterValues, saveSettings, serializeParameters, setGroupMode, startActivity, time, toFieldMetadata, toFieldMetadataList, toFilterFieldType, toFilterTree, toLocalDateTime, toLocalISODate, toLocalISODateTime, toReport, toReportParameter, toReportParameters, toSavedFilter, toSavedFilters, updateNodeAtPath, version, waitForDialog };
|