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.

101 lines
3.7 KiB

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