@olenbetong/appframe-core 2.0.4 → 2.1.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.
@@ -1,10 +1,3 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => {
4
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- return value;
6
- };
7
-
8
1
  // src/errorHandler.ts
9
2
  window.addEventListener("error", (event) => {
10
3
  try {
@@ -46,181 +39,42 @@ function mitt_default(n) {
46
39
  } };
47
40
  }
48
41
 
49
- // src/has.ts
50
- function has(obj, key) {
51
- return Object.prototype.hasOwnProperty.call(obj, key);
52
- }
53
-
54
- // src/getLocalizedString.ts
55
- async function fetchLocalizedString(text) {
56
- try {
57
- let response = await fetch(`/api/user/localize/new/${af.article.id}`, {
58
- body: JSON.stringify({ text }),
59
- headers: {
60
- Accept: "application/json; */*",
61
- "Content-Type": "application/json",
62
- "X-Requested-With": "XMLHttpRequest"
63
- },
64
- method: "POST"
65
- });
66
- if (response.ok) {
67
- return await response.json();
68
- }
69
- } catch (error) {
70
- }
71
- return text;
72
- }
73
- function getLocalizedString(string, wait) {
74
- if (typeof string === "string" && string.trim().length === 0) {
75
- return string;
76
- }
77
- if (string === void 0 || string === "" || string === null) {
78
- return;
79
- }
80
- if (af?.article?.i18n !== void 0) {
81
- if (!has(af.article.i18n, string)) {
82
- af.article.i18n[string] = string;
83
- let promise = fetchLocalizedString(string).then((text) => {
84
- af.article.i18n[string] = text;
85
- return text;
86
- });
87
- if (wait) {
88
- return promise;
89
- }
90
- }
91
- return af.article.i18n[string];
92
- } else {
93
- return string;
94
- }
95
- }
96
-
97
42
  // src/SessionHandler.ts
98
- var _SessionHandler = class {
43
+ var SessionHandler = class {
99
44
  eventHandler = mitt_default();
100
- showingDialog = false;
101
- dialog = null;
102
- expired(callback) {
103
- af.userSession = af.userSession || {
104
- login: null
105
- };
106
- if (typeof callback !== "function") {
107
- throw new TypeError("Parameter pCallback passed to the expired function must be a function!");
108
- }
109
- const once = () => {
110
- this.detachEvent("onAuthenticated", once);
111
- callback();
112
- };
113
- this.attachEvent("onAuthenticated", once);
114
- if (this.showingDialog !== true) {
115
- this.showingDialog = true;
116
- const contentContainer = document.createElement("div");
117
- contentContainer.className = "session-expired-container";
118
- contentContainer.innerHTML = `
119
- <div class="session-expired-log-in" style="margin-top: 20px;">
120
- <div class="row">
121
- <div class="sessionExpired">
122
- <h4 class="message">Frakoblet - tidsavbrudd</h4>
123
- <div class="form-group">
124
- <label for="username">${getLocalizedString("Username")}</label>
125
- <input type="text" class="form-control" id="username" autofocus>
126
- </div>
127
- <div class="form-group">
128
- <label for="password">${getLocalizedString("Password")}</label>
129
- <input type="password" autocomplete="password" class="form-control" id="password" >
130
- </div>
131
- <div class="form-group">
132
- <div>
133
- <button type="submit" class="btn btn-default sign-in">
134
- ${getLocalizedString("Log in")}
135
- </button>
136
- </div>
137
- </div>
138
- </div>
139
- </div>
140
- <div class="row">
141
- <div class="" style="display:none; margin: 0px 30px;">
142
- <div class="alert alert-warning" role="alert" style="margin-top:10px;"></div>
143
- </div>
144
- </div>
145
- </div>
146
- `;
147
- const message = af.userSession.login ? _SessionHandler.messages.relog : _SessionHandler.messages.login;
148
- const messageElem = contentContainer.querySelector(".message");
149
- const statusElem = contentContainer.querySelector(".alert-warning");
150
- const userElem = contentContainer.querySelector("input[type=text]");
151
- const signInElem = contentContainer.querySelector(".sign-in");
152
- const passwordElem = contentContainer.querySelector("input[type=password]");
153
- if (messageElem !== null && userElem !== null && statusElem !== null && userElem !== null && signInElem !== null && passwordElem !== null) {
154
- messageElem.innerText = getLocalizedString(message);
155
- userElem.value = af.userSession.login || "";
156
- userElem.disabled = !!af.userSession.login;
157
- userElem.readOnly = !!af.userSession.login;
158
- document.body.appendChild(contentContainer);
159
- const doLogin = async () => {
160
- passwordElem.disabled = true;
161
- const request = new Request("/login", {
162
- body: JSON.stringify({
163
- username: userElem.value,
164
- password: passwordElem.value,
165
- remember: false
166
- }),
167
- headers: {
168
- Accept: "application/json",
169
- "Content-Type": "application/json; charset=utf-8",
170
- "X-Requested-With": "XMLHttpRequest"
171
- },
172
- method: "POST"
173
- });
174
- try {
175
- const response = await fetch(request);
176
- const json = await response.json();
177
- if (json.success) {
178
- document.body.removeChild(contentContainer);
179
- this.showingDialog = false;
180
- this.eventHandler.emit("onAuthenticated");
181
- if (statusElem.parentNode) {
182
- statusElem.parentNode.style.display = "none";
183
- }
184
- } else {
185
- throw new Error(json.error);
186
- }
187
- } catch (err) {
188
- if (err && typeof err === "object") {
189
- let error = err;
190
- passwordElem.disabled = false;
191
- passwordElem.classList.toggle("invalid", true);
192
- passwordElem.value = "";
193
- passwordElem.focus();
194
- statusElem.innerText = error.message ? error.message : String(error);
195
- if (statusElem.parentNode) {
196
- statusElem.parentNode.style.display = "initial";
197
- }
198
- }
199
- }
200
- };
201
- signInElem.addEventListener("click", doLogin);
202
- passwordElem.addEventListener("keyup", (evt) => {
203
- if (evt.key === "Enter") {
204
- doLogin();
205
- }
206
- });
207
- passwordElem.addEventListener("change", (evt) => {
208
- if (evt.key === "Enter") {
209
- doLogin();
210
- }
211
- });
212
- userElem.addEventListener("keyup", (evt) => {
213
- if (evt.key === "Enter") {
214
- doLogin();
215
- }
216
- });
217
- userElem.addEventListener("change", (evt) => {
218
- if (evt.key === "Enter") {
219
- doLogin();
220
- }
221
- });
222
- passwordElem.focus();
45
+ authenticated() {
46
+ this.eventHandler.emit("onAuthenticated");
47
+ }
48
+ async expired(callback, type) {
49
+ let authPromise = new Promise((resolve) => {
50
+ let eventHandler = () => {
51
+ this.eventHandler.off("onAuthenticated", eventHandler);
52
+ callback?.();
53
+ resolve();
54
+ };
55
+ this.eventHandler.on("onAuthenticated", eventHandler);
56
+ });
57
+ let dialog = document.querySelector("dialog.session-expired-dialog");
58
+ if (!dialog) {
59
+ dialog = document.createElement("dialog");
60
+ dialog.className = "session-expired-dialog";
61
+ dialog.style.display = "flex";
62
+ dialog.style.padding = "0";
63
+ dialog.style.left = "50%";
64
+ dialog.style.transform = "translateX(-50%)";
65
+ let link = "/login?ReturnUrl=%2fapi/sessionexpired/remove";
66
+ if (type !== "RequireTwoFactor") {
67
+ await fetch("/logout", { method: "POST" });
68
+ } else {
69
+ link = "/login?RequireTwoFactor=1&ReturnUrl=%2fapi/sessionexpired/remove";
223
70
  }
71
+ 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>`;
72
+ document.body.append(dialog);
73
+ dialog.showModal();
74
+ await authPromise;
75
+ dialog.remove();
76
+ } else {
77
+ await authPromise;
224
78
  }
225
79
  }
226
80
  attachEvent(eventName, handler) {
@@ -230,11 +84,6 @@ var _SessionHandler = class {
230
84
  return this.eventHandler.off(eventName, handler);
231
85
  }
232
86
  };
233
- var SessionHandler = _SessionHandler;
234
- __publicField(SessionHandler, "messages", {
235
- login: "Log in",
236
- relog: "Session expired - log in"
237
- });
238
87
 
239
88
  // src/UserSession.ts
240
89
  var UserSession = class extends SessionHandler {
@@ -257,7 +106,6 @@ var UserSession = class extends SessionHandler {
257
106
  shortDateFormat = "dd.MM.yyyy";
258
107
  culture = "en-GB";
259
108
  uiculture = "en-GB";
260
- expired = () => null;
261
109
  constructor(userData) {
262
110
  super();
263
111
  this.formats = {};
@@ -328,7 +176,6 @@ async function downloadRequest(request, { fileName, signal, onProgress }) {
328
176
  });
329
177
  response = new Response(stream);
330
178
  } catch (error) {
331
- console.warn("Failed to replace response stream to log download progress.");
332
179
  }
333
180
  const blob = await response.blob();
334
181
  reportProgress({ bytesReceived, status: "downloaded" });
@@ -343,6 +190,11 @@ async function downloadRequest(request, { fileName, signal, onProgress }) {
343
190
  }
344
191
  }
345
192
 
193
+ // src/has.ts
194
+ function has(obj, key) {
195
+ return Object.prototype.hasOwnProperty.call(obj, key);
196
+ }
197
+
346
198
  // src/parseFunc.ts
347
199
  var 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)*)\}$/;
348
200
  function parseFunc(func) {
@@ -371,19 +223,10 @@ function requireNamespace(root, namespace) {
371
223
  current = current[namespace[i]] = current[namespace[i]] || new Namespace_default();
372
224
  }
373
225
  return current;
374
- } else {
375
- throw new TypeError("Invalid type of parameter 'namespace'");
376
226
  }
377
- } else {
378
- throw new TypeError("Invalid type of parameter 'root'");
379
227
  }
380
228
  }
381
229
 
382
- // src/warn.ts
383
- function warn(message, fallbackToAlert) {
384
- console.warn(message);
385
- }
386
-
387
230
  // src/expose.ts
388
231
  function expose(name, value) {
389
232
  if (arguments.length !== 2) {
@@ -419,13 +262,69 @@ function expose(name, value) {
419
262
  throw new TypeError("Invalid typeof parameter 'name', or unable to parse pValue!");
420
263
  }
421
264
  if (has(namespace, keyName)) {
422
- warn([name, keyName].join(".") + " is already defined!");
265
+ console.warn([name, keyName].join(".") + " is already defined!");
423
266
  } else {
424
267
  namespace[keyName] = value;
425
268
  }
426
269
  }
427
270
  }
428
271
 
272
+ // src/getLocalizedString.ts
273
+ async function fetchLocalizedString(text) {
274
+ try {
275
+ let response = await fetch(`/api/user/localize/new/${af.article.id}`, {
276
+ body: JSON.stringify({ text }),
277
+ headers: {
278
+ Accept: "application/json; */*",
279
+ "Content-Type": "application/json",
280
+ "X-Requested-With": "XMLHttpRequest"
281
+ },
282
+ method: "POST"
283
+ });
284
+ if (response.ok) {
285
+ return await response.json();
286
+ }
287
+ } catch (error) {
288
+ }
289
+ return text;
290
+ }
291
+ function getLocalizedString(string, wait) {
292
+ if (typeof string === "string" && string.trim().length === 0) {
293
+ return string;
294
+ }
295
+ if (string === void 0 || string === "" || string === null) {
296
+ return;
297
+ }
298
+ if (af?.article?.i18n !== void 0) {
299
+ if (!has(af.article.i18n, string)) {
300
+ af.article.i18n[string] = string;
301
+ let promise = fetchLocalizedString(string).then((text) => {
302
+ af.article.i18n[string] = text;
303
+ return text;
304
+ });
305
+ if (wait) {
306
+ return promise;
307
+ }
308
+ }
309
+ return af.article.i18n[string];
310
+ } else {
311
+ return string;
312
+ }
313
+ }
314
+
315
+ // src/localize.ts
316
+ function localize(stringParts, ...args) {
317
+ if (!Array.isArray(stringParts) || stringParts?.length || stringParts.length !== args.length + 1) {
318
+ throw TypeError("Method 'localize' must be used as a tag function");
319
+ }
320
+ let stringToLocalize = stringParts.map((s, i) => i === stringParts.length - 1 ? s : `${s}$${i}`).join("");
321
+ let localized = getLocalizedString(stringToLocalize);
322
+ args.forEach((arg, index) => {
323
+ localized = localized.replace("$" + index, arg);
324
+ });
325
+ return localized;
326
+ }
327
+
429
328
  // src/localStorage.ts
430
329
  function getLocalKey(key) {
431
330
  return [af?.article?.id || "", "common", key].join(".");
@@ -497,7 +396,7 @@ function saveSettings(setting, value, article) {
497
396
  }
498
397
 
499
398
  // src/index.ts
500
- var version = "2.0.4";
399
+ var version = "2.1.0";
501
400
  export {
502
401
  SessionHandler,
503
402
  UserSession,
@@ -508,6 +407,7 @@ export {
508
407
  getSettings,
509
408
  has,
510
409
  localStorage_default as localStorage,
410
+ localize,
511
411
  mitt_default as mitt,
512
412
  saveSettings,
513
413
  version