From 2ca1972f01ae1bd5483aff66ac5dc3c5ca7ad733 Mon Sep 17 00:00:00 2001 From: Scott Fairgrieve Date: Fri, 29 Aug 2014 10:59:40 -0400 Subject: [PATCH 1/2] 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"; From edf199a8d3f803277d3d3c4b7c6c5c8e43df132e Mon Sep 17 00:00:00 2001 From: Scott Fairgrieve Date: Fri, 29 Aug 2014 11:06:00 -0400 Subject: [PATCH 2/2] Updated network.html documentation to include brokenImage property --- docs/network.html | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/network.html b/docs/network.html index 63a68dd8..e6791994 100644 --- a/docs/network.html +++ b/docs/network.html @@ -964,6 +964,12 @@ All options defined per-node override these global settings. undefined Default image url for the nodes. only applicable to shape image. + + brokenImage + String + undefined + Image url to use in the event that the url specified in the image property fails to load. only applicable to shape image. + mass number