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.

91 lines
3.7 KiB

  1. /**
  2. * @class Images
  3. * This class loads images and keeps them stored.
  4. */
  5. class Images{
  6. constructor(callback){
  7. this.images = {};
  8. this.imageBroken = {};
  9. this.callback = callback;
  10. }
  11. /**
  12. * @param {string} url The Url to cache the image as
  13. * @return {Image} imageToLoadBrokenUrlOn The image object
  14. */
  15. _addImageToCache (url, imageToCache) {
  16. // IE11 fix -- thanks dponch!
  17. if (imageToCache.width === 0) {
  18. document.body.appendChild(imageToCache);
  19. imageToCache.width = imageToCache.offsetWidth;
  20. imageToCache.height = imageToCache.offsetHeight;
  21. document.body.removeChild(imageToCache);
  22. }
  23. this.images[url] = imageToCache;
  24. }
  25. /**
  26. * @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
  27. * @param {string} brokenUrl Url the broken image to try and load
  28. * @return {Image} imageToLoadBrokenUrlOn The image object
  29. */
  30. _tryloadBrokenUrl (url, brokenUrl, imageToLoadBrokenUrlOn) {
  31. //If any of the parameters aren't specified then exit the function because nothing constructive can be done
  32. if (url === undefined || brokenUrl === undefined || imageToLoadBrokenUrlOn === undefined) return;
  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.onerror = () => {
  35. console.error("Could not load brokenImage:", brokenUrl);
  36. //Add an empty image to the cache so that when subsequent load calls are made for the url we don't try load the image and broken image again
  37. this._addImageToCache(url, new Image());
  38. };
  39. //Set the source of the image to the brokenUrl, this is actually what kicks off the loading of the broken image
  40. imageToLoadBrokenUrlOn.src = brokenUrl;
  41. }
  42. /**
  43. * @return {Image} imageToRedrawWith The images that will be passed to the callback when it is invoked
  44. */
  45. _redrawWithImage (imageToRedrawWith) {
  46. if (this.callback) {
  47. this.callback(imageToRedrawWith);
  48. }
  49. }
  50. /**
  51. * @param {string} url Url of the image
  52. * @param {string} brokenUrl Url of an image to use if the url image is not found
  53. * @return {Image} img The image object
  54. */
  55. load (url, brokenUrl, id) {
  56. //Try and get the image from the cache, if successful then return the cached image
  57. var cachedImage = this.images[url];
  58. if (cachedImage) return cachedImage;
  59. //Create a new image
  60. var img = new Image();
  61. //Subscribe to the event that is raised if the image loads successfully
  62. img.onload = () => {
  63. //Add the image to the cache and then request a redraw
  64. this._addImageToCache(url, img);
  65. this._redrawWithImage(img);
  66. };
  67. //Subscribe to the event that is raised if the image fails to load
  68. img.onerror = () => {
  69. console.error("Could not load image:", url);
  70. //Try and load the image specified by the brokenUrl using
  71. this._tryloadBrokenUrl(url, brokenUrl, img);
  72. }
  73. //Set the source of the image to the url, this is actuall what kicks off the loading of the image
  74. img.src = url;
  75. //Return the new image
  76. return img;
  77. }
  78. }
  79. export default Images;