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.

74 lines
1.8 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. *
  12. * @param {string} url Url of the image
  13. * @param {string} url Url of an image to use if the url image is not found
  14. * @return {Image} img The image object
  15. */
  16. Images.prototype.load = function(url, brokenUrl) {
  17. var img = this.images[url]; // make a pointer
  18. if (img === undefined) {
  19. // create the image
  20. var me = this;
  21. img = new Image();
  22. img.onload = function () {
  23. // IE11 fix -- thanks dponch!
  24. if (this.width == 0) {
  25. document.body.appendChild(this);
  26. this.width = this.offsetWidth;
  27. this.height = this.offsetHeight;
  28. document.body.removeChild(this);
  29. }
  30. if (me.callback) {
  31. me.images[url] = img;
  32. me.callback(this);
  33. }
  34. };
  35. img.onerror = function () {
  36. if (brokenUrl === undefined) {
  37. console.error("Could not load image:", url);
  38. delete this.src;
  39. if (me.callback) {
  40. me.callback(this);
  41. }
  42. }
  43. else {
  44. if (me.imageBroken[url] === true) {
  45. if (this.src == brokenUrl) {
  46. console.error("Could not load brokenImage:", brokenUrl);
  47. delete this.src;
  48. if (me.callback) {
  49. me.callback(this);
  50. }
  51. }
  52. else {
  53. console.error("Could not load image:", url);
  54. this.src = brokenUrl;
  55. }
  56. }
  57. else {
  58. console.error("Could not load image:", url);
  59. this.src = brokenUrl;
  60. me.imageBroken[url] = true;
  61. }
  62. }
  63. };
  64. img.src = url;
  65. }
  66. return img;
  67. };
  68. module.exports = Images;