@ramathibodi/nuxt-commons 0.1.4 → 0.1.5
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/module.json +1 -1
- package/dist/runtime/composables/alert.mjs +2 -1
- package/dist/runtime/composables/api.mjs +18 -9
- package/dist/runtime/composables/document/template.mjs +16 -8
- package/dist/runtime/composables/graphql.d.ts +3 -3
- package/dist/runtime/composables/graphql.mjs +8 -4
- package/dist/runtime/composables/menu.mjs +4 -2
- package/dist/runtime/plugins/permission.mjs +4 -2
- package/dist/runtime/utils/datetime.mjs +44 -22
- package/dist/runtime/utils/object.mjs +2 -1
- package/package.json +2 -2
package/dist/module.json
CHANGED
|
@@ -23,7 +23,8 @@ export function createAlert() {
|
|
|
23
23
|
},
|
|
24
24
|
takeAlert(location = "default") {
|
|
25
25
|
const item = head(items.value.filter((i) => i.alertLocation === location));
|
|
26
|
-
if (item)
|
|
26
|
+
if (item)
|
|
27
|
+
remove(items.value, (i) => i === item);
|
|
27
28
|
return item;
|
|
28
29
|
},
|
|
29
30
|
hasAlert(location = "default") {
|
|
@@ -5,13 +5,16 @@ export function useApi() {
|
|
|
5
5
|
function urlBuilder(url) {
|
|
6
6
|
let returnUrl = "";
|
|
7
7
|
if (Array.isArray(url)) {
|
|
8
|
-
if (url[0].toLowerCase() == "agent")
|
|
8
|
+
if (url[0].toLowerCase() == "agent")
|
|
9
|
+
url[0] = config?.public.WS_AGENT;
|
|
9
10
|
returnUrl = url.join("/");
|
|
10
11
|
} else {
|
|
11
12
|
returnUrl = url;
|
|
12
13
|
}
|
|
13
|
-
if (returnUrl.startsWith("http://") || returnUrl.startsWith("https://"))
|
|
14
|
-
|
|
14
|
+
if (returnUrl.startsWith("http://") || returnUrl.startsWith("https://"))
|
|
15
|
+
return returnUrl;
|
|
16
|
+
else
|
|
17
|
+
return trimEnd(config?.public.WS_API, "/") + "/" + trimStart(returnUrl, "/");
|
|
15
18
|
}
|
|
16
19
|
function optionBuilder(method, body, params, options) {
|
|
17
20
|
let returnOption = {
|
|
@@ -25,8 +28,10 @@ export function useApi() {
|
|
|
25
28
|
};
|
|
26
29
|
if (options) {
|
|
27
30
|
options = Object.assign(options, returnOption);
|
|
28
|
-
if (options.headers)
|
|
29
|
-
|
|
31
|
+
if (options.headers)
|
|
32
|
+
options.headers = Object.assign(options.headers, headers);
|
|
33
|
+
else
|
|
34
|
+
options.headers = headers;
|
|
30
35
|
}
|
|
31
36
|
returnOption = Object.assign(returnOption, options);
|
|
32
37
|
return returnOption;
|
|
@@ -37,8 +42,10 @@ export function useApi() {
|
|
|
37
42
|
function getPromise(url, body, params, options) {
|
|
38
43
|
return new Promise(async (resolve, reject) => {
|
|
39
44
|
const { data, error } = await get(url, body, params, options);
|
|
40
|
-
if (!error.value)
|
|
41
|
-
|
|
45
|
+
if (!error.value)
|
|
46
|
+
resolve(data.value);
|
|
47
|
+
else
|
|
48
|
+
reject(error.value);
|
|
42
49
|
});
|
|
43
50
|
}
|
|
44
51
|
function post(url, body, params, options) {
|
|
@@ -47,8 +54,10 @@ export function useApi() {
|
|
|
47
54
|
function postPromise(url, body, params, options) {
|
|
48
55
|
return new Promise(async (resolve, reject) => {
|
|
49
56
|
const { data, error } = await post(url, body, params, options);
|
|
50
|
-
if (!error.value)
|
|
51
|
-
|
|
57
|
+
if (!error.value)
|
|
58
|
+
resolve(data.value);
|
|
59
|
+
else
|
|
60
|
+
reject(error.value);
|
|
52
61
|
});
|
|
53
62
|
}
|
|
54
63
|
return { urlBuilder, get, getPromise, post, postPromise };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export const validationRulesRegex = /^(require(?:\([^)]*\))?|requireIf(?:\([^)]*\))?|requireTrue(?:\([^)]*\))?|requireTrueIf(?:\([^)]*\))?|numeric(?:\([^)]*\))?|range(?:\([^)]*\))?|integer(?:\([^)]*\))?|unique(?:\([^)]*\))?|length(?:\([^)]*\))?|lengthGreater(?:\([^)]*\))?|lengthLess(?:\([^)]*\))?|telephone(?:\([^)]*\))?|email(?:\([^)]*\))?|regex(?:\([^)]*\))?)(,(require(?:\([^)]*\))?|requireIf(?:\([^)]*\))?|requireTrue(?:\([^)]*\))?|requireTrueIf(?:\([^)]*\))?|numeric(?:\([^)]*\))?|range(?:\([^)]*\))?|integer(?:\([^)]*\))?|unique(?:\([^)]*\))?|length(?:\([^)]*\))?|lengthGreater(?:\([^)]*\))?|lengthLess(?:\([^)]*\))?|telephone(?:\([^)]*\))?|email(?:\([^)]*\))?|regex(?:\([^)]*\))?))*$/;
|
|
2
2
|
export function useDocumentTemplate(items) {
|
|
3
|
-
if (!items)
|
|
3
|
+
if (!items)
|
|
4
|
+
return "";
|
|
4
5
|
if (typeof items === "string") {
|
|
5
6
|
try {
|
|
6
7
|
items = JSON.parse(items);
|
|
@@ -35,8 +36,10 @@ function templateItemToString(item) {
|
|
|
35
36
|
optionString = item.inputOptions.split(",").join(" ");
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
|
-
if (item.inputType == "FormDateTime" && !item.inputAttributes?.includes("dense"))
|
|
39
|
-
|
|
39
|
+
if (item.inputType == "FormDateTime" && !item.inputAttributes?.includes("dense"))
|
|
40
|
+
item.inputAttributes = (item.inputAttributes?.trim() + " dense").trim();
|
|
41
|
+
if (item.validationRules)
|
|
42
|
+
validationRules = buildValidationRules(item.validationRules);
|
|
40
43
|
let templateString = "";
|
|
41
44
|
switch (item.inputType) {
|
|
42
45
|
case "CustomCode":
|
|
@@ -57,8 +60,10 @@ function templateItemToString(item) {
|
|
|
57
60
|
default:
|
|
58
61
|
templateString = `<${item.inputType} v-model="data.${item.variableName}" label="${item.inputLabel || item.variableName}"${item.inputAttributes ? " " + item.inputAttributes : ""}${optionString ? " " + optionString.trim() : ""}${validationRules ? " " + validationRules.trim() : ""}></${item.inputType}>`;
|
|
59
62
|
}
|
|
60
|
-
if (!["Separator", "Header"].includes(item.inputType))
|
|
61
|
-
|
|
63
|
+
if (!["Separator", "Header"].includes(item.inputType))
|
|
64
|
+
templateString = `<v-col${item.width ? ' cols="' + item.width + '"' : ""}${item.columnAttributes ? " " + item.columnAttributes : ""}>${templateString}</v-col>`;
|
|
65
|
+
if (["Header"].includes(item.inputType))
|
|
66
|
+
templateString = `</v-row><v-row dense><v-col${item.columnAttributes ? " " + item.columnAttributes : ""}>${templateString}</v-col></v-row><v-row dense>`;
|
|
62
67
|
return templateString;
|
|
63
68
|
}
|
|
64
69
|
function optionStringToChoiceObject(option) {
|
|
@@ -79,9 +84,12 @@ function optionStringToChoiceObject(option) {
|
|
|
79
84
|
}
|
|
80
85
|
function buildValidationRules(validationString) {
|
|
81
86
|
validationString = validationString.trim();
|
|
82
|
-
if (validationString.startsWith("["))
|
|
83
|
-
|
|
84
|
-
if (
|
|
87
|
+
if (validationString.startsWith("["))
|
|
88
|
+
validationString = validationString.substring(1);
|
|
89
|
+
if (validationString.endsWith("]"))
|
|
90
|
+
validationString = validationString.substring(0, validationString.length - 1);
|
|
91
|
+
if (!validationRulesRegex.test(validationString))
|
|
92
|
+
return "";
|
|
85
93
|
validationString = validationString.split(",").map((rule) => {
|
|
86
94
|
rule = rule.trim();
|
|
87
95
|
if (!rule.startsWith("rules.")) {
|
|
@@ -10,8 +10,8 @@ declare type VariableOptions = {
|
|
|
10
10
|
};
|
|
11
11
|
export declare function useGraphQl(): {
|
|
12
12
|
query: <T>(operation: string, fields: Array<string | Object> | ClassConstructor<any>, variables?: VariableOptions, cache?: boolean) => any;
|
|
13
|
-
queryPromise: <
|
|
14
|
-
mutation: <
|
|
15
|
-
mutationPromise: <
|
|
13
|
+
queryPromise: <T_1>(operation: string, fields: Array<string | Object> | ClassConstructor<any>, variables?: VariableOptions, cache?: boolean) => Promise<T_1>;
|
|
14
|
+
mutation: <T_2>(operation: string, fields: Array<string | Object> | ClassConstructor<any>, variables?: VariableOptions) => any;
|
|
15
|
+
mutationPromise: <T_3>(operation: string, fields: Array<string | Object> | ClassConstructor<any>, variables?: VariableOptions) => Promise<T_3>;
|
|
16
16
|
};
|
|
17
17
|
export {};
|
|
@@ -4,7 +4,8 @@ import { classAttributes, isClassConstructor } from "../utils/object.mjs";
|
|
|
4
4
|
import { gql, useAsyncQuery, useMutation } from "#imports";
|
|
5
5
|
export function useGraphQl() {
|
|
6
6
|
function query(operation, fields, variables, cache = false) {
|
|
7
|
-
if (isClassConstructor(fields))
|
|
7
|
+
if (isClassConstructor(fields))
|
|
8
|
+
fields = classAttributes(fields);
|
|
8
9
|
const builder = gqlQuery({ operation, fields, variables });
|
|
9
10
|
const options = {
|
|
10
11
|
query: gql(builder.query),
|
|
@@ -14,7 +15,8 @@ export function useGraphQl() {
|
|
|
14
15
|
return useAsyncQuery(options);
|
|
15
16
|
}
|
|
16
17
|
function queryPromise(operation, fields, variables, cache = false) {
|
|
17
|
-
if (isClassConstructor(fields))
|
|
18
|
+
if (isClassConstructor(fields))
|
|
19
|
+
fields = classAttributes(fields);
|
|
18
20
|
const builder = gqlQuery({ operation, fields, variables });
|
|
19
21
|
return new Promise((resolve, reject) => {
|
|
20
22
|
const { onResult, onError } = useQuery(gql(builder.query), builder.variables, { fetchPolicy: cache ? "cache-first" : "no-cache" });
|
|
@@ -32,12 +34,14 @@ export function useGraphQl() {
|
|
|
32
34
|
});
|
|
33
35
|
}
|
|
34
36
|
function mutation(operation, fields, variables) {
|
|
35
|
-
if (isClassConstructor(fields))
|
|
37
|
+
if (isClassConstructor(fields))
|
|
38
|
+
fields = classAttributes(fields);
|
|
36
39
|
const builder = gqlMutation({ operation, fields, variables });
|
|
37
40
|
return useMutation(gql(builder.query), { variables: builder.variables });
|
|
38
41
|
}
|
|
39
42
|
function mutationPromise(operation, fields, variables) {
|
|
40
|
-
if (isClassConstructor(fields))
|
|
43
|
+
if (isClassConstructor(fields))
|
|
44
|
+
fields = classAttributes(fields);
|
|
41
45
|
const builder = gqlMutation({ operation, fields, variables });
|
|
42
46
|
return new Promise(async (resolve, reject) => {
|
|
43
47
|
const { mutate, error } = useMutation(gql(builder.query), { variables: builder.variables });
|
|
@@ -21,7 +21,8 @@ export function routeToMenuItem(route) {
|
|
|
21
21
|
const menuItems = new Array();
|
|
22
22
|
for (const children of route.children) {
|
|
23
23
|
const childMenuItem = routeToMenuItem(children);
|
|
24
|
-
if (childMenuItem)
|
|
24
|
+
if (childMenuItem)
|
|
25
|
+
menuItems.push(childMenuItem);
|
|
25
26
|
menuItem.menuItems = menuItems;
|
|
26
27
|
}
|
|
27
28
|
}
|
|
@@ -36,7 +37,8 @@ export function createMenu() {
|
|
|
36
37
|
if (paths.length == 2) {
|
|
37
38
|
if (route.path != "/login" && route.path != "/") {
|
|
38
39
|
const menuItem = routeToMenuItem(route);
|
|
39
|
-
if (menuItem)
|
|
40
|
+
if (menuItem)
|
|
41
|
+
menuAll.value.push(menuItem);
|
|
40
42
|
}
|
|
41
43
|
}
|
|
42
44
|
}
|
|
@@ -2,7 +2,8 @@ import { defineNuxtPlugin } from "nuxt/app";
|
|
|
2
2
|
import { useAuthentication } from "#imports";
|
|
3
3
|
export default defineNuxtPlugin(async (nuxtApp) => {
|
|
4
4
|
function permission(permissionId) {
|
|
5
|
-
if (!permissionId || useAuthentication()?.hasPermission(permissionId) === void 0)
|
|
5
|
+
if (!permissionId || useAuthentication()?.hasPermission(permissionId) === void 0)
|
|
6
|
+
return true;
|
|
6
7
|
return useAuthentication()?.hasPermission(permissionId);
|
|
7
8
|
}
|
|
8
9
|
nuxtApp.vueApp.config.globalProperties.$permission = permission;
|
|
@@ -14,7 +15,8 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
14
15
|
um();
|
|
15
16
|
});
|
|
16
17
|
}
|
|
17
|
-
if (el.parentElement)
|
|
18
|
+
if (el.parentElement)
|
|
19
|
+
el.parentElement.removeChild(el);
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
22
|
});
|
|
@@ -2,10 +2,13 @@ import { DateTime } from "luxon";
|
|
|
2
2
|
export const Datetime = () => ({
|
|
3
3
|
luxonDateTime: DateTime.invalid("Uninitialized"),
|
|
4
4
|
fromString: function(dateTime, formatDate, locale = "EN") {
|
|
5
|
-
if (!dateTime)
|
|
5
|
+
if (!dateTime)
|
|
6
|
+
return this.setDateTime(DateTime.invalid("No datetime string provided"), locale);
|
|
6
7
|
let inputDate = void 0;
|
|
7
|
-
if (formatDate)
|
|
8
|
-
|
|
8
|
+
if (formatDate)
|
|
9
|
+
inputDate = DateTime.fromFormat(dateTime, formatDate, { locale });
|
|
10
|
+
if (!inputDate?.isValid)
|
|
11
|
+
inputDate = DateTime.fromISO(dateTime, { locale });
|
|
9
12
|
if (!inputDate?.isValid) {
|
|
10
13
|
const formats = [
|
|
11
14
|
"ddMMyyyy",
|
|
@@ -23,15 +26,18 @@ export const Datetime = () => ({
|
|
|
23
26
|
];
|
|
24
27
|
for (let i = 0; i < formats.length; ++i) {
|
|
25
28
|
inputDate = DateTime.fromFormat(dateTime, formats[i], { locale });
|
|
26
|
-
if (inputDate.isValid)
|
|
29
|
+
if (inputDate.isValid)
|
|
30
|
+
break;
|
|
27
31
|
}
|
|
28
32
|
}
|
|
29
33
|
return this.setDateTime(inputDate, locale);
|
|
30
34
|
},
|
|
31
35
|
fromStringTime: function(time, formatTime, locale = "EN") {
|
|
32
|
-
if (!time)
|
|
36
|
+
if (!time)
|
|
37
|
+
return this.setDateTime(DateTime.invalid("No datetime string provided"), locale);
|
|
33
38
|
let inputTime = void 0;
|
|
34
|
-
if (formatTime)
|
|
39
|
+
if (formatTime)
|
|
40
|
+
inputTime = DateTime.fromFormat(time, formatTime, { locale });
|
|
35
41
|
if (!inputTime?.isValid) {
|
|
36
42
|
const formats = [
|
|
37
43
|
"HHmm",
|
|
@@ -43,10 +49,12 @@ export const Datetime = () => ({
|
|
|
43
49
|
];
|
|
44
50
|
for (let i = 0; i < formats.length; ++i) {
|
|
45
51
|
inputTime = DateTime.fromFormat(time, formats[i], { locale });
|
|
46
|
-
if (inputTime.isValid)
|
|
52
|
+
if (inputTime.isValid)
|
|
53
|
+
break;
|
|
47
54
|
}
|
|
48
55
|
}
|
|
49
|
-
if (!inputTime?.isValid)
|
|
56
|
+
if (!inputTime?.isValid)
|
|
57
|
+
inputTime = DateTime.fromISO(time, { locale });
|
|
50
58
|
return this.setDateTime(inputTime, locale);
|
|
51
59
|
},
|
|
52
60
|
fromISO: function(dateTime, locale = "EN") {
|
|
@@ -59,31 +67,45 @@ export const Datetime = () => ({
|
|
|
59
67
|
},
|
|
60
68
|
setDateTime: function(inputDate, locale = "EN") {
|
|
61
69
|
if (inputDate.isValid) {
|
|
62
|
-
if (inputDate.year - DateTime.now().year > 300)
|
|
70
|
+
if (inputDate.year - DateTime.now().year > 300)
|
|
71
|
+
inputDate = inputDate.set({ year: inputDate.year - 543 });
|
|
63
72
|
}
|
|
64
73
|
this.luxonDateTime = inputDate;
|
|
65
|
-
if (locale == "TH")
|
|
66
|
-
|
|
74
|
+
if (locale == "TH")
|
|
75
|
+
this.luxonDateTime = this.luxonDateTime.setLocale(locale).reconfigure({ outputCalendar: "buddhist" });
|
|
76
|
+
else
|
|
77
|
+
this.luxonDateTime = this.luxonDateTime.setLocale(locale).reconfigure({ outputCalendar: "gregory" });
|
|
67
78
|
return this;
|
|
68
79
|
},
|
|
69
80
|
toFormat: function(format, locale = "EN") {
|
|
70
|
-
if (format == "tinyDate")
|
|
71
|
-
|
|
72
|
-
if (format == "
|
|
73
|
-
|
|
74
|
-
if (format == "
|
|
75
|
-
|
|
76
|
-
if (format == "
|
|
77
|
-
|
|
78
|
-
|
|
81
|
+
if (format == "tinyDate")
|
|
82
|
+
return this.toTinyDate(locale);
|
|
83
|
+
if (format == "monthShortDate")
|
|
84
|
+
return this.toMonthShortDate(locale);
|
|
85
|
+
if (format == "shortDate")
|
|
86
|
+
return this.toShortDate(locale);
|
|
87
|
+
if (format == "longDate")
|
|
88
|
+
return this.toLongDate(locale);
|
|
89
|
+
if (format == "tinyDateTime")
|
|
90
|
+
return this.toTinyDateTime(locale);
|
|
91
|
+
if (format == "shortDateTime")
|
|
92
|
+
return this.toShortDateTime(locale);
|
|
93
|
+
if (format == "longDateTime")
|
|
94
|
+
return this.toLongDateTime(locale);
|
|
95
|
+
if (locale == "TH")
|
|
96
|
+
this.luxonDateTime = this.luxonDateTime.setLocale(locale).reconfigure({ outputCalendar: "buddhist" });
|
|
97
|
+
else
|
|
98
|
+
this.luxonDateTime = this.luxonDateTime.setLocale(locale).reconfigure({ outputCalendar: "gregory" });
|
|
79
99
|
return this.luxonDateTime.toFormat(format);
|
|
80
100
|
},
|
|
81
101
|
toISO: function() {
|
|
82
102
|
return this.luxonDateTime.toISO();
|
|
83
103
|
},
|
|
84
104
|
toLocaleFormat: function(locale, format) {
|
|
85
|
-
if (locale == "TH")
|
|
86
|
-
|
|
105
|
+
if (locale == "TH")
|
|
106
|
+
this.luxonDateTime = this.luxonDateTime.setLocale(locale).reconfigure({ outputCalendar: "buddhist" });
|
|
107
|
+
else
|
|
108
|
+
this.luxonDateTime = this.luxonDateTime.setLocale(locale).reconfigure({ outputCalendar: "gregory" });
|
|
87
109
|
return this.luxonDateTime.setLocale(locale).toLocaleString(format);
|
|
88
110
|
},
|
|
89
111
|
toTinyDate: function(locale = "EN") {
|
|
@@ -11,7 +11,8 @@ export function classAttributes(cls) {
|
|
|
11
11
|
export function onlyAttributes(keys, object) {
|
|
12
12
|
const returnObject = cloneDeep(object);
|
|
13
13
|
Object.keys(object).forEach((localKey) => {
|
|
14
|
-
if (!keys.includes(localKey))
|
|
14
|
+
if (!keys.includes(localKey))
|
|
15
|
+
delete returnObject[localKey];
|
|
15
16
|
});
|
|
16
17
|
return returnObject;
|
|
17
18
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ramathibodi/nuxt-commons",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Ramathibodi Nuxt modules for common components",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -67,6 +67,7 @@
|
|
|
67
67
|
"vue": "^3.4.25",
|
|
68
68
|
"vue-signature-pad": "^3.0.2",
|
|
69
69
|
"vuetify": "^3.6.8",
|
|
70
|
+
"prettier": "3.3.2",
|
|
70
71
|
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.0/xlsx-0.20.0.tgz"
|
|
71
72
|
},
|
|
72
73
|
"devDependencies": {
|
|
@@ -84,7 +85,6 @@
|
|
|
84
85
|
"eslint": "^9.1.1",
|
|
85
86
|
"nuxt": "^3.11.2",
|
|
86
87
|
"nuxt-lodash": "^2.5.3",
|
|
87
|
-
"prettier": "3.3.2",
|
|
88
88
|
"sass": "^1.75.0",
|
|
89
89
|
"typescript": "^5.4.5",
|
|
90
90
|
"vitest": "^1.5.1",
|