vendor/assets/javascripts/holder.js in holder_rails-1.7.0 vs vendor/assets/javascripts/holder.js in holder_rails-1.9.0

- old
+ new

@@ -1,9 +1,9 @@ /* -Holder - 1.7 - client side image placeholders -(c) 2012 Ivan Malopinsky / http://imsky.co +Holder - 1.9 - client side image placeholders +(c) 2012-2013 Ivan Malopinsky / http://imsky.co Provided under the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0 Commercial use requires attribution. */ @@ -52,50 +52,54 @@ function draw(ctx, dimensions, template, ratio) { var ts = text_size(dimensions.width, dimensions.height, template); var text_height = ts.height; var width = dimensions.width * ratio, height = dimensions.height * ratio; + var font = template.font ? template.font : "sans-serif"; canvas.width = width; canvas.height = height; ctx.textAlign = "center"; ctx.textBaseline = "middle"; ctx.fillStyle = template.background; ctx.fillRect(0, 0, width, height); ctx.fillStyle = template.foreground; - ctx.font = "bold " + text_height + "px sans-serif"; + ctx.font = "bold " + text_height + "px "+font; var text = template.text ? template.text : (dimensions.width + "x" + dimensions.height); if (ctx.measureText(text).width / width > 1) { text_height = template.size / (ctx.measureText(text).width / width); } - ctx.font = "bold " + (text_height * ratio) + "px sans-serif"; + //Resetting font size if necessary + ctx.font = "bold " + (text_height * ratio) + "px "+font; ctx.fillText(text, (width / 2), (height / 2), width); return canvas.toDataURL("image/png"); } function render(mode, el, holder, src) { - var dimensions = holder.dimensions, theme = holder.theme, - text = holder.text; + text = holder.text ? decodeURIComponent(holder.text) : holder.text; var dimensions_caption = dimensions.width + "x" + dimensions.height; - theme = (text ? extend(theme, { - text: text - }) : theme); + theme = (text ? extend(theme, { text: text }) : theme); + theme = (holder.font ? extend(theme, {font: holder.font}) : theme); var ratio = 1; if(window.devicePixelRatio && window.devicePixelRatio > 1){ ratio = window.devicePixelRatio; } if (mode == "image") { el.setAttribute("data-src", src); el.setAttribute("alt", text ? text : theme.text ? theme.text + " [" + dimensions_caption + "]" : dimensions_caption); - el.style.width = dimensions.width + "px"; - el.style.height = dimensions.height + "px"; - + + if(fallback || !holder.auto){ + el.style.width = dimensions.width + "px"; + el.style.height = dimensions.height + "px"; + } + if (fallback) { el.style.backgroundColor = theme.background; + } else{ el.setAttribute("src", draw(ctx, dimensions, theme, ratio)); } } else { @@ -121,20 +125,34 @@ fluid.style.color = theme.foreground; fluid.className = el.className + " holderjs-fluid"; fluid.style.width = holder.dimensions.width + (holder.dimensions.width.indexOf("%")>0?"":"px"); fluid.style.height = holder.dimensions.height + (holder.dimensions.height.indexOf("%")>0?"":"px"); fluid.id = el.id; - + + el.style.width=0; + el.style.height=0; + if (theme.text) { fluid.appendChild(document.createTextNode(theme.text)) } else { fluid.appendChild(document.createTextNode(dimensions_caption)) fluid_images.push(fluid); setTimeout(fluid_update, 0); } - el.parentNode.replaceChild(fluid, el); + el.parentNode.insertBefore(fluid, el.nextSibling) + + if(window.jQuery){ + jQuery(function($){ + $(el).on("load", function(){ + el.style.width = fluid.style.width; + el.style.height = fluid.style.height; + $(el).show(); + $(fluid).remove(); + }); + }) + } } function fluid_update() { for (i in fluid_images) { if(!fluid_images.hasOwnProperty(i)) continue; @@ -166,11 +184,16 @@ } else if (options.themes[flag]) { //If a theme is specified, it will override custom colors ret.theme = options.themes[flag]; } else if (app.flags.text.match(flag)) { ret.text = app.flags.text.output(flag); + } else if(app.flags.font.match(flag)){ + ret.font = app.flags.font.output(flag); } + else if(app.flags.auto.match(flag)){ + ret.auto = true; + } } return render ? ret : false; }; @@ -190,11 +213,11 @@ var fluid_images = []; var settings = { domain: "holder.js", images: "img", - elements: ".holderjs", + bgnodes: ".holderjs", themes: { "gray": { background: "#eee", foreground: "#aaa", size: 12 @@ -249,10 +272,19 @@ text: { regex: /text\:(.*)/, output: function (val) { return this.regex.exec(val)[1]; } + }, + font: { + regex: /font\:(.*)/, + output: function(val){ + return this.regex.exec(val)[1]; + } + }, + auto: { + regex: /^auto$/ } } for (var flag in app.flags) { if(!app.flags.hasOwnProperty(flag)) continue; @@ -277,32 +309,61 @@ } return app; }; app.run = function (o) { - var options = extend(settings, o), - images_nodes = selector(options.images), - elements = selector(options.elements), - preempted = true, - images = []; + var options = extend(settings, o), images = []; + + if(options.images instanceof window.NodeList){ + imageNodes = options.images; + } + else if(options.images instanceof window.Node){ + imageNodes = [options.images]; + } + else{ + imageNodes = selector(options.images); + } + + if(options.elements instanceof window.NodeList){ + bgnodes = options.bgnodes; + } + else if(options.bgnodes instanceof window.Node){ + bgnodes = [options.bgnodes]; + } + else{ + bgnodes = selector(options.bgnodes); + } + + preempted = true; + + for (i = 0, l = imageNodes.length; i < l; i++) images.push(imageNodes[i]); + + var holdercss = document.getElementById("holderjs-style"); + + if(!holdercss){ + holdercss = document.createElement("style"); + holdercss.setAttribute("id", "holderjs-style"); + holdercss.type = "text/css"; + document.getElementsByTagName("head")[0].appendChild(holdercss); + } - for (i = 0, l = images_nodes.length; i < l; i++) images.push(images_nodes[i]); - - var holdercss = document.createElement("style"); - holdercss.type = "text/css"; - holdercss.styleSheet ? holdercss.styleSheet.cssText = options.stylesheet : holdercss.textContent = options.stylesheet; - document.getElementsByTagName("head")[0].appendChild(holdercss); - + if(holdercss.styleSheet){ + holdercss.styleSheet += options.stylesheet; + } + else{ + holdercss.textContent+= options.stylesheet; + } + var cssregex = new RegExp(options.domain + "\/(.*?)\"?\\)"); - for (var l = elements.length, i = 0; i < l; i++) { - var src = window.getComputedStyle(elements[i], null) + for (var l = bgnodes.length, i = 0; i < l; i++) { + var src = window.getComputedStyle(bgnodes[i], null) .getPropertyValue("background-image"); var flags = src.match(cssregex); if (flags) { var holder = parse_flags(flags[1].split("/"), options); if (holder) { - render("background", elements[i], holder, src); + render("background", bgnodes[i], holder, src); } } } for (var l = images.length, i = 0; i < l; i++) {