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.

105 lines
3.8 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 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. * @return {Image} imageToRedrawWith The images that will be passed to the callback when it is invoked
  34. */
  35. _redrawWithImage (imageToRedrawWith) {
  36. if (this.callback) {
  37. this.callback(imageToRedrawWith);
  38. }
  39. }
  40. /**
  41. * @param {string} url Url of the image
  42. * @param {string} brokenUrl Url of an image to use if the url image is not found
  43. * @return {Image} img The image object
  44. */
  45. load (url, brokenUrl, id) {
  46. //Try and get the image from the cache, if successful then return the cached image
  47. var cachedImage = this.images[url];
  48. if (cachedImage) return cachedImage;
  49. //Create a new image
  50. var img = new CachedImage();
  51. // Need to add to cache here, otherwise final return will spawn different copies of the same image,
  52. // Also, there will be multiple loads of the same image.
  53. this.images[url] = img;
  54. //Subscribe to the event that is raised if the image loads successfully
  55. img.image.onload = () => {
  56. // Properly init the cached item and then request a redraw
  57. this._fixImageCoordinates(img.image);
  58. img.init();
  59. this._redrawWithImage(img);
  60. };
  61. //Subscribe to the event that is raised if the image fails to load
  62. img.image.onerror = () => {
  63. console.error("Could not load image:", url);
  64. //Try and load the image specified by the brokenUrl using
  65. this._tryloadBrokenUrl(url, brokenUrl, img);
  66. }
  67. //Set the source of the image to the url, this is what actually kicks off the loading of the image
  68. img.image.src = url;
  69. //Return the new image
  70. return img;
  71. }
  72. /**
  73. * IE11 fix -- thanks dponch!
  74. *
  75. * Local helper function
  76. *
  77. * @private
  78. */
  79. _fixImageCoordinates(imageToCache) {
  80. if (imageToCache.width === 0) {
  81. document.body.appendChild(imageToCache);
  82. imageToCache.width = imageToCache.offsetWidth;
  83. imageToCache.height = imageToCache.offsetHeight;
  84. document.body.removeChild(imageToCache);
  85. }
  86. }
  87. }
  88. export default Images;