js/holder/holder.js
changeset 0 ba8ab09f730e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/holder/holder.js	Fri Jul 04 16:42:41 2014 +0400
     1.3 @@ -0,0 +1,401 @@
     1.4 +/*
     1.5 +
     1.6 +Holder - 1.9 - client side image placeholders
     1.7 +(c) 2012-2013 Ivan Malopinsky / http://imsky.co
     1.8 +
     1.9 +Provided under the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
    1.10 +Commercial use requires attribution.
    1.11 +
    1.12 +*/
    1.13 +
    1.14 +var Holder = Holder || {};
    1.15 +(function (app, win) {
    1.16 +
    1.17 +var preempted = false,
    1.18 +fallback = false,
    1.19 +canvas = document.createElement('canvas');
    1.20 +
    1.21 +//getElementsByClassName polyfill
    1.22 +document.getElementsByClassName||(document.getElementsByClassName=function(e){var t=document,n,r,i,s=[];if(t.querySelectorAll)return t.querySelectorAll("."+e);if(t.evaluate){r=".//*[contains(concat(' ', @class, ' '), ' "+e+" ')]",n=t.evaluate(r,t,null,0,null);while(i=n.iterateNext())s.push(i)}else{n=t.getElementsByTagName("*"),r=new RegExp("(^|\\s)"+e+"(\\s|$)");for(i=0;i<n.length;i++)r.test(n[i].className)&&s.push(n[i])}return s})
    1.23 +
    1.24 +//getComputedStyle polyfill
    1.25 +window.getComputedStyle||(window.getComputedStyle=function(e,t){return this.el=e,this.getPropertyValue=function(t){var n=/(\-([a-z]){1})/g;return t=="float"&&(t="styleFloat"),n.test(t)&&(t=t.replace(n,function(){return arguments[2].toUpperCase()})),e.currentStyle[t]?e.currentStyle[t]:null},this})
    1.26 +
    1.27 +//http://javascript.nwbox.com/ContentLoaded by Diego Perini with modifications
    1.28 +function contentLoaded(n,t){var l="complete",s="readystatechange",u=!1,h=u,c=!0,i=n.document,a=i.documentElement,e=i.addEventListener?"addEventListener":"attachEvent",v=i.addEventListener?"removeEventListener":"detachEvent",f=i.addEventListener?"":"on",r=function(e){(e.type!=s||i.readyState==l)&&((e.type=="load"?n:i)[v](f+e.type,r,u),!h&&(h=!0)&&t.call(n,null))},o=function(){try{a.doScroll("left")}catch(n){setTimeout(o,50);return}r("poll")};if(i.readyState==l)t.call(n,"lazy");else{if(i.createEventObject&&a.doScroll){try{c=!n.frameElement}catch(y){}c&&o()}i[e](f+"DOMContentLoaded",r,u),i[e](f+s,r,u),n[e](f+"load",r,u)}};
    1.29 +
    1.30 +//https://gist.github.com/991057 by Jed Schmidt with modifications
    1.31 +function selector(a){
    1.32 +	a=a.match(/^(\W)?(.*)/);var b=document["getElement"+(a[1]?a[1]=="#"?"ById":"sByClassName":"sByTagName")](a[2]);
    1.33 +	var ret=[];	b!=null&&(b.length?ret=b:b.length==0?ret=b:ret=[b]);	return ret;
    1.34 +}
    1.35 +
    1.36 +//shallow object property extend
    1.37 +function extend(a,b){var c={};for(var d in a)c[d]=a[d];for(var e in b)c[e]=b[e];return c}
    1.38 +
    1.39 +//hasOwnProperty polyfill
    1.40 +if (!Object.prototype.hasOwnProperty)
    1.41 +	Object.prototype.hasOwnProperty = function(prop) {
    1.42 +		var proto = this.__proto__ || this.constructor.prototype;
    1.43 +		return (prop in this) && (!(prop in proto) || proto[prop] !== this[prop]);
    1.44 +	}
    1.45 +
    1.46 +function text_size(width, height, template) {
    1.47 +	var dimension_arr = [height, width].sort();
    1.48 +	var maxFactor = Math.round(dimension_arr[1] / 16),
    1.49 +		minFactor = Math.round(dimension_arr[0] / 16);
    1.50 +	var text_height = Math.max(template.size, maxFactor);
    1.51 +	return {
    1.52 +		height: text_height
    1.53 +	}
    1.54 +}
    1.55 +
    1.56 +function draw(ctx, dimensions, template, ratio) {
    1.57 +	var ts = text_size(dimensions.width, dimensions.height, template);
    1.58 +	var text_height = ts.height;
    1.59 +	var width = dimensions.width * ratio, height = dimensions.height * ratio;
    1.60 +	var font = template.font ? template.font : "sans-serif";
    1.61 +	canvas.width = width;
    1.62 +	canvas.height = height;
    1.63 +	ctx.textAlign = "center";
    1.64 +	ctx.textBaseline = "middle";
    1.65 +	ctx.fillStyle = template.background;
    1.66 +	ctx.fillRect(0, 0, width, height);
    1.67 +	ctx.fillStyle = template.foreground;
    1.68 +	ctx.font = "bold " + text_height + "px "+font;
    1.69 +	var text = template.text ? template.text : (dimensions.width + "x" + dimensions.height);
    1.70 +	if (ctx.measureText(text).width / width > 1) {
    1.71 +		text_height = template.size / (ctx.measureText(text).width / width);
    1.72 +	}
    1.73 +	//Resetting font size if necessary
    1.74 +	ctx.font = "bold " + (text_height * ratio) + "px "+font;
    1.75 +	ctx.fillText(text, (width / 2), (height / 2), width);
    1.76 +	return canvas.toDataURL("image/png");
    1.77 +}
    1.78 +
    1.79 +function render(mode, el, holder, src) {
    1.80 +	var dimensions = holder.dimensions,
    1.81 +		theme = holder.theme,
    1.82 +		text = holder.text ? decodeURIComponent(holder.text) : holder.text;
    1.83 +	var dimensions_caption = dimensions.width + "x" + dimensions.height;
    1.84 +	theme = (text ? extend(theme, {	text: text }) : theme);
    1.85 +	theme = (holder.font ? extend(theme, {font: holder.font}) : theme);
    1.86 +
    1.87 +	var ratio = 1;
    1.88 +	if(window.devicePixelRatio && window.devicePixelRatio > 1){
    1.89 +		ratio = window.devicePixelRatio;
    1.90 +	}
    1.91 +
    1.92 +	if (mode == "image") {
    1.93 +		el.setAttribute("data-src", src);
    1.94 +		el.setAttribute("alt", text ? text : theme.text ? theme.text + " [" + dimensions_caption + "]" : dimensions_caption);
    1.95 +
    1.96 +		if(fallback || !holder.auto){
    1.97 +		    el.style.width = dimensions.width + "px";
    1.98 +		    el.style.height = dimensions.height + "px";
    1.99 +		}
   1.100 +
   1.101 +		if (fallback) {
   1.102 +			el.style.backgroundColor = theme.background;
   1.103 +
   1.104 +		}
   1.105 +		else{
   1.106 +			el.setAttribute("src", draw(ctx, dimensions, theme, ratio));
   1.107 +		}
   1.108 +	} else {
   1.109 +		if (!fallback) {
   1.110 +			el.style.backgroundImage = "url(" + draw(ctx, dimensions, theme, ratio) + ")";
   1.111 +			el.style.backgroundSize = dimensions.width+"px "+dimensions.height+"px";
   1.112 +		}
   1.113 +	}
   1.114 +};
   1.115 +
   1.116 +function fluid(el, holder, src) {
   1.117 +	var dimensions = holder.dimensions,
   1.118 +		theme = holder.theme,
   1.119 +		text = holder.text;
   1.120 +	var dimensions_caption = dimensions.width + "x" + dimensions.height;
   1.121 +	theme = (text ? extend(theme, {
   1.122 +		text: text
   1.123 +	}) : theme);
   1.124 +
   1.125 +	var fluid = document.createElement("div");
   1.126 +
   1.127 +	fluid.style.backgroundColor = theme.background;
   1.128 +	fluid.style.color = theme.foreground;
   1.129 +	fluid.className = el.className + " holderjs-fluid";
   1.130 +	fluid.style.width = holder.dimensions.width + (holder.dimensions.width.indexOf("%")>0?"":"px");
   1.131 +	fluid.style.height = holder.dimensions.height + (holder.dimensions.height.indexOf("%")>0?"":"px");
   1.132 +	fluid.id = el.id;
   1.133 +
   1.134 +	el.style.width=0;
   1.135 +	el.style.height=0;
   1.136 +
   1.137 +	if (theme.text) {
   1.138 +		fluid.appendChild(document.createTextNode(theme.text))
   1.139 +	} else {
   1.140 +		fluid.appendChild(document.createTextNode(dimensions_caption))
   1.141 +		fluid_images.push(fluid);
   1.142 +		setTimeout(fluid_update, 0);
   1.143 +	}
   1.144 +
   1.145 +	el.parentNode.insertBefore(fluid, el.nextSibling)
   1.146 +
   1.147 +	if(window.jQuery){
   1.148 +	    jQuery(function($){
   1.149 +		$(el).on("load", function(){
   1.150 +		   el.style.width = fluid.style.width;
   1.151 +		   el.style.height = fluid.style.height;
   1.152 +		   $(el).show();
   1.153 +		   $(fluid).remove();
   1.154 +		});
   1.155 +	    })
   1.156 +	}
   1.157 +}
   1.158 +
   1.159 +function fluid_update() {
   1.160 +	for (i in fluid_images) {
   1.161 +		if(!fluid_images.hasOwnProperty(i)) continue;
   1.162 +		var el = fluid_images[i],
   1.163 +			label = el.firstChild;
   1.164 +
   1.165 +		el.style.lineHeight = el.offsetHeight+"px";
   1.166 +		label.data = el.offsetWidth + "x" + el.offsetHeight;
   1.167 +	}
   1.168 +}
   1.169 +
   1.170 +function parse_flags(flags, options) {
   1.171 +
   1.172 +	var ret = {
   1.173 +		theme: settings.themes.gray
   1.174 +	}, render = false;
   1.175 +
   1.176 +	for (sl = flags.length, j = 0; j < sl; j++) {
   1.177 +		var flag = flags[j];
   1.178 +		if (app.flags.dimensions.match(flag)) {
   1.179 +			render = true;
   1.180 +			ret.dimensions = app.flags.dimensions.output(flag);
   1.181 +		} else if (app.flags.fluid.match(flag)) {
   1.182 +			render = true;
   1.183 +			ret.dimensions = app.flags.fluid.output(flag);
   1.184 +			ret.fluid = true;
   1.185 +		} else if (app.flags.colors.match(flag)) {
   1.186 +			ret.theme = app.flags.colors.output(flag);
   1.187 +		} else if (options.themes[flag]) {
   1.188 +			//If a theme is specified, it will override custom colors
   1.189 +			ret.theme = options.themes[flag];
   1.190 +		} else if (app.flags.text.match(flag)) {
   1.191 +			ret.text = app.flags.text.output(flag);
   1.192 +		} else if(app.flags.font.match(flag)){
   1.193 +			ret.font = app.flags.font.output(flag);
   1.194 +		}
   1.195 +		else if(app.flags.auto.match(flag)){
   1.196 +			ret.auto = true;
   1.197 +		}
   1.198 +	}
   1.199 +
   1.200 +	return render ? ret : false;
   1.201 +
   1.202 +};
   1.203 +
   1.204 +if (!canvas.getContext) {
   1.205 +	fallback = true;
   1.206 +} else {
   1.207 +	if (canvas.toDataURL("image/png")
   1.208 +		.indexOf("data:image/png") < 0) {
   1.209 +		//Android doesn't support data URI
   1.210 +		fallback = true;
   1.211 +	} else {
   1.212 +		var ctx = canvas.getContext("2d");
   1.213 +	}
   1.214 +}
   1.215 +
   1.216 +var fluid_images = [];
   1.217 +
   1.218 +var settings = {
   1.219 +	domain: "holder.js",
   1.220 +	images: "img",
   1.221 +	bgnodes: ".holderjs",
   1.222 +	themes: {
   1.223 +		"gray": {
   1.224 +			background: "#eee",
   1.225 +			foreground: "#aaa",
   1.226 +			size: 12
   1.227 +		},
   1.228 +			"social": {
   1.229 +			background: "#3a5a97",
   1.230 +			foreground: "#fff",
   1.231 +			size: 12
   1.232 +		},
   1.233 +			"industrial": {
   1.234 +			background: "#434A52",
   1.235 +			foreground: "#C2F200",
   1.236 +			size: 12
   1.237 +		}
   1.238 +	},
   1.239 +	stylesheet: ".holderjs-fluid {font-size:16px;font-weight:bold;text-align:center;font-family:sans-serif;margin:0}"
   1.240 +};
   1.241 +
   1.242 +
   1.243 +app.flags = {
   1.244 +	dimensions: {
   1.245 +		regex: /^(\d+)x(\d+)$/,
   1.246 +		output: function (val) {
   1.247 +			var exec = this.regex.exec(val);
   1.248 +			return {
   1.249 +				width: +exec[1],
   1.250 +				height: +exec[2]
   1.251 +			}
   1.252 +		}
   1.253 +	},
   1.254 +	fluid: {
   1.255 +		regex: /^([0-9%]+)x([0-9%]+)$/,
   1.256 +		output: function (val) {
   1.257 +			var exec = this.regex.exec(val);
   1.258 +			return {
   1.259 +				width: exec[1],
   1.260 +				height: exec[2]
   1.261 +			}
   1.262 +		}
   1.263 +	},
   1.264 +	colors: {
   1.265 +		regex: /#([0-9a-f]{3,})\:#([0-9a-f]{3,})/i,
   1.266 +		output: function (val) {
   1.267 +			var exec = this.regex.exec(val);
   1.268 +			return {
   1.269 +				size: settings.themes.gray.size,
   1.270 +				foreground: "#" + exec[2],
   1.271 +				background: "#" + exec[1]
   1.272 +			}
   1.273 +		}
   1.274 +	},
   1.275 +	text: {
   1.276 +		regex: /text\:(.*)/,
   1.277 +		output: function (val) {
   1.278 +			return this.regex.exec(val)[1];
   1.279 +		}
   1.280 +	},
   1.281 +	font: {
   1.282 +	    regex: /font\:(.*)/,
   1.283 +	    output: function(val){
   1.284 +		return this.regex.exec(val)[1];
   1.285 +	    }
   1.286 +	},
   1.287 +	auto: {
   1.288 +	    regex: /^auto$/
   1.289 +	}
   1.290 +}
   1.291 +
   1.292 +for (var flag in app.flags) {
   1.293 +	if(!app.flags.hasOwnProperty(flag)) continue;
   1.294 +	app.flags[flag].match = function (val) {
   1.295 +		return val.match(this.regex)
   1.296 +	}
   1.297 +}
   1.298 +
   1.299 +app.add_theme = function (name, theme) {
   1.300 +	name != null && theme != null && (settings.themes[name] = theme);
   1.301 +	return app;
   1.302 +};
   1.303 +
   1.304 +app.add_image = function (src, el) {
   1.305 +	var node = selector(el);
   1.306 +	if (node.length) {
   1.307 +		for (var i = 0, l = node.length; i < l; i++) {
   1.308 +			var img = document.createElement("img")
   1.309 +			img.setAttribute("data-src", src);
   1.310 +			node[i].appendChild(img);
   1.311 +		}
   1.312 +	}
   1.313 +	return app;
   1.314 +};
   1.315 +
   1.316 +app.run = function (o) {
   1.317 +	var options = extend(settings, o), images = [];
   1.318 +
   1.319 +	if(options.images instanceof window.NodeList){
   1.320 +	    imageNodes = options.images;
   1.321 +	}
   1.322 +	else if(options.images instanceof window.Node){
   1.323 +	    imageNodes = [options.images];
   1.324 +	}
   1.325 +	else{
   1.326 +	    imageNodes = selector(options.images);
   1.327 +	}
   1.328 +
   1.329 +	if(options.elements instanceof window.NodeList){
   1.330 +	    bgnodes = options.bgnodes;
   1.331 +	}
   1.332 +	else if(options.bgnodes instanceof window.Node){
   1.333 +	    bgnodes = [options.bgnodes];
   1.334 +	}
   1.335 +	else{
   1.336 +	    bgnodes = selector(options.bgnodes);
   1.337 +	}
   1.338 +
   1.339 +	preempted = true;
   1.340 +
   1.341 +	for (i = 0, l = imageNodes.length; i < l; i++) images.push(imageNodes[i]);
   1.342 +
   1.343 +	var holdercss = document.getElementById("holderjs-style");
   1.344 +
   1.345 +	if(!holdercss){
   1.346 +	    holdercss = document.createElement("style");
   1.347 +	    holdercss.setAttribute("id", "holderjs-style");
   1.348 +	    holdercss.type = "text/css";
   1.349 +	    document.getElementsByTagName("head")[0].appendChild(holdercss);
   1.350 +	}
   1.351 +
   1.352 +	if(holdercss.styleSheet){
   1.353 +	    holdercss.styleSheet += options.stylesheet;
   1.354 +	}
   1.355 +	else{
   1.356 +	    holdercss.textContent+= options.stylesheet;
   1.357 +	}
   1.358 +
   1.359 +	var cssregex = new RegExp(options.domain + "\/(.*?)\"?\\)");
   1.360 +
   1.361 +	for (var l = bgnodes.length, i = 0; i < l; i++) {
   1.362 +		var src = window.getComputedStyle(bgnodes[i], null)
   1.363 +			.getPropertyValue("background-image");
   1.364 +		var flags = src.match(cssregex);
   1.365 +		if (flags) {
   1.366 +			var holder = parse_flags(flags[1].split("/"), options);
   1.367 +			if (holder) {
   1.368 +				render("background", bgnodes[i], holder, src);
   1.369 +			}
   1.370 +		}
   1.371 +	}
   1.372 +
   1.373 +	for (var l = images.length, i = 0; i < l; i++) {
   1.374 +		var src = images[i].getAttribute("src") || images[i].getAttribute("data-src");
   1.375 +		if (src != null && src.indexOf(options.domain) >= 0) {
   1.376 +			var holder = parse_flags(src.substr(src.lastIndexOf(options.domain) + options.domain.length + 1)
   1.377 +				.split("/"), options);
   1.378 +			if (holder) {
   1.379 +				if (holder.fluid) {
   1.380 +					fluid(images[i], holder, src);
   1.381 +				} else {
   1.382 +					render("image", images[i], holder, src);
   1.383 +				}
   1.384 +			}
   1.385 +		}
   1.386 +	}
   1.387 +	return app;
   1.388 +};
   1.389 +
   1.390 +contentLoaded(win, function () {
   1.391 +	if (window.addEventListener) {
   1.392 +		window.addEventListener("resize", fluid_update, false);
   1.393 +		window.addEventListener("orientationchange", fluid_update, false);
   1.394 +	} else {
   1.395 +		window.attachEvent("onresize", fluid_update)
   1.396 +	}
   1.397 +	preempted || app.run();
   1.398 +});
   1.399 +
   1.400 +if ( typeof define === "function" && define.amd ) {
   1.401 +	define( "Holder", [], function () { return app; } );
   1.402 +}
   1.403 +
   1.404 +})(Holder, window);
   1.405 \ No newline at end of file