@saqimtiaz: Thank you for your suggestions. I have now implemented it as I outlined above, by “extending” the ImageWidget and adapting a wrapper macro that I already had in place.
Just in case someone else wants to do something similar, here is my approach…
I added these lines to $:/core/modules/widgets/image.js
:
domNode.addEventListener("error",function() {
$tw.utils.removeClass(domNode,"tc-image-loading");
$tw.utils.addClass(domNode,"tc-image-error");
},false);
$tw.utils.addEventListeners(domNode,[
{name: "error", handlerObject: this, handlerMethod: "handleErrorEvent"}
]);
// Insert element
and
this.imageAlt = this.getAttribute("alt");
this.errorActions = this.getAttribute("actions","");
};
(I showed the preceding and succeeding lines for reference).
Somewhere in the widget code goes this:
ImageWidget.prototype.handleErrorEvent = function(event) {
// Trigger actions
if(this.errorActions) {
this.invokeActionString(this.errorActions,this,event,{"source": this.imageSource});
}
};
The source
is passed to the error actions, just in case. I do not use it (yet).
The additional code was basically copy-pasted from the RadioWidget, with small adaptations. This is about my level of JavaScript expertise.
My wrapper macro for the ImageWidget looks like this:
\define error-actions()
<$action-setfield $tiddler="$:/temp/no-vpn-available" text="yes" />
\end
\define showImage(path, caption, width:800px)
<!-- picture with LAN address or from mapped drive -->
<$reveal type="nomatch" text="" default={{{ [{$:/temp/no-vpn-available}!match[yes]] [<__path__>search:title:regexp[^(?:\\\\|\w\:\\)]] +[nth[2]] }}}>
<$let formattedPath={{{ [<__path__>search-replace[\],[/]addprefix[file:///]] +[encodeuri[]] }}} >
<div class="image" style="max-width:$width$">
<$image source=<<formattedPath>> tooltip=<<__path__>> actions=<<error-actions>> /><br>
$caption$
</div>
</$let>
</$reveal>
<!-- picture from a subdirectory of the wiki -->
<$reveal type="match" text="" default={{{ [<__path__>search:title:regexp[^(?:\\\\|\w\:\\)]] }}}>
<$let formattedPath={{{ [<__path__>search-replace[\],[/]] +[encodeuri[]] }}} >
<div class="image" style="max-width:$width$">
<$image source=<<formattedPath>> tooltip=<<__path__>> /><br>
$caption$
</div>
</$let>
</$reveal>
<!-- LAN not reachable -->
<$reveal type="nomatch" text="" default={{{ [{$:/temp/no-vpn-available}match[yes]] [<__path__>search:title:regexp[^(?:\\\\|\w\:\\)]] +[nth[2]] }}}>
<$button class="tc-btn-invisible image mwi-broken-image" disabled="yes" tooltip={{{ [<__path__>addprefix[LAN resource not accessible: ]] }}}>
{{$:/mwi/images/broken-image}}
</$button>
</$reveal>
\end
This was one of my first TW macros and it shows. I don’t even have a check in there if a caption
is passed or not.
The neat thing is that it doesn’t generate a startup delay on each reload while it tries to contact the SMB server. With the first image that cannot be loaded, the flag is set so that no other images from the LAN are even tried. By placing it into the $:/temp
tree, a reload resets the flag. When I open the wiki on my other machine that has the LAN connection, pictures should be shown just fine.
Have a nice day
Yaisog