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.

72 lines
1.7 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, id) {
  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[id] && me.imageBroken[id][url] === true) {
  45. console.error("Could not load brokenImage:", brokenUrl);
  46. delete this.src;
  47. if (me.callback) {
  48. me.callback(this);
  49. }
  50. }
  51. else {
  52. console.error("Could not load image:", url);
  53. this.src = brokenUrl;
  54. if (me.imageBroken[id] === undefined) {
  55. me.imageBroken[id] = {};
  56. }
  57. me.imageBroken[id][url] = true;
  58. }
  59. }
  60. };
  61. img.src = url;
  62. }
  63. return img;
  64. };
  65. module.exports = Images;