detect span.counter and allow to put styles on the number

This commit is contained in:
Mathieu Jaumotte 2022-02-01 13:31:26 +01:00
parent 730199cca4
commit ec29758547
5 changed files with 49 additions and 5 deletions

View File

@ -25,6 +25,7 @@ import { chill } from './js/chill.js';
global.chill = chill;
require('./js/date.js');
require('./js/counter.js');
/// Load fonts
require('./fonts/OpenSans/OpenSans.scss')

View File

@ -0,0 +1,35 @@
/**
*
* This script search for span.counter elements like
* <span class="counter">Il y a 4 notifications</span>
* and return
* <span class="counter">Il y a <span>4</span> notifications</span>
*
*/
const isNum = (v) => !isNaN(v);
const parseCounter = () => {
document.querySelectorAll('span.counter')
.forEach(el => {
let r = [];
el.innerText
.trim()
.split(' ')
.forEach(w => {
if (isNum(w)) {
r.push(`<span>${w}</span>`);
} else {
r.push(w);
}
})
;
el.innerHTML = r.join(' ');
})
;
};
window.addEventListener('DOMContentLoaded', function (e) {
parseCounter();
});
export { parseCounter };

View File

@ -82,7 +82,7 @@ div#notification-fold {
// Counter
div.notification-counter {
span {
span.counter {
&:not(:first-child) {
&::before {
content: '/ ';
@ -90,3 +90,11 @@ div.notification-counter {
}
}
}
span.counter {
& > span {
font-weight: bold;
background-color: $chill-ll-gray;
padding: 0 0.4rem;
border-radius: 50%;
}
}

View File

@ -73,8 +73,8 @@
<div class="item-col item-meta">
{# TODO twig extension to count comments #}
<div class="comment-counter visually-hidden">
<span>4 commentaires</span>
<div class="comment-counter">
<span class="counter">4 commentaires</span>
</div>
</div>

View File

@ -1,11 +1,11 @@
<div class="notification-counter">
{% if counter.total > 0 %}
<span>
<span class="counter">
{{ 'notification.counter total notifications'|trans({'total': counter.total }) }}
</span>
{% endif %}
{% if counter.unread > 0 %}
<span>
<span class="counter">
{{ 'notification.counter unread notifications'|trans({'unread': counter.unread }) }}
</span>
{% endif %}