2022-07-07 05:19:05 +08:00
|
|
|
<script>
|
2022-07-18 05:16:09 +08:00
|
|
|
import { createEventDispatcher, tick } from "svelte";
|
2023-04-17 17:43:48 +08:00
|
|
|
import { slide } from "svelte/transition";
|
2022-07-07 05:19:05 +08:00
|
|
|
import CommonHelper from "@/utils/CommonHelper";
|
2023-08-30 03:10:57 +08:00
|
|
|
import { ClientResponseError } from "pocketbase";
|
2022-07-07 05:19:05 +08:00
|
|
|
import ApiClient from "@/utils/ApiClient";
|
2022-10-30 16:28:14 +08:00
|
|
|
import tooltip from "@/actions/tooltip";
|
2022-07-07 05:19:05 +08:00
|
|
|
import { setErrors } from "@/stores/errors";
|
|
|
|
import { confirm } from "@/stores/confirmation";
|
2023-10-01 17:53:26 +08:00
|
|
|
import { addSuccessToast, addErrorToast } from "@/stores/toasts";
|
2022-07-07 05:19:05 +08:00
|
|
|
import Field from "@/components/base/Field.svelte";
|
|
|
|
import Toggler from "@/components/base/Toggler.svelte";
|
2023-09-30 16:58:14 +08:00
|
|
|
import ModelDateIcon from "@/components/base/ModelDateIcon.svelte";
|
2022-07-07 05:19:05 +08:00
|
|
|
import OverlayPanel from "@/components/base/OverlayPanel.svelte";
|
2022-10-30 16:28:14 +08:00
|
|
|
import AuthFields from "@/components/records/fields/AuthFields.svelte";
|
2022-07-07 05:19:05 +08:00
|
|
|
import TextField from "@/components/records/fields/TextField.svelte";
|
|
|
|
import NumberField from "@/components/records/fields/NumberField.svelte";
|
|
|
|
import BoolField from "@/components/records/fields/BoolField.svelte";
|
|
|
|
import EmailField from "@/components/records/fields/EmailField.svelte";
|
|
|
|
import UrlField from "@/components/records/fields/UrlField.svelte";
|
|
|
|
import DateField from "@/components/records/fields/DateField.svelte";
|
|
|
|
import SelectField from "@/components/records/fields/SelectField.svelte";
|
|
|
|
import JsonField from "@/components/records/fields/JsonField.svelte";
|
|
|
|
import FileField from "@/components/records/fields/FileField.svelte";
|
|
|
|
import RelationField from "@/components/records/fields/RelationField.svelte";
|
2023-01-17 19:31:48 +08:00
|
|
|
import EditorField from "@/components/records/fields/EditorField.svelte";
|
2022-10-30 16:28:14 +08:00
|
|
|
import ExternalAuthsList from "@/components/records/ExternalAuthsList.svelte";
|
2022-07-07 05:19:05 +08:00
|
|
|
|
|
|
|
const dispatch = createEventDispatcher();
|
|
|
|
const formId = "record_" + CommonHelper.randomString(5);
|
2023-04-17 17:43:48 +08:00
|
|
|
const tabFormKey = "form";
|
|
|
|
const tabProviderKey = "providers";
|
2022-07-07 05:19:05 +08:00
|
|
|
|
|
|
|
export let collection;
|
|
|
|
|
|
|
|
let recordPanel;
|
|
|
|
let original = null;
|
2023-04-17 17:43:48 +08:00
|
|
|
let record = null;
|
|
|
|
let initialDraft = null;
|
2022-07-07 05:19:05 +08:00
|
|
|
let isSaving = false;
|
2023-10-01 17:53:26 +08:00
|
|
|
let confirmHide = false; // prevent close recursion
|
2022-07-07 05:19:05 +08:00
|
|
|
let uploadedFilesMap = {}; // eg.: {"field1":[File1, File2], ...}
|
2023-05-18 04:17:45 +08:00
|
|
|
let deletedFileNamesMap = {}; // eg.: {"field1":[0, 1], ...}
|
2023-10-01 17:53:26 +08:00
|
|
|
let originalSerializedData = JSON.stringify(original);
|
2023-04-17 17:43:48 +08:00
|
|
|
let serializedData = originalSerializedData;
|
|
|
|
let activeTab = tabFormKey;
|
2023-03-21 23:55:12 +08:00
|
|
|
let isNew = true;
|
2023-10-01 17:53:26 +08:00
|
|
|
let isLoading = true;
|
2022-07-07 05:19:05 +08:00
|
|
|
|
2023-08-15 02:20:49 +08:00
|
|
|
$: isAuthCollection = collection?.type === "auth";
|
|
|
|
|
2023-01-25 02:58:24 +08:00
|
|
|
$: hasEditorField = !!collection?.schema?.find((f) => f.type === "editor");
|
|
|
|
|
2022-07-07 05:19:05 +08:00
|
|
|
$: hasFileChanges =
|
2023-05-18 04:17:45 +08:00
|
|
|
CommonHelper.hasNonEmptyProps(uploadedFilesMap) || CommonHelper.hasNonEmptyProps(deletedFileNamesMap);
|
2022-07-07 05:19:05 +08:00
|
|
|
|
2023-04-17 17:43:48 +08:00
|
|
|
$: serializedData = JSON.stringify(record);
|
|
|
|
|
|
|
|
$: hasChanges = hasFileChanges || originalSerializedData != serializedData;
|
2022-07-07 05:19:05 +08:00
|
|
|
|
2023-08-15 02:20:49 +08:00
|
|
|
$: isNew = !original || !original.id;
|
2023-03-21 23:55:12 +08:00
|
|
|
|
2023-10-01 17:53:26 +08:00
|
|
|
$: canSave = !isLoading && (isNew || hasChanges);
|
2022-07-07 05:19:05 +08:00
|
|
|
|
2023-10-01 17:53:26 +08:00
|
|
|
$: if (!isLoading) {
|
2023-04-17 17:43:48 +08:00
|
|
|
updateDraft(serializedData);
|
|
|
|
}
|
|
|
|
|
2022-07-07 05:19:05 +08:00
|
|
|
export function show(model) {
|
|
|
|
load(model);
|
|
|
|
|
2023-10-01 17:53:26 +08:00
|
|
|
confirmHide = true;
|
2022-07-07 05:19:05 +08:00
|
|
|
|
2023-04-17 17:43:48 +08:00
|
|
|
activeTab = tabFormKey;
|
2022-10-30 16:28:14 +08:00
|
|
|
|
2023-04-15 16:23:59 +08:00
|
|
|
return recordPanel?.show();
|
2022-07-07 05:19:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function hide() {
|
2023-04-15 16:23:59 +08:00
|
|
|
return recordPanel?.hide();
|
2022-07-07 05:19:05 +08:00
|
|
|
}
|
|
|
|
|
2023-10-01 17:53:26 +08:00
|
|
|
function forceHide() {
|
|
|
|
confirmHide = false;
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function resolveModel(model) {
|
|
|
|
if (model && typeof model === "string") {
|
|
|
|
// load from id
|
|
|
|
try {
|
|
|
|
return await ApiClient.collection(collection.id).getOne(model);
|
|
|
|
} catch (err) {
|
|
|
|
if (!err.isAbort) {
|
|
|
|
forceHide();
|
|
|
|
console.warn("resolveModel:", err);
|
|
|
|
addErrorToast(`Unable to load record with id "${model}"`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
2022-07-18 05:16:09 +08:00
|
|
|
async function load(model) {
|
2023-10-01 17:53:26 +08:00
|
|
|
isLoading = true;
|
|
|
|
|
|
|
|
// resets
|
|
|
|
setErrors({});
|
2022-07-07 05:19:05 +08:00
|
|
|
uploadedFilesMap = {};
|
2023-05-18 04:17:45 +08:00
|
|
|
deletedFileNamesMap = {};
|
2023-04-17 17:43:48 +08:00
|
|
|
|
2023-10-01 17:53:26 +08:00
|
|
|
// load the minimum model data if possible to minimize layout shifts
|
|
|
|
original =
|
|
|
|
typeof model === "string"
|
|
|
|
? { id: model, collectionId: collection?.id, collectionName: collection?.name }
|
|
|
|
: model || {};
|
|
|
|
record = structuredClone(original);
|
|
|
|
|
|
|
|
// resolve the complete model
|
|
|
|
original = (await resolveModel(model)) || {};
|
|
|
|
record = structuredClone(original);
|
|
|
|
|
2023-04-17 17:43:48 +08:00
|
|
|
// wait to populate the fields to get the normalized values
|
|
|
|
await tick();
|
|
|
|
|
|
|
|
initialDraft = getDraft();
|
|
|
|
if (!initialDraft || areRecordsEqual(record, initialDraft)) {
|
|
|
|
initialDraft = null;
|
2023-04-17 18:28:41 +08:00
|
|
|
} else {
|
|
|
|
delete initialDraft.password;
|
|
|
|
delete initialDraft.passwordConfirm;
|
2023-04-17 17:43:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
originalSerializedData = JSON.stringify(record);
|
2023-10-01 17:53:26 +08:00
|
|
|
|
|
|
|
isLoading = false;
|
2023-04-17 17:43:48 +08:00
|
|
|
}
|
|
|
|
|
2023-04-20 19:30:29 +08:00
|
|
|
async function replaceOriginal(newOriginal) {
|
|
|
|
setErrors({}); // reset errors
|
2023-08-15 02:20:49 +08:00
|
|
|
original = newOriginal || {};
|
2023-04-19 15:33:24 +08:00
|
|
|
uploadedFilesMap = {};
|
2023-05-18 04:17:45 +08:00
|
|
|
deletedFileNamesMap = {};
|
2023-04-19 15:33:24 +08:00
|
|
|
|
|
|
|
// to avoid layout shifts we replace only the file and non-schema fields
|
|
|
|
const skipFields = collection?.schema?.filter((f) => f.type != "file")?.map((f) => f.name) || [];
|
2023-08-15 02:20:49 +08:00
|
|
|
for (let k in newOriginal) {
|
2023-04-19 15:33:24 +08:00
|
|
|
if (skipFields.includes(k)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
record[k] = newOriginal[k];
|
|
|
|
}
|
|
|
|
|
2023-04-20 19:30:29 +08:00
|
|
|
// wait to populate the fields to get the normalized values
|
|
|
|
await tick();
|
|
|
|
|
2023-04-19 15:33:24 +08:00
|
|
|
originalSerializedData = JSON.stringify(record);
|
|
|
|
|
|
|
|
deleteDraft();
|
|
|
|
}
|
|
|
|
|
2023-04-17 17:43:48 +08:00
|
|
|
function draftKey() {
|
|
|
|
return "record_draft_" + (collection?.id || "") + "_" + (original?.id || "");
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDraft(fallbackRecord) {
|
|
|
|
try {
|
|
|
|
const raw = window.localStorage.getItem(draftKey());
|
|
|
|
if (raw) {
|
2023-09-10 15:46:19 +08:00
|
|
|
return JSON.parse(raw);
|
2023-04-17 17:43:48 +08:00
|
|
|
}
|
|
|
|
} catch (_) {}
|
|
|
|
|
|
|
|
return fallbackRecord;
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateDraft(newSerializedData) {
|
2023-09-06 19:11:58 +08:00
|
|
|
try {
|
|
|
|
window.localStorage.setItem(draftKey(), newSerializedData);
|
|
|
|
} catch (e) {
|
|
|
|
// ignore local storage errors in case the serialized data
|
|
|
|
// exceed the browser localStorage single value quota
|
2023-09-10 15:46:19 +08:00
|
|
|
console.warn("updateDraft failure:", e);
|
2023-09-06 19:11:58 +08:00
|
|
|
}
|
2022-07-07 05:19:05 +08:00
|
|
|
}
|
|
|
|
|
2023-04-17 17:43:48 +08:00
|
|
|
function restoreDraft() {
|
|
|
|
if (initialDraft) {
|
|
|
|
record = initialDraft;
|
|
|
|
initialDraft = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function areRecordsEqual(recordA, recordB) {
|
2023-08-15 02:20:49 +08:00
|
|
|
const cloneA = structuredClone(recordA || {});
|
|
|
|
const cloneB = structuredClone(recordB || {});
|
2023-04-17 17:43:48 +08:00
|
|
|
|
|
|
|
const fileFields = collection?.schema?.filter((f) => f.type === "file");
|
|
|
|
for (let field of fileFields) {
|
2023-08-15 02:20:49 +08:00
|
|
|
delete cloneA[field.name];
|
|
|
|
delete cloneB[field.name];
|
2023-04-17 17:43:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete password props
|
2023-08-15 02:20:49 +08:00
|
|
|
delete cloneA.password;
|
|
|
|
delete cloneA.passwordConfirm;
|
|
|
|
delete cloneB.password;
|
|
|
|
delete cloneB.passwordConfirm;
|
2023-04-17 17:43:48 +08:00
|
|
|
|
|
|
|
return JSON.stringify(cloneA) == JSON.stringify(cloneB);
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteDraft() {
|
|
|
|
initialDraft = null;
|
|
|
|
window.localStorage.removeItem(draftKey());
|
2022-07-07 05:19:05 +08:00
|
|
|
}
|
|
|
|
|
2023-08-30 03:10:57 +08:00
|
|
|
async function save(hidePanel = true) {
|
2022-10-30 16:28:14 +08:00
|
|
|
if (isSaving || !canSave || !collection?.id) {
|
2022-07-07 05:19:05 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isSaving = true;
|
|
|
|
|
2023-08-30 03:10:57 +08:00
|
|
|
try {
|
|
|
|
const data = exportFormData();
|
2022-07-07 05:19:05 +08:00
|
|
|
|
2023-08-30 03:10:57 +08:00
|
|
|
let result;
|
|
|
|
if (isNew) {
|
|
|
|
result = await ApiClient.collection(collection.id).create(data);
|
|
|
|
} else {
|
|
|
|
result = await ApiClient.collection(collection.id).update(record.id, data);
|
|
|
|
}
|
2022-07-07 05:19:05 +08:00
|
|
|
|
2023-08-30 03:10:57 +08:00
|
|
|
addSuccessToast(isNew ? "Successfully created record." : "Successfully updated record.");
|
2023-04-19 15:33:24 +08:00
|
|
|
|
2023-08-30 03:10:57 +08:00
|
|
|
deleteDraft();
|
2023-04-19 15:33:24 +08:00
|
|
|
|
2023-08-30 03:10:57 +08:00
|
|
|
if (hidePanel) {
|
2023-10-01 17:53:26 +08:00
|
|
|
forceHide();
|
2023-08-30 03:10:57 +08:00
|
|
|
} else {
|
|
|
|
replaceOriginal(result);
|
|
|
|
}
|
2023-04-19 15:33:24 +08:00
|
|
|
|
2023-09-24 16:05:12 +08:00
|
|
|
dispatch("save", {
|
|
|
|
isNew: isNew,
|
|
|
|
record: result,
|
|
|
|
});
|
2023-08-30 03:10:57 +08:00
|
|
|
} catch (err) {
|
|
|
|
ApiClient.error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
isSaving = false;
|
2022-07-07 05:19:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function deleteConfirm() {
|
|
|
|
if (!original?.id) {
|
|
|
|
return; // nothing to delete
|
|
|
|
}
|
|
|
|
|
|
|
|
confirm(`Do you really want to delete the selected record?`, () => {
|
2022-10-30 16:28:14 +08:00
|
|
|
return ApiClient.collection(original.collectionId)
|
|
|
|
.delete(original.id)
|
2022-07-07 05:19:05 +08:00
|
|
|
.then(() => {
|
|
|
|
hide();
|
|
|
|
addSuccessToast("Successfully deleted record.");
|
|
|
|
dispatch("delete", original);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2023-05-14 03:10:14 +08:00
|
|
|
ApiClient.error(err);
|
2022-07-07 05:19:05 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function exportFormData() {
|
2023-08-15 02:20:49 +08:00
|
|
|
const data = structuredClone(record || {});
|
2022-07-07 05:19:05 +08:00
|
|
|
const formData = new FormData();
|
|
|
|
|
2023-03-21 23:55:12 +08:00
|
|
|
const exportableFields = {
|
|
|
|
id: data.id,
|
|
|
|
};
|
|
|
|
|
2023-08-30 03:10:57 +08:00
|
|
|
const jsonFields = {};
|
|
|
|
|
2022-07-07 05:19:05 +08:00
|
|
|
for (const field of collection?.schema || []) {
|
2022-10-30 16:28:14 +08:00
|
|
|
exportableFields[field.name] = true;
|
2023-08-30 03:10:57 +08:00
|
|
|
|
|
|
|
if (field.type == "json") {
|
|
|
|
jsonFields[field.name] = true;
|
|
|
|
}
|
2022-10-30 16:28:14 +08:00
|
|
|
}
|
|
|
|
|
2023-08-15 02:20:49 +08:00
|
|
|
if (isAuthCollection) {
|
2022-10-30 16:28:14 +08:00
|
|
|
exportableFields["username"] = true;
|
|
|
|
exportableFields["email"] = true;
|
|
|
|
exportableFields["emailVisibility"] = true;
|
|
|
|
exportableFields["password"] = true;
|
|
|
|
exportableFields["passwordConfirm"] = true;
|
|
|
|
exportableFields["verified"] = true;
|
2022-07-07 05:19:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// export base fields
|
|
|
|
for (const key in data) {
|
|
|
|
// skip non-schema fields
|
2022-10-30 16:28:14 +08:00
|
|
|
if (!exportableFields[key]) {
|
2022-07-07 05:19:05 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalize nullable values
|
|
|
|
if (typeof data[key] === "undefined") {
|
|
|
|
data[key] = null;
|
|
|
|
}
|
|
|
|
|
2023-08-30 03:10:57 +08:00
|
|
|
// "validate" json fields
|
|
|
|
if (jsonFields[key] && data[key] !== "") {
|
|
|
|
try {
|
|
|
|
JSON.parse(data[key]);
|
|
|
|
} catch (err) {
|
|
|
|
const fieldErr = {};
|
|
|
|
fieldErr[key] = {
|
|
|
|
code: "invalid_json",
|
|
|
|
message: err.toString(),
|
|
|
|
};
|
|
|
|
// emulate server error
|
|
|
|
throw new ClientResponseError({
|
|
|
|
status: 400,
|
|
|
|
response: {
|
|
|
|
data: fieldErr,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-07 05:19:05 +08:00
|
|
|
CommonHelper.addValueToFormData(formData, key, data[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add uploaded files (if any)
|
|
|
|
for (const key in uploadedFilesMap) {
|
|
|
|
const files = CommonHelper.toArray(uploadedFilesMap[key]);
|
|
|
|
for (const file of files) {
|
|
|
|
formData.append(key, file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// unset deleted files (if any)
|
2023-05-18 04:17:45 +08:00
|
|
|
for (const key in deletedFileNamesMap) {
|
|
|
|
const names = CommonHelper.toArray(deletedFileNamesMap[key]);
|
|
|
|
for (const name of names) {
|
|
|
|
formData.append(key + "." + name, "");
|
2022-07-07 05:19:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return formData;
|
|
|
|
}
|
2022-10-30 16:28:14 +08:00
|
|
|
|
|
|
|
function sendVerificationEmail() {
|
|
|
|
if (!collection?.id || !original?.email) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
confirm(`Do you really want to sent verification email to ${original.email}?`, () => {
|
|
|
|
return ApiClient.collection(collection.id)
|
|
|
|
.requestVerification(original.email)
|
|
|
|
.then(() => {
|
|
|
|
addSuccessToast(`Successfully sent verification email to ${original.email}.`);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2023-05-14 03:10:14 +08:00
|
|
|
ApiClient.error(err);
|
2022-10-30 16:28:14 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendPasswordResetEmail() {
|
|
|
|
if (!collection?.id || !original?.email) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
confirm(`Do you really want to sent password reset email to ${original.email}?`, () => {
|
|
|
|
return ApiClient.collection(collection.id)
|
|
|
|
.requestPasswordReset(original.email)
|
|
|
|
.then(() => {
|
|
|
|
addSuccessToast(`Successfully sent password reset email to ${original.email}.`);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2023-05-14 03:10:14 +08:00
|
|
|
ApiClient.error(err);
|
2022-10-30 16:28:14 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2023-01-25 04:30:42 +08:00
|
|
|
|
|
|
|
function duplicateConfirm() {
|
|
|
|
if (hasChanges) {
|
|
|
|
confirm("You have unsaved changes. Do you really want to discard them?", () => {
|
|
|
|
duplicate();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
duplicate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function duplicate() {
|
2023-08-15 02:20:49 +08:00
|
|
|
let clone = original ? structuredClone(original) : null;
|
2023-01-25 04:30:42 +08:00
|
|
|
|
|
|
|
if (clone) {
|
|
|
|
clone.id = "";
|
|
|
|
clone.created = "";
|
|
|
|
clone.updated = "";
|
|
|
|
|
|
|
|
// reset file fields
|
|
|
|
const fields = collection?.schema || [];
|
|
|
|
for (const field of fields) {
|
|
|
|
if (field.type === "file") {
|
|
|
|
delete clone[field.name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 19:35:49 +08:00
|
|
|
deleteDraft();
|
2023-01-25 04:30:42 +08:00
|
|
|
show(clone);
|
|
|
|
|
|
|
|
await tick();
|
|
|
|
|
2023-04-17 17:43:48 +08:00
|
|
|
originalSerializedData = "";
|
2023-01-25 04:30:42 +08:00
|
|
|
}
|
2023-04-17 19:35:49 +08:00
|
|
|
|
|
|
|
function handleFormKeydown(e) {
|
2023-04-20 19:30:29 +08:00
|
|
|
if ((e.ctrlKey || e.metaKey) && e.code == "KeyS") {
|
2023-04-17 19:35:49 +08:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
save(false);
|
|
|
|
}
|
|
|
|
}
|
2022-07-07 05:19:05 +08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<OverlayPanel
|
|
|
|
bind:this={recordPanel}
|
2023-01-25 02:58:24 +08:00
|
|
|
class="
|
|
|
|
record-panel
|
|
|
|
{hasEditorField ? 'overlay-panel-xl' : 'overlay-panel-lg'}
|
2023-08-15 02:20:49 +08:00
|
|
|
{isAuthCollection && !isNew ? 'colored-header' : ''}
|
2023-01-25 02:58:24 +08:00
|
|
|
"
|
2023-10-01 17:53:26 +08:00
|
|
|
btnClose={!isLoading}
|
|
|
|
escClose={!isLoading}
|
|
|
|
overlayClose={!isLoading}
|
2022-07-07 05:19:05 +08:00
|
|
|
beforeHide={() => {
|
2023-10-01 17:53:26 +08:00
|
|
|
if (hasChanges && confirmHide) {
|
2022-07-07 05:19:05 +08:00
|
|
|
confirm("You have unsaved changes. Do you really want to close the panel?", () => {
|
2023-10-01 17:53:26 +08:00
|
|
|
forceHide();
|
2022-07-07 05:19:05 +08:00
|
|
|
});
|
2023-04-17 17:43:48 +08:00
|
|
|
|
2022-07-07 05:19:05 +08:00
|
|
|
return false;
|
|
|
|
}
|
2023-04-17 17:43:48 +08:00
|
|
|
|
2022-10-30 16:28:14 +08:00
|
|
|
setErrors({});
|
2023-04-17 17:43:48 +08:00
|
|
|
deleteDraft();
|
|
|
|
|
2022-07-07 05:19:05 +08:00
|
|
|
return true;
|
|
|
|
}}
|
|
|
|
on:hide
|
|
|
|
on:show
|
|
|
|
>
|
|
|
|
<svelte:fragment slot="header">
|
2023-10-01 17:53:26 +08:00
|
|
|
{#if isLoading}
|
|
|
|
<span class="loader loader-sm" />
|
|
|
|
<h4 class="panel-title txt-hint">Loading...</h4>
|
|
|
|
{:else}
|
|
|
|
<h4 class="panel-title">
|
|
|
|
{isNew ? "New" : "Edit"}
|
|
|
|
<strong>{collection?.name}</strong> record
|
|
|
|
</h4>
|
|
|
|
{/if}
|
2022-07-07 05:19:05 +08:00
|
|
|
|
2023-03-21 23:55:12 +08:00
|
|
|
{#if !isNew}
|
2022-07-07 05:19:05 +08:00
|
|
|
<div class="flex-fill" />
|
2023-01-29 02:13:15 +08:00
|
|
|
<button type="button" aria-label="More" class="btn btn-sm btn-circle btn-transparent flex-gap-0">
|
|
|
|
<i class="ri-more-line" />
|
|
|
|
<Toggler class="dropdown dropdown-right dropdown-nowrap">
|
2023-08-15 02:20:49 +08:00
|
|
|
{#if isAuthCollection && !original.verified && original.email}
|
2023-01-25 04:30:42 +08:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="dropdown-item closable"
|
2023-01-29 02:13:15 +08:00
|
|
|
on:click={() => sendVerificationEmail()}
|
2023-01-25 04:30:42 +08:00
|
|
|
>
|
2023-01-29 02:13:15 +08:00
|
|
|
<i class="ri-mail-check-line" />
|
|
|
|
<span class="txt">Send verification email</span>
|
2023-01-25 04:30:42 +08:00
|
|
|
</button>
|
2023-01-29 02:13:15 +08:00
|
|
|
{/if}
|
2023-08-15 02:20:49 +08:00
|
|
|
{#if isAuthCollection && original.email}
|
2022-10-30 16:28:14 +08:00
|
|
|
<button
|
|
|
|
type="button"
|
2023-01-29 02:13:15 +08:00
|
|
|
class="dropdown-item closable"
|
|
|
|
on:click={() => sendPasswordResetEmail()}
|
2022-10-30 16:28:14 +08:00
|
|
|
>
|
2023-01-29 02:13:15 +08:00
|
|
|
<i class="ri-mail-lock-line" />
|
|
|
|
<span class="txt">Send password reset email</span>
|
2022-10-30 16:28:14 +08:00
|
|
|
</button>
|
2023-01-29 02:13:15 +08:00
|
|
|
{/if}
|
|
|
|
<button type="button" class="dropdown-item closable" on:click={() => duplicateConfirm()}>
|
|
|
|
<i class="ri-file-copy-line" />
|
|
|
|
<span class="txt">Duplicate</span>
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="dropdown-item txt-danger closable"
|
|
|
|
on:click|preventDefault|stopPropagation={() => deleteConfirm()}
|
|
|
|
>
|
|
|
|
<i class="ri-delete-bin-7-line" />
|
|
|
|
<span class="txt">Delete</span>
|
|
|
|
</button>
|
|
|
|
</Toggler>
|
2022-07-07 05:19:05 +08:00
|
|
|
</button>
|
|
|
|
{/if}
|
|
|
|
|
2023-08-15 02:20:49 +08:00
|
|
|
{#if isAuthCollection && !isNew}
|
2022-10-30 16:28:14 +08:00
|
|
|
<div class="tabs-header stretched">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="tab-item"
|
2023-04-17 17:43:48 +08:00
|
|
|
class:active={activeTab === tabFormKey}
|
|
|
|
on:click={() => (activeTab = tabFormKey)}
|
2022-10-30 16:28:14 +08:00
|
|
|
>
|
|
|
|
Account
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="tab-item"
|
2023-04-17 17:43:48 +08:00
|
|
|
class:active={activeTab === tabProviderKey}
|
|
|
|
on:click={() => (activeTab = tabProviderKey)}
|
2022-10-30 16:28:14 +08:00
|
|
|
>
|
|
|
|
Authorized providers
|
|
|
|
</button>
|
|
|
|
</div>
|
2022-07-07 05:19:05 +08:00
|
|
|
{/if}
|
2022-10-30 16:28:14 +08:00
|
|
|
</svelte:fragment>
|
|
|
|
|
|
|
|
<div class="tabs-content">
|
2023-09-06 19:11:58 +08:00
|
|
|
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
|
2022-10-30 16:28:14 +08:00
|
|
|
<form
|
|
|
|
id={formId}
|
|
|
|
class="tab-item"
|
2023-04-17 17:43:48 +08:00
|
|
|
class:active={activeTab === tabFormKey}
|
2022-10-30 16:28:14 +08:00
|
|
|
on:submit|preventDefault={save}
|
2023-04-17 19:35:49 +08:00
|
|
|
on:keydown={handleFormKeydown}
|
2022-10-30 16:28:14 +08:00
|
|
|
>
|
2023-10-01 17:53:26 +08:00
|
|
|
{#if !hasChanges && initialDraft && !isLoading}
|
2023-04-17 17:43:48 +08:00
|
|
|
<div class="block" out:slide={{ duration: 150 }}>
|
|
|
|
<div class="alert alert-info m-0">
|
|
|
|
<div class="icon">
|
|
|
|
<i class="ri-information-line" />
|
|
|
|
</div>
|
2023-05-19 18:48:58 +08:00
|
|
|
<div class="flex flex-gap-xs">
|
2023-04-17 17:43:48 +08:00
|
|
|
The record has previous unsaved changes.
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="btn btn-sm btn-secondary"
|
|
|
|
on:click={() => restoreDraft()}
|
|
|
|
>
|
|
|
|
Restore draft
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="close"
|
|
|
|
aria-label="Discard draft"
|
|
|
|
use:tooltip={"Discard draft"}
|
|
|
|
on:click|preventDefault={() => deleteDraft()}
|
|
|
|
>
|
|
|
|
<i class="ri-close-line" />
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div class="clearfix p-b-base" />
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
|
2023-04-14 20:28:24 +08:00
|
|
|
<Field class="form-field {!isNew ? 'readonly' : ''}" name="id" let:uniqueId>
|
2023-03-21 23:55:12 +08:00
|
|
|
<label for={uniqueId}>
|
|
|
|
<i class={CommonHelper.getFieldTypeIcon("primary")} />
|
|
|
|
<span class="txt">id</span>
|
|
|
|
<span class="flex-fill" />
|
|
|
|
</label>
|
|
|
|
{#if !isNew}
|
2022-10-30 16:28:14 +08:00
|
|
|
<div class="form-field-addon">
|
2023-09-30 16:58:14 +08:00
|
|
|
<ModelDateIcon model={record} />
|
2022-10-30 16:28:14 +08:00
|
|
|
</div>
|
2023-03-21 23:55:12 +08:00
|
|
|
{/if}
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
id={uniqueId}
|
2023-10-01 17:53:26 +08:00
|
|
|
placeholder={!isLoading ? "Leave empty to auto generate..." : ""}
|
2023-03-21 23:55:12 +08:00
|
|
|
minlength="15"
|
|
|
|
readonly={!isNew}
|
2023-04-14 20:28:24 +08:00
|
|
|
bind:value={record.id}
|
2023-03-21 23:55:12 +08:00
|
|
|
/>
|
|
|
|
</Field>
|
2022-07-07 05:19:05 +08:00
|
|
|
|
2023-08-15 02:20:49 +08:00
|
|
|
{#if isAuthCollection}
|
2023-04-17 18:28:41 +08:00
|
|
|
<AuthFields bind:record {isNew} {collection} />
|
2022-11-16 21:13:04 +08:00
|
|
|
|
|
|
|
{#if collection?.schema?.length}
|
|
|
|
<hr />
|
|
|
|
{/if}
|
2022-07-07 05:19:05 +08:00
|
|
|
{/if}
|
2022-10-30 16:28:14 +08:00
|
|
|
|
|
|
|
{#each collection?.schema || [] as field (field.name)}
|
|
|
|
{#if field.type === "text"}
|
|
|
|
<TextField {field} bind:value={record[field.name]} />
|
|
|
|
{:else if field.type === "number"}
|
|
|
|
<NumberField {field} bind:value={record[field.name]} />
|
|
|
|
{:else if field.type === "bool"}
|
|
|
|
<BoolField {field} bind:value={record[field.name]} />
|
|
|
|
{:else if field.type === "email"}
|
|
|
|
<EmailField {field} bind:value={record[field.name]} />
|
|
|
|
{:else if field.type === "url"}
|
|
|
|
<UrlField {field} bind:value={record[field.name]} />
|
2023-01-17 19:31:48 +08:00
|
|
|
{:else if field.type === "editor"}
|
|
|
|
<EditorField {field} bind:value={record[field.name]} />
|
2022-10-30 16:28:14 +08:00
|
|
|
{:else if field.type === "date"}
|
|
|
|
<DateField {field} bind:value={record[field.name]} />
|
|
|
|
{:else if field.type === "select"}
|
|
|
|
<SelectField {field} bind:value={record[field.name]} />
|
|
|
|
{:else if field.type === "json"}
|
|
|
|
<JsonField {field} bind:value={record[field.name]} />
|
|
|
|
{:else if field.type === "file"}
|
|
|
|
<FileField
|
|
|
|
{field}
|
|
|
|
{record}
|
|
|
|
bind:value={record[field.name]}
|
|
|
|
bind:uploadedFiles={uploadedFilesMap[field.name]}
|
2023-05-18 04:17:45 +08:00
|
|
|
bind:deletedFileNames={deletedFileNamesMap[field.name]}
|
2022-10-30 16:28:14 +08:00
|
|
|
/>
|
|
|
|
{:else if field.type === "relation"}
|
|
|
|
<RelationField {field} bind:value={record[field.name]} />
|
|
|
|
{/if}
|
|
|
|
{/each}
|
|
|
|
</form>
|
|
|
|
|
2023-08-15 02:20:49 +08:00
|
|
|
{#if isAuthCollection && !isNew}
|
2023-04-17 17:43:48 +08:00
|
|
|
<div class="tab-item" class:active={activeTab === tabProviderKey}>
|
2022-10-30 16:28:14 +08:00
|
|
|
<ExternalAuthsList {record} />
|
2022-07-07 05:19:05 +08:00
|
|
|
</div>
|
2022-10-30 16:28:14 +08:00
|
|
|
{/if}
|
|
|
|
</div>
|
2022-07-07 05:19:05 +08:00
|
|
|
|
|
|
|
<svelte:fragment slot="footer">
|
2023-10-01 17:53:26 +08:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="btn btn-transparent"
|
|
|
|
disabled={isSaving || isLoading}
|
|
|
|
on:click={() => hide()}
|
|
|
|
>
|
2022-07-07 05:19:05 +08:00
|
|
|
<span class="txt">Cancel</span>
|
|
|
|
</button>
|
2023-02-19 01:33:42 +08:00
|
|
|
|
2022-07-07 05:19:05 +08:00
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
form={formId}
|
|
|
|
class="btn btn-expanded"
|
2023-10-01 17:53:26 +08:00
|
|
|
class:btn-loading={isSaving || isLoading}
|
2022-07-07 05:19:05 +08:00
|
|
|
disabled={!canSave || isSaving}
|
|
|
|
>
|
2023-03-21 23:55:12 +08:00
|
|
|
<span class="txt">{isNew ? "Create" : "Save changes"}</span>
|
2022-07-07 05:19:05 +08:00
|
|
|
</button>
|
|
|
|
</svelte:fragment>
|
|
|
|
</OverlayPanel>
|
2023-04-17 19:35:49 +08:00
|
|
|
|
|
|
|
<style>
|
|
|
|
.panel-title {
|
|
|
|
line-height: var(--smBtnHeight);
|
|
|
|
}
|
|
|
|
</style>
|