Files
cm/static/js/main.js
2026-02-02 19:18:25 +03:00

87 lines
2.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Общие функции для всех страниц
// Инициализация табов
function initTabs() {
const tabs = document.querySelectorAll('.tab');
if (tabs.length > 0) {
tabs.forEach(tab => {
tab.addEventListener('click', function() {
const tabId = this.getAttribute('data-tab');
if (tabId) {
switchTab(tabId);
}
});
});
}
}
function switchTab(tabId) {
// Удаляем активный класс у всех вкладок
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
// Добавляем активный класс выбранной вкладке
const activeTab = document.querySelector(`.tab[data-tab="${tabId}"]`);
const activeContent = document.getElementById(`${tabId}-tab`);
if (activeTab) activeTab.classList.add('active');
if (activeContent) activeContent.classList.add('active');
}
// Таймер обратного отсчета
class GameTimer {
constructor(elementId, duration) {
this.element = document.getElementById(elementId);
this.duration = duration;
this.timeLeft = duration;
this.interval = null;
}
start() {
this.updateDisplay();
this.interval = setInterval(() => {
this.timeLeft--;
this.updateDisplay();
if (this.timeLeft <= 0) {
this.stop();
if (this.onComplete) this.onComplete();
}
}, 1000);
}
stop() {
if (this.interval) {
clearInterval(this.interval);
this.interval = null;
}
}
updateDisplay() {
if (this.element) {
const minutes = Math.floor(this.timeLeft / 60);
const seconds = this.timeLeft % 60;
this.element.textContent = `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
}
}
}
// Форматирование чисел (валюты)
function formatCurrency(amount) {
return new Intl.NumberFormat('ru-RU', {
style: 'currency',
currency: 'RUB',
minimumFractionDigits: 0
}).format(amount);
}
// Инициализация при загрузке страницы
document.addEventListener('DOMContentLoaded', function() {
initTabs();
// Инициализация текущего пользователя
const currentUser = localStorage.getItem('currentUser');
if (currentUser && document.getElementById('current-user')) {
document.getElementById('current-user').textContent = currentUser;
}
});