Repository navigation
Expand file tree
/
Copy pathui.js
More file actions
209 lines (183 loc) · 7.16 KB
/
Copy pathui.js
File metadata and controls
209 lines (183 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* ============================================
🐍 پایتونباز آرکید — رندرهای مشترک UI
============================================ */
PB.ui = (() => {
const { el } = PB.utils;
// ============ کدبلاک ============
function codeBlock(code) {
return el("div", { class: "code-block" }, [
el("pre", { html: PB.utils.highlightPython(code) }),
]);
}
// ============ ترمینال فیک ============
function fakeTerminal(title = "terminal", lines = [], opts = {}) {
const { runButton = false, onRun = null } = opts;
const body = el("div", { class: "term-body" }, lines.map((l) =>
el("div", { class: "term-line", html: l })
));
const term = el("div", { class: "fake-terminal" }, [
el("div", { class: "term-head" }, [
el("span", { class: "term-dot dot-red" }),
el("span", { class: "term-dot dot-yellow" }),
el("span", { class: "term-dot dot-green" }),
el("span", { class: "term-title", text: title }),
]),
body,
]);
if (runButton && onRun) {
const btn = el("button", { class: "btn btn-green btn-sm term-run" }, ["▶ RUN"]);
btn.addEventListener("click", onRun);
term.appendChild(btn);
}
return term;
}
// ============ نوار پیشرفت ============
function progressBar(percent, opts = {}) {
const { color = "", label = "" } = opts;
const fill = el("div", {
class: `progress-fill ${color === "green" ? "progress-fill-green" : color === "purple" ? "progress-fill-purple" : ""}`,
style: `width: ${PB.utils.clamp(percent, 0, 100)}%`,
});
const bar = el("div", { class: "progress-track" }, [fill]);
if (label) {
return el("div", { class: "progress-with-label" }, [
el("div", { class: "progress-label", text: label }),
bar,
]);
}
return bar;
}
// ============ ستارهها ============
function stars(count, max = 3, colorClass = "") {
const container = el("div", { class: "stars" });
for (let i = 1; i <= max; i++) {
const lit = i <= count;
container.appendChild(
el("span", { class: `star ${lit ? "lit" : ""} ${lit ? colorClass : ""}`, text: "★" })
);
}
return container;
}
// ============ بج ============
function badge(text, color = "ghost") {
return el("span", { class: `badge badge-${color}`, text });
}
// ============ قلب (انرژی) ============
function hearts(count, max = PB.game.MAX_ENERGY) {
const container = el("div", { class: "hearts", title: `${PB.utils.toFa(count)}/${PB.utils.toFa(max)} انرژی` });
for (let i = 1; i <= max; i++) {
container.appendChild(
el("span", { class: `heart ${i <= count ? "filled" : ""}`, text: "❤" })
);
}
return container;
}
// ============ مودال ============
function openModal({ title = "", content = null, large = false, onClose = null }) {
const root = document.getElementById("modal-root");
root.innerHTML = "";
const closeBtn = el("button", { class: "modal-close", text: "✕", "aria-label": "بستن" });
closeBtn.addEventListener("click", close);
const modal = el("div", { class: `modal ${large ? "modal-lg" : ""}` }, [
el("div", { class: "modal-header" }, [
el("h3", { text: title }),
closeBtn,
]),
el("div", { class: "modal-body" }, [content]),
]);
const overlay = el("div", { class: "modal-overlay" }, [modal]);
overlay.addEventListener("click", (e) => {
if (e.target === overlay) close();
});
root.appendChild(overlay);
function close() {
root.innerHTML = "";
if (onClose) onClose();
}
document.addEventListener("keydown", (e) => {
if (e.key === "Escape" && root.innerHTML) close();
}, { once: true });
return { close };
}
// ============ توست ============
function toast(message, type = "info") {
PB.utils.toast(message, type);
}
// ============ شیک ============
function shakeElement(node) {
node.classList.add("anim-shake");
setTimeout(() => node.classList.remove("anim-shake"), 500);
}
function glowElement(node, className = "glow-box") {
node.classList.add(className);
setTimeout(() => node.classList.remove(className), 800);
}
// ============ هدر صفحه ============
function pageHeader({ title, subtitle = "", actions = [] }) {
const actionsWrap = el("div", { class: "page-header-actions" }, actions);
const left = el("div", {}, [
el("h1", { text: title }),
subtitle ? el("div", { class: "text-muted", style: "font-size: 0.82rem;", text: subtitle }) : null,
].filter(Boolean));
return el("div", { class: "page-header" }, [left, actionsWrap]);
}
// ============ هدر با بک ============
function backButton(label = "بازگشت", path = "#/map") {
const btn = el("button", { class: "btn btn-ghost btn-sm" }, [label]);
btn.addEventListener("click", () => (window.location.hash = path));
return btn;
}
// ============ رندر صفحه اصلی ============
function renderApp(children) {
const app = document.getElementById("app");
app.innerHTML = "";
// پاک کردن فابهای قبلی (فقط صفحه اصلی فاب داره)
document.querySelectorAll(".fab").forEach((n) => n.remove());
const container = el("div", { class: "container" }, children);
app.appendChild(container);
return container;
}
// پاک کردن کامل app (شامل nav و fabs) قبل از رندر جدید
function resetApp() {
const app = document.getElementById("app");
app.innerHTML = "";
return app;
}
// ============ تایمر ============
function startTimer(seconds, onTick, onEnd) {
let remaining = seconds;
const interval = setInterval(() => {
remaining--;
if (remaining <= 0) {
clearInterval(interval);
onEnd && onEnd();
} else {
onTick && onTick(remaining);
}
}, 1000);
return {
stop() { clearInterval(interval); },
get remaining() { return remaining; },
};
}
return {
codeBlock,
fakeTerminal,
progressBar,
stars,
badge,
hearts,
openModal,
toast,
shakeElement,
glowElement,
pageHeader,
backButton,
renderApp,
startTimer,
// پراکسی به utils
showConfetti: () => PB.utils.showConfetti(),
showLoading: () => PB.utils.showLoading(),
hideLoading: () => PB.utils.hideLoading(),
};
})();