-
Notifications
You must be signed in to change notification settings - Fork 2
/
pldb-manage.html
375 lines (342 loc) · 14 KB
/
pldb-manage.html
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>管理相册</title>
<style>
/* 全局样式 */
body, html {
height: 100%;
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
/* 返回首页按钮样式 */
#returnHome {
position: fixed;
top: 10px;
right: 150px; /* 根据备份相册按钮的宽度调整,使其与备份相册按钮平齐 */
padding: 10px 20px; /* 与备份相册按钮的内边距相同 */
background-color: #28a745; /* 绿色背景 */
color: white;
border: none;
cursor: pointer;
}
/* 备份按钮样式 */
#backupAlbum {
position: fixed;
top: 10px;
right: 10px;
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
cursor: pointer;
}
/* 模态窗口(lightbox)样式 */
.modal {
display: none;
position: fixed;
z-index: 1000; /* 确保模态窗口在最上层 */
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.9);
}
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
.close {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}
.close:hover,
.close:focus {
color: #bbb;
text-decoration: none;
cursor: pointer;
}
/* 图片和删除按钮的容器样式 */
.img-container {
display: inline-block;
position: relative;
margin: 5px;
width: 150px; /* 设置图框的固定宽度 */
height: 100px; /* 设置图框的固定高度 */
background-color: rgba(255, 255, 255, 0); /* 半透明背景 */
background-repeat: no-repeat;
background-position: center;
background-size: cover;
text-align: center; /* 使文本居中 */
line-height: 100px; /* 使文本垂直居中 */
}
.img-container img {
width: 100%;
height: 100%;
object-fit: cover; /* 这将确保图片按比例缩放并填充整个容器 */
}
.img-text {
display: block; /* 默认不显示文本 */
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
padding: 5px; /* 添加内边距 */
box-sizing: border-box; /* 确保内边距不会影响总宽度和高度 */
background-color: rgba(255, 255, 255, 0.5); /* 半透明背景 */
color: black; /* 文本颜色 */
z-index: -1; /* 将文本放在底层 */
word-wrap: break-word; /* 允许自动换行 */
line-height: 2; /* 设置行间距为字体高度的2倍 */
overflow: hidden; /* 隐藏溢出的文本 */
}
.img-container:hover img {
box-shadow: inset 0 0 0 5px green; /* 鼠标悬停时添加金色边框 opacity: 0.9; /* 鼠标悬停时图片半透明 */*/
}
.delete-btn {
position: absolute;
top: 0;
left: 0;
background-color: red;
color: white;
border: none;
cursor: pointer;
z-index: 10; /* 确保删除按钮在最上层 */
}
.modal-open .delete-btn {
display: none; /* 当模态窗口打开时隐藏删除按钮 */
}
@media (max-width: 768px) {
.modal-content {
width: 90%;
}
.img-container {
width: calc(50% - 10px);
height: 150px; /* 调整为适合移动设备的大小 */
}
}
.photo-grid {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-gap: 10px;
margin-top: 20px;
}
.pagination-container {
text-align: center;
margin-top: 20px;
}
.pagination-button {
display: inline-block;
padding: 5px 10px;
margin: 0 5px;
background-color: #f2f2f2;
border: 1px solid #ccc;
border-radius: 5px;
cursor: pointer;
}
.pagination-button:hover {
background-color: #e9e9e9;
}
.pagination-active {
background-color: #28a745; /* 绿色背景 */
color: white; /* 白色文字 */
}
</style>
</head>
<body>
<!-- 添加返回首页按钮 -->
<button id="returnHome">返回首页</button>
<button id="backupAlbum">备份相册</button>
<h1 id="albumTitle"></h1>
<div id="photoWall"></div>
<textarea id="photoUrl" placeholder="输入图片URL地址,每行一个;无输入请选择图片上传:" cols="60" rows="5"></textarea>
<button id="submitPhoto">批量添加图片</button>
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
</div>
<script>
const params = new URLSearchParams(window.location.search);
const username = params.get('username');
const dirName = params.get('directory');
document.getElementById('albumTitle').textContent = `${dirName} 相册`;
const paginationContainer = document.createElement('div'); // Create a new pagination container element
paginationContainer.id = 'pagination'; // Set the id of the pagination container
document.body.appendChild(paginationContainer); // Append the pagination container to the body
const photoWall = document.getElementById('photoWall');
//const photoUrls = document.getElementById('photoUrl').value.trim().split('\n');
const photoUrlbox = document.getElementById('photoUrl');
photoWall.insertAdjacentElement('afterend', paginationContainer); // Insert the pagination container after the photoWall
photoUrlbox.insertAdjacentElement('beforebegin', paginationContainer); // Insert the pagination container before the photoUrl
let photoUrls = [];
function showModal(url) {
document.body.classList.add('modal-open');
document.getElementById('myModal').style.display = "block";
document.getElementById('img01').src = url;
}
async function createPhotoContainer(url) {
let imgContainer = document.createElement('div');
imgContainer.classList.add('img-container');
let imgText = document.createElement('div');
imgText.classList.add('img-text');
imgText.textContent = url;
imgContainer.appendChild(imgText);
let img = document.createElement('img');
const compressedImageUrl = await compressImage(url);//压缩预览图片
img.src = compressedImageUrl;
//img.src = url;
img.style.width = '100%';
img.style.height = '100%';
img.onclick = function() {
showModal(url);
}
imgContainer.appendChild(img);
let deleteButton = document.createElement('button');
deleteButton.textContent = '删除';
deleteButton.classList.add('delete-btn');
deleteButton.onclick = function() {
if (confirm('确定要删除这张图片吗?')) {
deletePhoto(url);
}
};
imgContainer.appendChild(deleteButton);
return imgContainer;
}
async function getPhotos() {
const response = await fetch(`./db/getdbab`, {
method: 'POST',
body: JSON.stringify({ username, dirName }),
headers: { 'Content-Type': 'application/json' }
});
photoUrls = await response.json();
photoWall.innerHTML = '';
await updatePagination(itemsPerPage);
}
async function deletePhoto(photoUrl) {
const response = await fetch('./db/deledbpho', {
method: 'POST',
body: JSON.stringify({ username, dirName, photoUrl }),
headers: { 'Content-Type': 'application/json' }
});
if (response.ok) {
alert('图片删除成功,请稍后刷新信息');
window.location.reload();
} else {
alert('图片删除失败,请稍后重试.....');
console.error('Failed to delete photo');
}
}
var modal = document.getElementById('myModal');
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
document.body.classList.remove('modal-open');
modal.style.display = "none";
}
async function AsubmitPhoto() {
const photoUrls = document.getElementById('photoUrl').value.trim().split('\n');
const validPhotoUrls = photoUrls.filter(url => url.trim() !== '');
const photoUrlsString = validPhotoUrls.join('\n');
const response = await fetch('./db/addbpho', {
method: 'POST',
body: JSON.stringify({ username, dirName, photoUrlsString }),
headers: { 'Content-Type': 'application/json' }
});
if (response.ok) {
window.location.reload();
} else {
console.error('Failed to submit photo');
}
}
document.getElementById('backupAlbum').onclick = function() {
const photoUrlsString = photoUrls.join('\n').replace(/(^\s*\n)|(\n\s*$)/g, '');
const blob = new Blob([photoUrlsString], { type: 'text/plain' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = `${dirName}.txt`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
getPhotos();
// 返回首页按钮的点击事件
document.getElementById('returnHome').onclick = function() {
window.location.href = '/'; // 这里的 '/' 表示网站的根目录,根据实际情况修改
};
let currentPage = 1;
const itemsPerPage = 15; // 5 columns * 3 rows = 15 items per page
async function displayPhotos(startIndex, endIndex) {
photoWall.innerHTML = ''; // Clear existing photos before displaying new ones
for (let i = startIndex; i < endIndex; i++) {
let url = photoUrls[i];
if (url) {
let imgContainer = await createPhotoContainer(url);
photoWall.appendChild(imgContainer);
}
}
}
async function lazyLoadPhotos(pageNumber) {
currentPage = pageNumber; // 更新当前页码
const startIndex = (currentPage - 1) * itemsPerPage;
const endIndex = currentPage * itemsPerPage;
await displayPhotos(startIndex, endIndex);
document.querySelectorAll('.pagination-button').forEach(btn => btn.classList.remove('pagination-active'));
document.querySelectorAll('.pagination-button')[currentPage - 1].classList.add('pagination-active'); // Highlight the current page button
}
async function updatePagination(photosPerPage) {
const totalPhotos = photoUrls.length; // Calculate total photos from photoUrls array
const totalPages = Math.ceil(totalPhotos / photosPerPage);
paginationContainer.innerHTML = ''; // Clear existing pagination buttons
for (let i = 1; i <= totalPages; i++) {
let pageButton = document.createElement('button');
pageButton.textContent = i;
pageButton.classList.add('pagination-button');
if (i === currentPage) {
pageButton.classList.add('pagination-active'); // Highlight the current page button
}
pageButton.addEventListener('click', function(page) {
return function() {
lazyLoadPhotos(page);
document.querySelectorAll('.pagination-button').forEach(btn => btn.classList.remove('active'));
pageButton.classList.add('active'); // Update active class on click
};
}(i));
paginationContainer.appendChild(pageButton);
};
await lazyLoadPhotos(totalPages); // Call lazyLoadPhotos after updating pagination
}
async function compressImage(url) {
try {
const response = await fetch(`https://nagose-compresizeimg.hf.space/resize?q=${encodeURIComponent(url)}`);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const imageUrl = await response.text(); // 直接获取文本响应
// 你可以在这里使用压缩后的图像 URL,例如在网页上显示图像
console.log('Compressed image URL:', imageUrl);
//return imageUrl;
return `data:image/jpeg;base64,${imageUrl}`; // 返回带有前缀的 Base64 数据
} catch (error) {
console.error('Error compressing image:', error);
}
}
// 示例调用
//compressImage('https://example.com/path/to/your/image.jpg');
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/browser-image-compression/2.0.2/browser-image-compression.min.js"></script>
<script src="album-management.js"></script>
</body>
</html>