$(function(){
//瀑布式加載
let scrollNow = 0,
loadBox = $(".vote-list-wrapper"),
loadLock = false,
nowPage = 1
$(window).on("scroll", function(){
if(loadBox[0].getBoundingClientRect().bottom < $(window).height() && !loadLock){
loadLock = true
$.ajax({
url: "/vote/ajax/ajax_get_vote_list.php",
type:"POST",
cache:false,
data:{
page: nowPage + 1,
cid: $('#Vote_Class_ID').val(),
},
dataType: 'json',
error:function(d){
alert('網路連線過慢,網頁請重新整理');
},
success:function(res){
//報名成功
loadBox.append(res.str)
nowPage += 1
if(res.end != 1){
loadLock = false
}
}
});
}
})
//彈窗
$(document).on("click", ".vote-list-item", function(e){
if(!$(e.target).hasClass("item-btn")){
getVotePopup("vote",$(this).data("id"))
}
})
//關閉跳窗
$(document).on("click", ".votePopup-close", function(){
$(".vote-popup").removeClass("in-active")
})
$(document).on("click", ".vote-popup", function(e){
if($(e.target).hasClass("vote-popup")){
$(".vote-popup").removeClass("in-active")
}
})
//複製連結
$(document).on("click", ".voteItem-community-copy", function(e){
window.navigator.clipboard.writeText($(this).data("copy"))
alert("複製成功")
})
//投票
$(document).on("click", ".voteItem-votebtn-btn, .item-btn", function(){
console.log('click vote')
const votedBox = `
`
let _this = $(this),
voteID = _this.data("id")
if(_this.hasClass("disabled")){
return false;
}
_this.attr("disabled",true); //防呆避免重複投票
$.ajax({
url: "/vote/ajax/ajax_vote_execute.php",
type:"POST",
cache:false,
dataType: "json",
data:{
id: voteID
},
error:function(d){
_this.attr("disabled",false); //防呆避免重複投票
alert('網路連線過慢,網頁請重新整理');
},
success:function(res){
_this.attr("disabled",false); //防呆避免重複投票
//報名成功
if(res.status == "OK"){
alert("投票成功")
_this.parents('.voteItem-wrapper').find('.voteItem-vote-num').html(res.vote_num)
$(".vote-list-item[data-id="+voteID+"]").find('.item-vote-num').html(res.vote_num)
if(res.voted == '0')
{
$('body').append(votedBox);
}
}else if(res.status == "ERROR"){
alert(res.msg);
if(res.url){
window.location.href = res.url;
}
}
if(res.remain <= 0){
$(".item-btn").addClass("disabled");
_this.addClass("disabled")
}
}
})
})
//搜尋
$(document).on("click", ".vote-search-btn", function(){
let keyword = $("#VoteSearch").val()
if(keyword){
$.ajax({
url: "/vote/ajax/ajax_search_votelist.php",
type:"POST",
cache:false,
data:{
keyword: keyword,
cid: $('#Vote_Class_ID').val(),
},
error:function(d){
alert('網路連線過慢,網頁請重新整理');
},
success:function(res){
loadBox.html(res)
loadLock = true
}
})
}else{
alert("請輸入關鍵字")
}
})
})
// ==========================================================
// 關閉voted-box
// ==========================================================
;(function($) {
$('body').on('click', '.voted-popup', function(e) {
if(e.target === e.currentTarget || $(e.target).hasClass('closer')) {
e.preventDefault()
$(e.currentTarget).remove()
}
})
})($)
function getVotePopup(type, id = "0"){
$.ajax({
url: "/vote/ajax/ajax_vote_popup.php",
type:"POST",
cache:false,
data:{
type : type, //type signup/vote
id : id
},
error:function(d){
alert('網路連線過慢,網頁請重新整理');
},
success:function(res){
//報名成功
$(".votePopup-box").html(res)
$(".vote-popup").addClass("in-active")
}
});
}