forked from imsky/holder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathholder.min.js
10 lines (7 loc) · 7.38 KB
/
holder.min.js
1
2
3
4
5
6
7
8
9
10
/*
Holder - 2.0.0 - client side image placeholders
(c) 2013 Ivan Malopinsky / http://imsky.co
Provided under the MIT License.
Commercial use requires attribution.
*/
var Holder=Holder||{};!function(a,b){function c(a,b){var c="complete",d="readystatechange",e=!1,f=e,g=!0,h=a.document,i=h.documentElement,j=h.addEventListener?"addEventListener":"attachEvent",k=h.addEventListener?"removeEventListener":"detachEvent",l=h.addEventListener?"":"on",m=function(g){(g.type!=d||h.readyState==c)&&(("load"==g.type?a:h)[k](l+g.type,m,e),!f&&(f=!0)&&b.call(a,null))},n=function(){try{i.doScroll("left")}catch(a){return setTimeout(n,50),void 0}m("poll")};if(h.readyState==c)b.call(a,"lazy");else{if(h.createEventObject&&i.doScroll){try{g=!a.frameElement}catch(o){}g&&n()}h[j](l+"DOMContentLoaded",m,e),h[j](l+d,m,e),a[j](l+"load",m,e)}}function d(a){a=a.match(/^(\W)?(.*)/);var b=document["getElement"+(a[1]?"#"==a[1]?"ById":"sByClassName":"sByTagName")](a[2]),c=[];return null!==b&&(c=b.length?b:0===b.length?b:[b]),c}function e(a,b){var c={};for(var d in a)c[d]=a[d];for(var e in b)c[e]=b[e];return c}function f(a,b,c){b=parseInt(b,10),a=parseInt(a,10);var d=Math.max(b,a),e=Math.min(b,a),f=1/12,g=Math.min(.75*e,.75*d*f);return{height:Math.round(Math.max(c.size,g))}}function g(a,b,c,d){var e=f(b.width,b.height,c),g=e.height,h=b.width*d,i=b.height*d,j=c.font?c.font:"sans-serif";n.width=h,n.height=i,a.textAlign="center",a.textBaseline="middle",a.fillStyle=c.background,a.fillRect(0,0,h,i),a.fillStyle=c.foreground,a.font="bold "+g+"px "+j;var k=c.text?c.text:Math.floor(b.width)+"x"+Math.floor(b.height),l=a.measureText(k).width;return l/h>=.75&&(g=Math.floor(.75*g*(h/l))),a.font="bold "+g*d+"px "+j,a.fillText(k,h/2,i/2,h),n.toDataURL("image/png")}function h(a,b,c,d){var f=c.dimensions,h=c.theme,j=c.text?decodeURIComponent(c.text):c.text,k=f.width+"x"+f.height;h=j?e(h,{text:j}):h,h=c.font?e(h,{font:c.font}):h,"image"==a?(b.setAttribute("data-src",d),b.setAttribute("alt",j?j:h.text?h.text+" ["+k+"]":k),(m||!c.auto)&&(b.style.width=f.width+"px",b.style.height=f.height+"px"),m?b.style.backgroundColor=h.background:b.setAttribute("src",g(o,f,h,r))):"background"==a?m||(b.style.backgroundImage="url("+g(o,f,h,r)+")",b.style.backgroundSize=f.width+"px "+f.height+"px"):"fluid"==a&&(b.setAttribute("data-src",d),b.setAttribute("alt",j?j:h.text?h.text+" ["+k+"]":k),b.style.height="%"==f.height.substr(-1)?f.height:f.height+"px",b.style.width="%"==f.width.substr(-1)?f.width:f.width+"px",("inline"==b.style.display||""===b.style.display)&&(b.style.display="block"),m?b.style.backgroundColor=h.background:(b.holderData=c,s.push(b),i(b)))}function i(a){var b;b=null==a.nodeType?s:[a];for(var c in b){var d=b[c];if(d.holderData){var e=d.holderData;d.setAttribute("src",g(o,{height:d.clientHeight,width:d.clientWidth},e.theme,r))}}}function k(b,c){var d={theme:t.themes.gray},e=!1;for(sl=b.length,j=0;sl>j;j++){var f=b[j];a.flags.dimensions.match(f)?(e=!0,d.dimensions=a.flags.dimensions.output(f)):a.flags.fluid.match(f)?(e=!0,d.dimensions=a.flags.fluid.output(f),d.fluid=!0):a.flags.colors.match(f)?d.theme=a.flags.colors.output(f):c.themes[f]?d.theme=c.themes[f]:a.flags.text.match(f)?d.text=a.flags.text.output(f):a.flags.font.match(f)?d.font=a.flags.font.output(f):a.flags.auto.match(f)&&(d.auto=!0)}return e?d:!1}var l=!1,m=!1,n=document.createElement("canvas");if(document.getElementsByClassName||(document.getElementsByClassName=function(a){var b,c,d,e=document,f=[];if(e.querySelectorAll)return e.querySelectorAll("."+a);if(e.evaluate)for(c=".//*[contains(concat(' ', @class, ' '), ' "+a+" ')]",b=e.evaluate(c,e,null,0,null);d=b.iterateNext();)f.push(d);else for(b=e.getElementsByTagName("*"),c=new RegExp("(^|\\s)"+a+"(\\s|$)"),d=0;d<b.length;d++)c.test(b[d].className)&&f.push(b[d]);return f}),window.getComputedStyle||(window.getComputedStyle=function(a){return this.el=a,this.getPropertyValue=function(b){var c=/(\-([a-z]){1})/g;return"float"==b&&(b="styleFloat"),c.test(b)&&(b=b.replace(c,function(){return arguments[2].toUpperCase()})),a.currentStyle[b]?a.currentStyle[b]:null},this}),Object.prototype.hasOwnProperty||(Object.prototype.hasOwnProperty=function(a){var b=this.__proto__||this.constructor.prototype;return a in this&&(!(a in b)||b[a]!==this[a])}),n.getContext)if(n.toDataURL("image/png").indexOf("data:image/png")<0)m=!0;else var o=n.getContext("2d");else m=!0;var p=1,q=1;m||(p=window.devicePixelRatio||1,q=o.webkitBackingStorePixelRatio||o.mozBackingStorePixelRatio||o.msBackingStorePixelRatio||o.oBackingStorePixelRatio||o.backingStorePixelRatio||1);var r=p/q,s=[],t={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}},stylesheet:""};a.flags={dimensions:{regex:/^(\d+)x(\d+)$/,output:function(a){var b=this.regex.exec(a);return{width:+b[1],height:+b[2]}}},fluid:{regex:/^([0-9%]+)x([0-9%]+)$/,output:function(a){var b=this.regex.exec(a);return{width:b[1],height:b[2]}}},colors:{regex:/#([0-9a-f]{3,})\:#([0-9a-f]{3,})/i,output:function(a){var b=this.regex.exec(a);return{size:t.themes.gray.size,foreground:"#"+b[2],background:"#"+b[1]}}},text:{regex:/text\:(.*)/,output:function(a){return this.regex.exec(a)[1]}},font:{regex:/font\:(.*)/,output:function(a){return this.regex.exec(a)[1]}},auto:{regex:/^auto$/}};for(var u in a.flags)a.flags.hasOwnProperty(u)&&(a.flags[u].match=function(a){return a.match(this.regex)});a.add_theme=function(b,c){return null!=b&&null!=c&&(t.themes[b]=c),a},a.add_image=function(b,c){var e=d(c);if(e.length)for(var f=0,g=e.length;g>f;f++){var h=document.createElement("img");h.setAttribute("data-src",b),e[f].appendChild(h)}return a},a.run=function(b){var c=e(t,b),f=[],g=[],i=[];for("string"==typeof c.images?g=d(c.images):window.NodeList&&c.images instanceof window.NodeList?g=c.images:window.Node&&c.images instanceof window.Node&&(g=[c.images]),"string"==typeof c.bgnodes?i=d(c.bgnodes):window.NodeList&&c.elements instanceof window.NodeList?i=c.bgnodes:window.Node&&c.bgnodes instanceof window.Node&&(i=[c.bgnodes]),l=!0,o=0,n=g.length;n>o;o++)f.push(g[o]);var j=document.getElementById("holderjs-style");j||(j=document.createElement("style"),j.setAttribute("id","holderjs-style"),j.type="text/css",document.getElementsByTagName("head")[0].appendChild(j)),c.nocss||(j.styleSheet?j.styleSheet.cssText+=c.stylesheet:j.appendChild(document.createTextNode(c.stylesheet)));for(var m=new RegExp(c.domain+'/(.*?)"?\\)'),n=i.length,o=0;n>o;o++){var p=window.getComputedStyle(i[o],null).getPropertyValue("background-image"),q=p.match(m),r=i[o].getAttribute("data-background-src");if(q){var s=k(q[1].split("/"),c);s&&h("background",i[o],s,p)}else if(null!=r){var s=k(r.substr(r.lastIndexOf(c.domain)+c.domain.length+1).split("/"),c);s&&h("background",i[o],s,p)}}for(n=f.length,o=0;n>o;o++){var u,v;v=u=p=null;try{v=f[o].getAttribute("src"),attr_datasrc=f[o].getAttribute("data-src")}catch(w){}if(null==attr_datasrc&&v&&v.indexOf(c.domain)>=0?p=v:attr_datasrc&&attr_datasrc.indexOf(c.domain)>=0&&(p=attr_datasrc),p){var s=k(p.substr(p.lastIndexOf(c.domain)+c.domain.length+1).split("/"),c);s&&(s.fluid?h("fluid",f[o],s,p):h("image",f[o],s,p))}}return a},c(b,function(){window.addEventListener?(window.addEventListener("resize",i,!1),window.addEventListener("orientationchange",i,!1)):window.attachEvent("onresize",i),l||a.run()}),"function"==typeof define&&define.amd&&define("Holder",[],function(){return a})}(Holder,window);