Wiuhh commited on
Commit
ff177ec
·
verified ·
1 Parent(s): c33ad35

Create static/js/app.js

Browse files
Files changed (1) hide show
  1. static/js/app.js +188 -0
static/js/app.js ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class SketchConverter {
2
+ constructor() {
3
+ this.initializeElements();
4
+ this.attachEventListeners();
5
+ this.currentFile = null;
6
+ this.currentSketchUrl = null;
7
+ }
8
+
9
+ initializeElements() {
10
+ this.uploadArea = document.getElementById('upload-area');
11
+ this.processingArea = document.getElementById('processing-area');
12
+ this.resultArea = document.getElementById('result-area');
13
+ this.settingsArea = document.getElementById('settings-area');
14
+ this.fileInput = document.getElementById('file-input');
15
+ this.browseBtn = document.getElementById('browse-btn');
16
+ this.originalImage = document.getElementById('original-image');
17
+ this.sketchImage = document.getElementById('sketch-image');
18
+ this.downloadBtn = document.getElementById('download-btn');
19
+ this.newUploadBtn = document.getElementById('new-upload-btn');
20
+ this.blurSelect = document.getElementById('blur-select');
21
+ this.enhanceCheck = document.getElementById('enhance-check');
22
+ }
23
+
24
+ attachEventListeners() {
25
+ // File input and browse button
26
+ this.browseBtn.addEventListener('click', () => this.fileInput.click());
27
+ this.fileInput.addEventListener('change', (e) => this.handleFileSelect(e.target.files[0]));
28
+
29
+ // Drag and drop
30
+ this.uploadArea.addEventListener('dragover', (e) => this.handleDragOver(e));
31
+ this.uploadArea.addEventListener('dragleave', (e) => this.handleDragLeave(e));
32
+ this.uploadArea.addEventListener('drop', (e) => this.handleDrop(e));
33
+
34
+ // Result actions
35
+ this.downloadBtn.addEventListener('click', () => this.downloadSketch());
36
+ this.newUploadBtn.addEventListener('click', () => this.resetInterface());
37
+
38
+ // Settings
39
+ this.blurSelect.addEventListener('change', () => this.processCurrentFile());
40
+ this.enhanceCheck.addEventListener('change', () => this.processCurrentFile());
41
+ }
42
+
43
+ handleDragOver(e) {
44
+ e.preventDefault();
45
+ e.stopPropagation();
46
+ this.uploadArea.classList.add('border-primary');
47
+ }
48
+
49
+ handleDragLeave(e) {
50
+ e.preventDefault();
51
+ e.stopPropagation();
52
+ this.uploadArea.classList.remove('border-primary');
53
+ }
54
+
55
+ handleDrop(e) {
56
+ e.preventDefault();
57
+ e.stopPropagation();
58
+ this.uploadArea.classList.remove('border-primary');
59
+
60
+ const files = e.dataTransfer.files;
61
+ if (files.length > 0) {
62
+ this.handleFileSelect(files[0]);
63
+ }
64
+ }
65
+
66
+ handleFileSelect(file) {
67
+ if (!file) return;
68
+
69
+ // Validate file type
70
+ if (!file.type.startsWith('image/')) {
71
+ this.showError('Please select a valid image file (JPG, PNG, etc.)');
72
+ return;
73
+ }
74
+
75
+ // Validate file size (max 10MB)
76
+ if (file.size > 10 * 1024 * 1024) {
77
+ this.showError('File size must be less than 10MB');
78
+ return;
79
+ }
80
+
81
+ this.currentFile = file;
82
+ this.showOriginalImage(file);
83
+ this.processCurrentFile();
84
+ }
85
+
86
+ showOriginalImage(file) {
87
+ const reader = new FileReader();
88
+ reader.onload = (e) => {
89
+ this.originalImage.src = e.target.result;
90
+ };
91
+ reader.readAsDataURL(file);
92
+ }
93
+
94
+ async processCurrentFile() {
95
+ if (!this.currentFile) return;
96
+
97
+ this.showProcessing();
98
+
99
+ const formData = new FormData();
100
+ formData.append('image', this.currentFile);
101
+ formData.append('blur', this.blurSelect.value);
102
+ formData.append('enhance', this.enhanceCheck.checked);
103
+
104
+ try {
105
+ const response = await fetch('/convert', {
106
+ method: 'POST',
107
+ body: formData
108
+ });
109
+
110
+ if (!response.ok) {
111
+ const errorData = await response.json();
112
+ throw new Error(errorData.error || 'Failed to process image');
113
+ }
114
+
115
+ const result = await response.json();
116
+ this.showResult(result.sketch_url);
117
+ } catch (error) {
118
+ console.error('Error processing image:', error);
119
+ this.showError(error.message || 'Failed to process image. Please try again.');
120
+ }
121
+ }
122
+
123
+ showProcessing() {
124
+ this.hideAllAreas();
125
+ this.processingArea.classList.remove('hidden');
126
+ this.settingsArea.classList.remove('hidden');
127
+ }
128
+
129
+ showResult(sketchUrl) {
130
+ this.currentSketchUrl = sketchUrl;
131
+ this.sketchImage.src = sketchUrl;
132
+
133
+ this.hideAllAreas();
134
+ this.resultArea.classList.remove('hidden');
135
+ this.settingsArea.classList.remove('hidden');
136
+ }
137
+
138
+ hideAllAreas() {
139
+ this.uploadArea.classList.add('hidden');
140
+ this.processingArea.classList.add('hidden');
141
+ this.resultArea.classList.add('hidden');
142
+ this.settingsArea.classList.add('hidden');
143
+ }
144
+
145
+ resetInterface() {
146
+ this.currentFile = null;
147
+ this.currentSketchUrl = null;
148
+ this.fileInput.value = '';
149
+
150
+ this.hideAllAreas();
151
+ this.uploadArea.classList.remove('hidden');
152
+
153
+ // Reset settings to defaults
154
+ this.blurSelect.value = '21';
155
+ this.enhanceCheck.checked = false;
156
+ }
157
+
158
+ downloadSketch() {
159
+ if (!this.currentSketchUrl) return;
160
+
161
+ const link = document.createElement('a');
162
+ link.href = this.currentSketchUrl;
163
+ link.download = 'sketch.jpg';
164
+ document.body.appendChild(link);
165
+ link.click();
166
+ document.body.removeChild(link);
167
+ }
168
+
169
+ showError(message) {
170
+ // Create a simple toast notification
171
+ const toast = document.createElement('div');
172
+ toast.className = 'fixed top-4 right-4 bg-red-500 text-white px-6 py-3 rounded-lg z-50';
173
+ toast.textContent = message;
174
+
175
+ document.body.appendChild(toast);
176
+
177
+ setTimeout(() => {
178
+ document.body.removeChild(toast);
179
+ }, 5000);
180
+
181
+ this.resetInterface();
182
+ }
183
+ }
184
+
185
+ // Initialize the application when DOM is loaded
186
+ document.addEventListener('DOMContentLoaded', () => {
187
+ new SketchConverter();
188
+ });