@churchapps/helpers 1.2.3 → 1.2.4
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/.eslintrc.json +28 -28
- package/.github/FUNDING.yml +1 -1
- package/.prettierrc +11 -11
- package/.yarnrc.yml +5 -5
- package/LICENSE +21 -21
- package/README.md +15 -15
- package/dist/CommonEnvironmentHelper.d.ts +1 -1
- package/dist/CommonEnvironmentHelper.d.ts.map +1 -1
- package/dist/CommonEnvironmentHelper.js +5 -5
- package/dist/CommonEnvironmentHelper.js.map +1 -1
- package/dist/PersonHelper.js +1 -1
- package/package.json +49 -49
- package/scripts/build-cjs.js +32 -32
- package/src/ApiHelper.ts +176 -176
- package/src/AppearanceHelper.ts +69 -69
- package/src/ArrayHelper.ts +157 -157
- package/src/CommonEnvironmentHelper.ts +104 -104
- package/src/CurrencyHelper.ts +10 -10
- package/src/DateHelper.ts +153 -153
- package/src/DonationHelper.ts +26 -26
- package/src/ErrorHelper.ts +39 -39
- package/src/EventHelper.ts +49 -49
- package/src/FileHelper.ts +55 -55
- package/src/PersonHelper.ts +82 -82
- package/src/UniqueIdHelper.ts +36 -36
- package/src/UserHelper.ts +59 -59
- package/src/index.ts +15 -15
- package/src/interfaces/Access.ts +138 -138
- package/src/interfaces/Attendance.ts +45 -45
- package/src/interfaces/Content.ts +84 -84
- package/src/interfaces/Doing.ts +93 -93
- package/src/interfaces/Donation.ts +190 -190
- package/src/interfaces/Error.ts +17 -17
- package/src/interfaces/Membership.ts +184 -184
- package/src/interfaces/Messaging.ts +96 -96
- package/src/interfaces/Permissions.ts +92 -92
- package/src/interfaces/Reporting.ts +41 -41
- package/src/interfaces/UserContextInterface.ts +13 -13
- package/src/interfaces/index.ts +13 -13
- package/tsconfig.json +36 -36
- package/CLAUDE.md +0 -98
package/src/ApiHelper.ts
CHANGED
|
@@ -1,176 +1,176 @@
|
|
|
1
|
-
import { ApiConfig, RolePermissionInterface, ApiListType } from "./interfaces";
|
|
2
|
-
import { ErrorHelper } from "./ErrorHelper";
|
|
3
|
-
|
|
4
|
-
// Global singleton pattern to ensure single instance across all packages
|
|
5
|
-
declare global {
|
|
6
|
-
interface Window {
|
|
7
|
-
__CHURCHAPPS_API_HELPER__?: ApiHelperClass;
|
|
8
|
-
}
|
|
9
|
-
var __CHURCHAPPS_API_HELPER__: ApiHelperClass | undefined;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
class ApiHelperClass {
|
|
13
|
-
|
|
14
|
-
apiConfigs: ApiConfig[] = [];
|
|
15
|
-
isAuthenticated = false;
|
|
16
|
-
onRequest: (url:string, requestOptions:any) => void;
|
|
17
|
-
onError: (url:string, requestOptions:any, error: any) => void;
|
|
18
|
-
|
|
19
|
-
getConfig(keyName: string) {
|
|
20
|
-
let result: ApiConfig = null;
|
|
21
|
-
this.apiConfigs.forEach(config => { if (config.keyName === keyName) result = config });
|
|
22
|
-
//if (result === null) throw new Error("Unconfigured API: " + keyName);
|
|
23
|
-
return result;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
setDefaultPermissions(jwt: string) {
|
|
27
|
-
this.apiConfigs.forEach(config => {
|
|
28
|
-
config.jwt = jwt;
|
|
29
|
-
config.permissions = [];
|
|
30
|
-
});
|
|
31
|
-
this.isAuthenticated = true;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
setPermissions(keyName: string, jwt: string, permissions: RolePermissionInterface[]) {
|
|
35
|
-
this.apiConfigs.forEach(config => {
|
|
36
|
-
if (config.keyName === keyName) {
|
|
37
|
-
config.jwt = jwt;
|
|
38
|
-
config.permissions = permissions;
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
this.isAuthenticated = true;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
clearPermissions() {
|
|
45
|
-
this.apiConfigs.forEach(config => { config.jwt = ""; config.permissions = []; });
|
|
46
|
-
this.isAuthenticated = false;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async get(path: string, apiName: ApiListType) {
|
|
50
|
-
const config = this.getConfig(apiName);
|
|
51
|
-
if (!config) throw new Error(`API configuration not found: ${apiName}`);
|
|
52
|
-
const requestOptions = { method: "GET", headers: { Authorization: "Bearer " + config.jwt } };
|
|
53
|
-
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
async getAnonymous(path: string, apiName: ApiListType) {
|
|
57
|
-
const config = this.getConfig(apiName);
|
|
58
|
-
const requestOptions = { method: "GET" };
|
|
59
|
-
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
async post(path: string, data: any[] | {}, apiName: ApiListType) {
|
|
63
|
-
const config = this.getConfig(apiName);
|
|
64
|
-
if (!config) throw new Error(`API configuration not found: ${apiName}`);
|
|
65
|
-
const requestOptions = {
|
|
66
|
-
method: "POST",
|
|
67
|
-
headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
|
|
68
|
-
body: JSON.stringify(data)
|
|
69
|
-
};
|
|
70
|
-
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async patch(path: string, data: any[] | {}, apiName: ApiListType) {
|
|
74
|
-
const config = this.getConfig(apiName);
|
|
75
|
-
if (!config) throw new Error(`API configuration not found: ${apiName}`);
|
|
76
|
-
const requestOptions = {
|
|
77
|
-
method: "PATCH",
|
|
78
|
-
headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
|
|
79
|
-
body: JSON.stringify(data)
|
|
80
|
-
};
|
|
81
|
-
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
async delete(path: string, apiName: ApiListType) {
|
|
85
|
-
const config = this.getConfig(apiName);
|
|
86
|
-
if (!config) throw new Error(`API configuration not found: ${apiName}`);
|
|
87
|
-
const requestOptions = {
|
|
88
|
-
method: "DELETE",
|
|
89
|
-
headers: { Authorization: "Bearer " + config.jwt }
|
|
90
|
-
};
|
|
91
|
-
if (this.onRequest) this.onRequest(config.url + path, requestOptions);
|
|
92
|
-
try {
|
|
93
|
-
const response = await fetch(config.url + path, requestOptions);
|
|
94
|
-
if (!response.ok) await this.throwApiError(response);
|
|
95
|
-
} catch (e) {
|
|
96
|
-
console.log(e);
|
|
97
|
-
if (this.onError) this.onError(config.url + path, requestOptions, e);
|
|
98
|
-
throw (e);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
async postAnonymous(path: string, data: any[] | {}, apiName: ApiListType) {
|
|
103
|
-
const config = this.getConfig(apiName);
|
|
104
|
-
const requestOptions = {
|
|
105
|
-
method: "POST",
|
|
106
|
-
headers: { "Content-Type": "application/json" },
|
|
107
|
-
body: JSON.stringify(data)
|
|
108
|
-
};
|
|
109
|
-
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
async fetchWithErrorHandling(url: string, requestOptions: any) {
|
|
113
|
-
if (this.onRequest) this.onRequest(url, requestOptions);
|
|
114
|
-
try {
|
|
115
|
-
const response = await fetch(url, requestOptions);
|
|
116
|
-
if (!response.ok) await this.throwApiError(response);
|
|
117
|
-
else {
|
|
118
|
-
if (response.status !== 204 ) {
|
|
119
|
-
return response.json();
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
} catch (e) {
|
|
123
|
-
console.log("Error loading url: " + url);
|
|
124
|
-
console.log(e)
|
|
125
|
-
throw (e);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
private async throwApiError(response: Response) {
|
|
130
|
-
let msg = response.statusText;
|
|
131
|
-
try {
|
|
132
|
-
msg = await response.text();
|
|
133
|
-
} catch {}
|
|
134
|
-
try {
|
|
135
|
-
const json = await response.json();
|
|
136
|
-
msg = json.errors[0];
|
|
137
|
-
} catch { }
|
|
138
|
-
console.log("RESPONSE", response)
|
|
139
|
-
ErrorHelper.logError(response.status.toString(), response.url, msg);
|
|
140
|
-
throw new Error(msg || "Error");
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// Force singleton with immediate global assignment
|
|
146
|
-
const getGlobalObject = () => {
|
|
147
|
-
if (typeof window !== 'undefined') return window;
|
|
148
|
-
if (typeof global !== 'undefined') return global;
|
|
149
|
-
if (typeof globalThis !== 'undefined') return globalThis;
|
|
150
|
-
return {};
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
// Get or create singleton immediately - FORCE SINGLE INSTANCE
|
|
154
|
-
const ensureSingleton = () => {
|
|
155
|
-
const globalObj = getGlobalObject() as any;
|
|
156
|
-
|
|
157
|
-
// Use a more unique key to avoid conflicts
|
|
158
|
-
const SINGLETON_KEY = '__CHURCHAPPS_API_HELPER_SINGLETON__';
|
|
159
|
-
|
|
160
|
-
// ALWAYS create a new instance and overwrite any existing one
|
|
161
|
-
// This ensures the latest module load wins
|
|
162
|
-
if (!globalObj[SINGLETON_KEY]) {
|
|
163
|
-
globalObj[SINGLETON_KEY] = new ApiHelperClass();
|
|
164
|
-
console.log('🔧 ApiHelper SINGLETON created (new instance)');
|
|
165
|
-
} else {
|
|
166
|
-
console.log('🔄 ApiHelper SINGLETON already exists - using existing (configs:', globalObj[SINGLETON_KEY].apiConfigs.length, ')');
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return globalObj[SINGLETON_KEY];
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
// Export the singleton instance
|
|
173
|
-
export const ApiHelper = ensureSingleton();
|
|
174
|
-
|
|
175
|
-
// Also export the class for type usage
|
|
176
|
-
export type { ApiHelperClass };
|
|
1
|
+
import { ApiConfig, RolePermissionInterface, ApiListType } from "./interfaces";
|
|
2
|
+
import { ErrorHelper } from "./ErrorHelper";
|
|
3
|
+
|
|
4
|
+
// Global singleton pattern to ensure single instance across all packages
|
|
5
|
+
declare global {
|
|
6
|
+
interface Window {
|
|
7
|
+
__CHURCHAPPS_API_HELPER__?: ApiHelperClass;
|
|
8
|
+
}
|
|
9
|
+
var __CHURCHAPPS_API_HELPER__: ApiHelperClass | undefined;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
class ApiHelperClass {
|
|
13
|
+
|
|
14
|
+
apiConfigs: ApiConfig[] = [];
|
|
15
|
+
isAuthenticated = false;
|
|
16
|
+
onRequest: (url:string, requestOptions:any) => void;
|
|
17
|
+
onError: (url:string, requestOptions:any, error: any) => void;
|
|
18
|
+
|
|
19
|
+
getConfig(keyName: string) {
|
|
20
|
+
let result: ApiConfig = null;
|
|
21
|
+
this.apiConfigs.forEach(config => { if (config.keyName === keyName) result = config });
|
|
22
|
+
//if (result === null) throw new Error("Unconfigured API: " + keyName);
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
setDefaultPermissions(jwt: string) {
|
|
27
|
+
this.apiConfigs.forEach(config => {
|
|
28
|
+
config.jwt = jwt;
|
|
29
|
+
config.permissions = [];
|
|
30
|
+
});
|
|
31
|
+
this.isAuthenticated = true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
setPermissions(keyName: string, jwt: string, permissions: RolePermissionInterface[]) {
|
|
35
|
+
this.apiConfigs.forEach(config => {
|
|
36
|
+
if (config.keyName === keyName) {
|
|
37
|
+
config.jwt = jwt;
|
|
38
|
+
config.permissions = permissions;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
this.isAuthenticated = true;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
clearPermissions() {
|
|
45
|
+
this.apiConfigs.forEach(config => { config.jwt = ""; config.permissions = []; });
|
|
46
|
+
this.isAuthenticated = false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async get(path: string, apiName: ApiListType) {
|
|
50
|
+
const config = this.getConfig(apiName);
|
|
51
|
+
if (!config) throw new Error(`API configuration not found: ${apiName}`);
|
|
52
|
+
const requestOptions = { method: "GET", headers: { Authorization: "Bearer " + config.jwt } };
|
|
53
|
+
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async getAnonymous(path: string, apiName: ApiListType) {
|
|
57
|
+
const config = this.getConfig(apiName);
|
|
58
|
+
const requestOptions = { method: "GET" };
|
|
59
|
+
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async post(path: string, data: any[] | {}, apiName: ApiListType) {
|
|
63
|
+
const config = this.getConfig(apiName);
|
|
64
|
+
if (!config) throw new Error(`API configuration not found: ${apiName}`);
|
|
65
|
+
const requestOptions = {
|
|
66
|
+
method: "POST",
|
|
67
|
+
headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
|
|
68
|
+
body: JSON.stringify(data)
|
|
69
|
+
};
|
|
70
|
+
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async patch(path: string, data: any[] | {}, apiName: ApiListType) {
|
|
74
|
+
const config = this.getConfig(apiName);
|
|
75
|
+
if (!config) throw new Error(`API configuration not found: ${apiName}`);
|
|
76
|
+
const requestOptions = {
|
|
77
|
+
method: "PATCH",
|
|
78
|
+
headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
|
|
79
|
+
body: JSON.stringify(data)
|
|
80
|
+
};
|
|
81
|
+
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async delete(path: string, apiName: ApiListType) {
|
|
85
|
+
const config = this.getConfig(apiName);
|
|
86
|
+
if (!config) throw new Error(`API configuration not found: ${apiName}`);
|
|
87
|
+
const requestOptions = {
|
|
88
|
+
method: "DELETE",
|
|
89
|
+
headers: { Authorization: "Bearer " + config.jwt }
|
|
90
|
+
};
|
|
91
|
+
if (this.onRequest) this.onRequest(config.url + path, requestOptions);
|
|
92
|
+
try {
|
|
93
|
+
const response = await fetch(config.url + path, requestOptions);
|
|
94
|
+
if (!response.ok) await this.throwApiError(response);
|
|
95
|
+
} catch (e) {
|
|
96
|
+
console.log(e);
|
|
97
|
+
if (this.onError) this.onError(config.url + path, requestOptions, e);
|
|
98
|
+
throw (e);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async postAnonymous(path: string, data: any[] | {}, apiName: ApiListType) {
|
|
103
|
+
const config = this.getConfig(apiName);
|
|
104
|
+
const requestOptions = {
|
|
105
|
+
method: "POST",
|
|
106
|
+
headers: { "Content-Type": "application/json" },
|
|
107
|
+
body: JSON.stringify(data)
|
|
108
|
+
};
|
|
109
|
+
return await this.fetchWithErrorHandling(config.url + path, requestOptions);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async fetchWithErrorHandling(url: string, requestOptions: any) {
|
|
113
|
+
if (this.onRequest) this.onRequest(url, requestOptions);
|
|
114
|
+
try {
|
|
115
|
+
const response = await fetch(url, requestOptions);
|
|
116
|
+
if (!response.ok) await this.throwApiError(response);
|
|
117
|
+
else {
|
|
118
|
+
if (response.status !== 204 ) {
|
|
119
|
+
return response.json();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
} catch (e) {
|
|
123
|
+
console.log("Error loading url: " + url);
|
|
124
|
+
console.log(e)
|
|
125
|
+
throw (e);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private async throwApiError(response: Response) {
|
|
130
|
+
let msg = response.statusText;
|
|
131
|
+
try {
|
|
132
|
+
msg = await response.text();
|
|
133
|
+
} catch {}
|
|
134
|
+
try {
|
|
135
|
+
const json = await response.json();
|
|
136
|
+
msg = json.errors[0];
|
|
137
|
+
} catch { }
|
|
138
|
+
console.log("RESPONSE", response)
|
|
139
|
+
ErrorHelper.logError(response.status.toString(), response.url, msg);
|
|
140
|
+
throw new Error(msg || "Error");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Force singleton with immediate global assignment
|
|
146
|
+
const getGlobalObject = () => {
|
|
147
|
+
if (typeof window !== 'undefined') return window;
|
|
148
|
+
if (typeof global !== 'undefined') return global;
|
|
149
|
+
if (typeof globalThis !== 'undefined') return globalThis;
|
|
150
|
+
return {};
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// Get or create singleton immediately - FORCE SINGLE INSTANCE
|
|
154
|
+
const ensureSingleton = () => {
|
|
155
|
+
const globalObj = getGlobalObject() as any;
|
|
156
|
+
|
|
157
|
+
// Use a more unique key to avoid conflicts
|
|
158
|
+
const SINGLETON_KEY = '__CHURCHAPPS_API_HELPER_SINGLETON__';
|
|
159
|
+
|
|
160
|
+
// ALWAYS create a new instance and overwrite any existing one
|
|
161
|
+
// This ensures the latest module load wins
|
|
162
|
+
if (!globalObj[SINGLETON_KEY]) {
|
|
163
|
+
globalObj[SINGLETON_KEY] = new ApiHelperClass();
|
|
164
|
+
console.log('🔧 ApiHelper SINGLETON created (new instance)');
|
|
165
|
+
} else {
|
|
166
|
+
console.log('🔄 ApiHelper SINGLETON already exists - using existing (configs:', globalObj[SINGLETON_KEY].apiConfigs.length, ')');
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return globalObj[SINGLETON_KEY];
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
// Export the singleton instance
|
|
173
|
+
export const ApiHelper = ensureSingleton();
|
|
174
|
+
|
|
175
|
+
// Also export the class for type usage
|
|
176
|
+
export type { ApiHelperClass };
|
package/src/AppearanceHelper.ts
CHANGED
|
@@ -1,69 +1,69 @@
|
|
|
1
|
-
|
|
2
|
-
export interface AppearanceInterface { primaryColor?: string, primaryContrast?: string, secondaryColor?: string, secondaryContrast?: string, logoLight?: string, logoDark?: string, favicon_400x400?: string, favicon_16x16?: string }
|
|
3
|
-
|
|
4
|
-
export class AppearanceHelper {
|
|
5
|
-
|
|
6
|
-
public static getLogoDark(appearanceSettings: AppearanceInterface, defaultLogo: string) {
|
|
7
|
-
return (appearanceSettings?.logoDark) ? appearanceSettings.logoDark : defaultLogo;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
public static getLogoLight(appearanceSettings: AppearanceInterface, defaultLogo: string) {
|
|
11
|
-
return (appearanceSettings?.logoLight) ? appearanceSettings.logoLight : defaultLogo;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public static getFavicon(appearanceSettings: AppearanceInterface, size: "400" | "16") {
|
|
15
|
-
if (size === "400") {
|
|
16
|
-
return appearanceSettings?.favicon_400x400;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (size === "16") {
|
|
20
|
-
return appearanceSettings?.favicon_16x16;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public static getLogo(appearanceSettings: AppearanceInterface, defaultLogoLight: string, defaultLogoDark: string, backgroundColor: string) {
|
|
27
|
-
const isDark = (appearanceSettings.logoDark) ? this.isDark(backgroundColor) : false;
|
|
28
|
-
if (isDark) return this.getLogoDark(appearanceSettings, defaultLogoDark);
|
|
29
|
-
else return this.getLogoLight(appearanceSettings, defaultLogoLight);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
private static isDark(backgroundColor: string) {
|
|
33
|
-
let valid = false;
|
|
34
|
-
let r = 0;
|
|
35
|
-
let g = 0;
|
|
36
|
-
let b = 0;
|
|
37
|
-
|
|
38
|
-
if (backgroundColor.match(/#[0-9a-fA-F]{6}/)) {
|
|
39
|
-
r = this.getHexValue(backgroundColor.substring(1, 3));
|
|
40
|
-
g = this.getHexValue(backgroundColor.substring(3, 5));
|
|
41
|
-
b = this.getHexValue(backgroundColor.substring(5, 7));
|
|
42
|
-
valid = true;
|
|
43
|
-
} else if (backgroundColor.match(/#[0-9a-fA-F]{3}/)) {
|
|
44
|
-
r = this.getHexValue(backgroundColor.substring(1, 2));
|
|
45
|
-
g = this.getHexValue(backgroundColor.substring(2, 3));
|
|
46
|
-
b = this.getHexValue(backgroundColor.substring(3, 4));
|
|
47
|
-
valid = true;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (!valid) return false;
|
|
51
|
-
else {
|
|
52
|
-
//HSP brightness formula. Some colors have a bigger impact on our perceived brightness than others.
|
|
53
|
-
const rWeight = .299 * Math.pow(r, 2);
|
|
54
|
-
const gWeight = .587 * Math.pow(g, 2);
|
|
55
|
-
const bWeight = .114 * Math.pow(b, 2);
|
|
56
|
-
const brightness = Math.sqrt(rWeight + gWeight + bWeight);
|
|
57
|
-
//return brightness < 128; //
|
|
58
|
-
return brightness < 156;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
private static getHexValue(hex: string) {
|
|
64
|
-
let result = parseInt(hex, 16);
|
|
65
|
-
if (hex.length === 1) result = result * 16;
|
|
66
|
-
return result;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
}
|
|
1
|
+
|
|
2
|
+
export interface AppearanceInterface { primaryColor?: string, primaryContrast?: string, secondaryColor?: string, secondaryContrast?: string, logoLight?: string, logoDark?: string, favicon_400x400?: string, favicon_16x16?: string }
|
|
3
|
+
|
|
4
|
+
export class AppearanceHelper {
|
|
5
|
+
|
|
6
|
+
public static getLogoDark(appearanceSettings: AppearanceInterface, defaultLogo: string) {
|
|
7
|
+
return (appearanceSettings?.logoDark) ? appearanceSettings.logoDark : defaultLogo;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public static getLogoLight(appearanceSettings: AppearanceInterface, defaultLogo: string) {
|
|
11
|
+
return (appearanceSettings?.logoLight) ? appearanceSettings.logoLight : defaultLogo;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public static getFavicon(appearanceSettings: AppearanceInterface, size: "400" | "16") {
|
|
15
|
+
if (size === "400") {
|
|
16
|
+
return appearanceSettings?.favicon_400x400;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (size === "16") {
|
|
20
|
+
return appearanceSettings?.favicon_16x16;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public static getLogo(appearanceSettings: AppearanceInterface, defaultLogoLight: string, defaultLogoDark: string, backgroundColor: string) {
|
|
27
|
+
const isDark = (appearanceSettings.logoDark) ? this.isDark(backgroundColor) : false;
|
|
28
|
+
if (isDark) return this.getLogoDark(appearanceSettings, defaultLogoDark);
|
|
29
|
+
else return this.getLogoLight(appearanceSettings, defaultLogoLight);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
private static isDark(backgroundColor: string) {
|
|
33
|
+
let valid = false;
|
|
34
|
+
let r = 0;
|
|
35
|
+
let g = 0;
|
|
36
|
+
let b = 0;
|
|
37
|
+
|
|
38
|
+
if (backgroundColor.match(/#[0-9a-fA-F]{6}/)) {
|
|
39
|
+
r = this.getHexValue(backgroundColor.substring(1, 3));
|
|
40
|
+
g = this.getHexValue(backgroundColor.substring(3, 5));
|
|
41
|
+
b = this.getHexValue(backgroundColor.substring(5, 7));
|
|
42
|
+
valid = true;
|
|
43
|
+
} else if (backgroundColor.match(/#[0-9a-fA-F]{3}/)) {
|
|
44
|
+
r = this.getHexValue(backgroundColor.substring(1, 2));
|
|
45
|
+
g = this.getHexValue(backgroundColor.substring(2, 3));
|
|
46
|
+
b = this.getHexValue(backgroundColor.substring(3, 4));
|
|
47
|
+
valid = true;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (!valid) return false;
|
|
51
|
+
else {
|
|
52
|
+
//HSP brightness formula. Some colors have a bigger impact on our perceived brightness than others.
|
|
53
|
+
const rWeight = .299 * Math.pow(r, 2);
|
|
54
|
+
const gWeight = .587 * Math.pow(g, 2);
|
|
55
|
+
const bWeight = .114 * Math.pow(b, 2);
|
|
56
|
+
const brightness = Math.sqrt(rWeight + gWeight + bWeight);
|
|
57
|
+
//return brightness < 128; //
|
|
58
|
+
return brightness < 156;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private static getHexValue(hex: string) {
|
|
64
|
+
let result = parseInt(hex, 16);
|
|
65
|
+
if (hex.length === 1) result = result * 16;
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
}
|