demarches-normaliennes/app/javascript/new_design/messagerie.js

59 lines
1.4 KiB
JavaScript
Raw Normal View History

import { scrollTo, scrollToBottom } from '@utils';
2018-08-09 11:53:59 +02:00
2020-10-06 17:10:34 +02:00
function scrollMessagerie() {
2018-10-09 11:43:38 +02:00
const ul = document.querySelector('.messagerie ul');
2018-10-09 11:43:38 +02:00
if (ul) {
const elementToScroll = document.querySelector('.date.highlighted');
2018-10-09 11:43:38 +02:00
if (elementToScroll) {
scrollTo(ul, elementToScroll);
} else {
2018-10-09 11:43:38 +02:00
scrollToBottom(ul);
}
}
}
2021-08-17 11:11:01 +02:00
function saveMessageContent() {
const commentaireForms = Array.from(
document.querySelectorAll('form[data-persisted-content-id]')
);
if (commentaireForms.length) {
const commentaireInputs = Array.from(
document.querySelectorAll('.persisted-input')
);
const persistedContentIds = commentaireForms.map(
(form) => form.dataset.persistedContentId
);
const keys = persistedContentIds.map((key) => `persisted-value-${key}`);
const object = commentaireInputs.map((input, index) => {
return {
input: input,
form: commentaireForms[index],
key: keys[index]
};
});
for (const el of object) {
if (localStorage.getItem(el.key)) {
el.input.value = localStorage.getItem(el.key);
}
el.input.addEventListener('change', (event) => {
localStorage.setItem(el.key, event.target.value);
});
el.form.addEventListener('submit', () => {
localStorage.removeItem(el.key);
});
}
}
}
addEventListener('DOMContentLoaded', scrollMessagerie);
addEventListener('DOMContentLoaded', saveMessageContent);