Spaces:
Running
Running
File size: 4,861 Bytes
6e3f0df | 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 |
document.addEventListener('DOMContentLoaded', () => {
const callForm = document.getElementById('callForm');
const callHistory = document.getElementById('callHistory');
let callTimeout;
// Form validation
function validatePhoneNumber(number) {
return /^\+?[\d\s\-\(\)]{7,
}
// Initialize with some sample calls if empty
if (callHistory.children.length === 1) {
setTimeout(() => {
simulateCall(Date.now()-5000, '+1 (555) 123-4567', '+1 (555) 987-6543');
simulateCall(Date.now()-10000, '+44 7911 123456', '+49 176 9876543');
}, 1000);
}
$/.test(number);
}
callForm.addEventListener('submit', (e) => {
e.preventDefault();
const callerId = document.getElementById('callerId').value.trim();
const recipientNumber = document.getElementById('recipientNumber').value.trim();
// Clear any previous timeouts
if (callTimeout) clearTimeout(callTimeout);
if (!callerId || !recipientNumber) {
showAlert('Please enter both caller ID and recipient number', 'error');
return;
}
if (!validatePhoneNumber(callerId) || !validatePhoneNumber(recipientNumber)) {
showAlert('Please enter valid phone numbers', 'error');
return;
}
// Simulate call initiation
const callId = Date.now(); // Unique ID for each call
simulateCall(callId, callerId, recipientNumber);
});
// Add service worker for PWA functionality
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js').then(registration => {
console.log('ServiceWorker registration successful');
}).catch(err => {
console.log('ServiceWorker registration failed: ', err);
});
});
}
function showAlert(message, type = 'success') {
const alert = document.createElement('div');
alert.className = `fixed top-4 right-4 px-4 py-3 rounded-lg shadow-lg z-50 ${
type === 'error' ? 'bg-red-500' : 'bg-green-500'
} text-white`;
alert.textContent = message;
document.body.appendChild(alert);
setTimeout(() => {
alert.remove();
}, 3000);
}
function simulateCall(callId, callerId, recipientNumber) {
// Add to call history with unique ID
const callItem = document.createElement('div');
callItem.dataset.callId = callId;
callItem.className = 'bg-gray-800 p-4 rounded-lg flex justify-between items-center';
callItem.innerHTML = `
<div>
<span class="text-purple-400 font-medium">${callerId}</span>
<i data-feather="arrow-right" class="inline mx-2 w-4 h-4"></i>
<span class="text-pink-400 font-medium">${recipientNumber}</span>
</div>
<div class="flex items-center space-x-3">
<span class="text-green-400 text-sm">Calling...</span>
<div class="flex space-x-2">
<button class="end-call-btn p-1 rounded-full bg-red-500 hover:bg-red-600 transition">
<i data-feather="phone-off" class="w-4 h-4"></i>
</button>
<button class="delete-call-btn p-1 rounded-full bg-gray-600 hover:bg-gray-700 transition">
<i data-feather="trash-2" class="w-4 h-4"></i>
</button>
</div>
</div>
`;
callHistory.prepend(callItem);
feather.replace();
// Add event listeners to buttons
callItem.querySelector('.end-call-btn').addEventListener('click', () => {
endCall(callId, callItem);
});
callItem.querySelector('.delete-call-btn').addEventListener('click', () => {
callItem.remove();
showAlert('Call removed from history', 'info');
});
// Simulate call completion after random time (3-10 seconds)
const callDuration = 3000 + Math.random() * 7000;
callTimeout = setTimeout(() => {
if (callItem.parentNode) { // Only if still in DOM
callItem.querySelector('.text-green-400').textContent = 'Completed';
callItem.querySelector('.end-call-btn').remove();
feather.replace();
showAlert(`Call to ${recipientNumber} completed`);
}
}, callDuration);
}
function endCall(callId, callItem) {
clearTimeout(callTimeout);
callItem.querySelector('.text-green-400').textContent = 'Ended';
callItem.querySelector('.end-call-btn').remove();
feather.replace();
showAlert('Call ended manually', 'info');
}
}); |