Files

165 lines
6.7 KiB
JavaScript
Raw Permalink Normal View History

2022-08-10 17:38:05 +08:00
jQuery(document).ready(function () {
2022-09-01 02:47:17 +08:00
function getImgPosition(e, imgEle) {
2022-09-01 18:31:09 +08:00
// console.log(e, imgEle)
2022-08-23 02:25:51 +08:00
let imgWidth = imgEle.prop('naturalWidth')
let imgHeight = imgEle.prop("naturalHeight")
2022-08-31 23:08:26 +08:00
let ratio = imgWidth / imgHeight;
2022-09-01 02:47:17 +08:00
let offsetX = 10;
let offsetY = 10;
2022-09-01 18:31:09 +08:00
let width = window.innerWidth - e.clientX;
let height = window.innerHeight - e.clientY;
2022-09-01 12:47:31 +08:00
let changeOffsetY = 0;
2022-09-01 02:47:17 +08:00
let changeOffsetX = false;
2022-09-01 19:08:19 +08:00
if (e.clientX > window.innerWidth / 2 && e.clientX + imgWidth > window.innerWidth) {
2022-09-01 02:47:17 +08:00
changeOffsetX = true
2022-09-01 18:31:09 +08:00
width = e.clientX
2022-09-01 02:47:17 +08:00
}
2022-09-01 18:31:09 +08:00
if (e.clientY > window.innerHeight / 2) {
if (e.clientY + imgHeight/2 > window.innerHeight) {
2022-09-01 12:47:31 +08:00
changeOffsetY = 1
2022-09-01 18:31:09 +08:00
height = e.clientY
} else if (e.clientY + imgHeight > window.innerHeight) {
2022-09-01 12:47:31 +08:00
changeOffsetY = 2
2022-09-01 18:31:09 +08:00
height = e.clientY
2022-09-01 12:47:31 +08:00
}
2022-08-31 23:08:26 +08:00
}
2022-09-01 03:02:48 +08:00
let log = `innerWidth: ${window.innerWidth}, innerHeight: ${window.innerHeight}, pageX: ${e.pageX}, pageY: ${e.pageY}, imgWidth: ${imgWidth}, imgHeight: ${imgHeight}, width: ${width}, height: ${height}, offsetX: ${offsetX}, offsetY: ${offsetY}, changeOffsetX: ${changeOffsetX}, changeOffsetY: ${changeOffsetY}`
2022-09-01 02:47:17 +08:00
console.log(log)
if (imgWidth > width) {
imgWidth = width;
2022-09-01 03:02:48 +08:00
imgHeight = imgWidth / ratio;
2022-09-01 02:47:17 +08:00
}
if (imgHeight > height) {
imgHeight = height;
2022-08-31 23:08:26 +08:00
imgWidth = imgHeight * ratio;
}
2022-09-01 02:47:17 +08:00
if (changeOffsetX) {
2022-09-01 19:08:19 +08:00
offsetX = -(e.clientX - width + 10)
2022-09-01 02:47:17 +08:00
}
2022-09-01 12:47:31 +08:00
if (changeOffsetY == 1) {
2022-09-01 19:08:19 +08:00
offsetY = - (imgHeight - (window.innerHeight - e.clientY))
2022-09-01 12:47:31 +08:00
} else if (changeOffsetY == 2) {
offsetY = - imgHeight/2
2022-08-23 02:25:51 +08:00
}
2022-09-01 02:47:17 +08:00
return {imgWidth, imgHeight,offsetX, offsetY}
}
// preview
function getPosition(e, position) {
2025-11-11 22:20:54 +08:00
if (!position) {
return {};
}
2022-09-01 02:47:17 +08:00
return {
left: e.pageX + position.offsetX,
top: e.pageY + position.offsetY,
width: position.imgWidth,
height: position.imgHeight
2022-08-23 02:25:51 +08:00
}
}
2022-09-05 15:29:39 +08:00
var previewEle = jQuery('#nexus-preview')
2022-09-01 02:47:17 +08:00
var imgEle, selector = 'img.preview', imgPosition
2022-08-24 13:36:14 +08:00
jQuery("body").on("mouseover", selector, function (e) {
2022-08-23 02:25:51 +08:00
imgEle = jQuery(this);
2022-09-05 15:29:39 +08:00
// previewEle = jQuery('<img style="display: none;position:absolute;">').appendTo(imgEle.parent())
2022-09-01 02:47:17 +08:00
imgPosition = getImgPosition(e, imgEle)
let position = getPosition(e, imgPosition)
2022-08-23 02:25:51 +08:00
let src = imgEle.attr("src")
2022-08-10 17:38:05 +08:00
if (src) {
2025-02-12 11:02:22 +08:00
previewEle.stop(true, true).attr("src", src).css(position).fadeIn("fast");
2022-08-10 17:38:05 +08:00
}
2022-08-24 13:36:14 +08:00
}).on("mouseout", selector, function (e) {
2022-09-05 15:29:39 +08:00
// previewEle.remove()
// previewEle = null
previewEle.hide()
2022-08-24 13:36:14 +08:00
}).on("mousemove", selector, function (e) {
2022-09-01 02:47:17 +08:00
let position = getPosition(e, imgPosition)
2022-08-23 02:25:51 +08:00
previewEle.css(position)
2022-08-21 15:22:08 +08:00
})
2022-08-24 13:36:14 +08:00
2022-08-31 23:08:26 +08:00
// lazy load
if ("IntersectionObserver" in window) {
2025-11-11 22:20:54 +08:00
const fallbackImage = 'pic/misc/spinner.svg';
const domainList = ['img1.doubanio.com', 'img2.doubanio.com', 'img3.doubanio.com', 'img9.doubanio.com'];
const imgList = [...document.querySelectorAll('.nexus-lazy-load')];
const loadedImages = {};
const io = new IntersectionObserver((entries) => {
entries.forEach(entry => {
const el = entry.target;
const intersectionRatio = entry.intersectionRatio;
2022-09-01 23:48:50 +08:00
if (intersectionRatio > 0 && intersectionRatio <= 1 && !el.classList.contains('preview')) {
2025-11-11 22:20:54 +08:00
let src = el.dataset.src;
if (src && src.includes('doubanio.com') && src.includes('l_ratio_poster')) {
src = src.replace('l_ratio_poster', 's_ratio_poster');
el.dataset.src = src;
}
el.src = src;
el.classList.add('preview');
loadedImages[src] = true;
el.onload = el.onerror = () => io.unobserve(el);
el.onerror = () => handleImageError(el, src);
2022-08-31 23:08:26 +08:00
}
2025-11-11 22:20:54 +08:00
});
});
imgList.forEach(img => io.observe(img));
function handleImageError(img, currentSrc) {
if (!currentSrc.includes('doubanio.com')) {
img.src = fallbackImage;
} else {
tryNextDomain(img, currentSrc, 0);
}
}
function tryNextDomain(img, currentSrc, index = 0) {
if (index >= domainList.length) {
img.src = fallbackImage;
return;
}
img.src = currentSrc.replace(/https:\/\/[a-zA-Z0-9.-]+\.doubanio\.com/, `https://${domainList[index]}`);
img.onerror = () => tryNextDomain(img, currentSrc, index + 1);
}
2022-08-31 23:08:26 +08:00
}
2022-09-05 19:23:10 +08:00
//claim
jQuery("body").on("click", "[data-claim_id]", function () {
let _this = jQuery(this)
let box = _this.closest('td')
let claimId = _this.attr("data-claim_id")
let torrentId = _this.attr("data-torrent_id")
let action = _this.attr("data-action")
let reload = _this.attr("data-reload")
let confirmText = _this.attr("data-confirm")
let showStyle = "width: max-content;display: flex;align-items: center";
let hideStyle = "width: max-content;display: none;align-items: center";
let params = {}
if (claimId > 0) {
params.id = claimId
} else {
params.torrent_id = torrentId
}
let modalConfig = {title: "Info", btn: ['OK', 'Cancel'], btnAlign: 'c'}
layer.confirm(confirmText, modalConfig, function (confirmIndex) {
jQuery.post("ajax.php", {"action": action, params: params}, function (response) {
console.log(response)
if (response.ret != 0) {
layer.alert(response.msg, modalConfig)
return
}
if (reload > 0) {
window.location.reload();
return;
}
if (claimId > 0) {
//do remove, show add
box.find("[data-action=addClaim]").attr("style", showStyle).attr("data-claim_id", 0)
box.find("[data-action=removeClaim]").attr("style", hideStyle)
} else {
//do add, show remove, update claim_id
box.find("[data-action=addClaim]").attr("style", hideStyle)
box.find("[data-action=removeClaim]").attr("style", showStyle).attr("data-claim_id", response.data.id)
}
layer.close(confirmIndex)
}, "json")
})
})
2022-08-10 17:38:05 +08:00
})