File size: 12,573 Bytes
f2ef360
 
 
 
 
 
 
 
 
 
7017b8d
f2ef360
 
 
7017b8d
f2ef360
 
 
 
7017b8d
f2ef360
 
 
7017b8d
f2ef360
 
 
 
 
 
7017b8d
 
f2ef360
 
 
 
 
 
 
 
 
 
ff7275e
 
 
 
 
f2ef360
 
 
 
ff7275e
7017b8d
 
f2ef360
 
 
 
 
 
7017b8d
f2ef360
 
 
 
 
 
 
 
ff7275e
f2ef360
 
7017b8d
 
 
ff7275e
 
f2ef360
7017b8d
f2ef360
 
2c623f1
ff7275e
 
7017b8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2ef360
 
7017b8d
 
 
f2ef360
 
 
 
 
 
 
7017b8d
f2ef360
ff7275e
7017b8d
 
 
f2ef360
 
7017b8d
 
 
 
ff7275e
f2ef360
 
 
 
 
 
 
ff7275e
7017b8d
 
 
 
 
 
 
 
 
 
 
 
 
 
f2ef360
 
7017b8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff7275e
f2ef360
 
7017b8d
 
 
 
f2ef360
 
 
 
 
7017b8d
 
 
 
f2ef360
7017b8d
f2ef360
7017b8d
 
 
 
 
 
 
 
 
f2ef360
7017b8d
f2ef360
7017b8d
 
 
f2ef360
7017b8d
 
 
 
 
 
 
 
 
f2ef360
 
7017b8d
2c623f1
7017b8d
 
 
2c623f1
 
 
7017b8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2ef360
 
 
7017b8d
ff7275e
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
class InkBoard {
    constructor() {
        this.currentCreationId = null;
        this.currentSection = 'create';
        this.init();
    }

    init() {
        this.setupEventListeners();
        this.loadGallery();
        this.showSection('create');
    }

    setupEventListeners() {
        document.getElementById('scene-form').addEventListener('submit', (e) => {
            e.preventDefault();
            this.generateContent();
        });

        document.getElementById('save-journal').addEventListener('click', () => {
            this.saveJournal();
        });

        document.getElementById('journalModal').addEventListener('hidden.bs.modal', () => {
            this.currentCreationId = null;
            document.getElementById('journal-text').value = '';
        });
    }

