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.

65 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. if (this.images[url] == undefined) {
  25. // create the image
  26. var me = this;
  27. var img = new Image();
  28. img.onload = function () {
  29. // IE11 fix -- thanks dponch!
  30. if (this.width == 0) {
  31. document.body.appendChild(this);
  32. this.width = this.offsetWidth;
  33. this.height = this.offsetHeight;
  34. document.body.removeChild(this);
  35. }
  36. if (me.callback) {
  37. me.images[url] = img;
  38. me.callback(this);
  39. }
  40. };
  41. img.onerror = function () {
  42. if (brokenUrl === undefined) {
  43. console.error("Could not load image:", url);
  44. delete this.src;
  45. if (me.callback) {
  46. me.callback(this);
  47. }
  48. }
  49. else {
  50. this.src = brokenUrl;
  51. }
  52. };
  53. img.src = url;
  54. }
  55. return img;
  56. };
  57. module.exports = Images;