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.

107 lines
3.8 KiB

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