Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -936,6 +936,42 @@ class PresetGallery(gr.HTML):
|
|
| 936 |
""")
|
| 937 |
|
| 938 |
html_template = f"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 939 |
<div class="pg-wrap" data-pg="{uid}">
|
| 940 |
<div class="pg-title">
|
| 941 |
<div class="pg-h1">{title}</div>
|
|
@@ -943,17 +979,31 @@ class PresetGallery(gr.HTML):
|
|
| 943 |
<div class="pg-grid">
|
| 944 |
{''.join(cards_html)}
|
| 945 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 946 |
</div>
|
| 947 |
"""
|
| 948 |
|
| 949 |
# --- JAVASCRIPT LOGIC UPDATED ---
|
| 950 |
-
#
|
| 951 |
-
# All logic is now inside "pointerdown" (click event).
|
| 952 |
js_on_load = r"""
|
| 953 |
(() => {
|
| 954 |
const wrap = element.querySelector(".pg-wrap");
|
| 955 |
const cards = Array.from(element.querySelectorAll(".pg-card"));
|
| 956 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 957 |
|
| 958 |
function setDim(activeIdx) {
|
| 959 |
cards.forEach((c, i) => {
|
|
@@ -966,12 +1016,50 @@ class PresetGallery(gr.HTML):
|
|
| 966 |
|
| 967 |
let lastSent = null;
|
| 968 |
let lock = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 969 |
|
| 970 |
cards.forEach((card) => {
|
| 971 |
-
// REMOVED: "pointerenter" event listener. No action on hover.
|
| 972 |
-
// REMOVED: "pointerleave" event listener. No action on hover.
|
| 973 |
-
|
| 974 |
-
// Use pointerdown (click/tap) for all actions.
|
| 975 |
card.addEventListener("pointerdown", (e) => {
|
| 976 |
e.preventDefault();
|
| 977 |
e.stopPropagation();
|
|
@@ -980,18 +1068,10 @@ class PresetGallery(gr.HTML):
|
|
| 980 |
lock = true;
|
| 981 |
setTimeout(() => (lock = false), 250);
|
| 982 |
|
| 983 |
-
|
| 984 |
-
|
|
|
|
| 985 |
|
| 986 |
-
// 1. Update visual state immediately on click.
|
| 987 |
-
setDim(idxNumber);
|
| 988 |
-
|
| 989 |
-
// 2. Only update Python backend if the value has actually changed.
|
| 990 |
-
if (idxString === lastSent) return;
|
| 991 |
-
lastSent = idxString;
|
| 992 |
-
|
| 993 |
-
// ✅ Set props.value to send data to Python.
|
| 994 |
-
props.value = idxString;
|
| 995 |
}, { passive: false });
|
| 996 |
});
|
| 997 |
|
|
@@ -1000,7 +1080,6 @@ class PresetGallery(gr.HTML):
|
|
| 1000 |
})();
|
| 1001 |
"""
|
| 1002 |
|
| 1003 |
-
|
| 1004 |
super().__init__(value="", html_template=html_template, js_on_load=js_on_load, **kwargs)
|
| 1005 |
|
| 1006 |
|
|
|
|
| 936 |
""")
|
| 937 |
|
| 938 |
html_template = f"""
|
| 939 |
+
<style>
|
| 940 |
+
.pg-modal-overlay-custom {{
|
| 941 |
+
position: fixed; top: 0; left: 0; right: 0; bottom: 0;
|
| 942 |
+
background: rgba(0,0,0,0.6); display: none;
|
| 943 |
+
align-items: center; justify-content: center; z-index: 999999;
|
| 944 |
+
backdrop-filter: blur(2px);
|
| 945 |
+
}}
|
| 946 |
+
.pg-modal-custom {{
|
| 947 |
+
background: #2b2b2b; padding: 24px; border-radius: 16px;
|
| 948 |
+
border: 1px solid rgba(255,255,255,0.12); color: rgba(255,255,255,0.9);
|
| 949 |
+
text-align: center; font-family: ui-sans-serif, system-ui, -apple-system, sans-serif;
|
| 950 |
+
min-width: 320px; box-shadow: 0 10px 30px rgba(0,0,0,0.5);
|
| 951 |
+
direction: rtl;
|
| 952 |
+
}}
|
| 953 |
+
.pg-modal-custom p {{
|
| 954 |
+
font-size: 16px; margin: 0 0 20px 0; font-weight: 600;
|
| 955 |
+
}}
|
| 956 |
+
.pg-modal-btns-custom {{
|
| 957 |
+
display: flex; gap: 12px; justify-content: center;
|
| 958 |
+
}}
|
| 959 |
+
.pg-modal-btns-custom button {{
|
| 960 |
+
padding: 10px 24px; border: none; border-radius: 10px; cursor: pointer;
|
| 961 |
+
font-weight: 700; font-size: 14px; font-family: inherit; transition: opacity 0.2s;
|
| 962 |
+
}}
|
| 963 |
+
.pg-modal-btns-custom button:hover {{
|
| 964 |
+
opacity: 0.8;
|
| 965 |
+
}}
|
| 966 |
+
.pg-btn-confirm-custom {{
|
| 967 |
+
background: #4b2ab5; color: white;
|
| 968 |
+
}}
|
| 969 |
+
.pg-btn-cancel-custom {{
|
| 970 |
+
background: transparent; color: rgba(255,255,255,0.7);
|
| 971 |
+
border: 1px solid rgba(255,255,255,0.2) !important;
|
| 972 |
+
}}
|
| 973 |
+
</style>
|
| 974 |
+
|
| 975 |
<div class="pg-wrap" data-pg="{uid}">
|
| 976 |
<div class="pg-title">
|
| 977 |
<div class="pg-h1">{title}</div>
|
|
|
|
| 979 |
<div class="pg-grid">
|
| 980 |
{''.join(cards_html)}
|
| 981 |
</div>
|
| 982 |
+
|
| 983 |
+
<!-- پیام تاییدی (Modal) اضافه شده -->
|
| 984 |
+
<div class="pg-modal-overlay-custom">
|
| 985 |
+
<div class="pg-modal-custom">
|
| 986 |
+
<p>آیا میخواهید نمونه به بالا اضافه بشه؟</p>
|
| 987 |
+
<div class="pg-modal-btns-custom">
|
| 988 |
+
<button class="pg-btn-confirm-custom">تایید</button>
|
| 989 |
+
<button class="pg-btn-cancel-custom">انصراف</button>
|
| 990 |
+
</div>
|
| 991 |
+
</div>
|
| 992 |
+
</div>
|
| 993 |
</div>
|
| 994 |
"""
|
| 995 |
|
| 996 |
# --- JAVASCRIPT LOGIC UPDATED ---
|
| 997 |
+
# Logic updated to show confirmation prompt before triggering action
|
|
|
|
| 998 |
js_on_load = r"""
|
| 999 |
(() => {
|
| 1000 |
const wrap = element.querySelector(".pg-wrap");
|
| 1001 |
const cards = Array.from(element.querySelectorAll(".pg-card"));
|
| 1002 |
+
const modalOverlay = element.querySelector(".pg-modal-overlay-custom");
|
| 1003 |
+
const btnConfirm = element.querySelector(".pg-btn-confirm-custom");
|
| 1004 |
+
const btnCancel = element.querySelector(".pg-btn-cancel-custom");
|
| 1005 |
+
|
| 1006 |
+
if (!wrap || !cards.length || !modalOverlay) return;
|
| 1007 |
|
| 1008 |
function setDim(activeIdx) {
|
| 1009 |
cards.forEach((c, i) => {
|
|
|
|
| 1016 |
|
| 1017 |
let lastSent = null;
|
| 1018 |
let lock = false;
|
| 1019 |
+
let pendingIdxNumber = null;
|
| 1020 |
+
|
| 1021 |
+
// Action for "تایید"
|
| 1022 |
+
btnConfirm.addEventListener("pointerdown", (e) => {
|
| 1023 |
+
e.preventDefault();
|
| 1024 |
+
e.stopPropagation();
|
| 1025 |
+
modalOverlay.style.display = "none"; // Hide Modal
|
| 1026 |
+
|
| 1027 |
+
if (pendingIdxNumber !== null) {
|
| 1028 |
+
const idxString = String(pendingIdxNumber);
|
| 1029 |
+
|
| 1030 |
+
// 1. Update visual state immediately on confirmed click.
|
| 1031 |
+
setDim(pendingIdxNumber);
|
| 1032 |
+
|
| 1033 |
+
// 2. Only update Python backend if the value has actually changed.
|
| 1034 |
+
if (idxString !== lastSent) {
|
| 1035 |
+
lastSent = idxString;
|
| 1036 |
+
// ✅ Set props.value to send data to Python.
|
| 1037 |
+
props.value = idxString;
|
| 1038 |
+
}
|
| 1039 |
+
|
| 1040 |
+
pendingIdxNumber = null;
|
| 1041 |
+
}
|
| 1042 |
+
});
|
| 1043 |
+
|
| 1044 |
+
// Action for "انصراف"
|
| 1045 |
+
btnCancel.addEventListener("pointerdown", (e) => {
|
| 1046 |
+
e.preventDefault();
|
| 1047 |
+
e.stopPropagation();
|
| 1048 |
+
modalOverlay.style.display = "none"; // Hide Modal
|
| 1049 |
+
pendingIdxNumber = null;
|
| 1050 |
+
});
|
| 1051 |
+
|
| 1052 |
+
// Close modal if clicked directly on background overlay
|
| 1053 |
+
modalOverlay.addEventListener("pointerdown", (e) => {
|
| 1054 |
+
if (e.target === modalOverlay) {
|
| 1055 |
+
e.preventDefault();
|
| 1056 |
+
e.stopPropagation();
|
| 1057 |
+
modalOverlay.style.display = "none";
|
| 1058 |
+
pendingIdxNumber = null;
|
| 1059 |
+
}
|
| 1060 |
+
});
|
| 1061 |
|
| 1062 |
cards.forEach((card) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1063 |
card.addEventListener("pointerdown", (e) => {
|
| 1064 |
e.preventDefault();
|
| 1065 |
e.stopPropagation();
|
|
|
|
| 1068 |
lock = true;
|
| 1069 |
setTimeout(() => (lock = false), 250);
|
| 1070 |
|
| 1071 |
+
// Save clicked index and display modal
|
| 1072 |
+
pendingIdxNumber = Number(card.dataset.idx);
|
| 1073 |
+
modalOverlay.style.display = "flex";
|
| 1074 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1075 |
}, { passive: false });
|
| 1076 |
});
|
| 1077 |
|
|
|
|
| 1080 |
})();
|
| 1081 |
"""
|
| 1082 |
|
|
|
|
| 1083 |
super().__init__(value="", html_template=html_template, js_on_load=js_on_load, **kwargs)
|
| 1084 |
|
| 1085 |
|