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.

83 lines
2.0 KiB

  1. /**
  2. * @class Images
  3. * This class loads images and keeps them stored.
  4. */
  5. function Images() {
  6. this.images = {};
  7. this.imageBroken = {};
  8. this.callback = undefined;
  9. }
  10. /**
  11. * Set an onload callback function. This will be called each time an image
  12. * is loaded
  13. * @param {function} callback
  14. */
  15. Images.prototype.setOnloadCallback = function(callback) {
  16. this.callback = callback;
  17. };
  18. /**
  19. *
  20. * @param {string} url Url of the image
  21. * @param {string} url Url of an image to use if the url image is not found
  22. * @return {Image} img The image object
  23. */
  24. Images.prototype.load = function(url, brokenUrl) {
  25. var img = this.images[url]; // make a pointer
  26. if (img === undefined) {
  27. // create the image
  28. var me = this;
  29. img = new Image();
  30. img.onload = function () {
  31. // IE11 fix -- thanks dponch!
  32. if (this.width == 0) {
  33. document.body.appendChild(this);
  34. this.width = this.offsetWidth;
  35. this.height = this.offsetHeight;
  36. document.body.removeChild(this);
  37. }
  38. if (me.callback) {
  39. me.images[url] = img;
  40. me.callback(this);
  41. }
  42. };
  43. img.onerror = function () {
  44. if (brokenUrl === undefined) {
  45. console.error("Could not load image:", url);
  46. delete this.src;
  47. if (me.callback) {
  48. me.callback(this);
  49. }
  50. }
  51. else {
  52. if (me.imageBroken[url] === true) {
  53. if (this.src == brokenUrl) {
  54. console.error("Could not load brokenImage:", brokenUrl);
  55. delete this.src;
  56. if (me.callback) {
  57. me.callback(this);
  58. }
  59. }
  60. else {
  61. console.error("Could not load image:", url);
  62. this.src = brokenUrl;
  63. }
  64. }
  65. else {
  66. console.error("Could not load image:", url);
  67. this.src = brokenUrl;
  68. me.imageBroken[url] = true;
  69. }
  70. }
  71. };
  72. img.src = url;
  73. }
  74. return img;
  75. };
  76. module.exports = Images;