mxImageBundle.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxImageBundle
  7. *
  8. * Maps from keys to base64 encoded images or file locations. All values must
  9. * be URLs or use the format data:image/format followed by a comma and the base64
  10. * encoded image data, eg. "data:image/gif,XYZ", where XYZ is the base64 encoded
  11. * image data.
  12. *
  13. * To add a new image bundle to an existing graph, the following code is used:
  14. *
  15. * (code)
  16. * var bundle = new mxImageBundle(alt);
  17. * bundle.putImage('myImage', 'data:image/gif,R0lGODlhEAAQAMIGAAAAAICAAICAgP' +
  18. * '//AOzp2O3r2////////yH+FUNyZWF0ZWQgd2l0aCBUaGUgR0lNUAAh+QQBCgAHACwAAAAA' +
  19. * 'EAAQAAADTXi63AowynnAMDfjPUDlnAAJhmeBFxAEloliKltWmiYCQvfVr6lBPB1ggxN1hi' +
  20. * 'laSSASFQpIV5HJBDyHpqK2ejVRm2AAgZCdmCGO9CIBADs=', fallback);
  21. * bundle.putImage('mySvgImage', 'data:image/svg+xml,' + encodeURIComponent(
  22. * '<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">' +
  23. * '<linearGradient id="gradient"><stop offset="10%" stop-color="#F00"/>' +
  24. * '<stop offset="90%" stop-color="#fcc"/></linearGradient>' +
  25. * '<rect fill="url(#gradient)" width="100%" height="100%"/></svg>'), fallback);
  26. * graph.addImageBundle(bundle);
  27. * (end);
  28. *
  29. * Alt is an optional boolean (default is false) that specifies if the value
  30. * or the fallback should be returned in <getImage>.
  31. *
  32. * The image can then be referenced in any cell style using image=myImage.
  33. * If you are using mxOutline, you should use the same image bundles in the
  34. * graph that renders the outline.
  35. *
  36. * The keys for images are resolved in <mxGraph.postProcessCellStyle> and
  37. * turned into a data URI if the returned value has a short data URI format
  38. * as specified above.
  39. *
  40. * A typical value for the fallback is a MTHML link as defined in RFC 2557.
  41. * Note that this format requires a file to be dynamically created on the
  42. * server-side, or the page that contains the graph to be modified to contain
  43. * the resources, this can be done by adding a comment that contains the
  44. * resource in the HEAD section of the page after the title tag.
  45. *
  46. * This type of fallback mechanism should be used in IE6 and IE7. IE8 does
  47. * support data URIs, but the maximum size is limited to 32 KB, which means
  48. * all data URIs should be limited to 32 KB.
  49. */
  50. function mxImageBundle(alt)
  51. {
  52. this.images = [];
  53. this.alt = (alt != null) ? alt : false;
  54. };
  55. /**
  56. * Variable: images
  57. *
  58. * Maps from keys to images.
  59. */
  60. mxImageBundle.prototype.images = null;
  61. /**
  62. * Variable: alt
  63. *
  64. * Specifies if the fallback representation should be returned.
  65. */
  66. mxImageBundle.prototype.alt = null;
  67. /**
  68. * Function: putImage
  69. *
  70. * Adds the specified entry to the map. The entry is an object with a value and
  71. * fallback property as specified in the arguments.
  72. */
  73. mxImageBundle.prototype.putImage = function(key, value, fallback)
  74. {
  75. this.images[key] = {value: value, fallback: fallback};
  76. };
  77. /**
  78. * Function: getImage
  79. *
  80. * Returns the value for the given key. This returns the value
  81. * or fallback, depending on <alt>. The fallback is returned if
  82. * <alt> is true, the value is returned otherwise.
  83. */
  84. mxImageBundle.prototype.getImage = function(key)
  85. {
  86. var result = null;
  87. if (key != null)
  88. {
  89. var img = this.images[key];
  90. if (img != null)
  91. {
  92. result = (this.alt) ? img.fallback : img.value;
  93. }
  94. }
  95. return result;
  96. };