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.

97 lines
3.7 KiB

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