File size: 1,561 Bytes
17bca00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// script-modularized/utils.js



export function generateAvatarUrl(name, background = '3b82f6', color = 'fff') {
    return `https://ui-avatars.com/api/?name=${encodeURIComponent(name || '')}&background=${background}&color=${color}`;
  }
  
  export function escapeHtml(text) {
    if (!text) return '';
    return text.replace(/[&<>"]/g, c => {
      return {
        '&': '&amp;',
        '<': '&lt;',
        '>': '&gt;',
        '"': '&quot;'
      }[c];
    });
  }
  
  export function escapeJs(text) {
    if (!text) return '';
    return text
      .replace(/\\/g, '\\\\')
      .replace(/`/g, '\\`')
      .replace(/\$/g, '\\$')
      .replace(/'/g, "\\'")
      .replace(/\"/g, '\\"');
  }
  
  export function formatDate(timestamp) {
    return timestamp?.toDate ? moment(timestamp.toDate()).format('HH:mm DD/MM/YYYY') : '';
  }
  
  export function toggleButtonLoading(buttonId, isLoading) {
    const button = document.getElementById(buttonId);
    if (!button) return;
  
    const defaultText = button.getAttribute('data-original-text') || button.innerText;
    if (!button.hasAttribute('data-original-text')) {
      button.setAttribute('data-original-text', defaultText);
    }
  
    if (isLoading) {
      button.disabled = true;
      button.innerHTML = '<i class="fas fa-spinner fa-spin mr-2"></i>Đang xử lý...';
      button.classList.add('opacity-50', 'cursor-not-allowed');
    } else {
      button.disabled = false;
      button.innerHTML = defaultText;
      button.classList.remove('opacity-50', 'cursor-not-allowed');
    }
  }