-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscr3.js
216 lines (194 loc) · 8.43 KB
/
scr3.js
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
// Enhanced Video App Module
if (typeof VideoApp === 'undefined') {
const VideoApp = (function() {
// Enhanced state combining both implementations
const state = {
store: null,
currentProjects: [],
currentFilter: 'all',
searchQuery: '',
currentSort: 'dateDesc', // Changed to match the select options
lastVideoLoad: 0,
favorites: new Set(),
loadingStates: new Map(),
initialized: false,
userReviews: {}, // Added from VideoManager
userRecommendations: new Set() // Added from VideoManager
};
// Initialize store (keeping original implementation)
async function initializeStore() {
if (!state.store) {
state.store = localforage.createInstance({
name: 'approVideo',
storeName: 'projects'
});
}
}
// Enhanced video data loading
async function loadVideoData() {
try {
if (typeof videoData !== 'undefined') {
console.log('Loading from videoData variable');
return videoData;
}
console.log('Loading from file');
const response = await fetch('./videoData2b.js');
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const text = await response.text();
const cleanedText = text.replace(/^(?:var|const|let)\s+\w+\s*=\s*/, '').replace(/;$/, '');
return JSON.parse(cleanedText);
} catch (error) {
console.error('Error loading video data:', error);
showError('Failed to load video data');
return [];
}
}
// Enhanced initialize data function
async function initializeData() {
try {
const videoData = await loadVideoData();
console.log('Loaded video data:', videoData);
const existingData = await state.store.getItem('projects');
if (!existingData && videoData) {
await state.store.setItem('projects', videoData);
}
populateCategories(videoData); // Added category population
await loadFeed();
setupEventListeners();
} catch (error) {
console.error('Error initializing data:', error);
showError('Failed to initialize data');
}
}
// Added category population from VideoManager
function populateCategories(videos) {
const categoryFilter = document.getElementById('categoryFilter');
if (!categoryFilter || !videos.length) return;
const categories = new Set();
videos.forEach(video => {
if (video.category) categories.add(video.category);
});
categoryFilter.innerHTML = '<option value="all">All Categories</option>';
[...categories].sort().forEach(category => {
const option = document.createElement('option');
option.value = category;
option.textContent = category;
categoryFilter.appendChild(option);
});
}
// Enhanced event listeners
function setupEventListeners() {
const searchInput = document.getElementById('searchInput');
const sortSelect = document.getElementById('sortSelect');
const categoryFilter = document.getElementById('categoryFilter');
if (searchInput) {
searchInput.addEventListener('input', debounce((e) => {
state.searchQuery = e.target.value;
loadFeed();
}, 300));
}
if (sortSelect) {
sortSelect.addEventListener('change', (e) => {
state.currentSort = e.target.value;
loadFeed();
});
}
if (categoryFilter) {
categoryFilter.addEventListener('change', (e) => {
state.currentFilter = e.target.value;
loadFeed();
});
}
}
// Enhanced project rendering
function renderProjects(projects) {
const projectsList = document.getElementById('videoGrid'); // Changed to match HTML
if (!projectsList) return;
projectsList.innerHTML = projects.map(project => `
<div class="project-card bg-white rounded-lg shadow-md overflow-hidden">
<div class="aspect-w-16 aspect-h-9 bg-gray-100">
<img src="${project.thumbnail}"
alt="${project.title}"
class="w-full h-48 object-cover"
onerror="this.src='/placeholder-image.jpg'"
>
</div>
<div class="p-4">
<div class="flex justify-between items-start">
<h3 class="text-lg font-semibold text-gray-900">${project.title}</h3>
<button onclick="VideoApp.toggleFavorite('${project.id}')"
class="text-yellow-500 hover:text-yellow-600">
${state.favorites.has(project.id) ? '★' : '☆'}
</button>
</div>
<p class="text-gray-600 mb-4">${project.description.substring(0, 100)}...</p>
<div class="mt-2 flex items-center justify-between text-sm text-gray-500">
<span>Rating: ${project.rating}/5</span>
<span>Category: ${project.category}</span>
</div>
<button onclick="VideoApp.showProjectDetails(${JSON.stringify(project)})"
class="mt-3 w-full bg-blue-600 text-white py-2 px-4 rounded-md hover:bg-blue-700">
View Details
</button>
</div>
</div>
`).join('');
}
// Added debounce utility
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Enhanced error display
function showError(message) {
const errorDiv = document.createElement('div');
errorDiv.className = 'fixed top-16 right-4 max-w-sm z-50 p-4 rounded-lg shadow-lg bg-red-100 text-red-800';
errorDiv.textContent = message;
document.body.appendChild(errorDiv);
setTimeout(() => errorDiv.remove(), 3000);
}
// Added favorite toggle functionality
function toggleFavorite(projectId) {
if (state.favorites.has(projectId)) {
state.favorites.delete(projectId);
} else {
state.favorites.add(projectId);
}
localStorage.setItem('approVideo_favorites', JSON.stringify([...state.favorites]));
loadFeed(); // Refresh display
}
// Keep existing functions unchanged
// initialize, loadFeed, showLoading, showProjectDetails, playVideo,
// closeProjectModal, handleLogin, handleLogout, showLogin, showDashboard
// Return enhanced public API
return {
initialize,
handleLogin,
handleLogout,
showProjectDetails,
playVideo,
closeProjectModal,
toggleFavorite,
isLoggedIn: () => !!localStorage.getItem('approVideoToken'),
getState: () => ({...state}) // Added for debugging
};
})();
window.VideoApp = VideoApp;
}
// Initialize on page load
document.addEventListener('DOMContentLoaded', () => {
if (window.VideoApp) {
VideoApp.initialize().catch(error => {
console.error('Failed to initialize app:', error);
});
}
});