From d6982d97a9526d28e655626979f49e7e96ab5f91 Mon Sep 17 00:00:00 2001 From: igoradamenko Date: Sun, 11 Mar 2018 20:37:59 +0300 Subject: [PATCH] add embed-script for last-comments widget --- web/app/counter.js | 6 ++--- web/app/embed.js | 6 ++--- web/app/last-comments.js | 53 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 web/app/last-comments.js diff --git a/web/app/counter.js b/web/app/counter.js index 351bc386..5a505008 100644 --- a/web/app/counter.js +++ b/web/app/counter.js @@ -3,12 +3,12 @@ import { COUNTER_NODE_CLASSNAME } from './common/constants' import api from 'common/api'; if (document.readyState !== 'interactive') { - document.addEventListener('DOMContentLoaded', initEmbed); + document.addEventListener('DOMContentLoaded', init); } else { - initCounter(); + init(); } -function initCounter() { +function init() { const nodes = document.getElementsByClassName(COUNTER_NODE_CLASSNAME); if (!nodes) { diff --git a/web/app/embed.js b/web/app/embed.js index c772deee..cdeae4d9 100644 --- a/web/app/embed.js +++ b/web/app/embed.js @@ -1,12 +1,12 @@ import { BASE_URL, NODE_ID } from 'common/constants'; if (document.readyState !== 'interactive') { - document.addEventListener('DOMContentLoaded', initEmbed); + document.addEventListener('DOMContentLoaded', init); } else { - initEmbed(); + init(); } -function initEmbed() { +function init() { const node = document.getElementById(NODE_ID); if (!node) { diff --git a/web/app/last-comments.js b/web/app/last-comments.js new file mode 100644 index 00000000..1fc1ee72 --- /dev/null +++ b/web/app/last-comments.js @@ -0,0 +1,53 @@ +import { h, render } from 'preact'; + +import { BASE_URL, LAST_COMMENTS_DEFAULT_MAX, LAST_COMMENTS_NODE_CLASSNAME } from './common/constants' + +import api from 'common/api'; + +import ListComments from 'components/list-comments'; + +if (document.readyState !== 'interactive') { + document.addEventListener('DOMContentLoaded', init); +} else { + init(); +} + +function init() { + const nodes = document.getElementsByClassName(LAST_COMMENTS_NODE_CLASSNAME); + + if (!nodes) { + console.error('Remark42: Can\'t find last comments nodes.'); + return; + } + + try { + remark_config = remark_config || {} + } catch (e) { + console.error('Remark42: Config object is undefined.'); + return; + } + + if (!remark_config.site_id) { + console.error('Remark42: Site ID is undefined.'); + return; + } + + const styles = document.createElement('link'); + styles.href = `${BASE_URL}/web/remark.css`; + styles.rel = 'stylesheet'; + (document.head || document.body).appendChild(styles); + + [].slice.call(nodes).forEach(node => { + const max = node.dataset.max || remark_config.max_last_comments || LAST_COMMENTS_DEFAULT_MAX; + api.last({ max, siteId: remark_config.site_id }) + .then(comments => { + try { + render(, node) + } catch (e) { + console.error('Remark42: Something went wrong with last comments rendering'); + console.error(e); + } + }); + }); +} +