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.7 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 if (me.imageBroken[url] === true) {
  52. console.error("Could not load brokenImage:", brokenUrl);
  53. delete this.src;
  54. if (me.callback) {
  55. me.callback(this);
  56. }
  57. }
  58. else {
  59. this.src = brokenUrl;
  60. me.imageBroken[url] = true;
  61. }
  62. };
  63. img.src = url;
  64. }
  65. return img;
  66. };
  67. module.exports = Images;