1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-08-08 14:55:17 +00:00

Extend image widget to support alternate content if a remote image fails to load

...and use it to add an image for bags/recipes that do not have a $:/favicon.ico
This commit is contained in:
Jeremy Ruston 2024-01-23 22:05:22 +00:00
parent 627c3e20cc
commit d16746ab38
4 changed files with 34 additions and 6 deletions

View File

@ -43,6 +43,7 @@ ImageWidget.prototype = new Widget();
Render this widget into the DOM Render this widget into the DOM
*/ */
ImageWidget.prototype.render = function(parent,nextSibling) { ImageWidget.prototype.render = function(parent,nextSibling) {
var self = this;
this.parentDomNode = parent; this.parentDomNode = parent;
this.computeAttributes(); this.computeAttributes();
this.execute(); this.execute();
@ -118,6 +119,14 @@ ImageWidget.prototype.render = function(parent,nextSibling) {
if(this.lazyLoading && tag === "img") { if(this.lazyLoading && tag === "img") {
domNode.setAttribute("loading",this.lazyLoading); domNode.setAttribute("loading",this.lazyLoading);
} }
// Insert element
parent.insertBefore(domNode,nextSibling);
this.domNodes.push(domNode);
// Render children into a wrapper
this.childWrapper = this.document.createElement("span");
this.childWrapper.style.display = "none" ;
parent.insertBefore(this.childWrapper,nextSibling);
this.renderChildren(this.childWrapper,null);
// Add classes when the image loads or fails // Add classes when the image loads or fails
$tw.utils.addClass(domNode,"tc-image-loading"); $tw.utils.addClass(domNode,"tc-image-loading");
domNode.addEventListener("load",function() { domNode.addEventListener("load",function() {
@ -127,10 +136,9 @@ ImageWidget.prototype.render = function(parent,nextSibling) {
domNode.addEventListener("error",function() { domNode.addEventListener("error",function() {
$tw.utils.removeClass(domNode,"tc-image-loading"); $tw.utils.removeClass(domNode,"tc-image-loading");
$tw.utils.addClass(domNode,"tc-image-error"); $tw.utils.addClass(domNode,"tc-image-error");
self.childWrapper.style.display = "inline" ;
domNode.style.display = "none";
},false); },false);
// Insert element
parent.insertBefore(domNode,nextSibling);
this.domNodes.push(domNode);
}; };
/* /*
@ -146,6 +154,8 @@ ImageWidget.prototype.execute = function() {
this.imageTooltip = this.getAttribute("tooltip"); this.imageTooltip = this.getAttribute("tooltip");
this.imageAlt = this.getAttribute("alt"); this.imageAlt = this.getAttribute("alt");
this.lazyLoading = this.getAttribute("loading"); this.lazyLoading = this.getAttribute("loading");
// Make the child widgets
this.makeChildWidgets();
}; };
/* /*

View File

@ -51,7 +51,15 @@ title: MultiWikiServer Administration
\whitespace trim \whitespace trim
<$genesis $type=<<element-tag>> class={{{ mws-bag-pill [<is-topmost>match[yes]then[mws-bag-pill-topmost]] +[join[ ]] }}}> <$genesis $type=<<element-tag>> class={{{ mws-bag-pill [<is-topmost>match[yes]then[mws-bag-pill-topmost]] +[join[ ]] }}}>
<a class="mws-bag-pill-link" href=`/wiki/${ [{!!bag-name}encodeuricomponent[]] }$/bags/${ [{!!bag-name}encodeuricomponent[]] }$` rel="noopener noreferrer" target="_blank"> <a class="mws-bag-pill-link" href=`/wiki/${ [{!!bag-name}encodeuricomponent[]] }$/bags/${ [{!!bag-name}encodeuricomponent[]] }$` rel="noopener noreferrer" target="_blank">
<$image source=`/wiki/${ [{!!bag-name}encodeuricomponent[]] }$/bags/${ [{!!bag-name}encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico` class="mws-favicon-small"/> <$image
source=`/wiki/${ [{!!bag-name}encodeuricomponent[]] }$/bags/${ [{!!bag-name}encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico`
class="mws-favicon-small"
>
<$image
source="$:/MultiWikiServer/images/missing-favicon.png"
class="mws-favicon-small"
/>
</$image>
<span class="mws-bag-pill-label"> <span class="mws-bag-pill-label">
<$text text={{!!bag-name}}/> <$text text={{!!bag-name}}/>
</span> </span>
@ -64,7 +72,15 @@ title: MultiWikiServer Administration
\whitespace trim \whitespace trim
<a class="mws-wiki-card" href=`/wiki/${ [{!!recipe-name}encodeuricomponent[]] }$` rel="noopener noreferrer" target="_blank"> <a class="mws-wiki-card" href=`/wiki/${ [{!!recipe-name}encodeuricomponent[]] }$` rel="noopener noreferrer" target="_blank">
<div class="mws-wiki-card-image"> <div class="mws-wiki-card-image">
<$image source=`/wiki/${ [{!!recipe-name}encodeuricomponent[]] }$/recipes/${ [{!!recipe-name}encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico` class="mws-favicon"/> <$image
source=`/wiki/${ [{!!recipe-name}encodeuricomponent[]] }$/recipes/${ [{!!recipe-name}encodeuricomponent[]] }$/tiddlers/%24%3A%2Ffavicon.ico`
class="mws-favicon"
>
<$image
source="$:/MultiWikiServer/images/missing-favicon.png"
class="mws-favicon"
/>
</$image>
</div> </div>
<div class="mws-wiki-card-content"> <div class="mws-wiki-card-content">
<div class="mws-wiki-card-header"> <div class="mws-wiki-card-header">

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

View File

@ -0,0 +1,2 @@
title: $:/MultiWikiServer/images/missing-favicon.png
type: image/png