payment gateway for rpn cn
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

501 lignes
14KB

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