Widget:UPDateOnline: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
| (8 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
<includeonly> | <includeonly> | ||
<div | <div class="update-news"> | ||
<div class="update-online-loading"> | |||
Loading latest news... | Loading latest news... | ||
</div> | </div> | ||
<div | <div class="update-online-content" style="display:none;"> | ||
<div class="update-news-image"> | |||
<img class="update-online-image" | |||
src="" | |||
alt="" | |||
style="display:none;"> | |||
</div> | |||
<div class="update-news-content"> | |||
<div class="update-online-date"></div> | |||
<h2 class="update-online-title"></h2> | |||
<div class="update-online-excerpt"></div> | |||
<a class="update-online-link" | |||
href="" | |||
target="_blank" | |||
rel="noopener noreferrer"> | |||
Read more → | |||
</a> | |||
</ | |||
</div> | |||
</div> | </div> | ||
</div> | </div> | ||
<script> | <script> | ||
(function () { | (function () { | ||
/* | |||
'https://upd.edu.ph/wp-json/wp/v2/posts?categories=4&per_page=1' | * -------------------------------- | ||
) | * Find THIS widget instance | ||
* -------------------------------- | |||
*/ | |||
var widgets = | |||
document.querySelectorAll('.update-news'); | |||
var widget = | |||
widgets[widgets.length - 1]; | |||
if (!widget) { | |||
return; | |||
} | |||
/* | |||
* -------------------------------- | |||
* Find elements inside this widget | |||
* -------------------------------- | |||
*/ | |||
var loading = | |||
widget.querySelector( | |||
'.update-online-loading' | |||
); | |||
var content = | |||
widget.querySelector( | |||
'.update-online-content' | |||
); | |||
var image = | |||
widget.querySelector( | |||
'.update-online-image' | |||
); | |||
var title = | |||
widget.querySelector( | |||
'.update-online-title' | |||
); | |||
var excerpt = | |||
widget.querySelector( | |||
'.update-online-excerpt' | |||
); | |||
var link = | |||
widget.querySelector( | |||
'.update-online-link' | |||
); | |||
var dateElement = | |||
widget.querySelector( | |||
'.update-online-date' | |||
); | |||
/* | |||
* -------------------------------- | |||
* UPDate Online API | |||
* -------------------------------- | |||
*/ | |||
var api = | |||
'https://upd.edu.ph/wp-json/wp/v2/posts' + | |||
'?categories=4&per_page=1'; | |||
/* | |||
* -------------------------------- | |||
* Retrieve latest article | |||
* -------------------------------- | |||
*/ | |||
fetch(api) | |||
.then(function (response) { | |||
if (!response.ok) { | |||
throw new Error( | |||
'Unable to retrieve UPDate Online' | |||
); | |||
} | |||
return response.json(); | |||
}) | |||
.then(function (posts) { | |||
if (!posts || posts.length === 0) { | |||
throw new Error( | |||
'No UPDate Online article found' | |||
); | |||
} | |||
var post = | |||
posts[0]; | |||
/* | |||
* -------------------------------- | |||
* TITLE | |||
* -------------------------------- | |||
*/ | |||
title.innerHTML = | |||
post.title.rendered; | |||
/* | |||
* -------------------------------- | |||
* EXCERPT | |||
* -------------------------------- | |||
*/ | |||
excerpt.innerHTML = | |||
post.excerpt.rendered; | |||
/* | |||
* -------------------------------- | |||
* ARTICLE LINK | |||
* -------------------------------- | |||
*/ | |||
link.href = | |||
post.link; | |||
/* | |||
* -------------------------------- | |||
* DATE | |||
* -------------------------------- | |||
*/ | |||
var articleDate = | |||
new Date(post.date); | |||
dateElement.textContent = | |||
articleDate.toLocaleDateString( | |||
'en-US', | |||
{ | |||
year: 'numeric', | |||
month: 'long', | |||
day: 'numeric' | |||
} | |||
); | |||
/* | |||
* -------------------------------- | |||
* ARTICLE CONTENT | |||
* -------------------------------- | |||
*/ | |||
var article = | |||
document.createElement('div'); | |||
article.innerHTML = | |||
post.content.rendered; | |||
/* | |||
* -------------------------------- | |||
* FIND FIRST PARAGRAPH | |||
* -------------------------------- | |||
*/ | |||
var paragraphs = | |||
article.querySelectorAll('p'); | |||
var firstParagraph = | |||
null; | |||
for ( | |||
var i = 0; | |||
i < paragraphs.length; | |||
i++ | |||
) { | |||
if ( | |||
paragraphs[i] | |||
.textContent | |||
.trim() !== '' | |||
) { | |||
firstParagraph = | |||
paragraphs[i]; | |||
break; | |||
} | |||
} | |||
/* | |||
* -------------------------------- | |||
* FIND FIRST IMAGE AFTER | |||
* FIRST PARAGRAPH | |||
* -------------------------------- | |||
*/ | |||
var selectedImage = | |||
null; | |||
if (firstParagraph) { | |||
var images = | |||
article.querySelectorAll('img'); | |||
for ( | |||
var j = 0; | |||
j < images.length; | |||
j++ | |||
) { | |||
var position = | |||
firstParagraph.compareDocumentPosition( | |||
images[j] | |||
); | |||
if ( | |||
position & | |||
Node.DOCUMENT_POSITION_FOLLOWING | |||
) { | |||
selectedImage = | |||
images[j]; | |||
break; | |||
} | |||
} | |||
} | |||
/* | |||
* -------------------------------- | |||
* DISPLAY IMAGE | |||
* -------------------------------- | |||
*/ | |||
if (selectedImage) { | |||
var imageUrl = | |||
selectedImage.getAttribute( | |||
'src' | |||
); | |||
if (!imageUrl) { | |||
imageUrl = | |||
selectedImage.getAttribute( | |||
'data-src' | |||
); | |||
} | |||
if (!imageUrl) { | |||
imageUrl = | |||
selectedImage.getAttribute( | |||
'data-lazy-src' | |||
); | |||
} | |||
if (imageUrl) { | |||
image.src = | |||
imageUrl; | |||
image.alt = | |||
selectedImage.getAttribute( | |||
'alt' | |||
) || | |||
post.title.rendered; | |||
image.style.display = | |||
'block'; | |||
} | |||
} | |||
/* | |||
* -------------------------------- | |||
* SHOW CONTENT | |||
* -------------------------------- | |||
*/ | |||
loading.style.display = | |||
'none'; | |||
content.style.display = | |||
'block'; | |||
}) | |||
/* | |||
* -------------------------------- | |||
* ERROR HANDLING | |||
* -------------------------------- | |||
*/ | |||
.catch(function (error) { | |||
console.error( | |||
'UPDate Online Widget:', | |||
error | |||
); | |||
loading.textContent = | |||
'Unable to load the latest UPDate Online article.'; | |||
}); | |||
})(); | })(); | ||
</script> | </script> | ||
</includeonly> | </includeonly> | ||
<noinclude> | |||
== UPDate Online Widget == | |||
Displays the latest UPDate Online article | |||
and the first image appearing after the | |||
opening paragraph. | |||
=== Usage === | |||
<pre> | |||
{{#widget:UPDateOnline}} | |||
</pre> | |||
</noinclude> | |||
Latest revision as of 00:17, 7 October 2026
UPDate Online Widget
Displays the latest UPDate Online article and the first image appearing after the opening paragraph.
Usage
{{#widget:UPDateOnline}}