PageSourceSearch

https://www.menterwolde.info/js/holder.js

js menterwolde.info collected 2026-10-02 14:04:25 UTC 20,213 bytes, 657 lines download raw bytes

1/*!
2
3Holder - 2.3.2 - client side image placeholders
4(c) 2012-2014 Ivan Malopinsky / http://imsky.co
5
6Provided under the MIT License.
7Commercial use requires attribution.
8
9*/
10var Holder = Holder || {};
11(function (app, win) {
12var system_config = {
13	use_svg: false,
14	use_canvas: false,
15	use_fallback: false
16};
17var instance_config = {};
18var preempted = false;
19canvas = document.createElement('canvas');
20var dpr = 1, bsr = 1;
21var resizable_images = [];
22
23if (!canvas.getContext) {
24	system_config.use_fallback = true;
25} else {
26	if (canvas.toDataURL("image/png")
27		.indexOf("data:image/png") < 0) {
28		//Android doesn't support data URI
29		system_config.use_fallback = true;
30	} else {
31		var ctx = canvas.getContext("2d");
32	}
33}
34
35if(!!document.createElementNS && !!document.createElementNS('http://www.w3.org/2000/svg', 'svg').createSVGRect){
36	system_config.use_svg = true;
37	system_config.use_canvas = false;
38}
39
40if(!system_config.use_fallback){
41    dpr = window.devicePixelRatio || 1,
42    bsr = ctx.webkitBackingStorePixelRatio || ctx.mozBackingStorePixelRatio || ctx.msBackingStorePixelRatio || ctx.oBackingStorePixelRatio || ctx.backingStorePixelRatio || 1;
43}
44
45var ratio = dpr / bsr;
46
47var settings = {
48	domain: "holder.js",
49	images: "img",
50	bgnodes: ".holderjs",
51	themes: {
52		"gray": {
53			background: "#eee",
54			foreground: "#aaa",
55			size: 12
56		},
57		"social": {
58			background: "#3a5a97",
59			foreground: "#fff",
60			size: 12
61		},
62		"industrial": {
63			background: "#434A52",
64			foreground: "#C2F200",
65			size: 12
66		},
67		"sky": {
68			background: "#0D8FDB",
69			foreground: "#fff",
70			size: 12
71		},
72		"vine": {
73			background: "#39DBAC",
74			foreground: "#1E292C",
75			size: 12
76		},
77		"lava": {
78			background: "#F8591A",
79			foreground: "#1C2846",
80			size: 12
81		}
82	},
83	stylesheet: ""
84};
85app.flags = {
86	dimensions: {
87		regex: /^(\d+)x(\d+)$/,
88		output: function (val) {
89			var exec = this.regex.exec(val);
90			return {
91				width: +exec[1],
92				height: +exec[2]
93			}
94		}
95	},
96	fluid: {
97		regex: /^([0-9%]+)x([0-9%]+)$/,
98		output: function (val) {
99			var exec = this.regex.exec(val);
100			return {
101				width: exec[1],
102				height: exec[2]
103			}
104		}
105	},
106	colors: {
107		regex: /#([0-9a-f]{3,})\:#([0-9a-f]{3,})/i,
108		output: function (val) {
109			var exec = this.regex.exec(val);
110			return {
111				size: settings.themes.gray.size,
112				foreground: "#" + exec[2],
113				background: "#" + exec[1]
114			}
115		}
116	},
117	text: {
118		regex: /text\:(.*)/,
119		output: function (val) {
120			return this.regex.exec(val)[1];
121		}
122	},
123	font: {
124		regex: /font\:(.*)/,
125		output: function (val) {
126			return this.regex.exec(val)[1];
127		}
128	},
129	auto: {
130		regex: /^auto$/
131	},
132	textmode: {
133		regex: /textmode\:(.*)/,
134		output: function(val){
135			return this.regex.exec(val)[1];
136		}
137	}
138}
139
140function text_size(width, height, template) {
141	height = parseInt(height, 10);
142	width = parseInt(width, 10);
143	var bigSide = Math.max(height, width)
144	var smallSide = Math.min(height, width)
145	var scale = 1 / 12;
146	var newHeight = Math.min(smallSide * 0.75, 0.75 * bigSide * scale);
147	return {
148		height: Math.round(Math.max(template.size, newHeight))
149	}
150}
151
152var svg_el = (function(){
153	//Prevent IE <9 from initializing SVG renderer
154	if(!window.XMLSerializer) return;
155	var serializer = new XMLSerializer();
156	var svg_ns = "http://www.w3.org/2000/svg"
157	var svg = document.createElementNS(svg_ns, "svg");
158	//IE throws an exception if this is set and Chrome requires it to be set
159	if(svg.webkitMatchesSelector){
160		svg.setAttribute("xmlns", "http://www.w3.org/2000/svg")
161	}
162	var bg_el = document.createElementNS(svg_ns, "rect")
163	var text_el = document.createElementNS(svg_ns, "text")
164	var textnode_el = document.createTextNode(null)
165	text_el.setAttribute("text-anchor", "middle")
166	text_el.appendChild(textnode_el)
167	svg.appendChild(bg_el)
168	svg.appendChild(text_el)
169
170	return function(props){
171		svg.setAttribute("width",props.width);
172		svg.setAttribute("height", props.height);
173		bg_el.setAttribute("width", props.width);
174		bg_el.setAttribute("height", props.height);
175		bg_el.setAttribute("fill", props.template.background);
176		text_el.setAttribute("x", props.width/2)
177		text_el.setAttribute("y", props.height/2)
178		textnode_el.nodeValue=props.text
179		text_el.setAttribute("style", css_properties({
180		"fill": props.template.foreground,
181		"font-weight": "bold",
182		"font-size": props.text_height+"px",
183		"font-family":props.font,
184		"dominant-baseline":"central"
185		}))
186		return serializer.serializeToString(svg)
187	}
188})()
189
190function css_properties(props){
191	var ret = [];
192	for(p in props){
193		if(props.hasOwnProperty(p)){
194			ret.push(p+":"+props[p])
195		}
196	}
197	return ret.join(";")
198}
199
200function draw_canvas(args) {
201	var ctx = args.ctx,
202		dimensions = args.dimensions,
203		template = args.template,
204		ratio = args.ratio,
205		holder = args.holder,
206		literal = holder.textmode == "literal",
207		exact = holder.textmode == "exact";
208
209	var ts = text_size(dimensions.width, dimensions.height, template);
210	var text_height = ts.height;
211	var width = dimensions.width * ratio,
212		height = dimensions.height * ratio;
213	var font = template.font ? template.font : "Arial,Helvetica,sans-serif";
214	canvas.width = width;
215	canvas.height = height;
216	ctx.textAlign = "center";
217	ctx.textBaseline = "middle";
218	ctx.fillStyle = template.background;
219	ctx.fillRect(0, 0, width, height);
220	ctx.fillStyle = template.foreground;
221	ctx.font = "bold " + text_height + "px " + font;
222	var text = template.text ? template.text : (Math.floor(dimensions.width) + "x" + Math.floor(dimensions.height));
223	if (literal) {
224		var dimensions = holder.dimensions;
225		text = dimensions.width + "x" + dimensions.height;
226	}
227	else if(exact && holder.exact_dimensions){
228		var dimensions = holder.exact_dimensions;
229		text = (Math.floor(dimensions.width) + "x" + Math.floor(dimensions.height));
230	}
231	var text_width = ctx.measureText(text).width;
232	if (text_width / width >= 0.75) {
233		text_height = Math.floor(text_height * 0.75 * (width / text_width));
234	}
235	//Resetting font size if necessary
236	ctx.font = "bold " + (text_height * ratio) + "px " + font;
237	ctx.fillText(text, (width / 2), (height / 2), width);
238	return canvas.toDataURL("image/png");
239}
240
241function draw_svg(args){
242	var dimensions = args.dimensions,
243		template = args.template,
244		holder = args.holder,
245		literal = holder.textmode == "literal",
246		exact = holder.textmode == "exact";
247
248	var ts = text_size(dimensions.width, dimensions.height, template);
249	var text_height = ts.height;
250	var width = dimensions.width,
251		height = dimensions.height;
252		
253	var font = template.font ? template.font : "Arial,Helvetica,sans-serif";
254	var text = template.text ? template.text : (Math.floor(dimensions.width) + "x" + Math.floor(dimensions.height));
255	
256	if (literal) {
257		var dimensions = holder.dimensions;
258		text = dimensions.width + "x" + dimensions.height;
259	}
260	else if(exact && holder.exact_dimensions){
261		var dimensions = holder.exact_dimensions;
262		text = (Math.floor(dimensions.width) + "x" + Math.floor(dimensions.height));
263	}
264	var string = svg_el({
265		text: text, 
266		width:width, 
267		height:height, 
268		text_height:text_height, 
269		font:font, 
270		template:template
271	})
272	return "data:image/svg+xml;base64,"+btoa(unescape(encodeURIComponent(string)));
273}
274
275function draw(args) {
276	if(instance_config.use_canvas && !instance_config.use_svg){
277		return draw_canvas(args);
278	}
279	else{
280		return draw_svg(args);
281	}
282}
283
284function render(mode, el, holder, src) {
285	var dimensions = holder.dimensions,
286		theme = holder.theme,
287		text = holder.text ? decodeURIComponent(holder.text) : holder.text;
288	var dimensions_caption = dimensions.width + "x" + dimensions.height;
289	theme = (text ? extend(theme, {
290		text: text
291	}) : theme);
292	theme = (holder.font ? extend(theme, {
293		font: holder.font
294	}) : theme);
295	el.setAttribute("data-src", src);
296	holder.theme = theme;
297	el.holder_data = holder;
298	
299	if (mode == "image") {
300		el.setAttribute("alt", text ? text : theme.text ? theme.text + " [" + dimensions_caption + "]" : dimensions_caption);
301		if (instance_config.use_fallback || !holder.auto) {
302			el.style.width = dimensions.width + "px";
303		}
304		if (instance_config.use_fallback) {
305			el.style.backgroundColor = theme.background;
306		} else {
307			el.setAttribute("src", draw({ctx: ctx, dimensions: dimensions, template: theme, ratio:ratio, holder: holder}));
308			
309			if(holder.textmode && holder.textmode == "exact"){
310				resizable_images.push(el);
311				resizable_update(el);
312			}
313			
314		}
315	} else if (mode == "background") {
316		if (!instance_config.use_fallback) {
317			el.style.backgroundImage = "url(" + draw({ctx:ctx, dimensions: dimensions, template: theme, ratio: ratio, holder: holder}) + ")";
318			el.style.backgroundSize = dimensions.width + "px " + dimensions.height + "px";
319		}
320	} else if (mode == "fluid") {
321		el.setAttribute("alt", text ? text : theme.text ? theme.text + " [" + dimensions_caption + "]" : dimensions_caption);
322		if (dimensions.height.slice(-1) == "%") {
323			el.style.height = dimensions.height
324		} else if(holder.auto == null || !holder.auto){
325			el.style.height = dimensions.height + "px"
326		}
327		if (dimensions.width.slice(-1) == "%") {
328			el.style.width = dimensions.width
329		} else if(holder.auto == null || !holder.auto){
330			el.style.width = dimensions.width + "px"
331		}
332		if (el.style.display == "inline" || el.style.display === "" || el.style.display == "none") {
333			el.style.display = "block";
334		}
335		
336		set_initial_dimensions(el)
337		
338		if (instance_config.use_fallback) {
339			el.style.backgroundColor = theme.background;
340		} else {
341			resizable_images.push(el);
342			resizable_update(el);
343		}
344	}
345}
346
347function dimension_check(el, callback) {
348	var dimensions = {
349		height: el.clientHeight,
350		width: el.clientWidth
351	};
352	if (!dimensions.height && !dimensions.width) {
353		el.setAttribute("data-holder-invisible", true)
354		callback.call(this, el)
355	}
356	else{
357		el.removeAttribute("data-holder-invisible")
358		return dimensions;
359	}
360}
361
362function set_initial_dimensions(el){
363	if(el.holder_data){
364		var dimensions = dimension_check(el, app.invisible_error_fn( set_initial_dimensions))
365		if(dimensions){
366			var holder = el.holder_data;
367			holder.initial_dimensions = dimensions;
368			holder.fluid_data = {
369				fluid_height: holder.dimensions.height.slice(-1) == "%",
370				fluid_width: holder.dimensions.width.slice(-1) == "%",
371				mode: null
372			}
373			if(holder.fluid_data.fluid_width && !holder.fluid_data.fluid_height){
374				holder.fluid_data.mode = "width"
375				holder.fluid_data.ratio = holder.initial_dimensions.width / parseFloat(holder.dimensions.height)
376			}
377			else if(!holder.fluid_data.fluid_width && holder.fluid_data.fluid_height){
378				holder.fluid_data.mode = "height";
379				holder.fluid_data.ratio = parseFloat(holder.dimensions.width) / holder.initial_dimensions.height
380			}
381		}
382	}
383}
384
385function resizable_update(element) {
386	var images;
387	if (element.nodeType == null) {
388		images = resizable_images;
389	} else {
390		images = [element]
391	}
392	for (var i in images) {
393		if (!images.hasOwnProperty(i)) {
394			continue;
395		}
396		var el = images[i]
397		if (el.holder_data) {
398			var holder = el.holder_data;
399			var dimensions = dimension_check(el, app.invisible_error_fn( resizable_update))
400			if(dimensions){
401				if(holder.fluid){
402					if(holder.auto){
403						switch(holder.fluid_data.mode){
404							case "width":
405								dimensions.height = dimensions.width / holder.fluid_data.ratio;
406							break;
407							case "height":
408								dimensions.width = dimensions.height * holder.fluid_data.ratio;
409							break;
410						}
411					}
412					el.setAttribute("src", draw({
413						ctx: ctx,
414						dimensions: dimensions,
415						template: holder.theme,
416						ratio: ratio,
417						holder: holder
418					}))
419				}
420				if(holder.textmode && holder.textmode == "exact"){
421					holder.exact_dimensions = dimensions;
422					el.setAttribute("src", draw({
423						ctx: ctx,
424						dimensions: holder.dimensions,
425						template: holder.theme,
426						ratio: ratio,
427						holder: holder
428					}))
429				}
430			}
431		}
432	}
433}
434
435function parse_flags(flags, options) {
436	var ret = {
437		theme: extend(settings.themes.gray, {})
438	};
439	var render = false;
440	for (var fl = flags.length, j = 0; j < fl; j++) {
441		var flag = flags[j];
442		if (app.flags.dimensions.match(flag)) {
443			render = true;
444			ret.dimensions = app.flags.dimensions.output(flag);
445		} else if (app.flags.fluid.match(flag)) {
446			render = true;
447			ret.dimensions = app.flags.fluid.output(flag);
448			ret.fluid = true;
449		} else if (app.flags.textmode.match(flag)) {
450			ret.textmode = app.flags.textmode.output(flag)
451		} else if (app.flags.colors.match(flag)) {
452			ret.theme = app.flags.colors.output(flag);
453		} else if (options.themes[flag]) {
454			//If a theme is specified, it will override custom colors
455			if(options.themes.hasOwnProperty(flag)){
456				ret.theme = extend(options.themes[flag], {});
457			}
458		} else if (app.flags.font.match(flag)) {
459			ret.font = app.flags.font.output(flag);
460		} else if (app.flags.auto.match(flag)) {
461			ret.auto = true;
462		} else if (app.flags.text.match(flag)) {
463			ret.text = app.flags.text.output(flag);
464		}
465	}
466	return render ? ret : false;
467}
468
469for (var flag in app.flags) {
470	if (!app.flags.hasOwnProperty(flag)) continue;
471	app.flags[flag].match = function (val) {
472		return val.match(this.regex)
473	}
474}
475
476app.invisible_error_fn = function(fn){
477	return function(el){
478		if(el.hasAttribute("data-holder-invisible")){
479			throw new Error("Holder: invisible placeholder")
480		}
481	}
482}
483
484app.add_theme = function (name, theme) {
485	name != null && theme != null && (settings.themes[name] = theme);
486	return app;
487};
488
489app.add_image = function (src, el) {
490	var node = selector(el);
491	if (node.length) {
492		for (var i = 0, l = node.length; i < l; i++) {
493			var img = document.createElement("img")
494			img.setAttribute("data-src", src);
495			node[i].appendChild(img);
496		}
497	}
498	return app;
499};
500
501app.run = function (o) {
502
503	instance_config = extend({}, system_config)
504	preempted = true;
505
506	var options = extend(settings, o),
507		images = [],
508		imageNodes = [],
509		bgnodes = [];
510
511	if(options.use_canvas != null && options.use_canvas){
512		instance_config.use_canvas = true;
513		instance_config.use_svg = false;
514	}
515			
516	if (typeof (options.images) == "string") {
517		imageNodes = selector(options.images);
518	} else if (window.NodeList && options.images instanceof window.NodeList) {
519		imageNodes = options.images;
520	} else if (window.Node && options.images instanceof window.Node) {
521		imageNodes = [options.images];
522	} else if(window.HTMLCollection && options.images instanceof window.HTMLCollection){
523		imageNodes = options.images
524	}
525
526	if (typeof (options.bgnodes) == "string") {
527		bgnodes = selector(options.bgnodes);
528	} else if (window.NodeList && options.elements instanceof window.NodeList) {
529		bgnodes = options.bgnodes;
530	} else if (window.Node && options.bgnodes instanceof window.Node) {
531		bgnodes = [options.bgnodes];
532	}
533	for (i = 0, l = imageNodes.length; i < l; i++) images.push(imageNodes[i]);
534	
535	var holdercss = document.getElementById("holderjs-style");
536	if (!holdercss) {
537		holdercss = document.createElement("style");
538		holdercss.setAttribute("id", "holderjs-style");
539		holdercss.type = "text/css";
540		document.getElementsByTagName("head")[0].appendChild(holdercss);
541	}
542	
543	if (!options.nocss) {
544		if (holdercss.styleSheet) {
545			holdercss.styleSheet.cssText += options.stylesheet;
546		} else {
547			if(options.stylesheet.length){
548				holdercss.appendChild(document.createTextNode(options.stylesheet));
549			}
550		}
551	}
552	
553	var cssregex = new RegExp(options.domain + "\/(.*?)\"?\\)");
554	for (var l = bgnodes.length, i = 0; i < l; i++) {
555		var src = window.getComputedStyle(bgnodes[i], null)
556			.getPropertyValue("background-image");
557		var flags = src.match(cssregex);
558		var bgsrc = bgnodes[i].getAttribute("data-background-src");
559		if (flags) {
560			var holder = parse_flags(flags[1].split("/"), options);
561			if (holder) {
562				render("background", bgnodes[i], holder, src);
563			}
564		} else if (bgsrc != null) {
565			var holder = parse_flags(bgsrc.substr(bgsrc.lastIndexOf(options.domain) + options.domain.length + 1)
566				.split("/"), options);
567			if (holder) {
568				render("background", bgnodes[i], holder, src);
569			}
570		}
571	}
572	for (l = images.length, i = 0; i < l; i++) {
573		var attr_data_src, attr_src;
574		attr_src = attr_data_src = src = null;
575		try {
576			attr_src = images[i].getAttribute("src");
577			attr_datasrc = images[i].getAttribute("data-src");
578		} catch (e) {}
579		if (attr_datasrc == null && !! attr_src && attr_src.indexOf(options.domain) >= 0) {
580			src = attr_src;
581		} else if ( !! attr_datasrc && attr_datasrc.indexOf(options.domain) >= 0) {
582			src = attr_datasrc;
583		}
584		if (src) {
585			var holder = parse_flags(src.substr(src.lastIndexOf(options.domain) + options.domain.length + 1).split("/"), options);
586			if (holder) {
587				if (holder.fluid) {
588					render("fluid", images[i], holder, src)
589				} else {
590					render("image", images[i], holder, src);
591				}
592			}
593		}
594	}
595	return app;
596};
597
598contentLoaded(win, function () {
599	if (window.addEventListener) {
600		window.addEventListener("resize", resizable_update, false);
601		window.addEventListener("orientationchange", resizable_update, false);
602	} else {
603		window.attachEvent("onresize", resizable_update)
604	}
605	preempted || app.run({});
606
607	if (typeof window.Turbolinks === "object") {
608		document.addEventListener("page:change", function() { app.run({}) })
609	}
610});
611if (typeof define === "function" && define.amd) {
612	define([], function () {
613		return app;
614	});
615}
616
617//github.com/davidchambers/Base64.js
618(function(){function t(t){this.message=t}
618var e="undefined"!=typeof exports?exports:this,r="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";t.prototype=Error(),t.prototype.name="InvalidCharacterError",e.btoa||(e.btoa=function(e){for(var o,n,a=0,i=r,c="";e.charAt(0|a)||(i="=",a%1);c+=i.charAt(63&o>>8-8*(a%1))){if(n=e.charCodeAt(a+=.75),n>255)throw new t("'btoa' failed");o=o<<8|n}return c}),e.atob||(e.atob=function(e){if(e=e.replace(/=+$/,""),1==e.length%4)throw new t("'atob' failed");for(var o,n,a=0,i=0,c="";n=e.charAt(i++);~n&&(o=a%4?64*o+n:n,a++%4)?c+=String.fromCharCode(255&o>>(6&-2*a)):0)n=r.indexOf(n);return c})})();
619
620//getElementsByClassName polyfill
621document.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})
622
623//getComputedStyle polyfill
624window.getComputedStyle||(window.getComputedStyle=function(e){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})
625
626//http://javascript.nwbox.com/ContentLoaded by Diego Perini with modifications
627function 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)}}
628
629//https://gist.github.com/991057 by Jed Schmidt with modifications
630function selector(a,b){var a=a.match(/^(\W)?(.*)/),b=b||document,c=b["getElement"+(a[1]?"#"==a[1]?"ById":"sByClassName":"sByTagName")],d=c.call(b,a[2]),e=[];return null!==d&&(e=d.length||0===d.length?d:[d]),e}
631
632//shallow object property extend
633function extend(a,b){
634	var c={};
635	for(var i in a){
636		if(a.hasOwnProperty(i)){
637			c[i]=a[i];
638		}
639	}
640	for(var i in b){
641		if(b.hasOwnProperty(i)){
642			c[i]=b[i];
643		}
644	}
645	return c
646}
647
648//hasOwnProperty polyfill
649if (!Object.prototype.hasOwnProperty)
650    /*jshint -W001, -W103 */
651    Object.prototype.hasOwnProperty = function(prop) {
652		var proto = this.__proto__ || this.constructor.prototype;
653		return (prop in this) && (!(prop in proto) || proto[prop] !== this[prop]);
654	}
655    /*jshint +W001, +W103 */
656
657})(Holder, window);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.