-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpixabay-widget.min.js
3 lines (3 loc) · 7.26 KB
/
pixabay-widget.min.js
1
2
3
// Pixabay Image Gallery Widget v1.0.4
// https://github.com/Pixabay/JavaScript-PixabayWidget
!function(){function g(a){return a?a.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,"""):""}function h(a){return a.replace(/\w\S*/g,function(a){return a.charAt(0).toUpperCase()+a.substr(1).toLowerCase()})}function i(a,c,d,e,f){var g="pxw_callback_"+b;return window[g]=function(b){a(b,c,d,e,f);try{delete window[g]}catch(a){}},b++,g}function j(a,b){for(var c=a.matches||a.webkitMatchesSelector||a.mozMatchesSelector||a.msMatchesSelector;a;){if(c.bind(a)(b))return a;a=a.parentElement}}function k(a){for(var b="",c=0,d=a.attributes;c<d.length;c++)"data-attrstr"!=d[c].name&&"data-prefilled"!=d[c].name&&(b+=d[c].name+d[c].value);return b}function l(){for(var b=0,d=document.querySelectorAll("."+c.class_name);b<d.length;b++){var e=d[b];if(k(e)!=e.getAttribute("data-attrstr")){e.setAttribute("data-attrstr",k(e));var f=parseInt(e.getAttribute("data-page"))||1,g=parseInt(e.getAttribute("data-per-page"))||c.per_page,h=e.getAttribute("data-search")||"",j=e.getAttribute("data-user")||"";g=g>100?100:g,j&&(h="user:"+j+" "+h);var l="https://pixabay.com/api/?key="+c.key+"&lang="+(e.getAttribute("data-lang")||c.lang)+"&order="+(e.getAttribute("data-order")||c.order)+"&image_type="+(e.getAttribute("data-image-type")||c.image_type)+"&safesearch="+(e.getAttribute("data-safesearch")||c.safesearch)+"&editors_choice="+(e.getAttribute("data-editors-choice")||c.editors_choice)+"&per_page="+g+"&page="+f+"&q="+encodeURIComponent(h);if(e.getAttribute("data-prefilled"))e.removeAttribute("data-prefilled"),APIResponse(!1,e,f,g,l);else if(l in a)APIResponse(a[l],e,f,g,l);else{var m=document.createElement("script");m.src=l+"&callback="+i(APIResponse,e,f,g,l),document.body.appendChild(m)}}}}var a={},b=0,c={class_name:"pixabay_widget",row_height:170,per_page:20,max_rows:0,truncate:!0,lang:"en",image_type:"all",safesearch:!1,editors_choice:!1,order:"popular",target:"",navpos:"bottom",branding:!0,prev:"◄ PREV",next:"NEXT ►"};if("object"==typeof pixabayWidget)for(var d in pixabayWidget)Object.prototype.hasOwnProperty.call(pixabayWidget,d)&&(c[d]=pixabayWidget[d]);if(!("key"in c))return console.log('API key missing: Your Pixabay API key must be defined as "key" in the pixabayWidget configuration array.'),!1;styles="** .noselect { -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; }** a, ** b { color: #4e99c7; text-decoration: none; font-weight: normal; cursor: pointer; transition: .3s; }** a:hover, ** b:hover { opacity: .7; }** a img { border: 0; }** **_nav { clear: both; padding: 3px 7px; font: normal 12px arial, sans-serif; color: #777; }** **_nav span { color: #ddd; cursor: default; }** .branding { float: right; }**.flex_grid { overflow: hidden; }**.flex_grid .item { float: left; margin: 1px; box-sizing: content-box; overflow: hidden; position: relative; }**.flex_grid .item img { display: block; width: auto; height: 100%; background: #fff; transition: .3s; }**.flex_grid .item img:hover { opacity: .85; }".replace(/\*\*/g,"."+c.class_name);var e=document.createElement("style");e.type="text/css",e.styleSheet?e.styleSheet.cssText=styles:e.appendChild(document.createTextNode(styles)),document.getElementsByTagName("head")[0].appendChild(e);var f=function(){function a(a){function b(a,c,d,e){function f(a){d.maxRows&&l>d.maxRows||d.truncate&&a&&l>1?n[g][0].style.display="none":(n[g][4]&&(n[g][3].setAttribute("src",n[g][4]),n[g][4]=""),n[g][0].style.width=h+"px",n[g][0].style.height=p+"px",n[g][0].style.display="block")}for(var g,h,i,j,k=1,l=1,m=a.clientWidth-2,n=[],o=0,p=d.rowHeight,q=0;q<c.length;q++)if(n.push(c[q]),o+=c[q][2]+d.margin,o>=m){var r=n.length*d.margin;for(k=(m-r)/(o-r),p=Math.ceil(d.rowHeight*k),i=0,g=0;g<n.length;g++)h=Math.ceil(n[g][2]*k),i+=h+d.margin,i>m&&(h-=i-m),f();n=[],o=0,l++}for(g=0;g<n.length;g++)h=Math.floor(n[g][2]*k),j=Math.floor(d.rowHeight*k),f(!0);e||m==a.clientWidth||b(a,c,d,!0)}if(document.querySelector){var c={selector:0,container:".item",object:"img",rowHeight:180,maxRows:0,truncate:0};for(var d in a)a.hasOwnProperty(d)&&(c[d]=a[d]);for(var e="object"==typeof c.selector?[c.selector]:document.querySelectorAll(c.selector),f=0;f<e.length;f++){var g=e[f],h=g.querySelectorAll(c.container),i=[],j=(new Date).getTime();if(h.length){var k=window.getComputedStyle?getComputedStyle(h[0],null):h[0].currentStyle;c.margin=(parseInt(k.marginLeft)||0)+(parseInt(k.marginRight)||0)+(Math.round(parseFloat(k.borderLeftWidth))||0)+(Math.round(parseFloat(k.borderRightWidth))||0);for(var l=0;l<h.length;l++){var m=h[l],n=parseInt(m.getAttribute("data-w")),o=n*(c.rowHeight/parseInt(m.getAttribute("data-h"))),p=m.querySelector(c.object);i.push([m,n,o,p,p.getAttribute("data-src")])}b(g,i,c);var q=function(){b(g,i,c)};document.addEventListener?(window["flexImages_listener"+j]=q,window.removeEventListener("resize",window["flexImages_listener"+g.getAttribute("data-flex-t")]),delete window["flexImages_listener"+g.getAttribute("data-flex-t")],window.addEventListener("resize",window["flexImages_listener"+j])):g.onresize=q,g.setAttribute("data-flex-t",j)}}}}return a}();!function(){"function"==typeof define&&define.amd?define("flexImages",function(){return f}):"undefined"!=typeof module&&module.exports?module.exports=f:window.flexImages=f}(),APIResponse=function(b,d,e,i,j){var k="",l="",m=parseInt(d.getAttribute("data-row-height"))||c.row_height,n=parseInt(d.getAttribute("data-max-rows"))||c.max_rows,o=d.getAttribute("data-truncate")||c.truncate,p=d.getAttribute("data-target")||c.target,q=d.getAttribute("data-branding")||c.branding,r=d.getAttribute("data-prev")||c.prev,s=d.getAttribute("data-next")||c.next,t=d.getAttribute("data-navpos")||c.navpos;if((m<30||m>180)&&(m=170),"false"==o?o=0:"true"==o&&(o=1),"false"==q?q=0:"true"==q&&(q=1),0!=b){a[j]=b;var u=b.totalHits>i&&r&&s;(u||q)&&(k+='<div class="noselect '+c.class_name+'_nav">',q&&(k+='<div class="branding">Powered by <a href="https://pixabay.com/" target="'+p+'">Pixabay</a></div>'),u&&(k+=e>1?'<b class="'+c.class_name+'_prev">'+r+" </b>":"<span>"+r+" </span>",k+=e*i<b.totalHits?'<b class="'+c.class_name+'_next"> '+s+"</b>":"<span> "+s+"</span>"),k+="</div>"),"top"==t&&(l=k);for(var v=0,w=b.hits;v<w.length;v++){var x=w[v].previewWidth,y=w[v].previewHeight,z=w[v].previewURL;m>y-10&&(x*=180/(y+1),y=180,z=z.replace("_150","__180")),l+='<div class="item" data-w="'+x+'" data-h="'+y+'"><a title="'+g(h(w[v].tags))+'" href="'+w[v].pageURL+'" target="'+p+'"><img src="https://pixabay.com/static/img/blank.gif" data-src="'+z+'"></a></div>'}"bottom"==t&&(l+=k),d.innerHTML=l}d.className.indexOf("flex_grid")<0&&(d.className+=" flex_grid"),new f({selector:d,rowHeight:m,maxRows:n,truncate:o})},document.addEventListener?document.addEventListener("click",function(a){var b=0;if(a.target.className==c.class_name+"_prev"?b=-1:a.target.className==c.class_name+"_next"&&(b=1),b){var d=j(a.target,"."+c.class_name),e=(parseInt(d.getAttribute("data-page"))||1)+b;e&&(d.setAttribute("data-page",e),l()),a.preventDefault()}}):(c.prev="",c.next=""),this.initPixabayWidget=l,/bot|crawl|spider/i.test(navigator.userAgent)||("loading"!=document.readyState?l():document.addEventListener?document.addEventListener("DOMContentLoaded",l):document.attachEvent("onreadystatechange",function(){"complete"==document.readyState&&l()}))}();