-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
148 lines (121 loc) · 4.93 KB
/
app.js
File metadata and controls
148 lines (121 loc) · 4.93 KB
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
const { ipcRenderer } = require('electron');
const path = require('path');
const fs = require('fs');
const Plyr = require('plyr');
const ffmpeg = require('fluent-ffmpeg');
const moment = require('moment');
let dragPlayer, dragPlayerEnd, toggleClickToPlayState
const RecommendationSystem = require('./recommendation');
const recommendationSystem = new RecommendationSystem();
// const VideoPlayerManager = require('./components/video-player-manager');
// window.videoPlayerManager = new VideoPlayerManager();
const isDevelopment = process.env.NODE_ENV === "development";
let ffprobe;
if (isDevelopment) {
ffprobe = require('ffprobe-static');
console.log('DEV');
} else {
ffmpeg.setFfmpegPath(path.join(process.resourcesPath, 'ffmpeg.exe'));
ffmpeg.setFfprobePath(path.join(process.resourcesPath, 'ffprobe.exe'));
console.log('PROD');
}
const loadedScripts = {};
window.currentVideo = null;
// И добавьте функцию для установки текущего видео:
function setCurrentVideo(videoPath) {
window.currentVideo = videoPath;
}
window.customEvents = new EventTarget();
window.customEvents.on = function(eventName, handler) {
this.addEventListener(eventName, (event) => handler(event.detail));
};
// Экспортируйте эту функцию:
window.appFunctions = {
setCurrentVideo,
loadPage
};
// Функция для загрузки HTML компонентов
async function loadComponent(id, componentPath) {
try {
const response = await fetch(componentPath);
if (!response.ok) throw new Error(`Failed to fetch component at ${componentPath}`);
const html = await response.text();
document.getElementById(id).innerHTML = html;
} catch (error) {
console.error("Error loading component:", error);
}
}
async function loadPage(pageName) {
try {
// Очищаем предыдущие ресурсы
cleanupCurrentPage();
const response = await fetch(`./pages/${pageName}.html`);
if (!response.ok) throw new Error(`Failed to fetch page at ./pages/${pageName}.html`);
const html = await response.text();
document.getElementById("page-content").innerHTML = html;
// Удаляем предыдущий скрипт
if (loadedScripts[pageName]) {
document.body.removeChild(loadedScripts[pageName]);
delete loadedScripts[pageName];
}
const script = document.createElement('script');
script.src = `./pages/${pageName}.js`;
script.onload = () => {
loadedScripts[pageName] = script;
};
document.body.appendChild(script);
} catch (error) {
console.error("Error loading page:", error);
}
}
function cleanupCurrentPage() {
// Очищаем все обработчики событий
const pageContent = document.getElementById("page-content");
const oldElement = pageContent.cloneNode(false);
pageContent.parentNode.replaceChild(oldElement, pageContent);
// Очищаем память от неиспользуемых ресурсов
if (window.gc) window.gc();
}
let startupVideo = false;
async function initApp() {
// Загрузка компонентов
await loadComponent("header-component", "./components/header.html");
// Загрузка начальной страницы
if(startupVideo === false)
await loadPage("index");
if (!loadedScripts['header']) {
const headerScript = document.createElement('script');
headerScript.src = './components/header.js';
headerScript.onload = () => {
loadedScripts['header'] = headerScript;
};
document.body.appendChild(headerScript);
}
}
ipcRenderer.on('startup-video', (event, videoPath) => {
if (fs.existsSync(videoPath)) {
startupVideo = true;
setCurrentVideo(videoPath);
loadPage('player');
}
});
// Запуск инициализации после загрузки DOM
document.addEventListener("DOMContentLoaded", initApp);
// Обработчик для переключения страниц
ipcRenderer.on('navigate', (event, pageName) => {
loadPage(pageName);
});
ipcRenderer.on('load-video', (event, videoPath) => {
if (document.getElementById('video-player')) {
// Если элемент плеера существует, отправляем событие загрузки видео
document.dispatchEvent(new CustomEvent('video-ready-to-load', { detail: videoPath }));
} else {
// Если элемент плеера еще не создан, ждем его создания
const checkInterval = setInterval(() => {
if (document.getElementById('video-player')) {
clearInterval(checkInterval);
document.dispatchEvent(new CustomEvent('video-ready-to-load', { detail: videoPath }));
}
}, 100);
}
});