-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathloadgo-nojquery.min.js
6 lines (6 loc) · 7.28 KB
/
loadgo-nojquery.min.js
1
2
3
4
5
6
/**
* @preserve LoadGo v2.2.1 (http://franverona.com/loadgo)
* 2018 - Fran Verona
* Licensed under MIT (https://github.com/franverona/loadgo/blob/master/LICENSE)
*/
(function(){var e=function(e,t){for(var i=0,r=e.length;i<r;i++)if(e[i]===t)return i;return-1},t=function(e,t){var i,r,o={};if(void 0===e)r=JSON.parse(JSON.stringify(t)),t.resize&&(r.resize=t.resize),o=r;else if(void 0===t)i=JSON.parse(JSON.stringify(e)),e.resize&&(i.resize=e.resize),o=i;else for(var s in i=JSON.parse(JSON.stringify(e)),e.resize&&(i.resize=e.resize),e=i,r=JSON.parse(JSON.stringify(t)),t.resize&&(r.resize=t.resize),o=e,t=r)o[s]=t[s];return o},i=function(e){for(var t=0,i=n.length;t<i;t++)if(n[t].id===e)return n[t].properties;return null},r=function(e){for(var t=0,i=n.length;t<i;t++)if(n[t].id===e)return t;return-1},o=function(e){if(void 0===e||null===e)return!1;if("IMG"!==e.nodeName)throw new Error("LoadGo only works on img elements.");if(e.length>1)throw new Error("LoadGo only works on one element at a time. Try with a valid #id.");return!0},s=function(e,t){var i=e.style[t];if("auto"===i){if(-1!==t.toLowerCase().indexOf("left")||-1!==t.toLowerCase().indexOf("right"))return parseFloat(e.offsetLeft);if(-1!==t.toLowerCase().indexOf("top")||-1!==t.toLowerCase().indexOf("bottom"))return parseFloat(e.offsetTop)}return-1!==i.indexOf("px")?parseFloat(i):0},n=[],l={bgcolor:"#FFFFFF",opacity:"0.5",animated:!0,image:null,class:null,resize:null,direction:"lr",filter:null};Loadgo=window.Loadgo||{},Loadgo.init=function(e,l){if(o(e)){var a=r(e.id);-1===a?(n.push({id:e.id,properties:{}}),a=n.length-1):n[a].properties={};var d=Loadgo.options(e,l),p=document.createElement("div");p.id="loadgo-"+(new Date).getTime().toString();var f=["loadgo-overlay"];d.class&&f.push(d.class),p.className=f.join(" "),p.style.backgroundColor=d.bgcolor,p.style.opacity=d.opacity;var g=e.getBoundingClientRect();if(g.width?p.style.width=g.width+"px":p.style.width=e.offsetWidth,g.height?p.style.height=g.height+"px":p.style.height=e.offsetWidth,p.style.position="absolute",d.animated&&(p.style.transition="all 0.6s ease",p.style["-webkit-transition"]="all 0.6s ease",p.style["-moz-transition"]="all 0.6s ease",p.style["-ms-transition"]="all 0.6s ease",p.style["-o-transition"]="all 0.6s ease"),d.filter&&("blur"===d.filter?e.style["-webkit-filter"]=d.filter+"(10px)":"hue-rotate"===d.filter?e.style["-webkit-filter"]=d.filter+"(360deg)":"opacity"===d.filter?e.style["-webkit-filter"]=d.filter+"(0)":e.style["-webkit-filter"]=d.filter+"(1)",d.animated&&(e.style.transition="0.6s filter ease",e.style["-webkit-transition"]="0.6s -webkit-filter ease",e.style["-moz-transition"]="0.6s -moz-filter ease",e.style["-ms-transition"]="0.6s -ms-filter ease",e.style["-o-transition"]="0.6s -o-filter ease")),d.image){var y="100% 0%";"rl"===d.direction?y="0% 50%":"bt"===d.direction?y="100% 0%":"tb"===d.direction&&(y="0% 100%"),p.style["background-image"]='url("'+d.image+'")',p.style["background-repeat"]="no-repeat",p.style["background-size"]="cover",p.style["background-color"]="transparent",p.style["background-position"]=y}var c={progress:0};if(null===d.filter){var h=document.createElement("div");h.className="loadgo-container",h.style.position="relative",e.before(h),h.appendChild(e),h.appendChild(p);var u=s(e,"paddingLeft"),v=s(e,"paddingRight"),m=s(e,"paddingTop"),w=s(e,"paddingBottom"),b=s(e,"marginLeft"),L=s(e,"marginRight"),x=s(e,"marginTop"),z=s(e,"marginBottom");"lr"===d.direction?(p.style.right=v+L+"px",p.style.top=m+x+"px"):"rl"===d.direction?(p.style.left=u+b+"px",p.style.top=m+x+"px"):"bt"===d.direction?(p.style.top=m+x+"px",p.style.left=u+b+"px"):"tb"===d.direction&&(p.style.bottom=w+z+"px",p.style.left=u+b+"px"),c.overlay=p.id,c.width=p.clientWidth,c.height=p.clientHeight}n[a].properties=t(d,c);var k=function(){var t=i(e.id),o=r(e.id);if(null!==t){var l=document.getElementById(t.overlay),a=e.getBoundingClientRect();if(l){a.width?l.style.width=a.width+"px":l.style.width=e.offsetWidth,a.height?l.style.height=a.height+"px":l.style.height=e.offsetWidth;var p=s(e,"paddingLeft"),f=s(e,"paddingRight"),g=s(e,"paddingTop"),y=s(e,"paddingBottom"),c=s(e,"marginLeft"),h=s(e,"marginRight"),u=s(e,"marginTop"),v=s(e,"marginBottom");"lr"===d.direction?(l.style.right=f+h+"px",l.style.top=g+u+"px"):"rl"===d.direction?(l.style.left=p+c+"px",l.style.top=g+u+"px"):"bt"===d.direction?(l.style.top=g+u+"px",l.style.left=p+c+"px"):"tb"===d.direction&&(l.style.bottom=y+v+"px",l.style.left=p+c+"px"),n[o].properties.width=parseFloat(l.style.width),n[o].properties.height=parseFloat(l.style.height),Loadgo.setprogress(e,t.progress)}}};d.resize&&(k=d.resize),window.addEventListener?window.addEventListener("resize",k,!1):window.attachEvent("onresize",k)}},Loadgo.options=function(i,s){if(o(i)){var a=r(i.id);if(-1!==a){var d=n[a].properties;if("{}"!==JSON.stringify(d))return d;void 0!==s&&void 0!==s.opacity&&(s.opacity=parseFloat(s.opacity)),d="{}"===JSON.stringify(d)?t(l,s):t(d,s);if(-1===e(["lr","rl","bt","tb"],d.direction.toLowerCase())&&(d.direction="lr"),d.filter){-1===e(["blur","grayscale","sepia","hue-rotate","invert","opacity"],d.filter.toLowerCase())&&(d.filter=null)}return n[a].properties=d,d}}},Loadgo.setprogress=function(e,t){if(o(e)&&!(t<0||t>100)){var s=r(e.id);if(-1!==s){var l=i(e.id);if(null!==l){var a,d,p=document.getElementById(l.overlay),f=l.width,g=l.height;if(p){var y=l.direction;"lr"===y?(a=f*(1-t/100),p.style.width=a+"px"):"rl"===y?(a=f*(1-t/100),p.style.width=a+"px"):"bt"===y?(d=g*(1-t/100),p.style.height=d+"px"):"tb"===y&&(d=g*(1-t/100),p.style.height=d+"px",p.style.top=g-d+"px")}else{var c;switch(l.filter){case"blur":c=(100-t)/10,e.style["-webkit-filter"]=l.filter+"("+c+"px)";break;case"hue-rotate":c=360*t/100,e.style["-webkit-filter"]=l.filter+"("+c+"deg)";break;case"opacity":c=t/100,e.style["-webkit-filter"]=l.filter+"("+c+")";break;default:c=1-t/100,e.style["-webkit-filter"]=l.filter+"("+c+")"}}}n[s].properties.progress=t}}},Loadgo.getprogress=function(e){if(o(e)){var t=i(e.id);return null!==t?t.progress:0}},Loadgo.resetprogress=function(e){Loadgo.setprogress(e,0)},Loadgo.loop=function(e,t){if(o(e))if(-1!==r(e.id)){var s=i(e.id);if(null!==s)if(s.interval)console.warn("LoadGo requires you to stop the current loop before modifying it.");else{var l=!0,a=r(e.id);n[a].properties.interval=setInterval(function(){l?(n[a].properties.progress+=1,n[a].properties.progress>=100&&(l=!1)):(n[a].properties.progress-=1,n[a].properties.progress<=0&&(l=!0));var t=document.getElementById(n[a].properties.overlay);t&&(t.style.transition="none",t.style["-webkit-transition"]="none",t.style["-moz-transition"]="none",t.style["-ms-transition"]="none",t.style["-o-transition"]="none"),Loadgo.setprogress(e,n[a].properties.progress)},t)}else console.warn("Element do not have Loadgo properties. Maybe it is uninitialized.")}else console.error('Trying to loop a non initialized element. You have to run "init" method first.')},Loadgo.stop=function(e){o(e)&&(-1!==r(e.id)?(clearInterval(n[r(e.id)].properties.interval),Loadgo.setprogress(e,100)):console.error('Trying to stop loop for a non initialized element. You have to run "init" method first.'))},Loadgo.destroy=function(e){var t=r(e.id);if(-1!==t){var i=Loadgo.options(e);n.splice(t),e.before(o);var o=e.parentNode,s=o.parentNode,l=document.getElementById(i.overlay);l&&l.parentNode&&o.removeChild(i.overlay),s&&(s.appendChild(e),s.removeChild(o))}},window.Loadgo=Loadgo}).call(this);