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.

66 lines
1.4 KiB

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