initial public commit

This commit is contained in:
Gani Georgiev
2022-07-07 00:19:05 +03:00
commit 3d07f0211d
484 changed files with 92412 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
import { writable } from "svelte/store";
// logged app admin
export const admin = writable({});
export function setAdmin(model) {
admin.set(model || {});
}
+68
View File
@@ -0,0 +1,68 @@
import { writable } from "svelte/store";
import ApiClient from "@/utils/ApiClient";
import CommonHelper from "@/utils/CommonHelper";
export const collections = writable([]);
export const activeCollection = writable({});
export const isCollectionsLoading = writable(false);
// add or update collection
export function addCollection(collection) {
activeCollection.update((current) => {
return CommonHelper.isEmpty(current?.id) || current.id === collection.id ? collection : current;
});
collections.update((list) => {
CommonHelper.pushOrReplaceByKey(list, collection, "id");
return list;
});
}
export function removeCollection(collection) {
collections.update((list) => {
CommonHelper.removeByKey(list, "id", collection.id);
activeCollection.update((current) => {
if (current.id === collection.id) {
// fallback to the first non-profile collection item
return list.find((c) => c.name != import.meta.env.PB_PROFILE_COLLECTION) || {}
}
return current;
});
return list;
});
}
// load all collections (excluding the user profile)
export async function loadCollections(activeId = null) {
isCollectionsLoading.set(true);
activeCollection.set({});
collections.set([]);
return ApiClient.Collections.getFullList(200, {
"sort": "+created",
})
.then((items) => {
collections.set(items);
const item = activeId && CommonHelper.findByKey(items, "id", activeId);
if (item) {
activeCollection.set(item);
} else if (items.length) {
// fallback to the first non-profile collection item
const nonProfile = items.find((c) => c.name != import.meta.env.PB_PROFILE_COLLECTION)
if (nonProfile) {
activeCollection.set(nonProfile);
}
}
})
.catch((err) => {
ApiClient.errorResponseHandler(err);
})
.finally(() => {
isCollectionsLoading.set(false);
});
}
+26
View File
@@ -0,0 +1,26 @@
import { writable } from "svelte/store";
// eg.
// {
// "text": "Do you really want to delete the selectedItem",
// "yesCallback": function() {...},
// "noCallback": function() {...},
// }
export const confirmation = writable({});
/**
* @param {String} text
* @param {Function} [yesCallback]
* @param {Function} [noCallback]
*/
export function confirm(text, yesCallback, noCallback) {
confirmation.set({
text: text,
yesCallback: yesCallback,
noCallback: noCallback,
});
}
export function resetConfirmation() {
confirmation.set({});
}
+32
View File
@@ -0,0 +1,32 @@
import { writable } from "svelte/store";
import CommonHelper from "@/utils/CommonHelper";
export const errors = writable({});
/**
* @param {Object} newErrors
*/
export function setErrors(newErrors) {
errors.set(newErrors || {});
}
/**
* @param {String} name
* @param {String|Array} message
*/
export function addError(name, message) {
errors.update((e) => {
CommonHelper.setByPath(e, name, CommonHelper.sentenize(message))
return e;
});
}
/**
* @param {String} name
*/
export function removeError(name) {
errors.update((e) => {
CommonHelper.deleteByPath(e, name);
return e;
});
}
+76
View File
@@ -0,0 +1,76 @@
import { writable } from "svelte/store";
import CommonHelper from "@/utils/CommonHelper";
export const toasts = writable([]);
export function addInfoToast(message, duration = 4000) {
return addToast(message, "info", duration);
}
export function addSuccessToast(message, duration = 3000) {
return addToast(message, "success", duration);
}
export function addErrorToast(message, duration = 4500) {
return addToast(message, "error", duration);
}
export function addWarningToast(message, duration = 4500) {
return addToast(message, "warning", duration);
}
export function addToast(message, type, duration) {
duration = duration || 4000;
const toast = {
message: message,
type: type,
duration: duration,
timeout: setTimeout(() => {
removeToast(toast);
}, duration)
};
toasts.update((t) => {
removeToastFromArray(t, toast.message)
CommonHelper.pushOrReplaceByKey(t, toast, "message");
return t;
});
}
export function removeToast(messageOrToast) {
toasts.update((t) => {
removeToastFromArray(t, messageOrToast);
return t;
});
}
export function removeAll() {
toasts.update((t) => {
for (let toast of t) {
removeToastFromArray(t, toast);
}
return [];
});
}
// Internal toast removal method (usually used to delete previous duplicated toasts).
// NB! This doesn't update the store value! Use `removeToast()` instead.
function removeToastFromArray(arr, messageOrToast) {
let toast;
if (typeof messageOrToast == "string") {
toast = CommonHelper.findByKey(arr, "message", messageOrToast);
} else {
toast = messageOrToast;
}
if (!toast) {
return;
}
clearTimeout(toast.timeout);
CommonHelper.removeByKey(arr, "message", toast.message);
}