From 2ca1972f01ae1bd5483aff66ac5dc3c5ca7ad733 Mon Sep 17 00:00:00 2001 From: Scott Fairgrieve Date: Fri, 29 Aug 2014 10:59:40 -0400 Subject: [PATCH] Update network/node image code to allow for specifying a broken image url to use when a node image fails to load --- .gitignore | 1 + lib/network/Images.js | 11 ++++++++++- lib/network/Node.js | 4 ++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 29c1ae2e..9471387e 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ node_modules .settings/org.eclipse.wst.jsdt.ui.superType.name npm-debug.log examples/graph/24_hierarchical_layout_userdefined2.html +.DS_Store diff --git a/lib/network/Images.js b/lib/network/Images.js index 266eb491..7d46eea5 100644 --- a/lib/network/Images.js +++ b/lib/network/Images.js @@ -20,9 +20,10 @@ Images.prototype.setOnloadCallback = function(callback) { /** * * @param {string} url Url of the image + * @param {string} url Url of an image to use if the url image is not found * @return {Image} img The image object */ -Images.prototype.load = function(url) { +Images.prototype.load = function(url, brokenUrl) { var img = this.images[url]; if (img == undefined) { // create the image @@ -34,6 +35,14 @@ Images.prototype.load = function(url) { images.callback(this); } }; + + img.onerror = function () { + this.src = brokenUrl; + if (images.callback) { + images.callback(this); + } + }; + img.src = url; } diff --git a/lib/network/Node.js b/lib/network/Node.js index cdaf0109..a9faec28 100644 --- a/lib/network/Node.js +++ b/lib/network/Node.js @@ -135,7 +135,7 @@ Node.prototype.setProperties = function(properties, constants) { return; } - var fields = ['borderWidth','borderWidthSelected','shape','image','radius','fontColor', + var fields = ['borderWidth','borderWidthSelected','shape','image','brokenImage','radius','fontColor', 'fontSize','fontFace','fontFill','group','mass' ]; util.selectiveDeepExtend(fields, this.options, properties); @@ -176,7 +176,7 @@ Node.prototype.setProperties = function(properties, constants) { if (this.options.image!== undefined && this.options.image!= "") { if (this.imagelist) { - this.imageObj = this.imagelist.load(this.options.image); + this.imageObj = this.imagelist.load(this.options.image, this.options.brokenImage); } else { throw "No imagelist provided";