    async generateContent() {
        const sceneIdea = document.getElementById('scene-idea').value.trim();
        
        if (!sceneIdea) {
            this.showAlert('Please enter a scene idea', 'danger');
            return;
        }

        try {
            this.showLoading(true);
            this.setButtonLoading(true);

            const response = await fetch('/generate', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ scene_idea: sceneIdea })
            });

            const data = await response.json();

            if (data.success) {
                this.displayResult(data);
                this.loadGallery();
                document.getElementById('scene-idea').value = '';
                this.showAlert('Story and image generated successfully!', 'success');
            } else {
                this.showAlert(data.error || 'Failed to generate content', 'danger');
            }

        } catch (error) {
            console.error('Error generating content:', error);
            this.showAlert('Network error. Please try again.', 'danger');
        } finally {
            this.showLoading(false);
            this.setButtonLoading(false);
        }
    }

    displayResult(data) {
        const resultsSection = document.getElementById('results-section');

        const imageSection = data.image_url ? `
            <div class="col-lg-6 mb-4">
                <div class="image-container">
                    <img src="${data.image_url}" alt="Generated Scene" class="img-fluid rounded shadow">
                </div>
            </div>` : '';

        const storyColClass = data.image_url ? 'col-lg-6' : 'col-12';

        const downloadButton = data.image_url ? `
            <button class="btn btn-outline-success btn-sm" onclick="inkBoard.downloadImage('${data.image_url}')">
                <i class="fas fa-download me-1"></i> Download Image
            </button>` : '';

        const resultHTML = `
            <div class="col-12 mb-5">
                <div class="card shadow-lg border-0">
                    <div class="card-body p-4">
                        <h4 class="card-title text-center mb-4"><i class="fas fa-sparkles me-2"></i>Your Creation</h4>
                        <div class="row">
                            ${imageSection}
                            <div class="${storyColClass}">
                                <div class="story-container">
                                    <h5 class="mb-3"><i class="fas fa-book-open me-2"></i>Your Story</h5>
                                    <p class="story-text">${data.story}</p>
                                    <div class="mt-3">
                                        <button class="btn btn-outline-primary btn-sm me-2" onclick="inkBoard.openJournal('${data.creation_id}')">
                                            <i class="fas fa-journal-whills me-1"></i>Add Journal Entry
                                        </button>
                                        ${downloadButton}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>`;

        resultsSection.innerHTML = resultHTML;
        resultsSection.scrollIntoView({ behavior: 'smooth' });
    }

    async loadGallery() {
        try {
            const response = await fetch('/get_creations');
            const data = await response.json();

            const galleryGrid = document.getElementById('gallery-grid');

            if (data.creations && data.creations.length > 0) {
                const galleryHTML = data.creations.map(creation => this.createGalleryItem(creation)).join('');
                galleryGrid.innerHTML = galleryHTML;
            } else {
                galleryGrid.innerHTML = `
                    <div class="col-12 text-center py-5">
                        <i class="fas fa-palette fa-3x text-muted mb-3"></i>
                        <h5 class="text-muted">No creations yet</h5>
                        <p class="text-muted">Start by describing a scene above!</p>
                    </div>`;
            }
        } catch (error) {
            console.error('Error loading gallery:', error);
        }
    }

    createGalleryItem(creation) {
        const journalEntry = creation.journal_entry ? `
            <div class="journal-entry">
                <small class="text-muted">
                    <i class="fas fa-journal-whills me-1"></i> Journal Entry:
                </small>
                <p class="mb-0 mt-1">${creation.journal_entry}</p>
            </div>` : '';

        const imageSection = creation.image_url ? `
            <img src="${creation.image_url}" alt="Scene: ${creation.scene_idea}" loading="lazy">` : '';

        const downloadButton = creation.image_url ? `
            <button class="btn btn-outline-success btn-sm" onclick="inkBoard.downloadImage('${creation.image_url}')">
                <i class="fas fa-download me-1"></i> Download
            </button>` : '';

        return `
            <div class="gallery-item">
                ${imageSection}
                <div class="gallery-item-content">
                    <div class="gallery-item-scene">
                        <i class="fas fa-quote-left me-1"></i> ${creation.scene_idea}
                    </div>
                    <div class="gallery-item-story">${creation.story}</div>
                    ${journalEntry}
                    <div class="gallery-item-actions">
                        <button class="btn btn-outline-primary btn-sm" onclick="inkBoard.openJournal('${creation.id}', '${creation.journal_entry || ''}')">
                            <i class="fas fa-journal-whills me-1"></i> ${creation.journal_entry ? 'Edit' : 'Add'} Journal
                        </button>
                        ${downloadButton}
                    </div>
                </div>
            </div>`;
    }

    openJournal(creationId, existingText = '') {
        this.currentCreationId = creationId;
        document.getElementById('journal-text').value = existingText;

        const modal = new bootstrap.Modal(document.getElementById('journalModal'));
        modal.show();
    }

    async saveJournal() {
        if (!this.currentCreationId) {
            this.showAlert('No creation selected', 'danger');
            return;
        }

        const journalText = document.getElementById('journal-text').value.trim();

        try {
            const response = await fetch('/save_journal', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    creation_id: this.currentCreationId,
                    journal_entry: journalText
                })
            });

            const data = await response.json();

            if (data.success) {
                this.showAlert('Journal entry saved!', 'success');
                this.loadGallery();

                const modal = bootstrap.Modal.getInstance(document.getElementById('journalModal'));
                modal.hide();
            } else {
                this.showAlert(data.error || 'Failed to save journal', 'danger');
            }
        } catch (error) {
            console.error('Error saving journal:', error);
            this.showAlert('Network error. Please try again.', 'danger');
        }
    }

    downloadImage(imageUrl) {
        const link = document.createElement('a');
        link.href = imageUrl;
        link.download = `inkboard-creation-${Date.now()}.png`;
        link.target = '_blank';
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        this.showAlert('Image download started!', 'success');
    }

    showLoading(show) {
        const loadingSection = document.getElementById('loading-section');
        const resultsSection = document.getElementById('results-section');

        if (show) {
            loadingSection.classList.remove('d-none');
            resultsSection.innerHTML = '';
        } else {
            loadingSection.classList.add('d-none');
        }
    }

    setButtonLoading(loading) {
        const btn = document.getElementById('generate-btn');
        const btnText = btn.querySelector('.btn-text');
        const spinner = btn.querySelector('.spinner-border');

        if (loading) {
            btn.classList.add('loading');
            btn.disabled = true;
            btnText.classList.add('d-none');
            spinner.classList.remove('d-none');
        } else {
            btn.classList.remove('loading');
            btn.disabled = false;
            btnText.classList.remove('d-none');
            spinner.classList.add('d-none');
        }
    }

    showAlert(message, type) {
        const existingAlert = document.querySelector('.alert');
        if (existingAlert) existingAlert.remove();

        const alert = document.createElement('div');
        alert.className = `alert alert-${type} alert-dismissible fade show`;
        alert.innerHTML = `${message}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>`;

        const main = document.querySelector('main');
        main.insertBefore(alert, main.firstChild);

        setTimeout(() => {
            if (alert.parentNode) alert.remove();
        }, 5000);
    }

    showSection(sectionName) {
        document.querySelectorAll('.dashboard-section').forEach(section => {
            section.classList.add('d-none');
        });

        document.querySelectorAll('.btn-nav').forEach(btn => {
            btn.classList.remove('active');
        });

        const targetSection = document.getElementById(`${sectionName}-section`);
        if (targetSection) targetSection.classList.remove('d-none');

        const buttonSelectors = {
            'create': 'Create Story',
            'gallery': 'Gallery',
            'journal': 'Journal'
        };

        document.querySelectorAll('.btn-nav').forEach(btn => {
            if (btn.textContent.trim().includes(buttonSelectors[sectionName])) {
                btn.classList.add('active');
            }
        });

        this.currentSection = sectionName;

        if (sectionName === 'gallery') {
            this.loadGallery();
        } else if (sectionName === 'journal') {
            this.loadJournalEntries();
        }
    }

    async loadJournalEntries() {
        try {
            const response = await fetch('/get_creations');
            const data = await response.json();

            const journalContainer = document.getElementById('journal-entries');
            const entriesWithJournal = data.creations?.filter(creation => creation.journal_entry) || [];

            if (entriesWithJournal.length > 0) {
                const journalHTML = entriesWithJournal.map(creation => `
                    <div class="journal-entry-card mb-3">
                        <h5>${creation.scene_idea}</h5>
                        <p>${creation.journal_entry}</p>
                    </div>`).join('');
                journalContainer.innerHTML = journalHTML;
            } else {
                journalContainer.innerHTML = `
                    <div class="col-12 text-center py-5">
                        <i class="fas fa-journal-whills fa-3x text-muted mb-3"></i>
                        <h5 class="text-muted">No journal entries yet</h5>
                    </div>`;
            }
        } catch (error) {
            console.error('Error loading journal entries:', error);
        }
    }
}

// Initialize
const inkBoard = new InkBoard();