vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.9 KiB

  1. import CachedImage from './CachedImage';
  2. /**
  3. * This callback is a callback that accepts an Image.
  4. * @callback ImageCallback
  5. * @param {Image} image
  6. */
  7. /**
  8. * This class loads images and keeps them stored.
  9. *
  10. * @param {ImageCallback} callback
  11. */
  12. class Images {
  13. /**
  14. * @param {ImageCallback} callback
  15. */
  16. constructor(callback){
  17. this.images = {};
  18. this.imageBroken = {};
  19. this.callback = callback;
  20. }
  21. /**
  22. * @param {string} url The original Url that failed to load, if the broken image is successfully loaded it will be added to the cache using this Url as the key so that subsequent requests for this Url will return the broken image
  23. * @param {string} brokenUrl Url the broken image to try and load
  24. * @param {Image} imageToLoadBrokenUrlOn The image object
  25. */
  26. _tryloadBrokenUrl (url, brokenUrl, imageToLoadBrokenUrlOn) {
  27. //If these parameters aren't specified then exit the function because nothing constructive can be done
  28. if (url === undefined || imageToLoadBrokenUrlOn === undefined) return;
  29. if (brokenUrl === undefined) {
  30. console.warn("No broken url image defined");
  31. return;
  32. }
  33. //Clear the old subscription to the error event and put a new in place that only handle errors in loading the brokenImageUrl
  34. imageToLoadBrokenUrlOn.image.onerror = () => {
  35. console.error("Could not load brokenImage:", brokenUrl);
  36. // cache item will contain empty image, this should be OK for default
  37. };
  38. //Set the source of the image to the brokenUrl, this is actually what kicks off the loading of the broken image
  39. imageToLoadBrokenUrlOn.image.src = brokenUrl;
  40. }
  41. /**
  42. *
  43. * @param {vis.Image} imageToRedrawWith
  44. * @private
  45. */
  46. _redrawWithImage (imageToRedrawWith) {
  47. if (this.callback) {
  48. this.callback(imageToRedrawWith);
  49. }
  50. }
  51. /**
  52. * @param {string} url Url of the image
  53. * @param {string} brokenUrl Url of an image to use if the url image is not found
  54. * @return {Image} img The image object
  55. */
  56. load (url, brokenUrl) {
  57. //Try and get the image from the cache, if successful then return the cached image
  58. var cachedImage = this.images[url];
  59. if (cachedImage) return cachedImage;
  60. //Create a new image
  61. var img = new CachedImage();
  62. // Need to add to cache here, otherwise final return will spawn different copies of the same image,
  63. // Also, there will be multiple loads of the same image.
  64. this.images[url] = img;
  65. //Subscribe to the event that is raised if the image loads successfully
  66. img.image.onload = () => {
  67. // Properly init the cached item and then request a redraw
  68. this._fixImageCoordinates(img.image);
  69. img.init();
  70. this._redrawWithImage(img);
  71. };
  72. //Subscribe to the event that is raised if the image fails to load
  73. img.image.onerror = () => {
  74. console.error("Could not load image:", url);
  75. //Try and load the image specified by the brokenUrl using
  76. this._tryloadBrokenUrl(url, brokenUrl, img);
  77. };
  78. //Set the source of the image to the url, this is what actually kicks off the loading of the image
  79. img.image.src = url;
  80. //Return the new image
  81. return img;
  82. }
  83. /**
  84. * IE11 fix -- thanks dponch!
  85. *
  86. * Local helper function
  87. * @param {vis.Image} imageToCache
  88. * @private
  89. */
  90. _fixImageCoordinates(imageToCache) {
  91. if (imageToCache.width === 0) {
  92. document.body.appendChild(imageToCache);
  93. imageToCache.width = imageToCache.offsetWidth;
  94. imageToCache.height = imageToCache.offsetHeight;
  95. document.body.removeChild(imageToCache);
  96. }
  97. }
  98. }
  99. export default Images;