1/* 2 * The contents of this file are subject to the license and copyright 3 * detailed in the LICENSE and NOTICE files at the root of the source 4 * tree and available online at 5 * 6 * http://www.dspace.org/license/ 7 */ 8/* 9 10Holder - 2.0 - client side image placeholders 11(c) 2012-2013 Ivan Malopinsky / http://imsky.co 12 13Provided under the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0 14Commercial use requires attribution. 15 16*/ 17 18var Holder = Holder || {}; 19(function (app, win) { 20 21var preempted = false, 22fallback = false, 23canvas = document.createElement('canvas'); 24 25//getElementsByClassName polyfill 26document.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}) 27 28//getComputedStyle polyfill 29window.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}) 30 31//http://javascript.nwbox.com/ContentLoaded by Diego Perini with modifications 32function 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)}}; 33 34//https://gist.github.com/991057 by Jed Schmidt with modifications 35function selector(a){ 36 a=a.match(/^(\W)?(.*)/);var b=document["getElement"+(a[1]?a[1]=="#"?"ById":"sByClassName":"sByTagName")](a[2]); 37 var ret=[]; b!=null&&(b.length?ret=b:b.length==0?ret=b:ret=[b]); return ret; 38} 39 40//shallow object property extend 41function 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} 42 43//hasOwnProperty polyfill 44if (!Object.prototype.hasOwnProperty) 45 Object.prototype.hasOwnProperty = function(prop) { 46 var proto = this.__proto__ || this.constructor.prototype; 47 return (prop in this) && (!(prop in proto) || proto[prop] !== this[prop]); 48 } 49 50function text_size(width, height, template) { 51 height = parseInt(height,10); 52 width = parseInt(width,10); 53 var bigSide = Math.max(height, width) 54 var smallSide = Math.min(height, width) 55 var scale = 1 / 12; 56 var newHeight = Math.min(smallSide * 0.75, 0.75 * bigSide * scale); 57 return { 58 height: Math.round(Math.max(template.size, newHeight)) 59 } 60} 61 62function draw(ctx, dimensions, template, ratio) { 63 var ts = text_size(dimensions.width, dimensions.height, template); 64 var text_height = ts.height; 65 var width = dimensions.width * ratio, 66 height = dimensions.height * ratio; 67 var font = template.font ? template.font : "sans-serif"; 68 canvas.width = width; 69 canvas.height = height; 70 ctx.textAlign = "center"; 71 ctx.textBaseline = "middle"; 72 ctx.fillStyle = template.background; 73 ctx.fillRect(0, 0, width, height); 74 ctx.fillStyle = template.foreground; 75 ctx.font = "bold " + text_height + "px " + font; 76 var text = template.text ? template.text : (Math.floor(dimensions.width) + "x" + Math.floor(dimensions.height)); 77 var text_width = ctx.measureText(text).width; 78 if (text_width / width >= 0.75) { 79 text_height = Math.floor(text_height * 0.75 * (width/text_width)); 80 } 81 //Resetting font size if necessary 82 ctx.font = "bold " + (text_height * ratio) + "px " + font; 83 ctx.fillText(text, (width / 2), (height / 2), width); 84 return canvas.toDataURL("image/png"); 85} 86 87function render(mode, el, holder, src) { 88 var dimensions = holder.dimensions, 89 theme = holder.theme, 90 text = holder.text ? decodeURIComponent(holder.text) : holder.text; 91 var dimensions_caption = dimensions.width + "x" + dimensions.height; 92 theme = (text ? extend(theme, { 93 text: text 94 }) : theme); 95 theme = (holder.font ? extend(theme, { 96 font: holder.font 97 }) : theme); 98 if (mode == "image") { 99 el.setAttribute("data-src", src); 100 el.setAttribute("alt", text ? text : theme.text ? theme.text + " [" + dimensions_caption + "]" : dimensions_caption); 101 if (fallback || !holder.auto) { 102 el.style.width = dimensions.width + "px"; 103 el.style.height = dimensions.height + "px"; 104 } 105 if (fallback) { 106 el.style.backgroundColor = theme.background; 107 } else { 108 el.setAttribute("src", draw(ctx, dimensions, theme, ratio)); 109 } 110 } else if (mode == "background") { 111 if (!fallback) { 112 el.style.backgroundImage = "url(" + draw(ctx, dimensions, theme, ratio) + ")"; 113 el.style.backgroundSize = dimensions.width + "px " + dimensions.height + "px"; 114 } 115 } else if (mode == "fluid") { 116 el.setAttribute("data-src", src); 117 el.setAttribute("alt", text ? text : theme.text ? theme.text + " [" + dimensions_caption + "]" : dimensions_caption); 118 if (dimensions.height.substr(-1) == "%") { 119 el.style.height = dimensions.height 120 } else { 121 el.style.height = dimensions.height + "px" 122 } 123 if (dimensions.width.substr(-1) == "%") { 124 el.style.width = dimensions.width 125 } else { 126 el.style.width = dimensions.width + "px" 127 } 128 if (el.style.display == "inline" || el.style.display == "") { 129 el.style.display = "block"; 130 } 131 if (fallback) { 132 el.style.backgroundColor = theme.background; 133 } else { 134 el.holderData = holder; 135 fluid_images.push(el); 136 fluid_update(el); 137 } 138 } 139}; 140 141function fluid_update(element) { 142 var images; 143 if (element.nodeType == null) { 144 images = fluid_images; 145 } else { 146 images = [element] 147 } 148 for (i in images) { 149 var el = images[i] 150 if (el.holderData) { 151 var holder = el.holderData; 152 el.setAttribute("src", draw(ctx, { 153 height: el.clientHeight, 154 width: el.clientWidth 155 }, holder.theme, ratio)); 156 } 157 } 158} 159 160function parse_flags(flags, options) { 161 162 var ret = { 163 theme: settings.themes.gray 164 }, render = false;
165 166 for (sl = flags.length, j = 0; j < sl; j++) { 167 var flag = flags[j]; 168 if (app.flags.dimensions.match(flag)) { 169 render = true; 170 ret.dimensions = app.flags.dimensions.output(flag); 171 } else if (app.flags.fluid.match(flag)) { 172 render = true; 173 ret.dimensions = app.flags.fluid.output(flag); 174 ret.fluid = true; 175 } else if (app.flags.colors.match(flag)) { 176 ret.theme = app.flags.colors.output(flag); 177 } else if (options.themes[flag]) { 178 //If a theme is specified, it will override custom colors 179 ret.theme = options.themes[flag]; 180 } else if (app.flags.text.match(flag)) { 181 ret.text = app.flags.text.output(flag); 182 } else if (app.flags.font.match(flag)) { 183 ret.font = app.flags.font.output(flag); 184 } else if (app.flags.auto.match(flag)) { 185 ret.auto = true; 186 } 187 } 188 189 return render ? ret : false; 190 191}; 192 193 194 195if (!canvas.getContext) { 196 fallback = true; 197} else { 198 if (canvas.toDataURL("image/png") 199 .indexOf("data:image/png") < 0) { 200 //Android doesn't support data URI 201 fallback = true; 202 } else { 203 var ctx = canvas.getContext("2d"); 204 } 205} 206 207var dpr = 1, bsr = 1; 208 209if(!fallback){ 210 dpr = window.devicePixelRatio || 1, 211 bsr = ctx.webkitBackingStorePixelRatio || ctx.mozBackingStorePixelRatio || ctx.msBackingStorePixelRatio || ctx.oBackingStorePixelRatio || ctx.backingStorePixelRatio || 1; 212} 213 214var ratio = dpr / bsr; 215 216var fluid_images = []; 217 218var settings = { 219 domain: "holder.js", 220 images: "img", 221 bgnodes: ".holderjs", 222 themes: { 223 "gray": { 224 background: "#eee", 225 foreground: "#aaa", 226 size: 12 227 }, 228 "social": { 229 background: "#3a5a97", 230 foreground: "#fff", 231 size: 12 232 }, 233 "industrial": { 234 background: "#434A52", 235 foreground: "#C2F200", 236 size: 12 237 } 238 }, 239 stylesheet: ".holderjs-fluid {font-size:16px;font-weight:bold;text-align:center;font-family:sans-serif;margin:0}" 240}; 241 242 243app.flags = { 244 dimensions: { 245 regex: /^(\d+)x(\d+)$/, 246 output: function (val) { 247 var exec = this.regex.exec(val); 248 return { 249 width: +exec[1], 250 height: +exec[2] 251 } 252 } 253 }, 254 fluid: { 255 regex: /^([0-9%]+)x([0-9%]+)$/, 256 output: function (val) { 257 var exec = this.regex.exec(val); 258 return { 259 width: exec[1], 260 height: exec[2] 261 } 262 } 263 }, 264 colors: { 265 regex: /#([0-9a-f]{3,})\:#([0-9a-f]{3,})/i, 266 output: function (val) { 267 var exec = this.regex.exec(val); 268 return { 269 size: settings.themes.gray.size, 270 foreground: "#" + exec[2], 271 background: "#" + exec[1] 272 } 273 } 274 }, 275 text: { 276 regex: /text\:(.*)/, 277 output: function (val) { 278 return this.regex.exec(val)[1]; 279 } 280 }, 281 font: { 282 regex: /font\:(.*)/, 283 output: function (val) { 284 return this.regex.exec(val)[1]; 285 } 286 }, 287 auto: { 288 regex: /^auto$/ 289 } 290} 291 292for (var flag in app.flags) { 293 if (!app.flags.hasOwnProperty(flag)) continue; 294 app.flags[flag].match = function (val) { 295 return val.match(this.regex) 296 } 297} 298 299app.add_theme = function (name, theme) { 300 name != null && theme != null && (settings.themes[name] = theme); 301 return app; 302}; 303 304app.add_image = function (src, el) { 305 var node = selector(el); 306 if (node.length) { 307 for (var i = 0, l = node.length; i < l; i++) { 308 var img = document.createElement("img") 309 img.setAttribute("data-src", src); 310 node[i].appendChild(img); 311 } 312 } 313 return app; 314}; 315 316app.run = function (o) { 317 var options = extend(settings, o), 318 images = [], imageNodes = [], bgnodes = []; 319 320 if(typeof(options.images) == "string"){ 321 imageNodes = selector(options.images); 322 } 323 else if (window.NodeList && options.images instanceof window.NodeList) { 324 imageNodes = options.images; 325 } else if (window.Node && options.images instanceof window.Node) { 326 imageNodes = [options.images]; 327 } 328 329 if(typeof(options.bgnodes) == "string"){ 330 bgnodes = selector(options.bgnodes); 331 } else if (window.NodeList && options.elements instanceof window.NodeList) { 332 bgnodes = options.bgnodes; 333 } else if (window.Node && options.bgnodes instanceof window.Node) { 334 bgnodes = [options.bgnodes]; 335 } 336 337 preempted = true; 338 339 for (i = 0, l = imageNodes.length; i < l; i++) images.push(imageNodes[i]); 340 341 var holdercss = document.getElementById("holderjs-style"); 342 if (!holdercss) { 343 holdercss = document.createElement("style"); 344 holdercss.setAttribute("id", "holderjs-style"); 345 holdercss.type = "text/css"; 346 document.getElementsByTagName("head")[0].appendChild(holdercss); 347 } 348 349 if (!options.nocss) { 350 if (holdercss.styleSheet) { 351 holdercss.styleSheet.cssText += options.stylesheet; 352 } else { 353 holdercss.appendChild(document.createTextNode(options.stylesheet)); 354 } 355 } 356 357 var cssregex = new RegExp(options.domain + "\/(.*?)\"?\\)"); 358 359 for (var l = bgnodes.length, i = 0; i < l; i++) { 360 var src = window.getComputedStyle(bgnodes[i], null) 361 .getPropertyValue("background-image"); 362 var flags = src.match(cssregex); 363 var bgsrc = bgnodes[i].getAttribute("data-background-src"); 364 365 if (flags) { 366 var holder = parse_flags(flags[1].split("/"), options); 367 if (holder) { 368 render("background", bgnodes[i], holder, src); 369 } 370 } 371 else if(bgsrc != null){ 372 var holder = parse_flags(bgsrc.substr(bgsrc.lastIndexOf(options.domain) + options.domain.length + 1) 373 .split("/"), options); 374 if(holder){ 375 render("background", bgnodes[i], holder, src); 376 } 377 } 378 } 379 380 for (l = images.length, i = 0; i < l; i++) { 381 382 var attr_src = attr_data_src = src = null; 383 384 try{ 385 attr_src = images[i].getAttribute("src"); 386 attr_datasrc = images[i].getAttribute("data-src"); 387 }catch(e){} 388 389 if (attr_datasrc == null && !! attr_src && attr_src.indexOf(options.domain) >= 0) { 390 src = attr_src; 391 } else if ( !! attr_datasrc && attr_datasrc.indexOf(options.domain) >= 0) { 392 src = attr_datasrc; 393 } 394 395 if (src) { 396 var holder = parse_flags(src.substr(src.lastIndexOf(options.domain) + options.domain.length + 1) 397 .split("/"), options); 398 if (holder) { 399 if (holder.fluid) { 400 render("fluid", images[i], holder, src) 401 } else { 402 render("image", images[i], holder, src); 403 } 404 } 405 } 406 } 407 return app; 408}; 409 410contentLoaded(win, function () { 411 if (window.addEventListener) { 412 window.addEventListener("resize", fluid_update, false); 413 window.addEventListener("orientationchange", fluid_update, false); 414 } else { 415 window.attachEvent("onresize", fluid_update) 416 } 417 preempted || app.run(); 418}); 419 420if (typeof define === "function" && define.amd) { 421 define("Holder", [], function () { 422 return app; 423 }); 424} 425 426})(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.