Widget:UPDateOnline: Difference between revisions
Appearance
Created page with "<includeonly> <div id="update-online-news" style=" margin: 20px 0; padding: 25px; border: 1px solid #ddd; border-radius: 8px; background: #fff; box-shadow: 0 2px 8px rgba(0,0,0,0.08); "> <!-- Heading --> <div style=" font-family:'Playfair Display',Georgia,serif; font-size:180%; font-weight:bold; margin-bottom:15px; "> UPDate Online </div> <!-- Loading message --> <div id="update-onl..." |
No edit summary |
||
| (9 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
<includeonly> | <includeonly> | ||
<div class="update-news"> | |||
<div class="update-online-loading"> | |||
<div | |||
Loading latest news... | Loading latest news... | ||
</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 () { | ||
/* | |||
* -------------------------------- | |||
* 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 = | var api = | ||
'https://upd.edu.ph/wp-json/wp/v2/posts?categories=4&per_page=1'; | 'https://upd.edu.ph/wp-json/wp/v2/posts' + | ||
'?categories=4&per_page=1'; | |||
/* | |||
* -------------------------------- | |||
* Retrieve latest article | |||
* -------------------------------- | |||
*/ | |||
fetch(api) | fetch(api) | ||
.then(function(response) { | .then(function (response) { | ||
if (!response.ok) { | if (!response.ok) { | ||
| Line 103: | Line 128: | ||
return response.json(); | return response.json(); | ||
}) | }) | ||
.then(function(posts) { | |||
.then(function (posts) { | |||
if (!posts || posts.length === 0) { | if (!posts || posts.length === 0) { | ||
throw new Error( | throw new Error( | ||
'No UPDate Online | 'No UPDate Online article found' | ||
); | ); | ||
} | } | ||
var post = posts[0]; | |||
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 | |||
* -------------------------------- | |||
*/ | */ | ||
document. | |||
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 | |||
* -------------------------------- | |||
*/ | |||
console.error(error); | .catch(function (error) { | ||
console.error( | |||
'UPDate Online Widget:', | |||
error | |||
); | |||
loading.textContent = | |||
'Unable to load the latest UPDate Online article.'; | |||
}); | }); | ||
| Line 184: | Line 378: | ||
})(); | })(); | ||
</script> | </script> | ||
</includeonly> | </includeonly> | ||
<noinclude> | <noinclude> | ||
== UPDate Online Widget == | == UPDate Online Widget == | ||
Displays the latest | Displays the latest UPDate Online article | ||
and the first image appearing after the | |||
opening paragraph. | |||
=== Usage === | === Usage === | ||
| Line 196: | Line 395: | ||
{{#widget:UPDateOnline}} | {{#widget:UPDateOnline}} | ||
</pre> | </pre> | ||
</noinclude> | </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}}