-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
186 lines (157 loc) · 5.76 KB
/
preload.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
const fs = require('fs');
const path = require('path');
const ipcRenderer = require("electron").ipcRenderer;
window.addEventListener('DOMContentLoaded', () => {
window.download_libraries();
window.init_content();
setTimeout(() => {
window.init_elements();
window.set_event_to_save_buttons()
}, 0);
document.getElementById('logout-yandex-button').onclick = () => {
ipcRenderer.send('logout');
}
document.getElementById('login-yandex-button').onclick = () => {
ipcRenderer.send('auth-window');
}
});
window.download_libraries = () => {
const downloadJSFiles = [
'app/general/drag-lib.js',
'app/general/bootstrap-lib.js',
'app/general/functions.js',
'app/general/drug.js'
];
downloadJSFiles.forEach((path) => {
let script = document.createElement('script');
script.src = path;
document.body.appendChild(script);
});
const downloadCssFiles = [
'app/general/bootstrap-lib.css',
'app/general/style.css'
];
downloadCssFiles.forEach((path) => {
let link = document.createElement('link');
link.rel = 'stylesheet';
link.href = path;
document.head.appendChild(link);
});
}
window.set_event_to_save_buttons = () => {
const saveButtons = document.getElementsByClassName('save_easy');
for (let i = 0; i < saveButtons.length; i++) {
saveButtons[i].onclick = function () {
window.save();
};
}
}
window.save = () => {
for (let elem of document.getElementsByClassName('draguble')) {
elem.classList.remove('draguble');
}
const content = document.getElementById('content-container').innerHTML;
saveOrganizerContent(content);
}
window.init_content = () => {
const contentContainer = document.getElementById('content-container');
ipcRenderer.send('auth-window');
}
ipcRenderer.on('auth-info', function (event, info) {
window.organizerYandexToken = info.token;
getOrganizerContent();
});
ipcRenderer.on('logout-processed', () => {
document.getElementById('content-container').innerHTML = '';
window.organizerYandexToken = null;
document.getElementById('login-yandex-button').style.display = 'block';
document.getElementById('logout-yandex-button').style.display = 'none';
});
function getOrganizerContent() {
fetch('https://cloud-api.yandex.net/v1/disk/resources?path=/organizer.html', {
"headers": {
'Authorization': `OAuth ${window.organizerYandexToken}`
},
"referrerPolicy": "strict-origin-when-cross-origin",
"method": "GET"
})
.then((response) => {
return response.json();
})
.then((data) => {
if (data.error === 'DiskNotFoundError') {
const template = fs.readFileSync(path.join(__dirname, 'content.html'));
saveOrganizerContent(template, true);
} else {
createContent(data.file);
}
});
}
function createContent(filepath) {
const contentContainer = document.getElementById('content-container');
fetch(filepath)
.then(response => response.text())
.then(data => {
contentContainer.innerHTML = data;
const activeFolders = contentContainer.getElementsByClassName('folder');
for (const folder of activeFolders) {
folder.classList.remove('folder-active');
if (folder.getAttribute('id') === 'global-folder') {
folder.classList.add('folder-active');
}
}
document.getElementById('login-yandex-button').style.display = 'none';
document.getElementById('logout-yandex-button').style.display = 'block';
window.dispatchEvent(new CustomEvent('content-created'));
})
.catch(error => {
console.error('Error fetching file:', error);
})
}
function saveOrganizerContent(content, isTemplate = false) {
const uploadUrl = 'https://cloud-api.yandex.net/v1/disk/resources/upload';
const params = {
path: '/organizer.html',
overwrite: 'true',
};
fetch(`${uploadUrl}?${new URLSearchParams(params)}`, {
method: 'GET',
headers: {
'Authorization': `OAuth ${window.organizerYandexToken}`
},
})
.then((response) => response.json())
.then((data) => {
const uploadLink = data.href;
return fetch(uploadLink, {
method: 'PUT',
headers: {
'Authorization': `OAuth ${window.organizerYandexToken}`
},
body: content,
});
})
.then(() => {
if (isTemplate === true) {
getOrganizerContent();
}
})
.catch((error) => {
console.error('Error uploading file:', error);
});
}
window.init_elements = () => {
const folders = [];
const files = fs.readdirSync(path.join(__dirname, 'app/element'));
files.forEach(file => {
let dropdown = fs.readFileSync(path.join(__dirname, 'app/element/' + file + '/dropdown.html'), 'utf8');
let fragment_dropdown = document.createRange().createContextualFragment(dropdown);
document.getElementById('element-dropdown-menu').appendChild(fragment_dropdown);
let script = document.createElement('script');
script.src = path.join(__dirname, 'app/element/' + file + '/script.js');
document.body.appendChild(script);
let modal = fs.readFileSync(path.join(__dirname, 'app/element/' + file + '/modal.html'), 'utf8');
let fragment_modal = document.createRange().createContextualFragment(modal);
document.body.appendChild(fragment_modal);
});
}