fairbox.js
Version:
1 lines • 7.76 kB
JavaScript
function Action_popup(e,t){this.type=e,this["class"]=t,this.switch_action(this.type,this["class"])}function Create_object(e,t,i,o){this.type=e,this["class"]=t,this.src=i,this.url=o,this.object=null,this.switchType(this.type,this["class"],this.src,this.url)}function Create_icon(e,t,i){this.name=e,this.type=t,this["class"]=i,this.object=null,this.switch_icon(this.name,this.type,this["class"])}function Preview_object(e,t,i,o,n){this.type=e,this["class"]=t,this.src=i,this.alt=n,this.container=o,this.object=null,this.createImage(this.type,this["class"],this.src,this.container,this.alt)}Action_popup.prototype.switch_action=function(e,t){switch(e){case"margin":this.add_margin(t);break;case"resize":this.resize_popup(t);break;case"add":this.add_class(t);break;case"scroll":this.save_scroll(t)}},Action_popup.prototype.save_scroll=function(e){oldScroll=$(e).scrollTop(),$(e).scrollTop(0)},Action_popup.prototype.add_class=function(e){$(html).addClass(e)},Action_popup.prototype.resize_popup=function(e){this.height=$(window).height(),$("."+e).css("height",this.height)},Create_object.prototype.switchType=function(e,t,i,o){var n=this;null===i?null===o?this.createDiv(e,t):(this.createMedia(e,t,o),n.init_pos(t,{left:0,right:0})):(this.createImage(e,t,i),n.init_pos(t,{left:0,right:0}))},Create_object.prototype.createDiv=function(e,t){switch(t===class_container_nav?this.height=$(window).height()-100:this.height=$(window).height(),this.object=$(e,{"class":t,height:this.height}),t){case class_container:this.object.appendTo("body");break;case class_container_preview:this.object.appendTo("."+class_container_nav);break;default:this.object.appendTo("."+class_container)}},Create_object.prototype.createImage=function(e,t,i){this.object=$(e,{id:id_media_image,"class":t,src:i}),this.object.appendTo("."+class_container_media)},Create_object.prototype.createMedia=function(e,t,i){this.object=$(e,{"class":t,src:i,frameborder:0,width:$(window).width(),height:$(window).height()}),this.object.appendTo("."+class_container_media)},Create_object.prototype.init_pos=function(e,t){var i=this;setTimeout(function(){$(i.object).css(t)},100)},Create_icon.prototype.switch_icon=function(e,t,i){switch(e){case"close":this.init_icon(e,t,i);break;case"direction-left":this.init_icon(e,t,i);break;case"direction-right":this.init_icon(e,t,i)}},Create_icon.prototype.init_icon=function(e,t,i){this.object=$(t,{id:"#"+e,"class":i}),this.object.appendTo("."+class_container_nav)},Create_icon.prototype.close_icon=function(){$("."+class_container).remove(),$(html).removeClass(class_body),$(window).scrollTop(oldScroll)},Create_icon.prototype.arrow_icon=function(e,t){var i=this,o=11,n=Element.index()%o,c=Element.prev().index()%o;"direction-left"===e?Element.index()>0&&(Element=t||Element.prev(),i.arrow_action(Element),i.switchPreview(c,"+")):Element.index()<nombre_media-1&&(Element=t||Element.next(),i.arrow_action(Element),i.switchPreview(n,"-"))},Create_icon.prototype.arrow_action=function(e){$(".showbox_container").empty(),e.data("youtube")?(source=e.find("img").data("youtube")||e.data("youtube"),showbox_media=new Create_object("<iframe>",class_media,null,urlYoutube+source+"?autoplay=1")):(source=e.find("img").attr("src")||e.attr("src"),showbox_media=new Create_object("<img>",class_media,source,null)),Element=e},Create_icon.prototype.switchPreview=function(e,t){$(".preview_image").removeClass("active"),Element.addClass("active");$(".showbox_container_preview").width();0===e&&0!==Element.index()&&1!==Element.index()&&("-"===t?multiplicateur++:multiplicateur--,this.switchDirection(t))},Create_icon.prototype.switchDirection=function(e){var t=1050*multiplicateur;$(".showbox_second_preview").css("transform","translateX(-"+t+"px)")},function(e,t){"function"==typeof define&&define.amd?define(["jquery"],t):"object"==typeof exports?module.exports=t(require("jquery")):e.Fairbox=t(e.jQuery)}(this,function(e){function t(){var t=this;e("img[data-fairbox]").click(function(){e(this).find('[data-fairbox!=""]')&&t.initPopup(e(this))})}return t.prototype.option=function(e){this.disablePreview=e.disablePreview,this.disableSlider=e.disableSlider},t.prototype.initPopup=function(t){Element=t,nombre_media=e(class_media_image).length,this.init_element(t,this.disablePreview),this.init_action(),this.init_icon(this.disableSlider)},t.prototype.init_element=function(t,i){new Create_object("<div>",class_container,null,null),new Create_object("<div>",class_container_nav,null,null),new Create_object("<div>",class_container_media,null,null);if(i!==!0){new Create_object("<div>",class_container_preview,null,null)}var o=new Preview_object("<div>",class_second_preview,null,class_container_preview);t.data("youtube")?(source=t.find("img").data("youtube")||t.data("youtube"),showbox_media=new Create_object("<iframe>",class_media,null,urlYoutube+source+"?autoplay=1")):(source=t.find("img").attr("src")||t.attr("src"),showbox_media=new Create_object("<img>",class_media,source,null)),e("img[data-fairbox]").each(function(){var t,i=e(this).attr("src"),o=e(this).data("youtube");t=o?"video":"image",showbox_preview=new Preview_object("<img>","preview_image "+t,i,class_second_preview,o),showbox_preview.addActive(showbox_media)}),e(".preview_image").click(function(){e(this).index()!==Element.index()&&(e(this).index()>Element.index()?showbox_right.arrow_icon("direction-right",e(this)):showbox_left.arrow_icon("direction-left",e(this)))}),o.addWidth()},t.prototype.init_icon=function(e){showbox_close=new Create_icon("close","<i>",class_icon_close),e!==!0&&(showbox_left=new Create_icon("direction-left","<i>",class_icon_left),showbox_right=new Create_icon("direction-right","<i>",class_icon_right)),this.init_input(showbox_close,showbox_right,showbox_left,"close","direction-right","direction-left")},t.prototype.init_action=function(){new Action_popup("scroll",window),new Action_popup("add",class_body);window.onresize=function(){new Action_popup("resize",class_container),new Action_popup("resize",class_container_nav)}},t.prototype.init_input=function(t,i,o,n,c,s){multiplicateur=0,e("."+n).click(function(e){t.close_icon()}),e("."+c).click(function(e){i.arrow_icon(c)}),e("."+s).click(function(e){o.arrow_icon(s)}),e(window).keydown(function(e){27==e.which&&t.close_icon(),39==e.which&&i.arrow_icon(c),37==e.which&&o.arrow_icon(s)}),this.mouseMove(window)},t.prototype.mouseMove=function(t){var i;t.addEventListener("mousemove",function(){e("."+class_container_nav).css("opacity",1),clearTimeout(i),i=setTimeout(function(){e("."+class_container_nav).css("opacity",0)},5e3)})},FairBox=new t}),Preview_object.prototype.createImage=function(e,t,i,o,n){this.object=$(e,{id:id_media_image,"class":t,src:i,data:{youtube:n}}),this.object.appendTo("."+o)},Preview_object.prototype.addWidth=function(){$(this.object).css("width",$(this.object).children().width()*$(this.object).children().length)},Preview_object.prototype.addActive=function(e){$(e).attr("src")===$(this.object).attr("src")&&$(this.object).addClass("active")};var html="html,body",class_container="showbox",class_container_nav="showbox_container_nav",class_container_preview="showbox_container_preview",class_second_preview="showbox_second_preview",class_container_media="showbox_container",class_media="showbox_media",class_icon_close="showbox_icon close fa fa-times",class_icon_left="showbox_icon direction-left fa fa-chevron-left",class_icon_right="showbox_icon direction-right fa fa-chevron-right",class_body="showbox_body",id_media_image="image",class_media_image="img[data-fairbox]",urlYoutube="https://www.youtube.com/embed/",oldScroll,source,sourceImage,showbox_margin,nombre_media,showbox_media,Element,multiplicateur,showbox_preview,showbox_close,showbox_left,showbox_right,nombre_preview=12,FairBox;