-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
238 lines (202 loc) · 7.61 KB
/
main.js
File metadata and controls
238 lines (202 loc) · 7.61 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* ModderOS Core - v4.0 (Error Free & Auto-Inject)
* Features: Auto Menu Injection, Null Checks, Modal Logic
*/
document.addEventListener('DOMContentLoaded', () => {
// 1. Global Menularni yaratish (HTMLda bo'lmasa ham ishlaydi)
injectGlobalMenus();
// 2. Soatni ishga tushirish (Xatoliksiz)
initClock();
// 3. Parolni ko'rsatish (Login page uchun)
initPasswordToggle();
// 5. Menyular logikasi
initContextLogic();
});
/* --- A. AUTO INJECT MENUS (Xatolikni oldini olish uchun) --- */
function injectGlobalMenus() {
// Agar Context Menu yo'q bo'lsa, yaratamiz
if (!document.getElementById('context-menu')) {
const ctxHtml = `
<div id="context-menu" class="ios-context-menu hidden">
<div class="ctx-item" data-action="refresh">Yangilash</div>
<div class="ctx-item" data-action="back">Orqaga</div>
<div class="ctx-separator"></div>
<div class="ctx-item" data-action="settings">Sozlamalar</div>
<div class="ctx-item destructive" data-action="logout">Chiqish</div>
</div>`;
document.body.insertAdjacentHTML('beforeend', ctxHtml);
}
// Agar Edit Menu yo'q bo'lsa, yaratamiz
if (!document.getElementById('edit-menu')) {
const editHtml = `
<div id="edit-menu" class="edit-pill-menu hidden">
<button class="edit-pill-btn" data-action="copy">Nusxalash</button>
<button class="edit-pill-btn" data-action="paste">Joylash</button>
<button class="edit-pill-btn" data-action="cut">Qirqish</button>
</div>`;
document.body.insertAdjacentHTML('beforeend', editHtml);
}
}
/* --- B. CLOCK (Null Check bilan) --- */
function initClock() {
const clockEl = document.getElementById('clock') || document.getElementById('system-clock');
if (!clockEl) return; // Agar soat elementi yo'q bo'lsa, to'xtaydi (Xatolik bermaydi)
const update = () => {
const now = new Date();
let h = now.getHours();
let m = now.getMinutes();
h = h % 12 || 12;
m = m < 10 ? '0' + m : m;
clockEl.textContent = `${h}:${m}`;
};
setInterval(update, 1000);
update();
}
/* --- C. PASSWORD TOGGLE --- */
function initPasswordToggle() {
const btn = document.getElementById('toggle-pass');
const input = document.getElementById('password');
if (btn && input) {
btn.addEventListener('click', () => {
const type = input.type === 'password' ? 'text' : 'password';
input.type = type;
btn.style.fill = type === 'text' ? '#0a84ff' : 'rgba(255,255,255,0.5)';
});
}
}
// /* --- D. LOGIN FORM --- */
// function initLoginForm() {
// const form = document.getElementById('login-form');
// if (form) {
// form.addEventListener('submit', (e) => {
// e.preventDefault();
// const btn = document.getElementById('btn-submit');
// const loader = document.getElementById('loader-container');
// if(btn) btn.style.display = 'none';
// if(loader) loader.style.display = 'flex';
// setTimeout(() => {
// window.location.href = '/dashboard'; // Dashboardga o'tish
// }, 2000);
// });
// }
// }
/* --- E. MENU & MODAL LOGIC (CORE) --- */
function initContextLogic() {
const contextMenu = document.getElementById('context-menu');
const editMenu = document.getElementById('edit-menu');
// Hamma menyuni yopish
const hideAll = () => {
if(contextMenu) contextMenu.classList.add('hidden');
if(editMenu) editMenu.classList.add('hidden');
};
// 1. Context Menu (Right Click)
document.addEventListener('contextmenu', (e) => {
e.preventDefault();
hideAll();
if (!contextMenu) return;
let x = e.clientX;
let y = e.clientY;
// Ekranda sig'ishini tekshirish
if (x + 200 > window.innerWidth) x -= 200;
if (y + 200 > window.innerHeight) y -= 200;
contextMenu.style.left = `${x}px`;
contextMenu.style.top = `${y}px`;
requestAnimationFrame(() => {
contextMenu.classList.remove('hidden');
});
});
// 2. Edit Menu (Text Selection)
document.addEventListener('mouseup', (e) => {
if (contextMenu && contextMenu.contains(e.target)) return;
setTimeout(() => {
const selection = window.getSelection();
const text = selection.toString();
if (text.length > 0 && editMenu) {
const range = selection.getRangeAt(0);
const rect = range.getBoundingClientRect();
hideAll();
let top = rect.top - 50 + window.scrollY;
let left = rect.left + (rect.width / 2) - (editMenu.offsetWidth / 2);
if (top < 10) top = rect.bottom + 10 + window.scrollY;
editMenu.style.top = `${top}px`;
editMenu.style.left = `${left}px`;
editMenu.classList.remove('hidden');
}
}, 50);
});
// 3. Global Click (Yopish)
document.addEventListener('mousedown', (e) => {
// Null check qo'shildi
const clickedCtx = contextMenu && contextMenu.contains(e.target);
const clickedEdit = editMenu && editMenu.contains(e.target);
if (!clickedCtx && !clickedEdit) {
hideAll();
}
});
// 4. Menu Actions
document.addEventListener('click', (e) => {
if (e.target.closest('[data-action]')) {
const action = e.target.getAttribute('data-action');
if(['copy', 'cut', 'paste'].includes(action)) {
document.execCommand(action);
} else if (action === 'refresh') {
window.location.reload();
} else if (action === 'logout') {
window.location.href = '/logout';
} else if (action === 'back') {
window.history.back();
} else if (action === 'settings') {
window.location.href='/settings';
}
hideAll();
}
});
}
/* --- F. MODAL SYSTEM (Dashboard uchun) --- */
function toggleModal(modalId) {
const modal = document.getElementById(modalId);
if (modal) {
if (modal.classList.contains('active')) {
modal.classList.remove('active');
setTimeout(() => modal.style.display = 'none', 300);
} else {
modal.style.display = 'flex';
setTimeout(() => modal.classList.add('active'), 10);
}
}
}
// Modaldan tashqariga bosganda yopish
window.onclick = function(event) {
if (event.target.classList.contains('modal-overlay')) {
event.target.classList.remove('active');
setTimeout(() => event.target.style.display = 'none', 300);
}
}
function toggleApp(appId) {
fetch(`/apps/toggle/${appId}/`, {
method: "POST",
headers: {
"X-CSRFToken": getCookie("csrftoken"),
}
})
.then(res => res.json())
.then(data => {
console.log("Updated:", data);
})
.catch(err => console.log("Error", err));
}
// Django CSRF cookie olish
function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== "") {
const cookies = document.cookie.split(";");
for (let cookie of cookies) {
cookie = cookie.trim();
if (cookie.substring(0, name.length + 1) === (name + "=")) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}