@olenbetong/appframe-core 1.0.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 +433 -0
- package/dist/esm/af.common.min.js +48 -0
- package/dist/esm/af.common.min.js.map +7 -0
- package/dist/iife/af.common.js +458 -0
- package/dist/iife/af.common.min.js +48 -0
- package/dist/iife/af.common.min.js.map +7 -0
- package/es/Namespace.d.ts +5 -0
- package/es/Namespace.js +3 -0
- package/es/SessionHandler.d.ts +15 -0
- package/es/SessionHandler.js +150 -0
- package/es/UserSession.d.ts +10 -0
- package/es/UserSession.js +13 -0
- package/es/expose.d.ts +6 -0
- package/es/expose.js +54 -0
- package/es/getLocalizedString.d.ts +8 -0
- package/es/getLocalizedString.js +114 -0
- package/es/has.d.ts +1 -0
- package/es/has.js +3 -0
- package/es/index.d.ts +7 -0
- package/es/index.js +7 -0
- package/es/localStorage.d.ts +10 -0
- package/es/localStorage.js +38 -0
- package/es/parseFunc.d.ts +5 -0
- package/es/parseFunc.js +8 -0
- package/es/requireNamespace.d.ts +1 -0
- package/es/requireNamespace.js +23 -0
- package/es/settings.d.ts +2 -0
- package/es/settings.js +27 -0
- package/es/warn.d.ts +1 -0
- package/es/warn.js +12 -0
- package/package.json +38 -0
- package/scripts/import-json.js +5 -0
- package/scripts/replace-version.js +37 -0
- package/src/Namespace.ts +4 -0
- package/src/SessionHandler.ts +175 -0
- package/src/UserSession.ts +16 -0
- package/src/expose.ts +68 -0
- package/src/getLocalizedString.ts +153 -0
- package/src/global.d.ts +56 -0
- package/src/has.ts +6 -0
- package/src/index.ts +7 -0
- package/src/localStorage.ts +49 -0
- package/src/parseFunc.ts +9 -0
- package/src/requireNamespace.ts +26 -0
- package/src/settings.ts +36 -0
- package/src/warn.ts +10 -0
- package/tsconfig.json +26 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import mitt from "mitt";
|
|
2
|
+
import { getLocalizedString } from "./getLocalizedString.js";
|
|
3
|
+
export default class SessionHandler {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.eventHandler = mitt();
|
|
6
|
+
this.showingDialog = false;
|
|
7
|
+
this.dialog = null;
|
|
8
|
+
}
|
|
9
|
+
expired(callback) {
|
|
10
|
+
af.userSession = af.userSession || {
|
|
11
|
+
login: null
|
|
12
|
+
};
|
|
13
|
+
if (typeof callback !== "function") {
|
|
14
|
+
throw new TypeError("Parameter pCallback passed to the expired function must be a function!");
|
|
15
|
+
}
|
|
16
|
+
const once = () => {
|
|
17
|
+
this.detachEvent("onAuthenticated", once);
|
|
18
|
+
callback();
|
|
19
|
+
};
|
|
20
|
+
this.attachEvent("onAuthenticated", once);
|
|
21
|
+
if (this.showingDialog !== true) {
|
|
22
|
+
this.showingDialog = true;
|
|
23
|
+
const contentContainer = document.createElement("div");
|
|
24
|
+
contentContainer.className = "session-expired-container";
|
|
25
|
+
contentContainer.innerHTML = `
|
|
26
|
+
<div class="session-expired-log-in" style="margin-top: 20px;">
|
|
27
|
+
<div class="row">
|
|
28
|
+
<div class="sessionExpired">
|
|
29
|
+
<h4 class="message">Frakoblet - tidsavbrudd</h4>
|
|
30
|
+
<div class="form-group">
|
|
31
|
+
<label for="username">${getLocalizedString("Username")}</label>
|
|
32
|
+
<input type="text" class="form-control" id="username" autofocus>
|
|
33
|
+
</div>
|
|
34
|
+
<div class="form-group">
|
|
35
|
+
<label for="password">${getLocalizedString("Password")}</label>
|
|
36
|
+
<input type="password" autocomplete="password" class="form-control" id="password" >
|
|
37
|
+
</div>
|
|
38
|
+
<div class="form-group">
|
|
39
|
+
<div>
|
|
40
|
+
<button type="submit" class="btn btn-default sign-in">
|
|
41
|
+
${getLocalizedString("Log in")}
|
|
42
|
+
</button>
|
|
43
|
+
</div>
|
|
44
|
+
</div>
|
|
45
|
+
</div>
|
|
46
|
+
</div>
|
|
47
|
+
<div class="row">
|
|
48
|
+
<div class="" style="display:none; margin: 0px 30px;">
|
|
49
|
+
<div class="alert alert-warning" role="alert" style="margin-top:10px;"></div>
|
|
50
|
+
</div>
|
|
51
|
+
</div>
|
|
52
|
+
</div>
|
|
53
|
+
`;
|
|
54
|
+
const message = af.userSession.login ? SessionHandler.messages.relog : SessionHandler.messages.login;
|
|
55
|
+
const messageElem = contentContainer.querySelector(".message");
|
|
56
|
+
const statusElem = contentContainer.querySelector(".alert-warning");
|
|
57
|
+
const userElem = contentContainer.querySelector("input[type=text]");
|
|
58
|
+
const signInElem = contentContainer.querySelector(".sign-in");
|
|
59
|
+
const passwordElem = contentContainer.querySelector("input[type=password]");
|
|
60
|
+
if (messageElem !== null &&
|
|
61
|
+
userElem !== null &&
|
|
62
|
+
statusElem !== null &&
|
|
63
|
+
userElem !== null &&
|
|
64
|
+
signInElem !== null &&
|
|
65
|
+
passwordElem !== null) {
|
|
66
|
+
messageElem.innerText = getLocalizedString(message);
|
|
67
|
+
userElem.value = af.userSession.login || "";
|
|
68
|
+
userElem.disabled = !!af.userSession.login;
|
|
69
|
+
userElem.readOnly = !!af.userSession.login;
|
|
70
|
+
document.body.appendChild(contentContainer);
|
|
71
|
+
const doLogin = async () => {
|
|
72
|
+
passwordElem.disabled = true;
|
|
73
|
+
const request = new Request("/login", {
|
|
74
|
+
body: JSON.stringify({
|
|
75
|
+
username: userElem.value,
|
|
76
|
+
password: passwordElem.value,
|
|
77
|
+
remember: false
|
|
78
|
+
}),
|
|
79
|
+
headers: {
|
|
80
|
+
Accept: "application/json",
|
|
81
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
82
|
+
"X-Requested-With": "XMLHttpRequest"
|
|
83
|
+
},
|
|
84
|
+
method: "POST"
|
|
85
|
+
});
|
|
86
|
+
try {
|
|
87
|
+
const response = await fetch(request);
|
|
88
|
+
const json = await response.json();
|
|
89
|
+
if (json.success) {
|
|
90
|
+
document.body.removeChild(contentContainer);
|
|
91
|
+
this.showingDialog = false;
|
|
92
|
+
this.eventHandler.emit("onAuthenticated");
|
|
93
|
+
if (statusElem.parentNode) {
|
|
94
|
+
statusElem.parentNode.style.display = "none";
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
throw new Error(json.error);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
if (err && typeof err === "object") {
|
|
103
|
+
let error = err;
|
|
104
|
+
passwordElem.disabled = false;
|
|
105
|
+
passwordElem.classList.toggle("invalid", true);
|
|
106
|
+
passwordElem.value = "";
|
|
107
|
+
passwordElem.focus();
|
|
108
|
+
statusElem.innerText = error.message ? error.message : String(error);
|
|
109
|
+
if (statusElem.parentNode) {
|
|
110
|
+
statusElem.parentNode.style.display = "initial";
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
signInElem.addEventListener("click", doLogin);
|
|
116
|
+
passwordElem.addEventListener("keyup", evt => {
|
|
117
|
+
if (evt.key === "Enter") {
|
|
118
|
+
doLogin();
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
passwordElem.addEventListener("change", evt => {
|
|
122
|
+
if (evt.key === "Enter") {
|
|
123
|
+
doLogin();
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
userElem.addEventListener("keyup", evt => {
|
|
127
|
+
if (evt.key === "Enter") {
|
|
128
|
+
doLogin();
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
userElem.addEventListener("change", evt => {
|
|
132
|
+
if (evt.key === "Enter") {
|
|
133
|
+
doLogin();
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
passwordElem.focus();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
attachEvent(eventName, handler) {
|
|
141
|
+
return this.eventHandler.on(eventName, handler);
|
|
142
|
+
}
|
|
143
|
+
detachEvent(eventName, handler) {
|
|
144
|
+
return this.eventHandler.off(eventName, handler);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
SessionHandler.messages = {
|
|
148
|
+
login: "Log in",
|
|
149
|
+
relog: "Session expired - log in"
|
|
150
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import SessionHandler from "./SessionHandler.js";
|
|
2
|
+
export default class UserSession extends SessionHandler {
|
|
3
|
+
decimalSeparator: string;
|
|
4
|
+
formats: any;
|
|
5
|
+
groupSeparator: string | null;
|
|
6
|
+
shortDateFormat: string;
|
|
7
|
+
login: string | null;
|
|
8
|
+
isDeveloper: boolean;
|
|
9
|
+
constructor(userData: any);
|
|
10
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import SessionHandler from "./SessionHandler.js";
|
|
2
|
+
export default class UserSession extends SessionHandler {
|
|
3
|
+
constructor(userData) {
|
|
4
|
+
super();
|
|
5
|
+
this.decimalSeparator = ".";
|
|
6
|
+
this.groupSeparator = null;
|
|
7
|
+
this.shortDateFormat = "dd.MM.yyyy";
|
|
8
|
+
this.login = null;
|
|
9
|
+
this.isDeveloper = false;
|
|
10
|
+
this.formats = {};
|
|
11
|
+
Object.assign(this, userData);
|
|
12
|
+
}
|
|
13
|
+
}
|
package/es/expose.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expose a value to a potentially deeply nested global variable
|
|
3
|
+
* @param name {string | object} Either a dot-separated path to the global variable, or an object to expose the value on
|
|
4
|
+
* @param value {object | Function | Function[]} The value(s) to expose
|
|
5
|
+
*/
|
|
6
|
+
export default function expose(name: object | string, value: object | Function | Function[]): void;
|
package/es/expose.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import has from "./has.js";
|
|
2
|
+
import parseFunc from "./parseFunc.js";
|
|
3
|
+
import requireNamespace from "./requireNamespace.js";
|
|
4
|
+
import warn from "./warn.js";
|
|
5
|
+
/**
|
|
6
|
+
* Expose a value to a potentially deeply nested global variable
|
|
7
|
+
* @param name {string | object} Either a dot-separated path to the global variable, or an object to expose the value on
|
|
8
|
+
* @param value {object | Function | Function[]} The value(s) to expose
|
|
9
|
+
*/
|
|
10
|
+
export default function expose(name, value) {
|
|
11
|
+
if (arguments.length !== 2) {
|
|
12
|
+
throw new Error("Incorrect number of arguments passed to the expose function!");
|
|
13
|
+
}
|
|
14
|
+
if (Array.isArray(value)) {
|
|
15
|
+
for (let func of value) {
|
|
16
|
+
const { name: funcName } = parseFunc(func);
|
|
17
|
+
if (funcName) {
|
|
18
|
+
expose(name, func); // Expose func on namespace found in pName
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
throw new Error("Unable to detect name of function! Name it, or only supply one function and include name in first parameter.");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
let keyName;
|
|
27
|
+
let namespace;
|
|
28
|
+
if (typeof value === "function") {
|
|
29
|
+
keyName = parseFunc(value).name;
|
|
30
|
+
}
|
|
31
|
+
if (typeof name === "string") {
|
|
32
|
+
let pathParts = name.split(".");
|
|
33
|
+
if (!keyName) {
|
|
34
|
+
keyName = pathParts[pathParts.length - 1];
|
|
35
|
+
pathParts = pathParts.slice(0, pathParts.length - 1);
|
|
36
|
+
}
|
|
37
|
+
namespace = requireNamespace(window, pathParts);
|
|
38
|
+
}
|
|
39
|
+
else if (keyName &&
|
|
40
|
+
(typeof name === "object" || typeof name === "function")) {
|
|
41
|
+
namespace = name;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
throw new TypeError("Invalid typeof parameter 'name', or unable to parse pValue!");
|
|
45
|
+
}
|
|
46
|
+
if (has(namespace, keyName)) {
|
|
47
|
+
// If class is already defined, warn or show an alert
|
|
48
|
+
warn([name, keyName].join(".") + " is already defined!");
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
namespace[keyName] = value;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
interface Window {
|
|
3
|
+
__UNLOCALIZED_STRINGS__: Set<string>;
|
|
4
|
+
__doNothingWithStrings?: () => Record<string, string>;
|
|
5
|
+
__getUnlocalizedStrings?: (forCustomRoute?: boolean) => string;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export declare function getLocalizedString(string: string): string;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
async function getLocalizedStringAsync(string) {
|
|
2
|
+
try {
|
|
3
|
+
let response = await fetch("/api/user/localize", {
|
|
4
|
+
body: JSON.stringify({ string }),
|
|
5
|
+
headers: {
|
|
6
|
+
Accept: "application/json; */*",
|
|
7
|
+
"Content-Type": "application/json",
|
|
8
|
+
"X-Requested-With": "XMLHttpRequest",
|
|
9
|
+
},
|
|
10
|
+
method: "POST",
|
|
11
|
+
});
|
|
12
|
+
if (response.ok) {
|
|
13
|
+
return (await response.json());
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
catch (error) { }
|
|
17
|
+
return string;
|
|
18
|
+
}
|
|
19
|
+
function afGetLocalizedString(string, wait) {
|
|
20
|
+
if (af?.article?.i18n !== undefined) {
|
|
21
|
+
if (!Object.prototype.hasOwnProperty.call(af.article.i18n, string)) {
|
|
22
|
+
af.article.i18n[string] = string;
|
|
23
|
+
let promise = getLocalizedStringAsync(string).then((text) => {
|
|
24
|
+
af.article.i18n[string] = text;
|
|
25
|
+
return text;
|
|
26
|
+
});
|
|
27
|
+
if (wait) {
|
|
28
|
+
return promise;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return af.article.i18n[string];
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
return string;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const key = "__UNLOCALIZED_STRINGS__";
|
|
38
|
+
const localizeSet = window[key] || new Set();
|
|
39
|
+
if (!window[key]) {
|
|
40
|
+
window[key] = localizeSet;
|
|
41
|
+
}
|
|
42
|
+
function handleReadyStateChange() {
|
|
43
|
+
if (document.readyState !== "loading") {
|
|
44
|
+
addDeveloperUtils();
|
|
45
|
+
document.removeEventListener("readystatechange", handleReadyStateChange);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (document.readyState !== "loading") {
|
|
49
|
+
addDeveloperUtils();
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
document.addEventListener("readystatechange", handleReadyStateChange);
|
|
53
|
+
}
|
|
54
|
+
function isDeveloper() {
|
|
55
|
+
const { af } = window;
|
|
56
|
+
return af && af.userSession && af.userSession.isDeveloper;
|
|
57
|
+
}
|
|
58
|
+
function addDeveloperUtils() {
|
|
59
|
+
if (isDeveloper()) {
|
|
60
|
+
if (typeof window.__doNothingWithStrings === "function") {
|
|
61
|
+
const addedStrings = window.__doNothingWithStrings();
|
|
62
|
+
for (let string of Object.keys(addedStrings)) {
|
|
63
|
+
localizeSet.add(string);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (!window[key]) {
|
|
67
|
+
window[key] = localizeSet;
|
|
68
|
+
}
|
|
69
|
+
if (!window.__getUnlocalizedStrings) {
|
|
70
|
+
window.__getUnlocalizedStrings = function (forCustomRoute) {
|
|
71
|
+
const strings = [];
|
|
72
|
+
for (let string of localizeSet) {
|
|
73
|
+
if (string) {
|
|
74
|
+
const escaped = string.replace(/'/g, "\\'");
|
|
75
|
+
if (forCustomRoute) {
|
|
76
|
+
const localized = afGetLocalizedString(string).replaceAll(/'/g, "\\'");
|
|
77
|
+
strings.push(`'${escaped}': '${localized}',`);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
strings.push(`s['${escaped}'] = @Html(@context.AddLocalizableString("${string.replaceAll('"', '\\"')}"));`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (forCustomRoute) {
|
|
85
|
+
return `// Localization hack, since it AddLocalizableString doesn't work properly when custom routes is used
|
|
86
|
+
if (af.userSession.uiculture === 'nb-NO') {
|
|
87
|
+
const strings = {
|
|
88
|
+
${strings.join("\n\t\t")}
|
|
89
|
+
};
|
|
90
|
+
af.article.i18n = af.article.i18n || {};
|
|
91
|
+
Object.assign(af.article.i18n, strings);
|
|
92
|
+
}`;
|
|
93
|
+
}
|
|
94
|
+
return `<script type="text/javascript">
|
|
95
|
+
function __doNothingWithStrings() {
|
|
96
|
+
@{
|
|
97
|
+
var context = @Appframe.Web.Context.RequestContext.Current;
|
|
98
|
+
}
|
|
99
|
+
let s = {};
|
|
100
|
+
${strings.join("\n")}
|
|
101
|
+
return s;
|
|
102
|
+
}
|
|
103
|
+
</script>`;
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
export function getLocalizedString(string) {
|
|
109
|
+
const localized = afGetLocalizedString(string);
|
|
110
|
+
if (localized === string && !localizeSet.has(string)) {
|
|
111
|
+
localizeSet.add(string);
|
|
112
|
+
}
|
|
113
|
+
return localized;
|
|
114
|
+
}
|
package/es/has.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function has(obj: any, key: string | number | symbol): boolean;
|
package/es/has.js
ADDED
package/es/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { default as mitt } from "mitt";
|
|
2
|
+
export { default as expose } from "./expose.js";
|
|
3
|
+
export { getLocalizedString } from "./getLocalizedString.js";
|
|
4
|
+
export { default as localStorage } from "./localStorage.js";
|
|
5
|
+
export { default as SessionHandler } from "./SessionHandler.js";
|
|
6
|
+
export { default as UserSession } from "./UserSession.js";
|
|
7
|
+
export declare const version: string;
|
package/es/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { default as mitt } from "mitt";
|
|
2
|
+
export { default as expose } from "./expose.js";
|
|
3
|
+
export { getLocalizedString } from "./getLocalizedString.js";
|
|
4
|
+
export { default as localStorage } from "./localStorage.js";
|
|
5
|
+
export { default as SessionHandler } from "./SessionHandler.js";
|
|
6
|
+
export { default as UserSession } from "./UserSession.js";
|
|
7
|
+
export const version = "1.0.0";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
declare class LocalStorage {
|
|
2
|
+
isPersisted(): boolean;
|
|
3
|
+
getLocalStorageKey(key: string, global?: boolean): string;
|
|
4
|
+
get(key: string, defaultValue: unknown, global?: boolean): any;
|
|
5
|
+
set(key: string, value: unknown, global?: boolean): void;
|
|
6
|
+
remove(key: string, global?: boolean): boolean;
|
|
7
|
+
clear(): void;
|
|
8
|
+
}
|
|
9
|
+
declare const _default: LocalStorage;
|
|
10
|
+
export default _default;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
function getLocalKey(key) {
|
|
2
|
+
return [af?.article?.id || "", "common", key].join(".");
|
|
3
|
+
}
|
|
4
|
+
class LocalStorage {
|
|
5
|
+
isPersisted() {
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
getLocalStorageKey(key, global) {
|
|
9
|
+
return global ? ["common", key].join(".") : getLocalKey(key);
|
|
10
|
+
}
|
|
11
|
+
get(key, defaultValue, global) {
|
|
12
|
+
const lsKey = this.getLocalStorageKey(key, global);
|
|
13
|
+
const value = localStorage.getItem(lsKey);
|
|
14
|
+
return value !== null ? JSON.parse(value) : defaultValue;
|
|
15
|
+
}
|
|
16
|
+
set(key, value, global) {
|
|
17
|
+
localStorage.setItem(this.getLocalStorageKey(key, global), JSON.stringify(value));
|
|
18
|
+
}
|
|
19
|
+
remove(key, global) {
|
|
20
|
+
const lsKey = this.getLocalStorageKey(key, global);
|
|
21
|
+
if (localStorage.getItem(lsKey) !== null) {
|
|
22
|
+
localStorage.removeItem(lsKey);
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
clear() {
|
|
30
|
+
const baseKey = getLocalKey("");
|
|
31
|
+
for (let key in localStorage) {
|
|
32
|
+
if (key.slice(0, baseKey.length) === baseKey) {
|
|
33
|
+
localStorage.removeItem(key);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export default new LocalStorage();
|
package/es/parseFunc.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
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)*)\}$/;
|
|
2
|
+
export default function parseFunc(func) {
|
|
3
|
+
var match = func.toString().match(funcRegex);
|
|
4
|
+
if (match === null) {
|
|
5
|
+
throw new TypeError("Unable to parse function!");
|
|
6
|
+
}
|
|
7
|
+
return { name: match[1], args: match[2].split(","), body: match[3] };
|
|
8
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function requireNamespace(root: any, namespace: string | Array<string>): any;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import Namespace from "./Namespace.js";
|
|
2
|
+
export default function requireNamespace(root, namespace) {
|
|
3
|
+
if (typeof root === "object") {
|
|
4
|
+
if (typeof namespace === "string") {
|
|
5
|
+
let path = namespace.split(".");
|
|
6
|
+
return requireNamespace(root, path);
|
|
7
|
+
}
|
|
8
|
+
else if (Array.isArray(namespace)) {
|
|
9
|
+
let current = root;
|
|
10
|
+
for (let i = 0; i < namespace.length; i++) {
|
|
11
|
+
current = current[namespace[i]] =
|
|
12
|
+
current[namespace[i]] || new Namespace();
|
|
13
|
+
}
|
|
14
|
+
return current;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
throw new TypeError("Invalid type of parameter 'namespace'");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
throw new TypeError("Invalid type of parameter 'root'");
|
|
22
|
+
}
|
|
23
|
+
}
|
package/es/settings.d.ts
ADDED
package/es/settings.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
function getKey(article) {
|
|
2
|
+
const articleKey = article || af.article.id;
|
|
3
|
+
return `${articleKey}.${af.userSession.login}`;
|
|
4
|
+
}
|
|
5
|
+
export function getSettings(setting, article) {
|
|
6
|
+
if (!af?.userSession)
|
|
7
|
+
return null;
|
|
8
|
+
const key = getKey(article);
|
|
9
|
+
const settings = JSON.parse(localStorage.getItem(key) || "{}");
|
|
10
|
+
if (setting) {
|
|
11
|
+
return settings[setting] || null;
|
|
12
|
+
}
|
|
13
|
+
return settings;
|
|
14
|
+
}
|
|
15
|
+
export function saveSettings(setting, value, article) {
|
|
16
|
+
if (!af?.userSession)
|
|
17
|
+
return;
|
|
18
|
+
const key = getKey(article);
|
|
19
|
+
let userSettings = JSON.parse(localStorage.getItem(key) || "{}");
|
|
20
|
+
if (typeof value !== "undefined") {
|
|
21
|
+
userSettings[setting] = value;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
userSettings = setting;
|
|
25
|
+
}
|
|
26
|
+
localStorage.setItem(key, JSON.stringify(userSettings));
|
|
27
|
+
}
|
package/es/warn.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function warn(message: string, fallbackToAlert?: boolean): void;
|
package/es/warn.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
export default function warn(message, fallbackToAlert) {
|
|
3
|
+
if (console && typeof console.warn === "function") {
|
|
4
|
+
console.warn(message);
|
|
5
|
+
}
|
|
6
|
+
else if (console && typeof console.log === "function") {
|
|
7
|
+
console.log(message);
|
|
8
|
+
}
|
|
9
|
+
else if (fallbackToAlert) {
|
|
10
|
+
alert(message);
|
|
11
|
+
}
|
|
12
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@olenbetong/appframe-core",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Javascript libraries to utilize the Appframe framework",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./es/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"default": "./es/index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"types": "./es/index.d.ts",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"prepack": "npm run build",
|
|
15
|
+
"build": "npm run build:esbuild && npm run build:tsc",
|
|
16
|
+
"build:esbuild": "node ../../shared/esbuild.js",
|
|
17
|
+
"build:tsc": " rm -rf ./es && tsc && node ./scripts/replace-version.js"
|
|
18
|
+
},
|
|
19
|
+
"author": "Bjørnar Vister Hansen <bvh@olenbetong.no>",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/olenbetong/ui-kit.git"
|
|
23
|
+
},
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"buildConfig": {
|
|
26
|
+
"entries": [
|
|
27
|
+
{
|
|
28
|
+
"libraryName": "af.common"
|
|
29
|
+
}
|
|
30
|
+
]
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"mitt": "^3.0.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"chalk": "^5.0.1"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
import importJson from "./import-json.js";
|
|
5
|
+
|
|
6
|
+
async function* getFiles(dir) {
|
|
7
|
+
const dirents = await fs.readdir(dir, { withFileTypes: true });
|
|
8
|
+
for (const dirent of dirents) {
|
|
9
|
+
const res = resolve(dir, dirent.name);
|
|
10
|
+
if (dirent.isDirectory()) {
|
|
11
|
+
yield* getFiles(res);
|
|
12
|
+
} else {
|
|
13
|
+
yield res;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
let packageConfig = await importJson("../package.json");
|
|
20
|
+
for await (let file of getFiles("./es")) {
|
|
21
|
+
if (file.endsWith(".js")) {
|
|
22
|
+
let fileContent = await fs.readFile(file, "utf-8");
|
|
23
|
+
let result = fileContent
|
|
24
|
+
.replace("__VERSION__", `"${packageConfig.version}"`)
|
|
25
|
+
.replace("process.env.NODE_ENV", '"production"');
|
|
26
|
+
if (result !== fileContent) {
|
|
27
|
+
await fs.writeFile(file, result, "utf-8");
|
|
28
|
+
console.log(file.substring(file.indexOf("/es/")));
|
|
29
|
+
} else {
|
|
30
|
+
console.log(chalk.dim(file.substring(file.indexOf("/es/"))));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error(error.message);
|
|
36
|
+
process.exit(1);
|
|
37
|
+
}
|
package/src/Namespace.ts
ADDED