PageRenderTime 70ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/libs/holder/1.8/holder.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 362 lines | 299 code | 45 blank | 18 comment | 59 complexity | 67a79db482f2b6cb80e45e68f8212f21 MD5 | raw file
  1. /*
  2. Holder - 1.7 - client side image placeholders
  3. (c) 2012 Ivan Malopinsky / http://imsky.co
  4. Provided under the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
  5. Commercial use requires attribution.
  6. */
  7. var Holder = Holder || {};
  8. (function (app, win) {
  9. var preempted = false,
  10. fallback = false,
  11. canvas = document.createElement('canvas');
  12. //getElementsByClassName polyfill
  13. 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})
  14. //getComputedStyle polyfill
  15. 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})
  16. //http://javascript.nwbox.com/ContentLoaded by Diego Perini with modifications
  17. 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)}};
  18. //https://gist.github.com/991057 by Jed Schmidt with modifications
  19. function selector(a){
  20. a=a.match(/^(\W)?(.*)/);var b=document["getElement"+(a[1]?a[1]=="#"?"ById":"sByClassName":"sByTagName")](a[2]);
  21. var ret=[]; b!=null&&(b.length?ret=b:b.length==0?ret=b:ret=[b]); return ret;
  22. }
  23. //shallow object property extend
  24. 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}
  25. //hasOwnProperty polyfill
  26. if (!Object.prototype.hasOwnProperty)
  27. Object.prototype.hasOwnProperty = function(prop) {
  28. var proto = this.__proto__ || this.constructor.prototype;
  29. return (prop in this) && (!(prop in proto) || proto[prop] !== this[prop]);
  30. }
  31. function text_size(width, height, template) {
  32. var dimension_arr = [height, width].sort();
  33. var maxFactor = Math.round(dimension_arr[1] / 16),
  34. minFactor = Math.round(dimension_arr[0] / 16);
  35. var text_height = Math.max(template.size, maxFactor);
  36. return {
  37. height: text_height
  38. }
  39. }
  40. function draw(ctx, dimensions, template, ratio) {
  41. var ts = text_size(dimensions.width, dimensions.height, template);
  42. var text_height = ts.height;
  43. var width = dimensions.width * ratio, height = dimensions.height * ratio;
  44. var font = template.font ? template.font : "sans-serif";
  45. canvas.width = width;
  46. canvas.height = height;
  47. ctx.textAlign = "center";
  48. ctx.textBaseline = "middle";
  49. ctx.fillStyle = template.background;
  50. ctx.fillRect(0, 0, width, height);
  51. ctx.fillStyle = template.foreground;
  52. ctx.font = "bold " + text_height + "px "+font;
  53. var text = template.text ? template.text : (dimensions.width + "x" + dimensions.height);
  54. if (ctx.measureText(text).width / width > 1) {
  55. text_height = template.size / (ctx.measureText(text).width / width);
  56. }
  57. //Resetting font size if necessary
  58. ctx.font = "bold " + (text_height * ratio) + "px "+font;
  59. ctx.fillText(text, (width / 2), (height / 2), width);
  60. return canvas.toDataURL("image/png");
  61. }
  62. function render(mode, el, holder, src) {
  63. var dimensions = holder.dimensions,
  64. theme = holder.theme,
  65. text = holder.text ? decodeURIComponent(holder.text) : holder.text;
  66. var dimensions_caption = dimensions.width + "x" + dimensions.height;
  67. theme = (text ? extend(theme, { text: text }) : theme);
  68. theme = (holder.font ? extend(theme, {font: holder.font}) : theme);
  69. var ratio = 1;
  70. if(window.devicePixelRatio && window.devicePixelRatio > 1){
  71. ratio = window.devicePixelRatio;
  72. }
  73. if (mode == "image") {
  74. el.setAttribute("data-src", src);
  75. el.setAttribute("alt", text ? text : theme.text ? theme.text + " [" + dimensions_caption + "]" : dimensions_caption);
  76. el.style.width = dimensions.width + "px";
  77. el.style.height = dimensions.height + "px";
  78. if (fallback) {
  79. el.style.backgroundColor = theme.background;
  80. }
  81. else{
  82. el.setAttribute("src", draw(ctx, dimensions, theme, ratio));
  83. }
  84. } else {
  85. if (!fallback) {
  86. el.style.backgroundImage = "url(" + draw(ctx, dimensions, theme, ratio) + ")";
  87. el.style.backgroundSize = dimensions.width+"px "+dimensions.height+"px";
  88. }
  89. }
  90. };
  91. function fluid(el, holder, src) {
  92. var dimensions = holder.dimensions,
  93. theme = holder.theme,
  94. text = holder.text;
  95. var dimensions_caption = dimensions.width + "x" + dimensions.height;
  96. theme = (text ? extend(theme, {
  97. text: text
  98. }) : theme);
  99. var fluid = document.createElement("div");
  100. fluid.style.backgroundColor = theme.background;
  101. fluid.style.color = theme.foreground;
  102. fluid.className = el.className + " holderjs-fluid";
  103. fluid.style.width = holder.dimensions.width + (holder.dimensions.width.indexOf("%")>0?"":"px");
  104. fluid.style.height = holder.dimensions.height + (holder.dimensions.height.indexOf("%")>0?"":"px");
  105. fluid.id = el.id;
  106. el.style.width=0;
  107. el.style.height=0;
  108. if (theme.text) {
  109. fluid.appendChild(document.createTextNode(theme.text))
  110. } else {
  111. fluid.appendChild(document.createTextNode(dimensions_caption))
  112. fluid_images.push(fluid);
  113. setTimeout(fluid_update, 0);
  114. }
  115. el.parentNode.insertBefore(fluid, el.nextSibling)
  116. if(jQuery){
  117. jQuery(function($){
  118. $(el).on("load", function(){
  119. el.style.width = fluid.style.width;
  120. el.style.height = fluid.style.height;
  121. $(el).show();
  122. $(fluid).remove();
  123. });
  124. })
  125. }
  126. }
  127. function fluid_update() {
  128. for (i in fluid_images) {
  129. if(!fluid_images.hasOwnProperty(i)) continue;
  130. var el = fluid_images[i],
  131. label = el.firstChild;
  132. el.style.lineHeight = el.offsetHeight+"px";
  133. label.data = el.offsetWidth + "x" + el.offsetHeight;
  134. }
  135. }
  136. function parse_flags(flags, options) {
  137. var ret = {
  138. theme: settings.themes.gray
  139. }, render = false;
  140. for (sl = flags.length, j = 0; j < sl; j++) {
  141. var flag = flags[j];
  142. if (app.flags.dimensions.match(flag)) {
  143. render = true;
  144. ret.dimensions = app.flags.dimensions.output(flag);
  145. } else if (app.flags.fluid.match(flag)) {
  146. render = true;
  147. ret.dimensions = app.flags.fluid.output(flag);
  148. ret.fluid = true;
  149. } else if (app.flags.colors.match(flag)) {
  150. ret.theme = app.flags.colors.output(flag);
  151. } else if (options.themes[flag]) {
  152. //If a theme is specified, it will override custom colors
  153. ret.theme = options.themes[flag];
  154. } else if (app.flags.text.match(flag)) {
  155. ret.text = app.flags.text.output(flag);
  156. } else if(app.flags.font.match(flag)){
  157. ret.font = app.flags.font.output(flag);
  158. }
  159. }
  160. return render ? ret : false;
  161. };
  162. if (!canvas.getContext) {
  163. fallback = true;
  164. } else {
  165. if (canvas.toDataURL("image/png")
  166. .indexOf("data:image/png") < 0) {
  167. //Android doesn't support data URI
  168. fallback = true;
  169. } else {
  170. var ctx = canvas.getContext("2d");
  171. }
  172. }
  173. var fluid_images = [];
  174. var settings = {
  175. domain: "holder.js",
  176. images: "img",
  177. elements: ".holderjs",
  178. themes: {
  179. "gray": {
  180. background: "#eee",
  181. foreground: "#aaa",
  182. size: 12
  183. },
  184. "social": {
  185. background: "#3a5a97",
  186. foreground: "#fff",
  187. size: 12
  188. },
  189. "industrial": {
  190. background: "#434A52",
  191. foreground: "#C2F200",
  192. size: 12
  193. }
  194. },
  195. stylesheet: ".holderjs-fluid {font-size:16px;font-weight:bold;text-align:center;font-family:sans-serif;margin:0}"
  196. };
  197. app.flags = {
  198. dimensions: {
  199. regex: /^(\d+)x(\d+)$/,
  200. output: function (val) {
  201. var exec = this.regex.exec(val);
  202. return {
  203. width: +exec[1],
  204. height: +exec[2]
  205. }
  206. }
  207. },
  208. fluid: {
  209. regex: /^([0-9%]+)x([0-9%]+)$/,
  210. output: function (val) {
  211. var exec = this.regex.exec(val);
  212. return {
  213. width: exec[1],
  214. height: exec[2]
  215. }
  216. }
  217. },
  218. colors: {
  219. regex: /#([0-9a-f]{3,})\:#([0-9a-f]{3,})/i,
  220. output: function (val) {
  221. var exec = this.regex.exec(val);
  222. return {
  223. size: settings.themes.gray.size,
  224. foreground: "#" + exec[2],
  225. background: "#" + exec[1]
  226. }
  227. }
  228. },
  229. text: {
  230. regex: /text\:(.*)/,
  231. output: function (val) {
  232. return this.regex.exec(val)[1];
  233. }
  234. },
  235. font: {
  236. regex: /font\:(.*)/,
  237. output: function(val){
  238. return this.regex.exec(val)[1];
  239. }
  240. }
  241. }
  242. for (var flag in app.flags) {
  243. if(!app.flags.hasOwnProperty(flag)) continue;
  244. app.flags[flag].match = function (val) {
  245. return val.match(this.regex)
  246. }
  247. }
  248. app.add_theme = function (name, theme) {
  249. name != null && theme != null && (settings.themes[name] = theme);
  250. return app;
  251. };
  252. app.add_image = function (src, el) {
  253. var node = selector(el);
  254. if (node.length) {
  255. for (var i = 0, l = node.length; i < l; i++) {
  256. var img = document.createElement("img")
  257. img.setAttribute("data-src", src);
  258. node[i].appendChild(img);
  259. }
  260. }
  261. return app;
  262. };
  263. app.run = function (o) {
  264. var options = extend(settings, o),
  265. images_nodes = selector(options.images),
  266. elements = selector(options.elements),
  267. preempted = true,
  268. images = [];
  269. for (i = 0, l = images_nodes.length; i < l; i++) images.push(images_nodes[i]);
  270. var holdercss = document.createElement("style");
  271. holdercss.type = "text/css";
  272. holdercss.styleSheet ? holdercss.styleSheet.cssText = options.stylesheet : holdercss.textContent = options.stylesheet;
  273. document.getElementsByTagName("head")[0].appendChild(holdercss);
  274. var cssregex = new RegExp(options.domain + "\/(.*?)\"?\\)");
  275. for (var l = elements.length, i = 0; i < l; i++) {
  276. var src = window.getComputedStyle(elements[i], null)
  277. .getPropertyValue("background-image");
  278. var flags = src.match(cssregex);
  279. if (flags) {
  280. var holder = parse_flags(flags[1].split("/"), options);
  281. if (holder) {
  282. render("background", elements[i], holder, src);
  283. }
  284. }
  285. }
  286. for (var l = images.length, i = 0; i < l; i++) {
  287. var src = images[i].getAttribute("src") || images[i].getAttribute("data-src");
  288. if (src != null && src.indexOf(options.domain) >= 0) {
  289. var holder = parse_flags(src.substr(src.lastIndexOf(options.domain) + options.domain.length + 1)
  290. .split("/"), options);
  291. if (holder) {
  292. if (holder.fluid) {
  293. fluid(images[i], holder, src);
  294. } else {
  295. render("image", images[i], holder, src);
  296. }
  297. }
  298. }
  299. }
  300. return app;
  301. };
  302. contentLoaded(win, function () {
  303. if (window.addEventListener) {
  304. window.addEventListener("resize", fluid_update, false);
  305. window.addEventListener("orientationchange", fluid_update, false);
  306. } else {
  307. window.attachEvent("onresize", fluid_update)
  308. }
  309. preempted || app.run();
  310. });
  311. if ( typeof define === "function" && define.amd ) {
  312. define( "Holder", [], function () { return app; } );
  313. }
  314. })(Holder, window);