diff --git a/js/solstice/eclipsefdn.newsroom-resources.js b/js/solstice/eclipsefdn.newsroom-resources.js new file mode 100644 index 0000000000000000000000000000000000000000..c4b190b8d4c78dc9ee5bfe5c1232fd471ea1d122 --- /dev/null +++ b/js/solstice/eclipsefdn.newsroom-resources.js @@ -0,0 +1,148 @@ +/*! + * Copyright (c) 2022 Eclipse Foundation, Inc. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * Contributors: + * Zhou Fang + * + * SPDX-License-Identifier: EPL-2.0 + */ + +import templateResourceList from './templates/newsroom-resources/resources-list.mustache'; +import templateResourceImage from './templates/newsroom-resources/resources-image.mustache'; +import templateResourceImageWithTitle from './templates/newsroom-resources/resources-image-with-title.mustache'; +import parse from 'parse-link-header'; +import { displayErrMsg } from '../utils/utils'; + +const eclipseFdnResources = (async () => { + const resourcesElements = document.querySelectorAll('.newsroom-resources'); + if (!resourcesElements || resourcesElements.length === 0) { + return; + } + + const resLink = function (linkType, item) { + let linkObj = { link: '', icon: '', type: '' }; + if (linkType === 'direct') { + return item.direct_link + ? (linkObj = { link: item.direct_link || '', icon: 'pdf', type: 'PDF' }) + : (linkObj = { link: item.landing_link || '', icon: 'text', type: 'HTML' }); + } + return item.landing_link + ? (linkObj = { link: item.landing_link || '', icon: 'text', type: 'HTML' }) + : (linkObj = { link: item.direct_link || '', icon: 'pdf', type: 'PDF' }); + }; + + resourcesElements.forEach(async (resourcesElement) => { + let hasError = false; + const titles = resourcesElement.getAttribute('data-res-title')?.replaceAll(', ', ',')?.split(','); + const resourceTypes = resourcesElement.getAttribute('data-res-type')?.replaceAll(', ', ',')?.split(','); + const viewMoreLinks = resourcesElement.getAttribute('data-res-view-more')?.replaceAll(', ', ',')?.split(','); + let resourcesData = []; + titles.forEach((title, index) => { + resourcesData.push({ + title, + resourceType: resourceTypes[index], + viewMoreLink: viewMoreLinks?.[index], + data: [], + isFetching: true, + }); + }); + + const formatDate = function () { + let theDate = new Date(this.date).toDateString(); + theDate = theDate.slice(0, 3) + ',' + theDate.slice(3); + return theDate; + }; + + const displayDataOnPage = (data) => { + switch (resourcesElement.getAttribute('data-res-template')) { + case 'list': + resourcesElement.innerHTML = templateResourceList({ + items: data, + resLink: function () { + return resLink(resourcesElement.getAttribute('data-res-link'), this); + }, + }); + break; + case 'image': + const skipItems = resourcesElement.getAttribute('data-res-skip'); + if (skipItems) { + data.forEach((item) => item.data.splice(0, skipItems)); + } + resourcesElement.innerHTML = templateResourceImage({ + items: data, + resClass: resourcesElement.getAttribute('data-res-class'), + resLink: function () { + return resLink(resourcesElement.getAttribute('data-res-link'), this); + }, + }); + break; + case 'image-with-title': + resourcesElement.innerHTML = templateResourceImageWithTitle({ + items: data, + resClass: resourcesElement.getAttribute('data-res-class'), + resLink: function () { + return resLink(resourcesElement.getAttribute('data-res-link'), this); + }, + formatDate, + }); + break; + default: + break; + } + }; + + const fetchWithPagination = async (resource, index) => { + let data = []; + const pagesizeAttribute = resourcesElement.getAttribute('data-res-limit'); + const pagesize = pagesizeAttribute || 100; + let url = `https://newsroom.eclipse.org/api/resources?pagesize=${pagesize}¶meters[publish_to]=${resourcesElement.getAttribute( + 'data-res-wg' + )}¶meters[resource_type][]=${resource.resourceType}&page=1`; + const getData = async () => { + try { + let response = await fetch(url); + if (response.ok) { + let newData = await response.json(); + data = [...data, ...newData.resources]; + const links = parse(response.headers.get('link')); + if (links?.next?.url && !pagesizeAttribute) { + url = links.next.url; + await getData(); + } + return; + } + hasError = true; + displayErrMsg(resourcesElement, response.status); + } catch (error) { + console.log(error); + hasError = true; + displayErrMsg(resourcesElement, error); + } + }; + + await getData(); + + if (hasError) { + return; + } + resourcesData[index] = { ...resourcesData[index], isFetching: false, data }; + }; + + resourcesElement.innerHTML = templateResourceList({ + items: resourcesData, + }); + + await Promise.all(resourcesData.map((item, index) => fetchWithPagination(item, index))); + + if (hasError) { + return; + } + displayDataOnPage(resourcesData); + }); +})(); + +export default eclipseFdnResources; diff --git a/js/solstice/index.js b/js/solstice/index.js index 7752d84c891f1e428beb7480bfe9a1277f2437d4..dc6158ae6045ebca627c8d68e4170e78f32ddaec 100644 --- a/js/solstice/index.js +++ b/js/solstice/index.js @@ -23,3 +23,4 @@ import './eclipsefdn.solstice-rss-blog-list'; import './eclipsefdn.solstice-slider'; import './eclipsefdn.time-conversion'; import './eclipsefdn.wgs-list.js' +import './eclipsefdn.newsroom-resources.js' diff --git a/js/solstice/templates/newsroom-resources/resources-image-with-title.mustache b/js/solstice/templates/newsroom-resources/resources-image-with-title.mustache new file mode 100644 index 0000000000000000000000000000000000000000..01cceb3a815707f7be88df0a8e9a3a6ddd67a4c3 --- /dev/null +++ b/js/solstice/templates/newsroom-resources/resources-image-with-title.mustache @@ -0,0 +1,30 @@ +{{#items}} +

{{title}}

+
+ {{#isFetching}} +
+ + Loading... +
+ {{/isFetching}} + {{^isFetching}} + {{#data}} +
+
+ + Cover of {{title}} + +

{{title}}

+

{{formatDate}}

+

+ {{resLink.type}} Link +

+
+
+ {{/data}} + {{/isFetching}} + {{#viewMoreLink}} +

View More

+ {{/viewMoreLink}} +
+{{/items}} diff --git a/js/solstice/templates/newsroom-resources/resources-image.mustache b/js/solstice/templates/newsroom-resources/resources-image.mustache new file mode 100644 index 0000000000000000000000000000000000000000..900c83bd504720d1af525a9b52056e9d8b40ff4a --- /dev/null +++ b/js/solstice/templates/newsroom-resources/resources-image.mustache @@ -0,0 +1,20 @@ +{{#items}} + {{#isFetching}} +
+ + Loading... +
+ {{/isFetching}} + {{^isFetching}} + {{#data}} +
+ + Cover of {{title}} + + + Download + +
+ {{/data}} + {{/isFetching}} +{{/items}} diff --git a/js/solstice/templates/newsroom-resources/resources-list.mustache b/js/solstice/templates/newsroom-resources/resources-list.mustache new file mode 100644 index 0000000000000000000000000000000000000000..75f2a126725b6cf689124eee7bf852071d21c388 --- /dev/null +++ b/js/solstice/templates/newsroom-resources/resources-list.mustache @@ -0,0 +1,18 @@ +{{#items}} +

{{title}}

+ {{#isFetching}} +
+ + Loading... +
+ {{/isFetching}} + {{^isFetching}} + + {{/isFetching}} +{{/items}}