2024-12-17 21:23:38 +00:00
|
|
|
#include xhr.js
|
2024-01-30 23:47:02 +00:00
|
|
|
|
|
|
|
const MszMessagesRecipient = function(element) {
|
|
|
|
if(!(element instanceof Element))
|
|
|
|
throw 'element must be an instance of Element';
|
|
|
|
|
|
|
|
const avatarElem = element.querySelector('.js-messages-recipient-avatar img');
|
|
|
|
const nameInput = element.querySelector('.js-messages-recipient-name');
|
|
|
|
|
|
|
|
let updateHandler = undefined;
|
|
|
|
const update = async () => {
|
2024-12-17 21:23:38 +00:00
|
|
|
const result = await $x.post(element.dataset.msgLookup, { type: 'json', csrf: true }, {
|
2024-01-30 23:47:02 +00:00
|
|
|
name: nameInput.value,
|
|
|
|
});
|
|
|
|
const body = result.body();
|
|
|
|
|
|
|
|
if(updateHandler !== undefined)
|
|
|
|
await updateHandler(body);
|
|
|
|
|
|
|
|
if(typeof body.avatar === 'string')
|
|
|
|
avatarElem.src = body.avatar;
|
|
|
|
|
|
|
|
if(typeof body.name === 'string')
|
|
|
|
nameInput.value = body.name;
|
|
|
|
};
|
|
|
|
|
|
|
|
let nameTimeout = null;
|
|
|
|
nameInput.addEventListener('input', () => {
|
|
|
|
if(nameTimeout !== undefined)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nameTimeout = setTimeout(() => {
|
|
|
|
update().finally(() => {
|
|
|
|
clearTimeout(nameTimeout);
|
|
|
|
nameTimeout = undefined;
|
|
|
|
});
|
|
|
|
}, 750);
|
|
|
|
});
|
|
|
|
|
|
|
|
update().finally(() => nameTimeout = undefined);
|
|
|
|
|
|
|
|
return {
|
|
|
|
getElement: () => element,
|
|
|
|
onUpdate: handler => {
|
|
|
|
if(typeof handler !== 'function')
|
|
|
|
throw 'handler must be a function';
|
|
|
|
|
|
|
|
updateHandler = handler;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|