This commit is contained in:
BoHung Chiu 2022-09-12 22:41:44 +08:00
parent c952afb56d
commit 56a4f3a3f8
2 changed files with 187 additions and 179 deletions

View File

@ -91,9 +91,17 @@ class AdImage
def embed_url def embed_url
uri = URI.parse(self.youtube) rescue nil uri = URI.parse(self.youtube) rescue nil
if !uri.nil? && uri.host == "www.youtube.com" if !uri.nil? && uri.host == "www.youtube.com"
params = CGI.parse(uri.query) if uri.path.start_with?('/embed/')
v = params['v'].first return self.youtube.sub(/http(s|):\/\//,'//')
return "//www.youtube.com/embed/#{v}" else
params = CGI.parse(uri.query.to_s)
v = params.blank? ? nil : params['v'].first
if v.blank?
return ""
else
return "//www.youtube.com/embed/#{v}"
end
end
else else
return "" return ""
end end

View File

@ -171,193 +171,193 @@
$("*[data-yt-binded=0]").each(function(){ $("*[data-yt-binded=0]").each(function(){
init_yt_banner(this); init_yt_banner(this);
}) })
if (typeof onYouTubeIframeAPIReady !== 'function'){ if (typeof onYouTubeIframeAPIReady !== 'function'){
$(document).ready(function() { $(document).ready(function() {
$(document).on('touchstart click mousedown',".jarallax-video-audio",function(){ $(document).on('touchstart click mousedown',".jarallax-video-audio",function(){
$(this).trigger('click'); $(this).trigger('click');
});
}); });
}); if(window.yt_players == undefined)
if(window.yt_players == undefined) window.yt_players = {};
window.yt_players = {}; function find_out_yt_event_list_key(yt_player){
function find_out_yt_event_list_key(yt_player){ var defalt_key = 'o';
var defalt_key = 'o'; var all_keys = Object.keys(yt_player).filter(function(s){return s.length == 1});
var all_keys = Object.keys(yt_player).filter(function(s){return s.length == 1}); var prop_nums_thresh = 6;
var prop_nums_thresh = 6; if(all_keys.indexOf(defalt_key) != -1){
if(all_keys.indexOf(defalt_key) != -1){ if((yt_player[defalt_key] instanceof Object) && Object.keys(yt_player[defalt_key]).length > prop_nums_thresh){
if((yt_player[defalt_key] instanceof Object) && Object.keys(yt_player[defalt_key]).length > prop_nums_thresh){
return defalt_key;
}
}
var event_key;
all_keys.forEach(function(k){
if((yt_player[k] instanceof Object) && Object.keys(yt_player[k]).length > prop_nums_thresh){
event_key = k;
return false;
}
})
return event_key;
}
function find_out_yt_event_list_count_key(yt_player, event_list_key){
var defalt_key = 'v';
var all_keys = Object.keys(yt_player).filter(function(s){return s.length == 1});
var equal_count = yt_player[event_list_key].length;
if(all_keys.indexOf(defalt_key) != -1){
if(yt_player[defalt_key] == equal_count){
return defalt_key;
}
}
var count_key;
all_keys.forEach(function(k){
if(yt_player[k] == equal_count){
count_key = k;
return false;
}
})
return count_key;
}
function find_out_yt_event_list_array_key(event_dict){
var defalt_key = 'i';
var all_keys = Object.keys(event_dict);
if(all_keys.indexOf(defalt_key) != -1){
var tmp = event_dict[defalt_key];
if(Array.isArray(tmp) && tmp.indexOf('onStateChange') != -1){
return defalt_key;
}
}
var array_key;
all_keys.forEach(function(k){
var tmp = event_dict[k];
if(Array.isArray(tmp) && tmp.indexOf('onStateChange') != -1){
array_key = k;
return false;
}
})
return array_key;
}
function find_out_yt_event_relation_key(event_dict, is_obj){ // is_obj = true => store event idx array. false => store whether event init(true or false)
var defalt_key = 'j';
var all_keys = Object.keys(event_dict);
if(all_keys.indexOf(defalt_key) != -1){
var tmp = event_dict[defalt_key];
if((tmp instanceof Object) && tmp['onStateChange']){
if(!is_obj || (tmp['onStateChange'] instanceof Object)){
return defalt_key; return defalt_key;
} }
} }
} var event_key;
var relation_key; all_keys.forEach(function(k){
all_keys.forEach(function(k){ if((yt_player[k] instanceof Object) && Object.keys(yt_player[k]).length > prop_nums_thresh){
var tmp = event_dict[k]; event_key = k;
if((tmp instanceof Object) && tmp['onStateChange']){
if(!is_obj || (tmp['onStateChange'] instanceof Object)){
relation_key = k;
return false; return false;
} }
}
})
return relation_key;
}
function onYouTubeIframeAPIReady(){
$(".w-ba-banner").on('cycle-post-initialize', function(){
init_banner(this);
});
function init_banner(banner){
var $banner = $(banner);
$banner.find('.w-ad-banner__slide').each(function(j, banner__slide){
init_yt_banner(banner__slide);
}) })
var iframes = $banner.find("iframe"); return event_key;
if(iframes.length > 0){ }
var id = $banner.attr("data-subpart-id"); function find_out_yt_event_list_count_key(yt_player, event_list_key){
if(yt_players[id] == undefined) var defalt_key = 'v';
yt_players[id] = {}; var all_keys = Object.keys(yt_player).filter(function(s){return s.length == 1});
var remove_ids = []; var equal_count = yt_player[event_list_key].length;
Object.keys(yt_players[id]).forEach(function(k){ if(all_keys.indexOf(defalt_key) != -1){
var yt_player = yt_players[id][k]; if(yt_player[defalt_key] == equal_count){
if($(yt_player.getIframe()).length == 0){ return defalt_key;
yt_player.destroy(); }
remove_ids.push(k);
}
})
remove_ids.forEach(function(k){
delete yt_players[id][k];
})
iframes.each(function(i,iframe){
var yt_id = $(iframe).attr("id");
var yt_player = yt_players[id][$(iframe).attr("id")];
if(yt_player){
}else{
yt_player = new YT.Player($(iframe).attr("id"), {
events: {
'onReady': function(event){
var yt_player = event.target;
var height = $(yt_player.getIframe()).height();
var banner_wrap = $(iframe).parents('.w-ba-banner__wrap').eq(0);
var carousel_wrap = banner_wrap.find(".cycle-carousel-wrap");
if(carousel_wrap.length){
carousel_wrap.css("top","3em");
height += parseInt(carousel_wrap.css('font-size')) * 3;
}
banner_wrap.height(height).css({"padding-bottom":"","padding-top":""});
var init_key = find_out_yt_event_relation_key(yt_player, false);
if(init_key){
delete yt_player[init_key].onStateChange;
}
else{
console.log("{onReady: true, onStateChange: true} missing!");
}
var event_list_key = find_out_yt_event_list_key(yt_player);
if(event_list_key){
var event_dict = yt_player[event_list_key];
var array_key = find_out_yt_event_list_array_key(event_dict);
var count_key = find_out_yt_event_list_count_key(event_dict, array_key);
var relation_key = find_out_yt_event_relation_key(event_dict, true);
var onStateChange_idx = event_dict[relation_key].onStateChange;
onStateChange_idx.reverse();
var event_size = 3;
onStateChange_idx.forEach(function(start_idx){
event_dict[array_key].splice(start_idx,event_size);
});
event_dict[relation_key].onStateChange = [];
event_dict[count_key] = event_dict[array_key].length;
yt_player.addEventListener('onStateChange',onPlayerStateChange);
banner_wrap.trigger('resize');
}else{
console.log("YT player changes its variables!")
}
{{extra_ready_script}}
},
'onStateChange': onPlayerStateChange
}
});
yt_players[id][$(iframe).attr("id")] = yt_player;
$(iframe).data("yt_player",yt_player);
}
})
} }
var count_key;
all_keys.forEach(function(k){
if(yt_player[k] == equal_count){
count_key = k;
return false;
}
})
return count_key;
} }
$(".w-ba-banner").each(function(i,banner){ function find_out_yt_event_list_array_key(event_dict){
init_banner(banner); var defalt_key = 'i';
}) var all_keys = Object.keys(event_dict);
} if(all_keys.indexOf(defalt_key) != -1){
function onPlayerStateChange(event){ var tmp = event_dict[defalt_key];
var iframe = $(event.target.getIframe()), if(Array.isArray(tmp) && tmp.indexOf('onStateChange') != -1){
cyclediv = iframe.parents("div.cycle-slideshow"); return defalt_key;
var widget = cyclediv.parents('.ba-banner-widget-youtube'); }
if(event.data == YT.PlayerState.PLAYING || event.data == YT.PlayerState.BUFFERING){ }
cyclediv[0].need_resume = !(cyclediv.hasClass("cycle-paused")); var array_key;
cyclediv.cycle("pause"); all_keys.forEach(function(k){
widget.find('.banner-pager,.controlplay,.button-mid,.ad-overlay,.banner-overlay').css('visibility','hidden') var tmp = event_dict[k];
ad_audio_button(iframe.parents(".w-ad-banner__slide").eq(0),false); if(Array.isArray(tmp) && tmp.indexOf('onStateChange') != -1){
}else if(event.data == YT.PlayerState.UNSTARTED || event.data == YT.PlayerState.PAUSED || event.data == YT.PlayerState.ENDED){ array_key = k;
if(cyclediv[0].need_resume) return false;
cyclediv.cycle("resume"); }
widget.find('.banner-pager,.controlplay,.button-mid,.ad-overlay,.banner-overlay').css('visibility','') })
ad_audio_button(iframe.parents(".w-ad-banner__slide").eq(0),true); return array_key;
}
function find_out_yt_event_relation_key(event_dict, is_obj){ // is_obj = true => store event idx array. false => store whether event init(true or false)
var defalt_key = 'j';
var all_keys = Object.keys(event_dict);
if(all_keys.indexOf(defalt_key) != -1){
var tmp = event_dict[defalt_key];
if((tmp instanceof Object) && tmp['onStateChange']){
if(!is_obj || (tmp['onStateChange'] instanceof Object)){
return defalt_key;
}
}
}
var relation_key;
all_keys.forEach(function(k){
var tmp = event_dict[k];
if((tmp instanceof Object) && tmp['onStateChange']){
if(!is_obj || (tmp['onStateChange'] instanceof Object)){
relation_key = k;
return false;
}
}
})
return relation_key;
}
function onYouTubeIframeAPIReady(){
$(".w-ba-banner").on('cycle-post-initialize', function(){
init_banner(this);
});
function init_banner(banner){
var $banner = $(banner);
$banner.find('.w-ad-banner__slide').each(function(j, banner__slide){
init_yt_banner(banner__slide);
})
var iframes = $banner.find("iframe");
if(iframes.length > 0){
var id = $banner.attr("data-subpart-id");
if(yt_players[id] == undefined)
yt_players[id] = {};
var remove_ids = [];
Object.keys(yt_players[id]).forEach(function(k){
var yt_player = yt_players[id][k];
if($(yt_player.getIframe()).length == 0){
yt_player.destroy();
remove_ids.push(k);
}
})
remove_ids.forEach(function(k){
delete yt_players[id][k];
})
iframes.each(function(i,iframe){
var yt_id = $(iframe).attr("id");
var yt_player = yt_players[id][$(iframe).attr("id")];
if(yt_player){
}else{
yt_player = new YT.Player($(iframe).attr("id"), {
events: {
'onReady': function(event){
var yt_player = event.target;
var height = $(yt_player.getIframe()).height();
var banner_wrap = $(iframe).parents('.w-ba-banner__wrap').eq(0);
var carousel_wrap = banner_wrap.find(".cycle-carousel-wrap");
if(carousel_wrap.length){
carousel_wrap.css("top","3em");
height += parseInt(carousel_wrap.css('font-size')) * 3;
}
banner_wrap.height(height).css({"padding-bottom":"","padding-top":""});
var init_key = find_out_yt_event_relation_key(yt_player, false);
if(init_key){
delete yt_player[init_key].onStateChange;
}
else{
console.log("{onReady: true, onStateChange: true} missing!");
}
var event_list_key = find_out_yt_event_list_key(yt_player);
if(event_list_key){
var event_dict = yt_player[event_list_key];
var array_key = find_out_yt_event_list_array_key(event_dict);
var count_key = find_out_yt_event_list_count_key(event_dict, array_key);
var relation_key = find_out_yt_event_relation_key(event_dict, true);
var onStateChange_idx = event_dict[relation_key].onStateChange;
onStateChange_idx.reverse();
var event_size = 3;
onStateChange_idx.forEach(function(start_idx){
event_dict[array_key].splice(start_idx,event_size);
});
event_dict[relation_key].onStateChange = [];
event_dict[count_key] = event_dict[array_key].length;
yt_player.addEventListener('onStateChange',onPlayerStateChange);
banner_wrap.trigger('resize');
}else{
console.log("YT player changes its variables!")
}
{{extra_ready_script}}
},
'onStateChange': onPlayerStateChange
}
});
yt_players[id][$(iframe).attr("id")] = yt_player;
$(iframe).data("yt_player",yt_player);
}
})
}
}
$(".w-ba-banner").each(function(i,banner){
init_banner(banner);
})
}
function onPlayerStateChange(event){
var iframe = $(event.target.getIframe()),
cyclediv = iframe.parents("div.cycle-slideshow");
var widget = cyclediv.parents('.ba-banner-widget-youtube');
if(event.data == YT.PlayerState.PLAYING || event.data == YT.PlayerState.BUFFERING){
cyclediv[0].need_resume = !(cyclediv.hasClass("cycle-paused"));
cyclediv.cycle("pause");
widget.find('.banner-pager,.controlplay,.button-mid,.ad-overlay,.banner-overlay').css('visibility','hidden')
ad_audio_button(iframe.parents(".w-ad-banner__slide").eq(0),false);
}else if(event.data == YT.PlayerState.UNSTARTED || event.data == YT.PlayerState.PAUSED || event.data == YT.PlayerState.ENDED){
if(cyclediv[0].need_resume)
cyclediv.cycle("resume");
widget.find('.banner-pager,.controlplay,.button-mid,.ad-overlay,.banner-overlay').css('visibility','')
ad_audio_button(iframe.parents(".w-ad-banner__slide").eq(0),true);
}
{{extra_state_chnage_script}}
} }
{{extra_state_chnage_script}}
} }
}
$(document).ready(function(){ $(document).ready(function(){
window.onYouTubePlayerAPIReady = function() { window.onYouTubePlayerAPIReady = function() {
onYouTubeIframeAPIReady.apply(this,arguments); onYouTubeIframeAPIReady.apply(this,arguments);