1 line
8.5 KiB
JavaScript
1 line
8.5 KiB
JavaScript
var Holder=Holder||{};!function(e,t){function n(e,t){var n="complete",i="readystatechange",o=!1,a=o,r=!0,s=e.document,d=s.documentElement,l=s.addEventListener?"addEventListener":"attachEvent",u=s.addEventListener?"removeEventListener":"detachEvent",h=s.addEventListener?"":"on",c=function(r){(r.type!=i||s.readyState==n)&&(("load"==r.type?e:s)[u](h+r.type,c,o),!a&&(a=!0)&&t.call(e,null))},g=function(){try{d.doScroll("left")}catch(e){return setTimeout(g,50),void 0}c("poll")};if(s.readyState==n)t.call(e,"lazy");else{if(s.createEventObject&&d.doScroll){try{r=!e.frameElement}catch(f){}r&&g()}s[l](h+"DOMContentLoaded",c,o),s[l](h+i,c,o),e[l](h+"load",c,o)}}function i(e){e=e.match(/^(\W)?(.*)/);var t=document["getElement"+(e[1]?"#"==e[1]?"ById":"sByClassName":"sByTagName")](e[2]),n=[];return null!==t&&(n=t.length?t:0===t.length?t:[t]),n}function o(e,t){var n={};for(var i in e)e.hasOwnProperty(i)&&(n[i]=e[i]);for(var i in t)t.hasOwnProperty(i)&&(n[i]=t[i]);return n}function a(e,t,n){t=parseInt(t,10),e=parseInt(e,10);var i=Math.max(t,e),o=Math.min(t,e),a=1/12,r=Math.min(.75*o,.75*i*a);return{height:Math.round(Math.max(n.size,r))}}function r(e){var t=e.ctx,n=e.dimensions,i=e.template,o=e.ratio,r=e.holder,s="literal"==r.textmode,d="exact"==r.textmode,l=a(n.width,n.height,i),u=l.height,h=n.width*o,c=n.height*o,f=i.font?i.font:"sans-serif";g.width=h,g.height=c,t.textAlign="center",t.textBaseline="middle",t.fillStyle=i.background,t.fillRect(0,0,h,c),t.fillStyle=i.foreground,t.font="bold "+u+"px "+f;var m=i.text?i.text:Math.floor(n.width)+"x"+Math.floor(n.height);if(s){var n=r.dimensions;m=n.width+"x"+n.height}else if(d&&r.exact_dimensions){var n=r.exact_dimensions;m=Math.floor(n.width)+"x"+Math.floor(n.height)}var x=t.measureText(m).width;return x/h>=.75&&(u=Math.floor(.75*u*(h/x))),t.font="bold "+u*o+"px "+f,t.fillText(m,h/2,c/2,h),g.toDataURL("image/png")}function s(e,t,n,i){var a=n.dimensions,s=n.theme,d=n.text?decodeURIComponent(n.text):n.text,u=a.width+"x"+a.height;s=d?o(s,{text:d}):s,s=n.font?o(s,{font:n.font}):s,t.setAttribute("data-src",i),n.theme=s,t.holder_data=n,"image"==e?(t.setAttribute("alt",d?d:s.text?s.text+" ["+u+"]":u),(c||!n.auto)&&(t.style.width=a.width+"px",t.style.height=a.height+"px"),c?t.style.backgroundColor=s.background:(t.setAttribute("src",r({ctx:p,dimensions:a,template:s,ratio:y,holder:n})),n.textmode&&"exact"==n.textmode&&(x.push(t),l(t)))):"background"==e?c||(t.style.backgroundImage="url("+r({ctx:p,dimensions:a,template:s,ratio:y,holder:n})+")",t.style.backgroundSize=a.width+"px "+a.height+"px"):"fluid"==e&&(t.setAttribute("alt",d?d:s.text?s.text+" ["+u+"]":u),t.style.height="%"==a.height.slice(-1)?a.height:a.height+"px",t.style.width="%"==a.width.slice(-1)?a.width:a.width+"px",("inline"==t.style.display||""===t.style.display||"none"==t.style.display)&&(t.style.display="block"),c?t.style.backgroundColor=s.background:(x.push(t),l(t)))}function d(e,t){var n={height:e.clientHeight,width:e.clientWidth};if(!n.height&&!n.width){if(e.hasAttribute("data-holder-invisible"))throw new Error("Holder: placeholder is not visible");return e.setAttribute("data-holder-invisible",!0),setTimeout(function(){t.call(this,e)},1),null}return e.removeAttribute("data-holder-invisible"),n}function l(e){var t;t=null==e.nodeType?x:[e];for(var n in t)if(t.hasOwnProperty(n)){var i=t[n];if(i.holder_data){var o=i.holder_data,a=d(i,l);a&&(o.fluid&&i.setAttribute("src",r({ctx:p,dimensions:a,template:o.theme,ratio:y,holder:o})),o.textmode&&"exact"==o.textmode&&(o.exact_dimensions=a,i.setAttribute("src",r({ctx:p,dimensions:o.dimensions,template:o.theme,ratio:y,holder:o}))))}}}function u(t,n){var i={theme:o(v.themes.gray,{})},a=!1;for(sl=t.length,j=0;sl>j;j++){var r=t[j];e.flags.dimensions.match(r)?(a=!0,i.dimensions=e.flags.dimensions.output(r)):e.flags.fluid.match(r)?(a=!0,i.dimensions=e.flags.fluid.output(r),i.fluid=!0):e.flags.textmode.match(r)?i.textmode=e.flags.textmode.output(r):e.flags.colors.match(r)?i.theme=e.flags.colors.output(r):n.themes[r]?n.themes.hasOwnProperty(r)&&(i.theme=o(n.themes[r],{})):e.flags.font.match(r)?i.font=e.flags.font.output(r):e.flags.auto.match(r)?i.auto=!0:e.flags.text.match(r)&&(i.text=e.flags.text.output(r))}return a?i:!1}var h=!1,c=!1,g=document.createElement("canvas"),f=1,m=1,x=[];if(g.getContext)if(g.toDataURL("image/png").indexOf("data:image/png")<0)c=!0;else var p=g.getContext("2d");else c=!0;c||(f=window.devicePixelRatio||1,m=p.webkitBackingStorePixelRatio||p.mozBackingStorePixelRatio||p.msBackingStorePixelRatio||p.oBackingStorePixelRatio||p.backingStorePixelRatio||1);var y=f/m,v={domain:"holder.js",images:"img",bgnodes:".holderjs",themes:{gray:{background:"#eee",foreground:"#aaa",size:12},social:{background:"#3a5a97",foreground:"#fff",size:12},industrial:{background:"#434A52",foreground:"#C2F200",size:12},sky:{background:"#0D8FDB",foreground:"#fff",size:12},vine:{background:"#39DBAC",foreground:"#1E292C",size:12},lava:{background:"#F8591A",foreground:"#1C2846",size:12}},stylesheet:""};e.flags={dimensions:{regex:/^(\d+)x(\d+)$/,output:function(e){var t=this.regex.exec(e);return{width:+t[1],height:+t[2]}}},fluid:{regex:/^([0-9%]+)x([0-9%]+)$/,output:function(e){var t=this.regex.exec(e);return{width:t[1],height:t[2]}}},colors:{regex:/#([0-9a-f]{3,})\:#([0-9a-f]{3,})/i,output:function(e){var t=this.regex.exec(e);return{size:v.themes.gray.size,foreground:"#"+t[2],background:"#"+t[1]}}},text:{regex:/text\:(.*)/,output:function(e){return this.regex.exec(e)[1]}},font:{regex:/font\:(.*)/,output:function(e){return this.regex.exec(e)[1]}},auto:{regex:/^auto$/},textmode:{regex:/textmode\:(.*)/,output:function(e){return this.regex.exec(e)[1]}}},document.getElementsByClassName||(document.getElementsByClassName=function(e){var t,n,i,o=document,a=[];if(o.querySelectorAll)return o.querySelectorAll("."+e);if(o.evaluate)for(n=".//*[contains(concat(' ', @class, ' '), ' "+e+" ')]",t=o.evaluate(n,o,null,0,null);i=t.iterateNext();)a.push(i);else for(t=o.getElementsByTagName("*"),n=new RegExp("(^|\\s)"+e+"(\\s|$)"),i=0;i<t.length;i++)n.test(t[i].className)&&a.push(t[i]);return a}),window.getComputedStyle||(window.getComputedStyle=function(e){return this.el=e,this.getPropertyValue=function(t){var n=/(\-([a-z]){1})/g;return"float"==t&&(t="styleFloat"),n.test(t)&&(t=t.replace(n,function(){return arguments[2].toUpperCase()})),e.currentStyle[t]?e.currentStyle[t]:null},this}),Object.prototype.hasOwnProperty||(Object.prototype.hasOwnProperty=function(e){var t=this.__proto__||this.constructor.prototype;return e in this&&(!(e in t)||t[e]!==this[e])});for(var w in e.flags)e.flags.hasOwnProperty(w)&&(e.flags[w].match=function(e){return e.match(this.regex)});e.add_theme=function(t,n){return null!=t&&null!=n&&(v.themes[t]=n),e},e.add_image=function(t,n){var o=i(n);if(o.length)for(var a=0,r=o.length;r>a;a++){var s=document.createElement("img");s.setAttribute("data-src",t),o[a].appendChild(s)}return e},e.run=function(t){h=!0;var n=o(v,t),a=[],r=[],d=[];for("string"==typeof n.images?r=i(n.images):window.NodeList&&n.images instanceof window.NodeList?r=n.images:window.Node&&n.images instanceof window.Node&&(r=[n.images]),"string"==typeof n.bgnodes?d=i(n.bgnodes):window.NodeList&&n.elements instanceof window.NodeList?d=n.bgnodes:window.Node&&n.bgnodes instanceof window.Node&&(d=[n.bgnodes]),f=0,g=r.length;g>f;f++)a.push(r[f]);var l=document.getElementById("holderjs-style");l||(l=document.createElement("style"),l.setAttribute("id","holderjs-style"),l.type="text/css",document.getElementsByTagName("head")[0].appendChild(l)),n.nocss||(l.styleSheet?l.styleSheet.cssText+=n.stylesheet:l.appendChild(document.createTextNode(n.stylesheet)));for(var c=new RegExp(n.domain+'/(.*?)"?\\)'),g=d.length,f=0;g>f;f++){var m=window.getComputedStyle(d[f],null).getPropertyValue("background-image"),x=m.match(c),p=d[f].getAttribute("data-background-src");if(x){var y=u(x[1].split("/"),n);y&&s("background",d[f],y,m)}else if(null!=p){var y=u(p.substr(p.lastIndexOf(n.domain)+n.domain.length+1).split("/"),n);y&&s("background",d[f],y,m)}}for(g=a.length,f=0;g>f;f++){var w,b;b=w=m=null;try{b=a[f].getAttribute("src"),attr_datasrc=a[f].getAttribute("data-src")}catch(k){}if(null==attr_datasrc&&b&&b.indexOf(n.domain)>=0?m=b:attr_datasrc&&attr_datasrc.indexOf(n.domain)>=0&&(m=attr_datasrc),m){var y=u(m.substr(m.lastIndexOf(n.domain)+n.domain.length+1).split("/"),n);y&&(y.fluid?s("fluid",a[f],y,m):s("image",a[f],y,m))}}return e},n(t,function(){window.addEventListener?(window.addEventListener("resize",l,!1),window.addEventListener("orientationchange",l,!1)):window.attachEvent("onresize",l),h||e.run()}),"function"==typeof define&&define.amd&&define([],function(){return e})}(Holder,window); |