File size: 4,492 Bytes
a2618a8
 
 
 
 
 
 
 
 
 
 
 
 
f2e0d4f
a2618a8
 
 
 
 
 
 
 
 
 
 
 
 
f2e0d4f
a2618a8
 
 
 
f2e0d4f
a2618a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2e0d4f
a2618a8
 
 
 
 
 
f2e0d4f
a2618a8
f2e0d4f
 
a2618a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2e0d4f
 
 
a2618a8
f2e0d4f
 
a2618a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2e0d4f
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
<!-- templates/flashcards.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flashcards</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
    <div class="flashcard" id="flashcard">
        <div class="content">
            <h2 id="card-header">Set: {{ set_name }} ({{ index + 1 }}/{{ total }})</h2>
            <p id="card-text"><strong>Japanese:</strong> {{ japanese }}</p>
            <audio controls id="audioPlayer">
                <source src="{{ audio_url }}" type="audio/mp3">
                Your browser does not support the audio element.
            </audio>
        </div>
        <div class="navigation">
            <button id="prevBtn">Previous</button>
            <button id="flipBtn">Flip</button>
            <button id="nextBtn">Next</button>
        </div>
    </div>

    <script>
        let showingJapanese = true;
        let currentIndex = {{ index }};
        const setName = '{{ set_name }}';
        const total = {{ total }};
        let japaneseText = "{{ japanese }}";
        let chineseText = "{{ chinese }}";

        const flipBtn = document.getElementById('flipBtn');
        const prevBtn = document.getElementById('prevBtn');
        const nextBtn = document.getElementById('nextBtn');
        const cardText = document.getElementById('card-text');
        const cardHeader = document.getElementById('card-header');
        const audioPlayer = document.getElementById('audioPlayer');

        // Function to fetch and update flashcard data
        function updateFlashcard(setName, newIndex) {
            fetch(`/api/flashcards?set=${setName}&index=${newIndex}`)
                .then(response => {
                    if (!response.ok) {
                        throw new Error('Network response was not ok');
                    }
                    return response.json();
                })
                .then(data => {
                    if (data.error) {
                        alert(data.error);
                        return;
                    }

                    japaneseText = data.japanese;
                    chineseText = data.chinese;
                    currentIndex = data.index;

                    // Update header
                    cardHeader.innerHTML = `Set: ${data.set_name} (${currentIndex + 1}/${data.total})`;

                    // Update text
                    if (showingJapanese) {
                        cardText.innerHTML = "<strong>Japanese:</strong> " + japaneseText;
                    } else {
                        cardText.innerHTML = "<strong>Chinese:</strong> " + chineseText;
                    }

                    // Update audio
                    audioPlayer.src = data.audio_url;
                    audioPlayer.play();

                    // Update URL without reloading the page
                    const newUrl = `/flashcards?set=${data.set_name}&index=${data.index}`;
                    window.history.pushState(null, '', newUrl);
                })
                .catch(error => {
                    console.error('Error fetching flashcard data:', error);
                });
        }

        // Flip button event
        flipBtn.addEventListener('click', function() {
            if (showingJapanese) {
                cardText.innerHTML = "<strong>Chinese:</strong> " + chineseText;
                showingJapanese = false;
            } else {
                cardText.innerHTML = "<strong>Japanese:</strong> " + japaneseText;
                showingJapanese = true;
            }
        });

        // Previous button event
        prevBtn.addEventListener('click', function() {
            let newIndex = (currentIndex - 1 + total) % total;
            updateFlashcard(setName, newIndex);
        });

        // Next button event
        nextBtn.addEventListener('click', function() {
            let newIndex = (currentIndex + 1) % total;
            updateFlashcard(setName, newIndex);
        });

        // Handle browser navigation (back/forward)
        window.addEventListener('popstate', function() {
            const urlParams = new URLSearchParams(window.location.search);
            const setName = urlParams.get('set') || 'A';
            const index = parseInt(urlParams.get('index')) || 0;
            updateFlashcard(setName, index);
        });
    </script>
</body>
</html>